clang 19.0.0git
Preprocessor.h
Go to the documentation of this file.
1//===- Preprocessor.h - C Language Family Preprocessor ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Defines the clang::Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
15#define LLVM_CLANG_LEX_PREPROCESSOR_H
16
20#include "clang/Basic/LLVM.h"
22#include "clang/Basic/Module.h"
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/MacroInfo.h"
30#include "clang/Lex/ModuleMap.h"
32#include "clang/Lex/Token.h"
34#include "llvm/ADT/ArrayRef.h"
35#include "llvm/ADT/DenseMap.h"
36#include "llvm/ADT/FoldingSet.h"
37#include "llvm/ADT/FunctionExtras.h"
38#include "llvm/ADT/PointerUnion.h"
39#include "llvm/ADT/STLExtras.h"
40#include "llvm/ADT/SmallPtrSet.h"
41#include "llvm/ADT/SmallVector.h"
42#include "llvm/ADT/StringRef.h"
43#include "llvm/ADT/TinyPtrVector.h"
44#include "llvm/ADT/iterator_range.h"
45#include "llvm/Support/Allocator.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/Registry.h"
48#include <cassert>
49#include <cstddef>
50#include <cstdint>
51#include <map>
52#include <memory>
53#include <optional>
54#include <string>
55#include <utility>
56#include <vector>
57
58namespace llvm {
59
60template<unsigned InternalLen> class SmallString;
61
62} // namespace llvm
63
64namespace clang {
65
66class CodeCompletionHandler;
67class CommentHandler;
68class DirectoryEntry;
69class EmptylineHandler;
70class ExternalPreprocessorSource;
71class FileEntry;
72class FileManager;
73class HeaderSearch;
74class MacroArgs;
75class PragmaHandler;
76class PragmaNamespace;
77class PreprocessingRecord;
78class PreprocessorLexer;
79class PreprocessorOptions;
80class ScratchBuffer;
81class TargetInfo;
82
83namespace Builtin {
84class Context;
85}
86
87/// Stores token information for comparing actual tokens with
88/// predefined values. Only handles simple tokens and identifiers.
90 tok::TokenKind Kind;
92
93public:
94 TokenValue(tok::TokenKind Kind) : Kind(Kind), II(nullptr) {
95 assert(Kind != tok::raw_identifier && "Raw identifiers are not supported.");
96 assert(Kind != tok::identifier &&
97 "Identifiers should be created by TokenValue(IdentifierInfo *)");
98 assert(!tok::isLiteral(Kind) && "Literals are not supported.");
99 assert(!tok::isAnnotation(Kind) && "Annotations are not supported.");
100 }
101
102 TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
103
104 bool operator==(const Token &Tok) const {
105 return Tok.getKind() == Kind &&
106 (!II || II == Tok.getIdentifierInfo());
107 }
108};
109
110/// Context in which macro name is used.
112 // other than #define or #undef
114
115 // macro name specified in #define
117
118 // macro name specified in #undef
119 MU_Undef = 2
121
122/// Engages in a tight little dance with the lexer to efficiently
123/// preprocess tokens.
124///
125/// Lexers know only about tokens within a single source file, and don't
126/// know anything about preprocessor-level issues like the \#include stack,
127/// token expansion, etc.
131
132 llvm::unique_function<void(const clang::Token &)> OnToken;
133 std::shared_ptr<PreprocessorOptions> PPOpts;
134 DiagnosticsEngine *Diags;
135 const LangOptions &LangOpts;
136 const TargetInfo *Target = nullptr;
137 const TargetInfo *AuxTarget = nullptr;
138 FileManager &FileMgr;
139 SourceManager &SourceMgr;
140 std::unique_ptr<ScratchBuffer> ScratchBuf;
141 HeaderSearch &HeaderInfo;
142 ModuleLoader &TheModuleLoader;
143
144 /// External source of macros.
146
147 /// A BumpPtrAllocator object used to quickly allocate and release
148 /// objects internal to the Preprocessor.
149 llvm::BumpPtrAllocator BP;
150
151 /// Identifiers for builtin macros and other builtins.
152 IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__
153 IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__
154 IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__
155 IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__
156 IdentifierInfo *Ident__FILE_NAME__; // __FILE_NAME__
157 IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__
158 IdentifierInfo *Ident__COUNTER__; // __COUNTER__
159 IdentifierInfo *Ident_Pragma, *Ident__pragma; // _Pragma, __pragma
160 IdentifierInfo *Ident__identifier; // __identifier
161 IdentifierInfo *Ident__VA_ARGS__; // __VA_ARGS__
162 IdentifierInfo *Ident__VA_OPT__; // __VA_OPT__
163 IdentifierInfo *Ident__has_feature; // __has_feature
164 IdentifierInfo *Ident__has_extension; // __has_extension
165 IdentifierInfo *Ident__has_builtin; // __has_builtin
166 IdentifierInfo *Ident__has_constexpr_builtin; // __has_constexpr_builtin
167 IdentifierInfo *Ident__has_attribute; // __has_attribute
168 IdentifierInfo *Ident__has_include; // __has_include
169 IdentifierInfo *Ident__has_include_next; // __has_include_next
170 IdentifierInfo *Ident__has_warning; // __has_warning
171 IdentifierInfo *Ident__is_identifier; // __is_identifier
172 IdentifierInfo *Ident__building_module; // __building_module
173 IdentifierInfo *Ident__MODULE__; // __MODULE__
174 IdentifierInfo *Ident__has_cpp_attribute; // __has_cpp_attribute
175 IdentifierInfo *Ident__has_c_attribute; // __has_c_attribute
176 IdentifierInfo *Ident__has_declspec; // __has_declspec_attribute
177 IdentifierInfo *Ident__is_target_arch; // __is_target_arch
178 IdentifierInfo *Ident__is_target_vendor; // __is_target_vendor
179 IdentifierInfo *Ident__is_target_os; // __is_target_os
180 IdentifierInfo *Ident__is_target_environment; // __is_target_environment
181 IdentifierInfo *Ident__is_target_variant_os;
182 IdentifierInfo *Ident__is_target_variant_environment;
183 IdentifierInfo *Ident__FLT_EVAL_METHOD__; // __FLT_EVAL_METHOD
184
185 // Weak, only valid (and set) while InMacroArgs is true.
186 Token* ArgMacro;
187
188 SourceLocation DATELoc, TIMELoc;
189
190 // FEM_UnsetOnCommandLine means that an explicit evaluation method was
191 // not specified on the command line. The target is queried to set the
192 // default evaluation method.
193 LangOptions::FPEvalMethodKind CurrentFPEvalMethod =
195
196 // The most recent pragma location where the floating point evaluation
197 // method was modified. This is used to determine whether the
198 // 'pragma clang fp eval_method' was used whithin the current scope.
199 SourceLocation LastFPEvalPragmaLocation;
200
201 LangOptions::FPEvalMethodKind TUFPEvalMethod =
203
204 // Next __COUNTER__ value, starts at 0.
205 unsigned CounterValue = 0;
206
207 enum {
208 /// Maximum depth of \#includes.
209 MaxAllowedIncludeStackDepth = 200
210 };
211
212 // State that is set before the preprocessor begins.
213 bool KeepComments : 1;
214 bool KeepMacroComments : 1;
215 bool SuppressIncludeNotFoundError : 1;
216
217 // State that changes while the preprocessor runs:
218 bool InMacroArgs : 1; // True if parsing fn macro invocation args.
219
220 /// Whether the preprocessor owns the header search object.
221 bool OwnsHeaderSearch : 1;
222
223 /// True if macro expansion is disabled.
224 bool DisableMacroExpansion : 1;
225
226 /// Temporarily disables DisableMacroExpansion (i.e. enables expansion)
227 /// when parsing preprocessor directives.
228 bool MacroExpansionInDirectivesOverride : 1;
229
230 class ResetMacroExpansionHelper;
231
232 /// Whether we have already loaded macros from the external source.
233 mutable bool ReadMacrosFromExternalSource : 1;
234
235 /// True if pragmas are enabled.
236 bool PragmasEnabled : 1;
237
238 /// True if the current build action is a preprocessing action.
239 bool PreprocessedOutput : 1;
240
241 /// True if we are currently preprocessing a #if or #elif directive
242 bool ParsingIfOrElifDirective;
243
244 /// True if we are pre-expanding macro arguments.
245 bool InMacroArgPreExpansion;
246
247 /// Mapping/lookup information for all identifiers in
248 /// the program, including program keywords.
249 mutable IdentifierTable Identifiers;
250
251 /// This table contains all the selectors in the program.
252 ///
253 /// Unlike IdentifierTable above, this table *isn't* populated by the
254 /// preprocessor. It is declared/expanded here because its role/lifetime is
255 /// conceptually similar to the IdentifierTable. In addition, the current
256 /// control flow (in clang::ParseAST()), make it convenient to put here.
257 ///
258 /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
259 /// the lifetime of the preprocessor.
260 SelectorTable Selectors;
261
262 /// Information about builtins.
263 std::unique_ptr<Builtin::Context> BuiltinInfo;
264
265 /// Tracks all of the pragmas that the client registered
266 /// with this preprocessor.
267 std::unique_ptr<PragmaNamespace> PragmaHandlers;
268
269 /// Pragma handlers of the original source is stored here during the
270 /// parsing of a model file.
271 std::unique_ptr<PragmaNamespace> PragmaHandlersBackup;
272
273 /// Tracks all of the comment handlers that the client registered
274 /// with this preprocessor.
275 std::vector<CommentHandler *> CommentHandlers;
276
277 /// Empty line handler.
278 EmptylineHandler *Emptyline = nullptr;
279
280 /// True to avoid tearing down the lexer etc on EOF
281 bool IncrementalProcessing = false;
282
283public:
284 /// The kind of translation unit we are processing.
286
287 /// Returns a pointer into the given file's buffer that's guaranteed
288 /// to be between tokens. The returned pointer is always before \p Start.
289 /// The maximum distance betweenthe returned pointer and \p Start is
290 /// limited by a constant value, but also an implementation detail.
291 /// If no such check point exists, \c nullptr is returned.
292 const char *getCheckPoint(FileID FID, const char *Start) const;
293
294private:
295 /// The code-completion handler.
296 CodeCompletionHandler *CodeComplete = nullptr;
297
298 /// The file that we're performing code-completion for, if any.
299 const FileEntry *CodeCompletionFile = nullptr;
300
301 /// The offset in file for the code-completion point.
302 unsigned CodeCompletionOffset = 0;
303
304 /// The location for the code-completion point. This gets instantiated
305 /// when the CodeCompletionFile gets \#include'ed for preprocessing.
306 SourceLocation CodeCompletionLoc;
307
308 /// The start location for the file of the code-completion point.
309 ///
310 /// This gets instantiated when the CodeCompletionFile gets \#include'ed
311 /// for preprocessing.
312 SourceLocation CodeCompletionFileLoc;
313
314 /// The source location of the \c import contextual keyword we just
315 /// lexed, if any.
316 SourceLocation ModuleImportLoc;
317
318 /// The import path for named module that we're currently processing.
320
321 llvm::DenseMap<FileID, SmallVector<const char *>> CheckPoints;
322 unsigned CheckPointCounter = 0;
323
324 /// Whether the import is an `@import` or a standard c++ modules import.
325 bool IsAtImport = false;
326
327 /// Whether the last token we lexed was an '@'.
328 bool LastTokenWasAt = false;
329
330 /// A position within a C++20 import-seq.
331 class StdCXXImportSeq {
332 public:
333 enum State : int {
334 // Positive values represent a number of unclosed brackets.
335 AtTopLevel = 0,
336 AfterTopLevelTokenSeq = -1,
337 AfterExport = -2,
338 AfterImportSeq = -3,
339 };
340
341 StdCXXImportSeq(State S) : S(S) {}
342
343 /// Saw any kind of open bracket.
344 void handleOpenBracket() {
345 S = static_cast<State>(std::max<int>(S, 0) + 1);
346 }
347 /// Saw any kind of close bracket other than '}'.
348 void handleCloseBracket() {
349 S = static_cast<State>(std::max<int>(S, 1) - 1);
350 }
351 /// Saw a close brace.
352 void handleCloseBrace() {
353 handleCloseBracket();
354 if (S == AtTopLevel && !AfterHeaderName)
355 S = AfterTopLevelTokenSeq;
356 }
357 /// Saw a semicolon.
358 void handleSemi() {
359 if (atTopLevel()) {
360 S = AfterTopLevelTokenSeq;
361 AfterHeaderName = false;
362 }
363 }
364
365 /// Saw an 'export' identifier.
366 void handleExport() {
367 if (S == AfterTopLevelTokenSeq)
368 S = AfterExport;
369 else if (S <= 0)
370 S = AtTopLevel;
371 }
372 /// Saw an 'import' identifier.
373 void handleImport() {
374 if (S == AfterTopLevelTokenSeq || S == AfterExport)
375 S = AfterImportSeq;
376 else if (S <= 0)
377 S = AtTopLevel;
378 }
379
380 /// Saw a 'header-name' token; do not recognize any more 'import' tokens
381 /// until we reach a top-level semicolon.
382 void handleHeaderName() {
383 if (S == AfterImportSeq)
384 AfterHeaderName = true;
385 handleMisc();
386 }
387
388 /// Saw any other token.
389 void handleMisc() {
390 if (S <= 0)
391 S = AtTopLevel;
392 }
393
394 bool atTopLevel() { return S <= 0; }
395 bool afterImportSeq() { return S == AfterImportSeq; }
396 bool afterTopLevelSeq() { return S == AfterTopLevelTokenSeq; }
397
398 private:
399 State S;
400 /// Whether we're in the pp-import-suffix following the header-name in a
401 /// pp-import. If so, a close-brace is not sufficient to end the
402 /// top-level-token-seq of an import-seq.
403 bool AfterHeaderName = false;
404 };
405
406 /// Our current position within a C++20 import-seq.
407 StdCXXImportSeq StdCXXImportSeqState = StdCXXImportSeq::AfterTopLevelTokenSeq;
408
409 /// Track whether we are in a Global Module Fragment
410 class TrackGMF {
411 public:
412 enum GMFState : int {
413 GMFActive = 1,
414 MaybeGMF = 0,
415 BeforeGMFIntroducer = -1,
416 GMFAbsentOrEnded = -2,
417 };
418
419 TrackGMF(GMFState S) : S(S) {}
420
421 /// Saw a semicolon.
422 void handleSemi() {
423 // If it is immediately after the first instance of the module keyword,
424 // then that introduces the GMF.
425 if (S == MaybeGMF)
426 S = GMFActive;
427 }
428
429 /// Saw an 'export' identifier.
430 void handleExport() {
431 // The presence of an 'export' keyword always ends or excludes a GMF.
432 S = GMFAbsentOrEnded;
433 }
434
435 /// Saw an 'import' identifier.
436 void handleImport(bool AfterTopLevelTokenSeq) {
437 // If we see this before any 'module' kw, then we have no GMF.
438 if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer)
439 S = GMFAbsentOrEnded;
440 }
441
442 /// Saw a 'module' identifier.
443 void handleModule(bool AfterTopLevelTokenSeq) {
444 // This was the first module identifier and not preceded by any token
445 // that would exclude a GMF. It could begin a GMF, but only if directly
446 // followed by a semicolon.
447 if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer)
448 S = MaybeGMF;
449 else
450 S = GMFAbsentOrEnded;
451 }
452
453 /// Saw any other token.
454 void handleMisc() {
455 // We saw something other than ; after the 'module' kw, so not a GMF.
456 if (S == MaybeGMF)
457 S = GMFAbsentOrEnded;
458 }
459
460 bool inGMF() { return S == GMFActive; }
461
462 private:
463 /// Track the transitions into and out of a Global Module Fragment,
464 /// if one is present.
465 GMFState S;
466 };
467
468 TrackGMF TrackGMFState = TrackGMF::BeforeGMFIntroducer;
469
470 /// Track the status of the c++20 module decl.
471 ///
472 /// module-declaration:
473 /// 'export'[opt] 'module' module-name module-partition[opt]
474 /// attribute-specifier-seq[opt] ';'
475 ///
476 /// module-name:
477 /// module-name-qualifier[opt] identifier
478 ///
479 /// module-partition:
480 /// ':' module-name-qualifier[opt] identifier
481 ///
482 /// module-name-qualifier:
483 /// identifier '.'
484 /// module-name-qualifier identifier '.'
485 ///
486 /// Transition state:
487 ///
488 /// NotAModuleDecl --- export ---> FoundExport
489 /// NotAModuleDecl --- module ---> ImplementationCandidate
490 /// FoundExport --- module ---> InterfaceCandidate
491 /// ImplementationCandidate --- Identifier ---> ImplementationCandidate
492 /// ImplementationCandidate --- period ---> ImplementationCandidate
493 /// ImplementationCandidate --- colon ---> ImplementationCandidate
494 /// InterfaceCandidate --- Identifier ---> InterfaceCandidate
495 /// InterfaceCandidate --- period ---> InterfaceCandidate
496 /// InterfaceCandidate --- colon ---> InterfaceCandidate
497 /// ImplementationCandidate --- Semi ---> NamedModuleImplementation
498 /// NamedModuleInterface --- Semi ---> NamedModuleInterface
499 /// NamedModuleImplementation --- Anything ---> NamedModuleImplementation
500 /// NamedModuleInterface --- Anything ---> NamedModuleInterface
501 ///
502 /// FIXME: We haven't handle attribute-specifier-seq here. It may not be bad
503 /// soon since we don't support any module attributes yet.
504 class ModuleDeclSeq {
505 enum ModuleDeclState : int {
506 NotAModuleDecl,
507 FoundExport,
508 InterfaceCandidate,
509 ImplementationCandidate,
510 NamedModuleInterface,
511 NamedModuleImplementation,
512 };
513
514 public:
515 ModuleDeclSeq() = default;
516
517 void handleExport() {
518 if (State == NotAModuleDecl)
519 State = FoundExport;
520 else if (!isNamedModule())
521 reset();
522 }
523
524 void handleModule() {
525 if (State == FoundExport)
526 State = InterfaceCandidate;
527 else if (State == NotAModuleDecl)
528 State = ImplementationCandidate;
529 else if (!isNamedModule())
530 reset();
531 }
532
533 void handleIdentifier(IdentifierInfo *Identifier) {
534 if (isModuleCandidate() && Identifier)
535 Name += Identifier->getName().str();
536 else if (!isNamedModule())
537 reset();
538 }
539
540 void handleColon() {
541 if (isModuleCandidate())
542 Name += ":";
543 else if (!isNamedModule())
544 reset();
545 }
546
547 void handlePeriod() {
548 if (isModuleCandidate())
549 Name += ".";
550 else if (!isNamedModule())
551 reset();
552 }
553
554 void handleSemi() {
555 if (!Name.empty() && isModuleCandidate()) {
556 if (State == InterfaceCandidate)
557 State = NamedModuleInterface;
558 else if (State == ImplementationCandidate)
559 State = NamedModuleImplementation;
560 else
561 llvm_unreachable("Unimaged ModuleDeclState.");
562 } else if (!isNamedModule())
563 reset();
564 }
565
566 void handleMisc() {
567 if (!isNamedModule())
568 reset();
569 }
570
571 bool isModuleCandidate() const {
572 return State == InterfaceCandidate || State == ImplementationCandidate;
573 }
574
575 bool isNamedModule() const {
576 return State == NamedModuleInterface ||
577 State == NamedModuleImplementation;
578 }
579
580 bool isNamedInterface() const { return State == NamedModuleInterface; }
581
582 bool isImplementationUnit() const {
583 return State == NamedModuleImplementation && !getName().contains(':');
584 }
585
586 StringRef getName() const {
587 assert(isNamedModule() && "Can't get name from a non named module");
588 return Name;
589 }
590
591 StringRef getPrimaryName() const {
592 assert(isNamedModule() && "Can't get name from a non named module");
593 return getName().split(':').first;
594 }
595
596 void reset() {
597 Name.clear();
598 State = NotAModuleDecl;
599 }
600
601 private:
602 ModuleDeclState State = NotAModuleDecl;
603 std::string Name;
604 };
605
606 ModuleDeclSeq ModuleDeclState;
607
608 /// Whether the module import expects an identifier next. Otherwise,
609 /// it expects a '.' or ';'.
610 bool ModuleImportExpectsIdentifier = false;
611
612 /// The identifier and source location of the currently-active
613 /// \#pragma clang arc_cf_code_audited begin.
614 std::pair<IdentifierInfo *, SourceLocation> PragmaARCCFCodeAuditedInfo;
615
616 /// The source location of the currently-active
617 /// \#pragma clang assume_nonnull begin.
618 SourceLocation PragmaAssumeNonNullLoc;
619
620 /// Set only for preambles which end with an active
621 /// \#pragma clang assume_nonnull begin.
622 ///
623 /// When the preamble is loaded into the main file,
624 /// `PragmaAssumeNonNullLoc` will be set to this to
625 /// replay the unterminated assume_nonnull.
626 SourceLocation PreambleRecordedPragmaAssumeNonNullLoc;
627
628 /// True if we hit the code-completion point.
629 bool CodeCompletionReached = false;
630
631 /// The code completion token containing the information
632 /// on the stem that is to be code completed.
633 IdentifierInfo *CodeCompletionII = nullptr;
634
635 /// Range for the code completion token.
636 SourceRange CodeCompletionTokenRange;
637
638 /// The directory that the main file should be considered to occupy,
639 /// if it does not correspond to a real file (as happens when building a
640 /// module).
641 OptionalDirectoryEntryRef MainFileDir;
642
643 /// The number of bytes that we will initially skip when entering the
644 /// main file, along with a flag that indicates whether skipping this number
645 /// of bytes will place the lexer at the start of a line.
646 ///
647 /// This is used when loading a precompiled preamble.
648 std::pair<int, bool> SkipMainFilePreamble;
649
650 /// Whether we hit an error due to reaching max allowed include depth. Allows
651 /// to avoid hitting the same error over and over again.
652 bool HasReachedMaxIncludeDepth = false;
653
654 /// The number of currently-active calls to Lex.
655 ///
656 /// Lex is reentrant, and asking for an (end-of-phase-4) token can often
657 /// require asking for multiple additional tokens. This counter makes it
658 /// possible for Lex to detect whether it's producing a token for the end
659 /// of phase 4 of translation or for some other situation.
660 unsigned LexLevel = 0;
661
662 /// The number of (LexLevel 0) preprocessor tokens.
663 unsigned TokenCount = 0;
664
665 /// Preprocess every token regardless of LexLevel.
666 bool PreprocessToken = false;
667
668 /// The maximum number of (LexLevel 0) tokens before issuing a -Wmax-tokens
669 /// warning, or zero for unlimited.
670 unsigned MaxTokens = 0;
671 SourceLocation MaxTokensOverrideLoc;
672
673public:
680
686 ElseLoc(ElseLoc) {}
687 };
688
690
691private:
692 friend class ASTReader;
693 friend class MacroArgs;
694
695 class PreambleConditionalStackStore {
696 enum State {
697 Off = 0,
698 Recording = 1,
699 Replaying = 2,
700 };
701
702 public:
703 PreambleConditionalStackStore() = default;
704
705 void startRecording() { ConditionalStackState = Recording; }
706 void startReplaying() { ConditionalStackState = Replaying; }
707 bool isRecording() const { return ConditionalStackState == Recording; }
708 bool isReplaying() const { return ConditionalStackState == Replaying; }
709
710 ArrayRef<PPConditionalInfo> getStack() const {
711 return ConditionalStack;
712 }
713
714 void doneReplaying() {
715 ConditionalStack.clear();
716 ConditionalStackState = Off;
717 }
718
719 void setStack(ArrayRef<PPConditionalInfo> s) {
720 if (!isRecording() && !isReplaying())
721 return;
722 ConditionalStack.clear();
723 ConditionalStack.append(s.begin(), s.end());
724 }
725
726 bool hasRecordedPreamble() const { return !ConditionalStack.empty(); }
727
728 bool reachedEOFWhileSkipping() const { return SkipInfo.has_value(); }
729
730 void clearSkipInfo() { SkipInfo.reset(); }
731
732 std::optional<PreambleSkipInfo> SkipInfo;
733
734 private:
735 SmallVector<PPConditionalInfo, 4> ConditionalStack;
736 State ConditionalStackState = Off;
737 } PreambleConditionalStack;
738
739 /// The current top of the stack that we're lexing from if
740 /// not expanding a macro and we are lexing directly from source code.
741 ///
742 /// Only one of CurLexer, or CurTokenLexer will be non-null.
743 std::unique_ptr<Lexer> CurLexer;
744
745 /// The current top of the stack that we're lexing from
746 /// if not expanding a macro.
747 ///
748 /// This is an alias for CurLexer.
749 PreprocessorLexer *CurPPLexer = nullptr;
750
751 /// Used to find the current FileEntry, if CurLexer is non-null
752 /// and if applicable.
753 ///
754 /// This allows us to implement \#include_next and find directory-specific
755 /// properties.
756 ConstSearchDirIterator CurDirLookup = nullptr;
757
758 /// The current macro we are expanding, if we are expanding a macro.
759 ///
760 /// One of CurLexer and CurTokenLexer must be null.
761 std::unique_ptr<TokenLexer> CurTokenLexer;
762
763 /// The kind of lexer we're currently working with.
764 typedef bool (*LexerCallback)(Preprocessor &, Token &);
765 LexerCallback CurLexerCallback = &CLK_Lexer;
766
767 /// If the current lexer is for a submodule that is being built, this
768 /// is that submodule.
769 Module *CurLexerSubmodule = nullptr;
770
771 /// Keeps track of the stack of files currently
772 /// \#included, and macros currently being expanded from, not counting
773 /// CurLexer/CurTokenLexer.
774 struct IncludeStackInfo {
775 LexerCallback CurLexerCallback;
776 Module *TheSubmodule;
777 std::unique_ptr<Lexer> TheLexer;
778 PreprocessorLexer *ThePPLexer;
779 std::unique_ptr<TokenLexer> TheTokenLexer;
780 ConstSearchDirIterator TheDirLookup;
781
782 // The following constructors are completely useless copies of the default
783 // versions, only needed to pacify MSVC.
784 IncludeStackInfo(LexerCallback CurLexerCallback, Module *TheSubmodule,
785 std::unique_ptr<Lexer> &&TheLexer,
786 PreprocessorLexer *ThePPLexer,
787 std::unique_ptr<TokenLexer> &&TheTokenLexer,
788 ConstSearchDirIterator TheDirLookup)
789 : CurLexerCallback(std::move(CurLexerCallback)),
790 TheSubmodule(std::move(TheSubmodule)), TheLexer(std::move(TheLexer)),
791 ThePPLexer(std::move(ThePPLexer)),
792 TheTokenLexer(std::move(TheTokenLexer)),
793 TheDirLookup(std::move(TheDirLookup)) {}
794 };
795 std::vector<IncludeStackInfo> IncludeMacroStack;
796
797 /// Actions invoked when some preprocessor activity is
798 /// encountered (e.g. a file is \#included, etc).
799 std::unique_ptr<PPCallbacks> Callbacks;
800
801 struct MacroExpandsInfo {
802 Token Tok;
803 MacroDefinition MD;
804 SourceRange Range;
805
806 MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range)
807 : Tok(Tok), MD(MD), Range(Range) {}
808 };
809 SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
810
811 /// Information about a name that has been used to define a module macro.
812 struct ModuleMacroInfo {
813 /// The most recent macro directive for this identifier.
814 MacroDirective *MD;
815
816 /// The active module macros for this identifier.
817 llvm::TinyPtrVector<ModuleMacro *> ActiveModuleMacros;
818
819 /// The generation number at which we last updated ActiveModuleMacros.
820 /// \see Preprocessor::VisibleModules.
821 unsigned ActiveModuleMacrosGeneration = 0;
822
823 /// Whether this macro name is ambiguous.
824 bool IsAmbiguous = false;
825
826 /// The module macros that are overridden by this macro.
827 llvm::TinyPtrVector<ModuleMacro *> OverriddenMacros;
828
829 ModuleMacroInfo(MacroDirective *MD) : MD(MD) {}
830 };
831
832 /// The state of a macro for an identifier.
833 class MacroState {
834 mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State;
835
836 ModuleMacroInfo *getModuleInfo(Preprocessor &PP,
837 const IdentifierInfo *II) const {
838 if (II->isOutOfDate())
839 PP.updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
840 // FIXME: Find a spare bit on IdentifierInfo and store a
841 // HasModuleMacros flag.
842 if (!II->hasMacroDefinition() ||
843 (!PP.getLangOpts().Modules &&
844 !PP.getLangOpts().ModulesLocalVisibility) ||
845 !PP.CurSubmoduleState->VisibleModules.getGeneration())
846 return nullptr;
847
848 auto *Info = State.dyn_cast<ModuleMacroInfo*>();
849 if (!Info) {
850 Info = new (PP.getPreprocessorAllocator())
851 ModuleMacroInfo(State.get<MacroDirective *>());
852 State = Info;
853 }
854
855 if (PP.CurSubmoduleState->VisibleModules.getGeneration() !=
856 Info->ActiveModuleMacrosGeneration)
857 PP.updateModuleMacroInfo(II, *Info);
858 return Info;
859 }
860
861 public:
862 MacroState() : MacroState(nullptr) {}
863 MacroState(MacroDirective *MD) : State(MD) {}
864
865 MacroState(MacroState &&O) noexcept : State(O.State) {
866 O.State = (MacroDirective *)nullptr;
867 }
868
869 MacroState &operator=(MacroState &&O) noexcept {
870 auto S = O.State;
871 O.State = (MacroDirective *)nullptr;
872 State = S;
873 return *this;
874 }
875
876 ~MacroState() {
877 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
878 Info->~ModuleMacroInfo();
879 }
880
881 MacroDirective *getLatest() const {
882 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
883 return Info->MD;
884 return State.get<MacroDirective*>();
885 }
886
887 void setLatest(MacroDirective *MD) {
888 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
889 Info->MD = MD;
890 else
891 State = MD;
892 }
893
894 bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const {
895 auto *Info = getModuleInfo(PP, II);
896 return Info ? Info->IsAmbiguous : false;
897 }
898
899 ArrayRef<ModuleMacro *>
900 getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const {
901 if (auto *Info = getModuleInfo(PP, II))
902 return Info->ActiveModuleMacros;
903 return std::nullopt;
904 }
905
906 MacroDirective::DefInfo findDirectiveAtLoc(SourceLocation Loc,
907 SourceManager &SourceMgr) const {
908 // FIXME: Incorporate module macros into the result of this.
909 if (auto *Latest = getLatest())
910 return Latest->findDirectiveAtLoc(Loc, SourceMgr);
911 return {};
912 }
913
914 void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) {
915 if (auto *Info = getModuleInfo(PP, II)) {
916 Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
917 Info->ActiveModuleMacros.begin(),
918 Info->ActiveModuleMacros.end());
919 Info->ActiveModuleMacros.clear();
920 Info->IsAmbiguous = false;
921 }
922 }
923
924 ArrayRef<ModuleMacro*> getOverriddenMacros() const {
925 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
926 return Info->OverriddenMacros;
927 return std::nullopt;
928 }
929
930 void setOverriddenMacros(Preprocessor &PP,
931 ArrayRef<ModuleMacro *> Overrides) {
932 auto *Info = State.dyn_cast<ModuleMacroInfo*>();
933 if (!Info) {
934 if (Overrides.empty())
935 return;
936 Info = new (PP.getPreprocessorAllocator())
937 ModuleMacroInfo(State.get<MacroDirective *>());
938 State = Info;
939 }
940 Info->OverriddenMacros.clear();
941 Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
942 Overrides.begin(), Overrides.end());
943 Info->ActiveModuleMacrosGeneration = 0;
944 }
945 };
946
947 /// For each IdentifierInfo that was associated with a macro, we
948 /// keep a mapping to the history of all macro definitions and #undefs in
949 /// the reverse order (the latest one is in the head of the list).
950 ///
951 /// This mapping lives within the \p CurSubmoduleState.
952 using MacroMap = llvm::DenseMap<const IdentifierInfo *, MacroState>;
953
954 struct SubmoduleState;
955
956 /// Information about a submodule that we're currently building.
957 struct BuildingSubmoduleInfo {
958 /// The module that we are building.
959 Module *M;
960
961 /// The location at which the module was included.
962 SourceLocation ImportLoc;
963
964 /// Whether we entered this submodule via a pragma.
965 bool IsPragma;
966
967 /// The previous SubmoduleState.
968 SubmoduleState *OuterSubmoduleState;
969
970 /// The number of pending module macro names when we started building this.
971 unsigned OuterPendingModuleMacroNames;
972
973 BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc, bool IsPragma,
974 SubmoduleState *OuterSubmoduleState,
975 unsigned OuterPendingModuleMacroNames)
976 : M(M), ImportLoc(ImportLoc), IsPragma(IsPragma),
977 OuterSubmoduleState(OuterSubmoduleState),
978 OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {}
979 };
980 SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack;
981
982 /// Information about a submodule's preprocessor state.
983 struct SubmoduleState {
984 /// The macros for the submodule.
985 MacroMap Macros;
986
987 /// The set of modules that are visible within the submodule.
988 VisibleModuleSet VisibleModules;
989
990 // FIXME: CounterValue?
991 // FIXME: PragmaPushMacroInfo?
992 };
993 std::map<Module *, SubmoduleState> Submodules;
994
995 /// The preprocessor state for preprocessing outside of any submodule.
996 SubmoduleState NullSubmoduleState;
997
998 /// The current submodule state. Will be \p NullSubmoduleState if we're not
999 /// in a submodule.
1000 SubmoduleState *CurSubmoduleState;
1001
1002 /// The files that have been included.
1003 IncludedFilesSet IncludedFiles;
1004
1005 /// The set of top-level modules that affected preprocessing, but were not
1006 /// imported.
1007 llvm::SmallSetVector<Module *, 2> AffectingClangModules;
1008
1009 /// The set of known macros exported from modules.
1010 llvm::FoldingSet<ModuleMacro> ModuleMacros;
1011
1012 /// The names of potential module macros that we've not yet processed.
1013 llvm::SmallVector<const IdentifierInfo *, 32> PendingModuleMacroNames;
1014
1015 /// The list of module macros, for each identifier, that are not overridden by
1016 /// any other module macro.
1017 llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro *>>
1018 LeafModuleMacros;
1019
1020 /// Macros that we want to warn because they are not used at the end
1021 /// of the translation unit.
1022 ///
1023 /// We store just their SourceLocations instead of
1024 /// something like MacroInfo*. The benefit of this is that when we are
1025 /// deserializing from PCH, we don't need to deserialize identifier & macros
1026 /// just so that we can report that they are unused, we just warn using
1027 /// the SourceLocations of this set (that will be filled by the ASTReader).
1028 using WarnUnusedMacroLocsTy = llvm::SmallDenseSet<SourceLocation, 32>;
1029 WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
1030
1031 /// This is a pair of an optional message and source location used for pragmas
1032 /// that annotate macros like pragma clang restrict_expansion and pragma clang
1033 /// deprecated. This pair stores the optional message and the location of the
1034 /// annotation pragma for use producing diagnostics and notes.
1035 using MsgLocationPair = std::pair<std::string, SourceLocation>;
1036
1037 struct MacroAnnotationInfo {
1038 SourceLocation Location;
1039 std::string Message;
1040 };
1041
1042 struct MacroAnnotations {
1043 std::optional<MacroAnnotationInfo> DeprecationInfo;
1044 std::optional<MacroAnnotationInfo> RestrictExpansionInfo;
1045 std::optional<SourceLocation> FinalAnnotationLoc;
1046
1047 static MacroAnnotations makeDeprecation(SourceLocation Loc,
1048 std::string Msg) {
1049 return MacroAnnotations{MacroAnnotationInfo{Loc, std::move(Msg)},
1050 std::nullopt, std::nullopt};
1051 }
1052
1053 static MacroAnnotations makeRestrictExpansion(SourceLocation Loc,
1054 std::string Msg) {
1055 return MacroAnnotations{
1056 std::nullopt, MacroAnnotationInfo{Loc, std::move(Msg)}, std::nullopt};
1057 }
1058
1059 static MacroAnnotations makeFinal(SourceLocation Loc) {
1060 return MacroAnnotations{std::nullopt, std::nullopt, Loc};
1061 }
1062 };
1063
1064 /// Warning information for macro annotations.
1065 llvm::DenseMap<const IdentifierInfo *, MacroAnnotations> AnnotationInfos;
1066
1067 /// A "freelist" of MacroArg objects that can be
1068 /// reused for quick allocation.
1069 MacroArgs *MacroArgCache = nullptr;
1070
1071 /// For each IdentifierInfo used in a \#pragma push_macro directive,
1072 /// we keep a MacroInfo stack used to restore the previous macro value.
1073 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>
1074 PragmaPushMacroInfo;
1075
1076 // Various statistics we track for performance analysis.
1077 unsigned NumDirectives = 0;
1078 unsigned NumDefined = 0;
1079 unsigned NumUndefined = 0;
1080 unsigned NumPragma = 0;
1081 unsigned NumIf = 0;
1082 unsigned NumElse = 0;
1083 unsigned NumEndif = 0;
1084 unsigned NumEnteredSourceFiles = 0;
1085 unsigned MaxIncludeStackDepth = 0;
1086 unsigned NumMacroExpanded = 0;
1087 unsigned NumFnMacroExpanded = 0;
1088 unsigned NumBuiltinMacroExpanded = 0;
1089 unsigned NumFastMacroExpanded = 0;
1090 unsigned NumTokenPaste = 0;
1091 unsigned NumFastTokenPaste = 0;
1092 unsigned NumSkipped = 0;
1093
1094 /// The predefined macros that preprocessor should use from the
1095 /// command line etc.
1096 std::string Predefines;
1097
1098 /// The file ID for the preprocessor predefines.
1099 FileID PredefinesFileID;
1100
1101 /// The file ID for the PCH through header.
1102 FileID PCHThroughHeaderFileID;
1103
1104 /// Whether tokens are being skipped until a #pragma hdrstop is seen.
1105 bool SkippingUntilPragmaHdrStop = false;
1106
1107 /// Whether tokens are being skipped until the through header is seen.
1108 bool SkippingUntilPCHThroughHeader = false;
1109
1110 /// \{
1111 /// Cache of macro expanders to reduce malloc traffic.
1112 enum { TokenLexerCacheSize = 8 };
1113 unsigned NumCachedTokenLexers;
1114 std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize];
1115 /// \}
1116
1117 /// Keeps macro expanded tokens for TokenLexers.
1118 //
1119 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1120 /// going to lex in the cache and when it finishes the tokens are removed
1121 /// from the end of the cache.
1122 SmallVector<Token, 16> MacroExpandedTokens;
1123 std::vector<std::pair<TokenLexer *, size_t>> MacroExpandingLexersStack;
1124
1125 /// A record of the macro definitions and expansions that
1126 /// occurred during preprocessing.
1127 ///
1128 /// This is an optional side structure that can be enabled with
1129 /// \c createPreprocessingRecord() prior to preprocessing.
1130 PreprocessingRecord *Record = nullptr;
1131
1132 /// Cached tokens state.
1133 using CachedTokensTy = SmallVector<Token, 1>;
1134
1135 /// Cached tokens are stored here when we do backtracking or
1136 /// lookahead. They are "lexed" by the CachingLex() method.
1137 CachedTokensTy CachedTokens;
1138
1139 /// The position of the cached token that CachingLex() should
1140 /// "lex" next.
1141 ///
1142 /// If it points beyond the CachedTokens vector, it means that a normal
1143 /// Lex() should be invoked.
1144 CachedTokensTy::size_type CachedLexPos = 0;
1145
1146 /// Stack of backtrack positions, allowing nested backtracks.
1147 ///
1148 /// The EnableBacktrackAtThisPos() method pushes a position to
1149 /// indicate where CachedLexPos should be set when the BackTrack() method is
1150 /// invoked (at which point the last position is popped).
1151 std::vector<CachedTokensTy::size_type> BacktrackPositions;
1152
1153 /// True if \p Preprocessor::SkipExcludedConditionalBlock() is running.
1154 /// This is used to guard against calling this function recursively.
1155 ///
1156 /// See comments at the use-site for more context about why it is needed.
1157 bool SkippingExcludedConditionalBlock = false;
1158
1159 /// Keeps track of skipped range mappings that were recorded while skipping
1160 /// excluded conditional directives. It maps the source buffer pointer at
1161 /// the beginning of a skipped block, to the number of bytes that should be
1162 /// skipped.
1163 llvm::DenseMap<const char *, unsigned> RecordedSkippedRanges;
1164
1165 void updateOutOfDateIdentifier(IdentifierInfo &II) const;
1166
1167public:
1168 Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
1169 DiagnosticsEngine &diags, const LangOptions &LangOpts,
1170 SourceManager &SM, HeaderSearch &Headers,
1171 ModuleLoader &TheModuleLoader,
1172 IdentifierInfoLookup *IILookup = nullptr,
1173 bool OwnsHeaderSearch = false,
1175
1176 ~Preprocessor();
1177
1178 /// Initialize the preprocessor using information about the target.
1179 ///
1180 /// \param Target is owned by the caller and must remain valid for the
1181 /// lifetime of the preprocessor.
1182 /// \param AuxTarget is owned by the caller and must remain valid for
1183 /// the lifetime of the preprocessor.
1184 void Initialize(const TargetInfo &Target,
1185 const TargetInfo *AuxTarget = nullptr);
1186
1187 /// Initialize the preprocessor to parse a model file
1188 ///
1189 /// To parse model files the preprocessor of the original source is reused to
1190 /// preserver the identifier table. However to avoid some duplicate
1191 /// information in the preprocessor some cleanup is needed before it is used
1192 /// to parse model files. This method does that cleanup.
1194
1195 /// Cleanup after model file parsing
1196 void FinalizeForModelFile();
1197
1198 /// Retrieve the preprocessor options used to initialize this
1199 /// preprocessor.
1200 PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
1201
1202 DiagnosticsEngine &getDiagnostics() const { return *Diags; }
1203 void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
1204
1205 const LangOptions &getLangOpts() const { return LangOpts; }
1206 const TargetInfo &getTargetInfo() const { return *Target; }
1207 const TargetInfo *getAuxTargetInfo() const { return AuxTarget; }
1208 FileManager &getFileManager() const { return FileMgr; }
1209 SourceManager &getSourceManager() const { return SourceMgr; }
1210 HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
1211
1212 IdentifierTable &getIdentifierTable() { return Identifiers; }
1213 const IdentifierTable &getIdentifierTable() const { return Identifiers; }
1214 SelectorTable &getSelectorTable() { return Selectors; }
1215 Builtin::Context &getBuiltinInfo() { return *BuiltinInfo; }
1216 llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
1217
1219 ExternalSource = Source;
1220 }
1221
1223 return ExternalSource;
1224 }
1225
1226 /// Retrieve the module loader associated with this preprocessor.
1227 ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
1228
1230 return TheModuleLoader.HadFatalFailure;
1231 }
1232
1233 /// Retrieve the number of Directives that have been processed by the
1234 /// Preprocessor.
1235 unsigned getNumDirectives() const {
1236 return NumDirectives;
1237 }
1238
1239 /// True if we are currently preprocessing a #if or #elif directive
1241 return ParsingIfOrElifDirective;
1242 }
1243
1244 /// Control whether the preprocessor retains comments in output.
1245 void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
1246 this->KeepComments = KeepComments | KeepMacroComments;
1247 this->KeepMacroComments = KeepMacroComments;
1248 }
1249
1250 bool getCommentRetentionState() const { return KeepComments; }
1251
1252 void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
1253 bool getPragmasEnabled() const { return PragmasEnabled; }
1254
1256 SuppressIncludeNotFoundError = Suppress;
1257 }
1258
1260 return SuppressIncludeNotFoundError;
1261 }
1262
1263 /// Sets whether the preprocessor is responsible for producing output or if
1264 /// it is producing tokens to be consumed by Parse and Sema.
1265 void setPreprocessedOutput(bool IsPreprocessedOutput) {
1266 PreprocessedOutput = IsPreprocessedOutput;
1267 }
1268
1269 /// Returns true if the preprocessor is responsible for generating output,
1270 /// false if it is producing tokens to be consumed by Parse and Sema.
1271 bool isPreprocessedOutput() const { return PreprocessedOutput; }
1272
1273 /// Return true if we are lexing directly from the specified lexer.
1274 bool isCurrentLexer(const PreprocessorLexer *L) const {
1275 return CurPPLexer == L;
1276 }
1277
1278 /// Return the current lexer being lexed from.
1279 ///
1280 /// Note that this ignores any potentially active macro expansions and _Pragma
1281 /// expansions going on at the time.
1282 PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
1283
1284 /// Return the current file lexer being lexed from.
1285 ///
1286 /// Note that this ignores any potentially active macro expansions and _Pragma
1287 /// expansions going on at the time.
1289
1290 /// Return the submodule owning the file being lexed. This may not be
1291 /// the current module if we have changed modules since entering the file.
1292 Module *getCurrentLexerSubmodule() const { return CurLexerSubmodule; }
1293
1294 /// Returns the FileID for the preprocessor predefines.
1295 FileID getPredefinesFileID() const { return PredefinesFileID; }
1296
1297 /// \{
1298 /// Accessors for preprocessor callbacks.
1299 ///
1300 /// Note that this class takes ownership of any PPCallbacks object given to
1301 /// it.
1302 PPCallbacks *getPPCallbacks() const { return Callbacks.get(); }
1303 void addPPCallbacks(std::unique_ptr<PPCallbacks> C) {
1304 if (Callbacks)
1305 C = std::make_unique<PPChainedCallbacks>(std::move(C),
1306 std::move(Callbacks));
1307 Callbacks = std::move(C);
1308 }
1309 /// \}
1310
1311 /// Get the number of tokens processed so far.
1312 unsigned getTokenCount() const { return TokenCount; }
1313
1314 /// Get the max number of tokens before issuing a -Wmax-tokens warning.
1315 unsigned getMaxTokens() const { return MaxTokens; }
1316
1318 MaxTokens = Value;
1319 MaxTokensOverrideLoc = Loc;
1320 };
1321
1322 SourceLocation getMaxTokensOverrideLoc() const { return MaxTokensOverrideLoc; }
1323
1324 /// Register a function that would be called on each token in the final
1325 /// expanded token stream.
1326 /// This also reports annotation tokens produced by the parser.
1327 void setTokenWatcher(llvm::unique_function<void(const clang::Token &)> F) {
1328 OnToken = std::move(F);
1329 }
1330
1331 void setPreprocessToken(bool Preprocess) { PreprocessToken = Preprocess; }
1332
1333 bool isMacroDefined(StringRef Id) {
1334 return isMacroDefined(&Identifiers.get(Id));
1335 }
1337 return II->hasMacroDefinition() &&
1338 (!getLangOpts().Modules || (bool)getMacroDefinition(II));
1339 }
1340
1341 /// Determine whether II is defined as a macro within the module M,
1342 /// if that is a module that we've already preprocessed. Does not check for
1343 /// macros imported into M.
1345 if (!II->hasMacroDefinition())
1346 return false;
1347 auto I = Submodules.find(M);
1348 if (I == Submodules.end())
1349 return false;
1350 auto J = I->second.Macros.find(II);
1351 if (J == I->second.Macros.end())
1352 return false;
1353 auto *MD = J->second.getLatest();
1354 return MD && MD->isDefined();
1355 }
1356
1358 if (!II->hasMacroDefinition())
1359 return {};
1360
1361 MacroState &S = CurSubmoduleState->Macros[II];
1362 auto *MD = S.getLatest();
1363 while (MD && isa<VisibilityMacroDirective>(MD))
1364 MD = MD->getPrevious();
1365 return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD),
1366 S.getActiveModuleMacros(*this, II),
1367 S.isAmbiguous(*this, II));
1368 }
1369
1371 SourceLocation Loc) {
1372 if (!II->hadMacroDefinition())
1373 return {};
1374
1375 MacroState &S = CurSubmoduleState->Macros[II];
1377 if (auto *MD = S.getLatest())
1378 DI = MD->findDirectiveAtLoc(Loc, getSourceManager());
1379 // FIXME: Compute the set of active module macros at the specified location.
1380 return MacroDefinition(DI.getDirective(),
1381 S.getActiveModuleMacros(*this, II),
1382 S.isAmbiguous(*this, II));
1383 }
1384
1385 /// Given an identifier, return its latest non-imported MacroDirective
1386 /// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd.
1388 if (!II->hasMacroDefinition())
1389 return nullptr;
1390
1391 auto *MD = getLocalMacroDirectiveHistory(II);
1392 if (!MD || MD->getDefinition().isUndefined())
1393 return nullptr;
1394
1395 return MD;
1396 }
1397
1398 const MacroInfo *getMacroInfo(const IdentifierInfo *II) const {
1399 return const_cast<Preprocessor*>(this)->getMacroInfo(II);
1400 }
1401
1403 if (!II->hasMacroDefinition())
1404 return nullptr;
1405 if (auto MD = getMacroDefinition(II))
1406 return MD.getMacroInfo();
1407 return nullptr;
1408 }
1409
1410 /// Given an identifier, return the latest non-imported macro
1411 /// directive for that identifier.
1412 ///
1413 /// One can iterate over all previous macro directives from the most recent
1414 /// one.
1416
1417 /// Add a directive to the macro directive history for this identifier.
1420 SourceLocation Loc) {
1421 DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc);
1422 appendMacroDirective(II, MD);
1423 return MD;
1424 }
1426 MacroInfo *MI) {
1427 return appendDefMacroDirective(II, MI, MI->getDefinitionLoc());
1428 }
1429
1430 /// Set a MacroDirective that was loaded from a PCH file.
1432 MacroDirective *MD);
1433
1434 /// Register an exported macro for a module and identifier.
1436 ArrayRef<ModuleMacro *> Overrides, bool &IsNew);
1438
1439 /// Get the list of leaf (non-overridden) module macros for a name.
1441 if (II->isOutOfDate())
1442 updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
1443 auto I = LeafModuleMacros.find(II);
1444 if (I != LeafModuleMacros.end())
1445 return I->second;
1446 return std::nullopt;
1447 }
1448
1449 /// Get the list of submodules that we're currently building.
1451 return BuildingSubmoduleStack;
1452 }
1453
1454 /// \{
1455 /// Iterators for the macro history table. Currently defined macros have
1456 /// IdentifierInfo::hasMacroDefinition() set and an empty
1457 /// MacroInfo::getUndefLoc() at the head of the list.
1458 using macro_iterator = MacroMap::const_iterator;
1459
1460 macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
1461 macro_iterator macro_end(bool IncludeExternalMacros = true) const;
1462
1463 llvm::iterator_range<macro_iterator>
1464 macros(bool IncludeExternalMacros = true) const {
1465 macro_iterator begin = macro_begin(IncludeExternalMacros);
1466 macro_iterator end = macro_end(IncludeExternalMacros);
1467 return llvm::make_range(begin, end);
1468 }
1469
1470 /// \}
1471
1472 /// Mark the given clang module as affecting the current clang module or translation unit.
1474 assert(M->isModuleMapModule());
1475 if (!BuildingSubmoduleStack.empty()) {
1476 if (M != BuildingSubmoduleStack.back().M)
1477 BuildingSubmoduleStack.back().M->AffectingClangModules.insert(M);
1478 } else {
1479 AffectingClangModules.insert(M);
1480 }
1481 }
1482
1483 /// Get the set of top-level clang modules that affected preprocessing, but were not
1484 /// imported.
1486 return AffectingClangModules;
1487 }
1488
1489 /// Mark the file as included.
1490 /// Returns true if this is the first time the file was included.
1492 HeaderInfo.getFileInfo(File);
1493 return IncludedFiles.insert(File).second;
1494 }
1495
1496 /// Return true if this header has already been included.
1498 HeaderInfo.getFileInfo(File);
1499 return IncludedFiles.count(File);
1500 }
1501
1502 /// Get the set of included files.
1503 IncludedFilesSet &getIncludedFiles() { return IncludedFiles; }
1504 const IncludedFilesSet &getIncludedFiles() const { return IncludedFiles; }
1505
1506 /// Return the name of the macro defined before \p Loc that has
1507 /// spelling \p Tokens. If there are multiple macros with same spelling,
1508 /// return the last one defined.
1510 ArrayRef<TokenValue> Tokens) const;
1511
1512 /// Get the predefines for this processor.
1513 /// Used by some third-party tools to inspect and add predefines (see
1514 /// https://github.com/llvm/llvm-project/issues/57483).
1515 const std::string &getPredefines() const { return Predefines; }
1516
1517 /// Set the predefines for this Preprocessor.
1518 ///
1519 /// These predefines are automatically injected when parsing the main file.
1520 void setPredefines(std::string P) { Predefines = std::move(P); }
1521
1522 /// Return information about the specified preprocessor
1523 /// identifier token.
1524 IdentifierInfo *getIdentifierInfo(StringRef Name) const {
1525 return &Identifiers.get(Name);
1526 }
1527
1528 /// Add the specified pragma handler to this preprocessor.
1529 ///
1530 /// If \p Namespace is non-null, then it is a token required to exist on the
1531 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1532 void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1534 AddPragmaHandler(StringRef(), Handler);
1535 }
1536
1537 /// Remove the specific pragma handler from this preprocessor.
1538 ///
1539 /// If \p Namespace is non-null, then it should be the namespace that
1540 /// \p Handler was added to. It is an error to remove a handler that
1541 /// has not been registered.
1542 void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1544 RemovePragmaHandler(StringRef(), Handler);
1545 }
1546
1547 /// Install empty handlers for all pragmas (making them ignored).
1548 void IgnorePragmas();
1549
1550 /// Set empty line handler.
1551 void setEmptylineHandler(EmptylineHandler *Handler) { Emptyline = Handler; }
1552
1553 EmptylineHandler *getEmptylineHandler() const { return Emptyline; }
1554
1555 /// Add the specified comment handler to the preprocessor.
1556 void addCommentHandler(CommentHandler *Handler);
1557
1558 /// Remove the specified comment handler.
1559 ///
1560 /// It is an error to remove a handler that has not been registered.
1561 void removeCommentHandler(CommentHandler *Handler);
1562
1563 /// Set the code completion handler to the given object.
1565 CodeComplete = &Handler;
1566 }
1567
1568 /// Retrieve the current code-completion handler.
1570 return CodeComplete;
1571 }
1572
1573 /// Clear out the code completion handler.
1575 CodeComplete = nullptr;
1576 }
1577
1578 /// Hook used by the lexer to invoke the "included file" code
1579 /// completion point.
1580 void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled);
1581
1582 /// Hook used by the lexer to invoke the "natural language" code
1583 /// completion point.
1585
1586 /// Set the code completion token for filtering purposes.
1588 CodeCompletionII = Filter;
1589 }
1590
1591 /// Set the code completion token range for detecting replacement range later
1592 /// on.
1594 const SourceLocation End) {
1595 CodeCompletionTokenRange = {Start, End};
1596 }
1598 return CodeCompletionTokenRange;
1599 }
1600
1601 /// Get the code completion token for filtering purposes.
1603 if (CodeCompletionII)
1604 return CodeCompletionII->getName();
1605 return {};
1606 }
1607
1608 /// Retrieve the preprocessing record, or NULL if there is no
1609 /// preprocessing record.
1611
1612 /// Create a new preprocessing record, which will keep track of
1613 /// all macro expansions, macro definitions, etc.
1615
1616 /// Returns true if the FileEntry is the PCH through header.
1617 bool isPCHThroughHeader(const FileEntry *FE);
1618
1619 /// True if creating a PCH with a through header.
1621
1622 /// True if using a PCH with a through header.
1624
1625 /// True if creating a PCH with a #pragma hdrstop.
1627
1628 /// True if using a PCH with a #pragma hdrstop.
1630
1631 /// Skip tokens until after the #include of the through header or
1632 /// until after a #pragma hdrstop.
1634
1635 /// Process directives while skipping until the through header or
1636 /// #pragma hdrstop is found.
1638 SourceLocation HashLoc);
1639
1640 /// Enter the specified FileID as the main source file,
1641 /// which implicitly adds the builtin defines etc.
1642 void EnterMainSourceFile();
1643
1644 /// Inform the preprocessor callbacks that processing is complete.
1645 void EndSourceFile();
1646
1647 /// Add a source file to the top of the include stack and
1648 /// start lexing tokens from it instead of the current buffer.
1649 ///
1650 /// Emits a diagnostic, doesn't enter the file, and returns true on error.
1652 SourceLocation Loc, bool IsFirstIncludeOfFile = true);
1653
1654 /// Add a Macro to the top of the include stack and start lexing
1655 /// tokens from it instead of the current buffer.
1656 ///
1657 /// \param Args specifies the tokens input to a function-like macro.
1658 /// \param ILEnd specifies the location of the ')' for a function-like macro
1659 /// or the identifier for an object-like macro.
1660 void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro,
1661 MacroArgs *Args);
1662
1663private:
1664 /// Add a "macro" context to the top of the include stack,
1665 /// which will cause the lexer to start returning the specified tokens.
1666 ///
1667 /// If \p DisableMacroExpansion is true, tokens lexed from the token stream
1668 /// will not be subject to further macro expansion. Otherwise, these tokens
1669 /// will be re-macro-expanded when/if expansion is enabled.
1670 ///
1671 /// If \p OwnsTokens is false, this method assumes that the specified stream
1672 /// of tokens has a permanent owner somewhere, so they do not need to be
1673 /// copied. If it is true, it assumes the array of tokens is allocated with
1674 /// \c new[] and the Preprocessor will delete[] it.
1675 ///
1676 /// If \p IsReinject the resulting tokens will have Token::IsReinjected flag
1677 /// set, see the flag documentation for details.
1678 void EnterTokenStream(const Token *Toks, unsigned NumToks,
1679 bool DisableMacroExpansion, bool OwnsTokens,
1680 bool IsReinject);
1681
1682public:
1683 void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1684 bool DisableMacroExpansion, bool IsReinject) {
1685 EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true,
1686 IsReinject);
1687 }
1688
1689 void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion,
1690 bool IsReinject) {
1691 EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false,
1692 IsReinject);
1693 }
1694
1695 /// Pop the current lexer/macro exp off the top of the lexer stack.
1696 ///
1697 /// This should only be used in situations where the current state of the
1698 /// top-of-stack lexer is known.
1699 void RemoveTopOfLexerStack();
1700
1701 /// From the point that this method is called, and until
1702 /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
1703 /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
1704 /// make the Preprocessor re-lex the same tokens.
1705 ///
1706 /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
1707 /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
1708 /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
1709 ///
1710 /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
1711 /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
1712 /// tokens will continue indefinitely.
1713 ///
1715
1716 /// Disable the last EnableBacktrackAtThisPos call.
1718
1719 /// Make Preprocessor re-lex the tokens that were lexed since
1720 /// EnableBacktrackAtThisPos() was previously called.
1721 void Backtrack();
1722
1723 /// True if EnableBacktrackAtThisPos() was called and
1724 /// caching of tokens is on.
1725 bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
1726
1727 /// Lex the next token for this preprocessor.
1728 void Lex(Token &Result);
1729
1730 /// Lex all tokens for this preprocessor until (and excluding) end of file.
1731 void LexTokensUntilEOF(std::vector<Token> *Tokens = nullptr);
1732
1733 /// Lex a token, forming a header-name token if possible.
1734 bool LexHeaderName(Token &Result, bool AllowMacroExpansion = true);
1735
1738
1740
1742 return CurSubmoduleState->VisibleModules.getImportLoc(M);
1743 }
1744
1745 /// Lex a string literal, which may be the concatenation of multiple
1746 /// string literals and may even come from macro expansion.
1747 /// \returns true on success, false if a error diagnostic has been generated.
1748 bool LexStringLiteral(Token &Result, std::string &String,
1749 const char *DiagnosticTag, bool AllowMacroExpansion) {
1750 if (AllowMacroExpansion)
1751 Lex(Result);
1752 else
1754 return FinishLexStringLiteral(Result, String, DiagnosticTag,
1755 AllowMacroExpansion);
1756 }
1757
1758 /// Complete the lexing of a string literal where the first token has
1759 /// already been lexed (see LexStringLiteral).
1760 bool FinishLexStringLiteral(Token &Result, std::string &String,
1761 const char *DiagnosticTag,
1762 bool AllowMacroExpansion);
1763
1764 /// Lex a token. If it's a comment, keep lexing until we get
1765 /// something not a comment.
1766 ///
1767 /// This is useful in -E -C mode where comments would foul up preprocessor
1768 /// directive handling.
1770 do
1771 Lex(Result);
1772 while (Result.getKind() == tok::comment);
1773 }
1774
1775 /// Just like Lex, but disables macro expansion of identifier tokens.
1777 // Disable macro expansion.
1778 bool OldVal = DisableMacroExpansion;
1779 DisableMacroExpansion = true;
1780 // Lex the token.
1781 Lex(Result);
1782
1783 // Reenable it.
1784 DisableMacroExpansion = OldVal;
1785 }
1786
1787 /// Like LexNonComment, but this disables macro expansion of
1788 /// identifier tokens.
1790 do
1792 while (Result.getKind() == tok::comment);
1793 }
1794
1795 /// Parses a simple integer literal to get its numeric value. Floating
1796 /// point literals and user defined literals are rejected. Used primarily to
1797 /// handle pragmas that accept integer arguments.
1798 bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value);
1799
1800 /// Disables macro expansion everywhere except for preprocessor directives.
1802 DisableMacroExpansion = true;
1803 MacroExpansionInDirectivesOverride = true;
1804 }
1805
1806 /// Peeks ahead N tokens and returns that token without consuming any
1807 /// tokens.
1808 ///
1809 /// LookAhead(0) returns the next token that would be returned by Lex(),
1810 /// LookAhead(1) returns the token after it, etc. This returns normal
1811 /// tokens after phase 5. As such, it is equivalent to using
1812 /// 'Lex', not 'LexUnexpandedToken'.
1813 const Token &LookAhead(unsigned N) {
1814 assert(LexLevel == 0 && "cannot use lookahead while lexing");
1815 if (CachedLexPos + N < CachedTokens.size())
1816 return CachedTokens[CachedLexPos+N];
1817 else
1818 return PeekAhead(N+1);
1819 }
1820
1821 /// When backtracking is enabled and tokens are cached,
1822 /// this allows to revert a specific number of tokens.
1823 ///
1824 /// Note that the number of tokens being reverted should be up to the last
1825 /// backtrack position, not more.
1826 void RevertCachedTokens(unsigned N) {
1827 assert(isBacktrackEnabled() &&
1828 "Should only be called when tokens are cached for backtracking");
1829 assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
1830 && "Should revert tokens up to the last backtrack position, not more");
1831 assert(signed(CachedLexPos) - signed(N) >= 0 &&
1832 "Corrupted backtrack positions ?");
1833 CachedLexPos -= N;
1834 }
1835
1836 /// Enters a token in the token stream to be lexed next.
1837 ///
1838 /// If BackTrack() is called afterwards, the token will remain at the
1839 /// insertion point.
1840 /// If \p IsReinject is true, resulting token will have Token::IsReinjected
1841 /// flag set. See the flag documentation for details.
1842 void EnterToken(const Token &Tok, bool IsReinject) {
1843 if (LexLevel) {
1844 // It's not correct in general to enter caching lex mode while in the
1845 // middle of a nested lexing action.
1846 auto TokCopy = std::make_unique<Token[]>(1);
1847 TokCopy[0] = Tok;
1848 EnterTokenStream(std::move(TokCopy), 1, true, IsReinject);
1849 } else {
1850 EnterCachingLexMode();
1851 assert(IsReinject && "new tokens in the middle of cached stream");
1852 CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
1853 }
1854 }
1855
1856 /// We notify the Preprocessor that if it is caching tokens (because
1857 /// backtrack is enabled) it should replace the most recent cached tokens
1858 /// with the given annotation token. This function has no effect if
1859 /// backtracking is not enabled.
1860 ///
1861 /// Note that the use of this function is just for optimization, so that the
1862 /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
1863 /// invoked.
1864 void AnnotateCachedTokens(const Token &Tok) {
1865 assert(Tok.isAnnotation() && "Expected annotation token");
1866 if (CachedLexPos != 0 && isBacktrackEnabled())
1867 AnnotatePreviousCachedTokens(Tok);
1868 }
1869
1870 /// Get the location of the last cached token, suitable for setting the end
1871 /// location of an annotation token.
1873 assert(CachedLexPos != 0);
1874 return CachedTokens[CachedLexPos-1].getLastLoc();
1875 }
1876
1877 /// Whether \p Tok is the most recent token (`CachedLexPos - 1`) in
1878 /// CachedTokens.
1879 bool IsPreviousCachedToken(const Token &Tok) const;
1880
1881 /// Replace token in `CachedLexPos - 1` in CachedTokens by the tokens
1882 /// in \p NewToks.
1883 ///
1884 /// Useful when a token needs to be split in smaller ones and CachedTokens
1885 /// most recent token must to be updated to reflect that.
1887
1888 /// Replace the last token with an annotation token.
1889 ///
1890 /// Like AnnotateCachedTokens(), this routine replaces an
1891 /// already-parsed (and resolved) token with an annotation
1892 /// token. However, this routine only replaces the last token with
1893 /// the annotation token; it does not affect any other cached
1894 /// tokens. This function has no effect if backtracking is not
1895 /// enabled.
1897 assert(Tok.isAnnotation() && "Expected annotation token");
1898 if (CachedLexPos != 0 && isBacktrackEnabled())
1899 CachedTokens[CachedLexPos-1] = Tok;
1900 }
1901
1902 /// Enter an annotation token into the token stream.
1904 void *AnnotationVal);
1905
1906 /// Determine whether it's possible for a future call to Lex to produce an
1907 /// annotation token created by a previous call to EnterAnnotationToken.
1909 return CurLexerCallback != CLK_Lexer;
1910 }
1911
1912 /// Update the current token to represent the provided
1913 /// identifier, in order to cache an action performed by typo correction.
1914 void TypoCorrectToken(const Token &Tok) {
1915 assert(Tok.getIdentifierInfo() && "Expected identifier token");
1916 if (CachedLexPos != 0 && isBacktrackEnabled())
1917 CachedTokens[CachedLexPos-1] = Tok;
1918 }
1919
1920 /// Recompute the current lexer kind based on the CurLexer/
1921 /// CurTokenLexer pointers.
1922 void recomputeCurLexerKind();
1923
1924 /// Returns true if incremental processing is enabled
1925 bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
1926
1927 /// Enables the incremental processing
1928 void enableIncrementalProcessing(bool value = true) {
1929 IncrementalProcessing = value;
1930 }
1931
1932 /// Specify the point at which code-completion will be performed.
1933 ///
1934 /// \param File the file in which code completion should occur. If
1935 /// this file is included multiple times, code-completion will
1936 /// perform completion the first time it is included. If NULL, this
1937 /// function clears out the code-completion point.
1938 ///
1939 /// \param Line the line at which code completion should occur
1940 /// (1-based).
1941 ///
1942 /// \param Column the column at which code completion should occur
1943 /// (1-based).
1944 ///
1945 /// \returns true if an error occurred, false otherwise.
1947 unsigned Column);
1948
1949 /// Determine if we are performing code completion.
1950 bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }
1951
1952 /// Returns the location of the code-completion point.
1953 ///
1954 /// Returns an invalid location if code-completion is not enabled or the file
1955 /// containing the code-completion point has not been lexed yet.
1956 SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
1957
1958 /// Returns the start location of the file of code-completion point.
1959 ///
1960 /// Returns an invalid location if code-completion is not enabled or the file
1961 /// containing the code-completion point has not been lexed yet.
1963 return CodeCompletionFileLoc;
1964 }
1965
1966 /// Returns true if code-completion is enabled and we have hit the
1967 /// code-completion point.
1968 bool isCodeCompletionReached() const { return CodeCompletionReached; }
1969
1970 /// Note that we hit the code-completion point.
1972 assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
1973 CodeCompletionReached = true;
1974 // Silence any diagnostics that occur after we hit the code-completion.
1976 }
1977
1978 /// The location of the currently-active \#pragma clang
1979 /// arc_cf_code_audited begin.
1980 ///
1981 /// Returns an invalid location if there is no such pragma active.
1982 std::pair<IdentifierInfo *, SourceLocation>
1984 return PragmaARCCFCodeAuditedInfo;
1985 }
1986
1987 /// Set the location of the currently-active \#pragma clang
1988 /// arc_cf_code_audited begin. An invalid location ends the pragma.
1990 SourceLocation Loc) {
1991 PragmaARCCFCodeAuditedInfo = {Ident, Loc};
1992 }
1993
1994 /// The location of the currently-active \#pragma clang
1995 /// assume_nonnull begin.
1996 ///
1997 /// Returns an invalid location if there is no such pragma active.
1999 return PragmaAssumeNonNullLoc;
2000 }
2001
2002 /// Set the location of the currently-active \#pragma clang
2003 /// assume_nonnull begin. An invalid location ends the pragma.
2005 PragmaAssumeNonNullLoc = Loc;
2006 }
2007
2008 /// Get the location of the recorded unterminated \#pragma clang
2009 /// assume_nonnull begin in the preamble, if one exists.
2010 ///
2011 /// Returns an invalid location if the premable did not end with
2012 /// such a pragma active or if there is no recorded preamble.
2014 return PreambleRecordedPragmaAssumeNonNullLoc;
2015 }
2016
2017 /// Record the location of the unterminated \#pragma clang
2018 /// assume_nonnull begin in the preamble.
2020 PreambleRecordedPragmaAssumeNonNullLoc = Loc;
2021 }
2022
2023 /// Set the directory in which the main file should be considered
2024 /// to have been found, if it is not a real file.
2025 void setMainFileDir(DirectoryEntryRef Dir) { MainFileDir = Dir; }
2026
2027 /// Instruct the preprocessor to skip part of the main source file.
2028 ///
2029 /// \param Bytes The number of bytes in the preamble to skip.
2030 ///
2031 /// \param StartOfLine Whether skipping these bytes puts the lexer at the
2032 /// start of a line.
2033 void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
2034 SkipMainFilePreamble.first = Bytes;
2035 SkipMainFilePreamble.second = StartOfLine;
2036 }
2037
2038 /// Forwarding function for diagnostics. This emits a diagnostic at
2039 /// the specified Token's location, translating the token's start
2040 /// position in the current buffer into a SourcePosition object for rendering.
2041 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
2042 return Diags->Report(Loc, DiagID);
2043 }
2044
2045 DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
2046 return Diags->Report(Tok.getLocation(), DiagID);
2047 }
2048
2049 /// Return the 'spelling' of the token at the given
2050 /// location; does not go up to the spelling location or down to the
2051 /// expansion location.
2052 ///
2053 /// \param buffer A buffer which will be used only if the token requires
2054 /// "cleaning", e.g. if it contains trigraphs or escaped newlines
2055 /// \param invalid If non-null, will be set \c true if an error occurs.
2057 SmallVectorImpl<char> &buffer,
2058 bool *invalid = nullptr) const {
2059 return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
2060 }
2061
2062 /// Return the 'spelling' of the Tok token.
2063 ///
2064 /// The spelling of a token is the characters used to represent the token in
2065 /// the source file after trigraph expansion and escaped-newline folding. In
2066 /// particular, this wants to get the true, uncanonicalized, spelling of
2067 /// things like digraphs, UCNs, etc.
2068 ///
2069 /// \param Invalid If non-null, will be set \c true if an error occurs.
2070 std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const {
2071 return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
2072 }
2073
2074 /// Get the spelling of a token into a preallocated buffer, instead
2075 /// of as an std::string.
2076 ///
2077 /// The caller is required to allocate enough space for the token, which is
2078 /// guaranteed to be at least Tok.getLength() bytes long. The length of the
2079 /// actual result is returned.
2080 ///
2081 /// Note that this method may do two possible things: it may either fill in
2082 /// the buffer specified with characters, or it may *change the input pointer*
2083 /// to point to a constant buffer with the data already in it (avoiding a
2084 /// copy). The caller is not allowed to modify the returned buffer pointer
2085 /// if an internal buffer is returned.
2086 unsigned getSpelling(const Token &Tok, const char *&Buffer,
2087 bool *Invalid = nullptr) const {
2088 return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
2089 }
2090
2091 /// Get the spelling of a token into a SmallVector.
2092 ///
2093 /// Note that the returned StringRef may not point to the
2094 /// supplied buffer if a copy can be avoided.
2095 StringRef getSpelling(const Token &Tok,
2096 SmallVectorImpl<char> &Buffer,
2097 bool *Invalid = nullptr) const;
2098
2099 /// Relex the token at the specified location.
2100 /// \returns true if there was a failure, false on success.
2102 bool IgnoreWhiteSpace = false) {
2103 return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace);
2104 }
2105
2106 /// Given a Token \p Tok that is a numeric constant with length 1,
2107 /// return the character.
2108 char
2110 bool *Invalid = nullptr) const {
2111 assert(Tok.is(tok::numeric_constant) &&
2112 Tok.getLength() == 1 && "Called on unsupported token");
2113 assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
2114
2115 // If the token is carrying a literal data pointer, just use it.
2116 if (const char *D = Tok.getLiteralData())
2117 return *D;
2118
2119 // Otherwise, fall back on getCharacterData, which is slower, but always
2120 // works.
2121 return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
2122 }
2123
2124 /// Retrieve the name of the immediate macro expansion.
2125 ///
2126 /// This routine starts from a source location, and finds the name of the
2127 /// macro responsible for its immediate expansion. It looks through any
2128 /// intervening macro argument expansions to compute this. It returns a
2129 /// StringRef that refers to the SourceManager-owned buffer of the source
2130 /// where that macro name is spelled. Thus, the result shouldn't out-live
2131 /// the SourceManager.
2133 return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
2134 }
2135
2136 /// Plop the specified string into a scratch buffer and set the
2137 /// specified token's location and length to it.
2138 ///
2139 /// If specified, the source location provides a location of the expansion
2140 /// point of the token.
2141 void CreateString(StringRef Str, Token &Tok,
2142 SourceLocation ExpansionLocStart = SourceLocation(),
2143 SourceLocation ExpansionLocEnd = SourceLocation());
2144
2145 /// Split the first Length characters out of the token starting at TokLoc
2146 /// and return a location pointing to the split token. Re-lexing from the
2147 /// split token will return the split token rather than the original.
2148 SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length);
2149
2150 /// Computes the source location just past the end of the
2151 /// token at this source location.
2152 ///
2153 /// This routine can be used to produce a source location that
2154 /// points just past the end of the token referenced by \p Loc, and
2155 /// is generally used when a diagnostic needs to point just after a
2156 /// token where it expected something different that it received. If
2157 /// the returned source location would not be meaningful (e.g., if
2158 /// it points into a macro), this routine returns an invalid
2159 /// source location.
2160 ///
2161 /// \param Offset an offset from the end of the token, where the source
2162 /// location should refer to. The default offset (0) produces a source
2163 /// location pointing just past the end of the token; an offset of 1 produces
2164 /// a source location pointing to the last character in the token, etc.
2166 return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
2167 }
2168
2169 /// Returns true if the given MacroID location points at the first
2170 /// token of the macro expansion.
2171 ///
2172 /// \param MacroBegin If non-null and function returns true, it is set to
2173 /// begin location of the macro.
2175 SourceLocation *MacroBegin = nullptr) const {
2176 return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
2177 MacroBegin);
2178 }
2179
2180 /// Returns true if the given MacroID location points at the last
2181 /// token of the macro expansion.
2182 ///
2183 /// \param MacroEnd If non-null and function returns true, it is set to
2184 /// end location of the macro.
2186 SourceLocation *MacroEnd = nullptr) const {
2187 return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
2188 }
2189
2190 /// Print the token to stderr, used for debugging.
2191 void DumpToken(const Token &Tok, bool DumpFlags = false) const;
2192 void DumpLocation(SourceLocation Loc) const;
2193 void DumpMacro(const MacroInfo &MI) const;
2194 void dumpMacroInfo(const IdentifierInfo *II);
2195
2196 /// Given a location that specifies the start of a
2197 /// token, return a new location that specifies a character within the token.
2199 unsigned Char) const {
2200 return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
2201 }
2202
2203 /// Increment the counters for the number of token paste operations
2204 /// performed.
2205 ///
2206 /// If fast was specified, this is a 'fast paste' case we handled.
2207 void IncrementPasteCounter(bool isFast) {
2208 if (isFast)
2209 ++NumFastTokenPaste;
2210 else
2211 ++NumTokenPaste;
2212 }
2213
2214 void PrintStats();
2215
2216 size_t getTotalMemory() const;
2217
2218 /// When the macro expander pastes together a comment (/##/) in Microsoft
2219 /// mode, this method handles updating the current state, returning the
2220 /// token on the next source line.
2222
2223 //===--------------------------------------------------------------------===//
2224 // Preprocessor callback methods. These are invoked by a lexer as various
2225 // directives and events are found.
2226
2227 /// Given a tok::raw_identifier token, look up the
2228 /// identifier information for the token and install it into the token,
2229 /// updating the token kind accordingly.
2231
2232private:
2233 llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
2234
2235public:
2236 /// Specifies the reason for poisoning an identifier.
2237 ///
2238 /// If that identifier is accessed while poisoned, then this reason will be
2239 /// used instead of the default "poisoned" diagnostic.
2240 void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
2241
2242 /// Display reason for poisoned identifier.
2244
2246 if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
2247 if(II->isPoisoned()) {
2249 }
2250 }
2251 }
2252
2253private:
2254 /// Identifiers used for SEH handling in Borland. These are only
2255 /// allowed in particular circumstances
2256 // __except block
2257 IdentifierInfo *Ident__exception_code,
2258 *Ident___exception_code,
2259 *Ident_GetExceptionCode;
2260 // __except filter expression
2261 IdentifierInfo *Ident__exception_info,
2262 *Ident___exception_info,
2263 *Ident_GetExceptionInfo;
2264 // __finally
2265 IdentifierInfo *Ident__abnormal_termination,
2266 *Ident___abnormal_termination,
2267 *Ident_AbnormalTermination;
2268
2269 const char *getCurLexerEndPos();
2270 void diagnoseMissingHeaderInUmbrellaDir(const Module &Mod);
2271
2272public:
2273 void PoisonSEHIdentifiers(bool Poison = true); // Borland
2274
2275 /// Callback invoked when the lexer reads an identifier and has
2276 /// filled in the tokens IdentifierInfo member.
2277 ///
2278 /// This callback potentially macro expands it or turns it into a named
2279 /// token (like 'for').
2280 ///
2281 /// \returns true if we actually computed a token, false if we need to
2282 /// lex again.
2284
2285 /// Callback invoked when the lexer hits the end of the current file.
2286 ///
2287 /// This either returns the EOF token and returns true, or
2288 /// pops a level off the include stack and returns false, at which point the
2289 /// client should call lex again.
2290 bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
2291
2292 /// Callback invoked when the current TokenLexer hits the end of its
2293 /// token stream.
2295
2296 /// Callback invoked when the lexer sees a # token at the start of a
2297 /// line.
2298 ///
2299 /// This consumes the directive, modifies the lexer/preprocessor state, and
2300 /// advances the lexer(s) so that the next token read is the correct one.
2302
2303 /// Ensure that the next token is a tok::eod token.
2304 ///
2305 /// If not, emit a diagnostic and consume up until the eod.
2306 /// If \p EnableMacros is true, then we consider macros that expand to zero
2307 /// tokens as being ok.
2308 ///
2309 /// \return The location of the end of the directive (the terminating
2310 /// newline).
2311 SourceLocation CheckEndOfDirective(const char *DirType,
2312 bool EnableMacros = false);
2313
2314 /// Read and discard all tokens remaining on the current line until
2315 /// the tok::eod token is found. Returns the range of the skipped tokens.
2317
2318 /// Returns true if the preprocessor has seen a use of
2319 /// __DATE__ or __TIME__ in the file so far.
2320 bool SawDateOrTime() const {
2321 return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
2322 }
2323 unsigned getCounterValue() const { return CounterValue; }
2324 void setCounterValue(unsigned V) { CounterValue = V; }
2325
2327 assert(CurrentFPEvalMethod != LangOptions::FEM_UnsetOnCommandLine &&
2328 "FPEvalMethod should be set either from command line or from the "
2329 "target info");
2330 return CurrentFPEvalMethod;
2331 }
2332
2334 return TUFPEvalMethod;
2335 }
2336
2338 return LastFPEvalPragmaLocation;
2339 }
2340
2344 "FPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2345 // This is the location of the '#pragma float_control" where the
2346 // execution state is modifed.
2347 LastFPEvalPragmaLocation = PragmaLoc;
2348 CurrentFPEvalMethod = Val;
2349 TUFPEvalMethod = Val;
2350 }
2351
2354 "TUPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2355 TUFPEvalMethod = Val;
2356 }
2357
2358 /// Retrieves the module that we're currently building, if any.
2360
2361 /// Retrieves the module whose implementation we're current compiling, if any.
2363
2364 /// If we are preprocessing a named module.
2365 bool isInNamedModule() const { return ModuleDeclState.isNamedModule(); }
2366
2367 /// If we are proprocessing a named interface unit.
2368 /// Note that a module implementation partition is not considered as an
2369 /// named interface unit here although it is importable
2370 /// to ease the parsing.
2372 return ModuleDeclState.isNamedInterface();
2373 }
2374
2375 /// Get the named module name we're preprocessing.
2376 /// Requires we're preprocessing a named module.
2377 StringRef getNamedModuleName() const { return ModuleDeclState.getName(); }
2378
2379 /// If we are implementing an implementation module unit.
2380 /// Note that the module implementation partition is not considered as an
2381 /// implementation unit.
2383 return ModuleDeclState.isImplementationUnit();
2384 }
2385
2386 /// If we're importing a standard C++20 Named Modules.
2388 // NamedModuleImportPath will be non-empty only if we're importing
2389 // Standard C++ named modules.
2390 return !NamedModuleImportPath.empty() && getLangOpts().CPlusPlusModules &&
2391 !IsAtImport;
2392 }
2393
2394 /// Allocate a new MacroInfo object with the provided SourceLocation.
2396
2397 /// Turn the specified lexer token into a fully checked and spelled
2398 /// filename, e.g. as an operand of \#include.
2399 ///
2400 /// The caller is expected to provide a buffer that is large enough to hold
2401 /// the spelling of the filename, but is also expected to handle the case
2402 /// when this method decides to use a different buffer.
2403 ///
2404 /// \returns true if the input filename was in <>'s or false if it was
2405 /// in ""'s.
2406 bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Buffer);
2407
2408 /// Given a "foo" or <foo> reference, look up the indicated file.
2409 ///
2410 /// Returns std::nullopt on failure. \p isAngled indicates whether the file
2411 /// reference is for system \#include's or not (i.e. using <> instead of "").
2413 LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
2414 ConstSearchDirIterator FromDir, const FileEntry *FromFile,
2415 ConstSearchDirIterator *CurDir, SmallVectorImpl<char> *SearchPath,
2416 SmallVectorImpl<char> *RelativePath,
2417 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
2418 bool *IsFrameworkFound, bool SkipCache = false,
2419 bool OpenFile = true, bool CacheFailures = true);
2420
2421 /// Return true if we're in the top-level file, not in a \#include.
2422 bool isInPrimaryFile() const;
2423
2424 /// Lex an on-off-switch (C99 6.10.6p2) and verify that it is
2425 /// followed by EOD. Return true if the token is not a valid on-off-switch.
2427
2428 bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
2429 bool *ShadowFlag = nullptr);
2430
2431 void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma);
2432 Module *LeaveSubmodule(bool ForPragma);
2433
2434private:
2436
2437 void PushIncludeMacroStack() {
2438 assert(CurLexerCallback != CLK_CachingLexer &&
2439 "cannot push a caching lexer");
2440 IncludeMacroStack.emplace_back(CurLexerCallback, CurLexerSubmodule,
2441 std::move(CurLexer), CurPPLexer,
2442 std::move(CurTokenLexer), CurDirLookup);
2443 CurPPLexer = nullptr;
2444 }
2445
2446 void PopIncludeMacroStack() {
2447 CurLexer = std::move(IncludeMacroStack.back().TheLexer);
2448 CurPPLexer = IncludeMacroStack.back().ThePPLexer;
2449 CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
2450 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
2451 CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule;
2452 CurLexerCallback = IncludeMacroStack.back().CurLexerCallback;
2453 IncludeMacroStack.pop_back();
2454 }
2455
2456 void PropagateLineStartLeadingSpaceInfo(Token &Result);
2457
2458 /// Determine whether we need to create module macros for #defines in the
2459 /// current context.
2460 bool needModuleMacros() const;
2461
2462 /// Update the set of active module macros and ambiguity flag for a module
2463 /// macro name.
2464 void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
2465
2466 DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
2467 SourceLocation Loc);
2468 UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
2469 VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
2470 bool isPublic);
2471
2472 /// Lex and validate a macro name, which occurs after a
2473 /// \#define or \#undef.
2474 ///
2475 /// \param MacroNameTok Token that represents the name defined or undefined.
2476 /// \param IsDefineUndef Kind if preprocessor directive.
2477 /// \param ShadowFlag Points to flag that is set if macro name shadows
2478 /// a keyword.
2479 ///
2480 /// This emits a diagnostic, sets the token kind to eod,
2481 /// and discards the rest of the macro line if the macro name is invalid.
2482 void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other,
2483 bool *ShadowFlag = nullptr);
2484
2485 /// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2486 /// entire line) of the macro's tokens and adds them to MacroInfo, and while
2487 /// doing so performs certain validity checks including (but not limited to):
2488 /// - # (stringization) is followed by a macro parameter
2489 /// \param MacroNameTok - Token that represents the macro name
2490 /// \param ImmediatelyAfterHeaderGuard - Macro follows an #ifdef header guard
2491 ///
2492 /// Either returns a pointer to a MacroInfo object OR emits a diagnostic and
2493 /// returns a nullptr if an invalid sequence of tokens is encountered.
2494 MacroInfo *ReadOptionalMacroParameterListAndBody(
2495 const Token &MacroNameTok, bool ImmediatelyAfterHeaderGuard);
2496
2497 /// The ( starting an argument list of a macro definition has just been read.
2498 /// Lex the rest of the parameters and the closing ), updating \p MI with
2499 /// what we learn and saving in \p LastTok the last token read.
2500 /// Return true if an error occurs parsing the arg list.
2501 bool ReadMacroParameterList(MacroInfo *MI, Token& LastTok);
2502
2503 /// Provide a suggestion for a typoed directive. If there is no typo, then
2504 /// just skip suggesting.
2505 ///
2506 /// \param Tok - Token that represents the directive
2507 /// \param Directive - String reference for the directive name
2508 void SuggestTypoedDirective(const Token &Tok, StringRef Directive) const;
2509
2510 /// We just read a \#if or related directive and decided that the
2511 /// subsequent tokens are in the \#if'd out portion of the
2512 /// file. Lex the rest of the file, until we see an \#endif. If \p
2513 /// FoundNonSkipPortion is true, then we have already emitted code for part of
2514 /// this \#if directive, so \#else/\#elif blocks should never be entered. If
2515 /// \p FoundElse is false, then \#else directives are ok, if not, then we have
2516 /// already seen one so a \#else directive is a duplicate. When this returns,
2517 /// the caller can lex the first valid token.
2518 void SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
2519 SourceLocation IfTokenLoc,
2520 bool FoundNonSkipPortion, bool FoundElse,
2521 SourceLocation ElseLoc = SourceLocation());
2522
2523 /// Information about the result for evaluating an expression for a
2524 /// preprocessor directive.
2525 struct DirectiveEvalResult {
2526 /// Whether the expression was evaluated as true or not.
2527 bool Conditional;
2528
2529 /// True if the expression contained identifiers that were undefined.
2530 bool IncludedUndefinedIds;
2531
2532 /// The source range for the expression.
2533 SourceRange ExprRange;
2534 };
2535
2536 /// Evaluate an integer constant expression that may occur after a
2537 /// \#if or \#elif directive and return a \p DirectiveEvalResult object.
2538 ///
2539 /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
2540 DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
2541
2542 /// Process a '__has_include("path")' expression.
2543 ///
2544 /// Returns true if successful.
2545 bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II);
2546
2547 /// Process '__has_include_next("path")' expression.
2548 ///
2549 /// Returns true if successful.
2550 bool EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II);
2551
2552 /// Get the directory and file from which to start \#include_next lookup.
2553 std::pair<ConstSearchDirIterator, const FileEntry *>
2554 getIncludeNextStart(const Token &IncludeNextTok) const;
2555
2556 /// Install the standard preprocessor pragmas:
2557 /// \#pragma GCC poison/system_header/dependency and \#pragma once.
2558 void RegisterBuiltinPragmas();
2559
2560 /// Register builtin macros such as __LINE__ with the identifier table.
2561 void RegisterBuiltinMacros();
2562
2563 /// If an identifier token is read that is to be expanded as a macro, handle
2564 /// it and return the next token as 'Tok'. If we lexed a token, return true;
2565 /// otherwise the caller should lex again.
2566 bool HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &MD);
2567
2568 /// Cache macro expanded tokens for TokenLexers.
2569 //
2570 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
2571 /// going to lex in the cache and when it finishes the tokens are removed
2572 /// from the end of the cache.
2573 Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
2574 ArrayRef<Token> tokens);
2575
2576 void removeCachedMacroExpandedTokensOfLastLexer();
2577
2578 /// Determine whether the next preprocessor token to be
2579 /// lexed is a '('. If so, consume the token and return true, if not, this
2580 /// method should have no observable side-effect on the lexed tokens.
2581 bool isNextPPTokenLParen();
2582
2583 /// After reading "MACRO(", this method is invoked to read all of the formal
2584 /// arguments specified for the macro invocation. Returns null on error.
2585 MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI,
2586 SourceLocation &MacroEnd);
2587
2588 /// If an identifier token is read that is to be expanded
2589 /// as a builtin macro, handle it and return the next token as 'Tok'.
2590 void ExpandBuiltinMacro(Token &Tok);
2591
2592 /// Read a \c _Pragma directive, slice it up, process it, then
2593 /// return the first token after the directive.
2594 /// This assumes that the \c _Pragma token has just been read into \p Tok.
2595 void Handle_Pragma(Token &Tok);
2596
2597 /// Like Handle_Pragma except the pragma text is not enclosed within
2598 /// a string literal.
2599 void HandleMicrosoft__pragma(Token &Tok);
2600
2601 /// Add a lexer to the top of the include stack and
2602 /// start lexing tokens from it instead of the current buffer.
2603 void EnterSourceFileWithLexer(Lexer *TheLexer, ConstSearchDirIterator Dir);
2604
2605 /// Set the FileID for the preprocessor predefines.
2606 void setPredefinesFileID(FileID FID) {
2607 assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
2608 PredefinesFileID = FID;
2609 }
2610
2611 /// Set the FileID for the PCH through header.
2612 void setPCHThroughHeaderFileID(FileID FID);
2613
2614 /// Returns true if we are lexing from a file and not a
2615 /// pragma or a macro.
2616 static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
2617 return L ? !L->isPragmaLexer() : P != nullptr;
2618 }
2619
2620 static bool IsFileLexer(const IncludeStackInfo& I) {
2621 return IsFileLexer(I.TheLexer.get(), I.ThePPLexer);
2622 }
2623
2624 bool IsFileLexer() const {
2625 return IsFileLexer(CurLexer.get(), CurPPLexer);
2626 }
2627
2628 //===--------------------------------------------------------------------===//
2629 // Caching stuff.
2630 void CachingLex(Token &Result);
2631
2632 bool InCachingLexMode() const {
2633 // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
2634 // that we are past EOF, not that we are in CachingLex mode.
2635 return !CurPPLexer && !CurTokenLexer && !IncludeMacroStack.empty();
2636 }
2637
2638 void EnterCachingLexMode();
2639 void EnterCachingLexModeUnchecked();
2640
2641 void ExitCachingLexMode() {
2642 if (InCachingLexMode())
2644 }
2645
2646 const Token &PeekAhead(unsigned N);
2647 void AnnotatePreviousCachedTokens(const Token &Tok);
2648
2649 //===--------------------------------------------------------------------===//
2650 /// Handle*Directive - implement the various preprocessor directives. These
2651 /// should side-effect the current preprocessor object so that the next call
2652 /// to Lex() will return the appropriate token next.
2653 void HandleLineDirective();
2654 void HandleDigitDirective(Token &Tok);
2655 void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
2656 void HandleIdentSCCSDirective(Token &Tok);
2657 void HandleMacroPublicDirective(Token &Tok);
2658 void HandleMacroPrivateDirective();
2659
2660 /// An additional notification that can be produced by a header inclusion or
2661 /// import to tell the parser what happened.
2662 struct ImportAction {
2663 enum ActionKind {
2664 None,
2665 ModuleBegin,
2666 ModuleImport,
2667 HeaderUnitImport,
2668 SkippedModuleImport,
2669 Failure,
2670 } Kind;
2671 Module *ModuleForHeader = nullptr;
2672
2673 ImportAction(ActionKind AK, Module *Mod = nullptr)
2674 : Kind(AK), ModuleForHeader(Mod) {
2675 assert((AK == None || Mod || AK == Failure) &&
2676 "no module for module action");
2677 }
2678 };
2679
2680 OptionalFileEntryRef LookupHeaderIncludeOrImport(
2681 ConstSearchDirIterator *CurDir, StringRef &Filename,
2682 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2683 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2684 bool &IsMapped, ConstSearchDirIterator LookupFrom,
2685 const FileEntry *LookupFromFile, StringRef &LookupFilename,
2686 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2687 ModuleMap::KnownHeader &SuggestedModule, bool isAngled);
2688
2689 // File inclusion.
2690 void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok,
2691 ConstSearchDirIterator LookupFrom = nullptr,
2692 const FileEntry *LookupFromFile = nullptr);
2693 ImportAction
2694 HandleHeaderIncludeOrImport(SourceLocation HashLoc, Token &IncludeTok,
2695 Token &FilenameTok, SourceLocation EndLoc,
2696 ConstSearchDirIterator LookupFrom = nullptr,
2697 const FileEntry *LookupFromFile = nullptr);
2698 void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
2699 void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
2700 void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
2701 void HandleMicrosoftImportDirective(Token &Tok);
2702
2703public:
2704 /// Check that the given module is available, producing a diagnostic if not.
2705 /// \return \c true if the check failed (because the module is not available).
2706 /// \c false if the module appears to be usable.
2707 static bool checkModuleIsAvailable(const LangOptions &LangOpts,
2708 const TargetInfo &TargetInfo,
2709 const Module &M, DiagnosticsEngine &Diags);
2710
2711 // Module inclusion testing.
2712 /// Find the module that owns the source or header file that
2713 /// \p Loc points to. If the location is in a file that was included
2714 /// into a module, or is outside any module, returns nullptr.
2715 Module *getModuleForLocation(SourceLocation Loc, bool AllowTextual);
2716
2717 /// We want to produce a diagnostic at location IncLoc concerning an
2718 /// unreachable effect at location MLoc (eg, where a desired entity was
2719 /// declared or defined). Determine whether the right way to make MLoc
2720 /// reachable is by #include, and if so, what header should be included.
2721 ///
2722 /// This is not necessarily fast, and might load unexpected module maps, so
2723 /// should only be called by code that intends to produce an error.
2724 ///
2725 /// \param IncLoc The location at which the missing effect was detected.
2726 /// \param MLoc A location within an unimported module at which the desired
2727 /// effect occurred.
2728 /// \return A file that can be #included to provide the desired effect. Null
2729 /// if no such file could be determined or if a #include is not
2730 /// appropriate (eg, if a module should be imported instead).
2732 SourceLocation MLoc);
2733
2734 bool isRecordingPreamble() const {
2735 return PreambleConditionalStack.isRecording();
2736 }
2737
2738 bool hasRecordedPreamble() const {
2739 return PreambleConditionalStack.hasRecordedPreamble();
2740 }
2741
2743 return PreambleConditionalStack.getStack();
2744 }
2745
2747 PreambleConditionalStack.setStack(s);
2748 }
2749
2751 ArrayRef<PPConditionalInfo> s, std::optional<PreambleSkipInfo> SkipInfo) {
2752 PreambleConditionalStack.startReplaying();
2753 PreambleConditionalStack.setStack(s);
2754 PreambleConditionalStack.SkipInfo = SkipInfo;
2755 }
2756
2757 std::optional<PreambleSkipInfo> getPreambleSkipInfo() const {
2758 return PreambleConditionalStack.SkipInfo;
2759 }
2760
2761private:
2762 /// After processing predefined file, initialize the conditional stack from
2763 /// the preamble.
2764 void replayPreambleConditionalStack();
2765
2766 // Macro handling.
2767 void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
2768 void HandleUndefDirective();
2769
2770 // Conditional Inclusion.
2771 void HandleIfdefDirective(Token &Result, const Token &HashToken,
2772 bool isIfndef, bool ReadAnyTokensBeforeDirective);
2773 void HandleIfDirective(Token &IfToken, const Token &HashToken,
2774 bool ReadAnyTokensBeforeDirective);
2775 void HandleEndifDirective(Token &EndifToken);
2776 void HandleElseDirective(Token &Result, const Token &HashToken);
2777 void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken,
2778 tok::PPKeywordKind Kind);
2779
2780 // Pragmas.
2781 void HandlePragmaDirective(PragmaIntroducer Introducer);
2782
2783public:
2784 void HandlePragmaOnce(Token &OnceTok);
2785 void HandlePragmaMark(Token &MarkTok);
2786 void HandlePragmaPoison();
2787 void HandlePragmaSystemHeader(Token &SysHeaderTok);
2788 void HandlePragmaDependency(Token &DependencyTok);
2789 void HandlePragmaPushMacro(Token &Tok);
2790 void HandlePragmaPopMacro(Token &Tok);
2792 void HandlePragmaModuleBuild(Token &Tok);
2793 void HandlePragmaHdrstop(Token &Tok);
2795
2796 // Return true and store the first token only if any CommentHandler
2797 // has inserted some tokens and getCommentRetentionState() is false.
2798 bool HandleComment(Token &result, SourceRange Comment);
2799
2800 /// A macro is used, update information about macros that need unused
2801 /// warnings.
2802 void markMacroAsUsed(MacroInfo *MI);
2803
2804 void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg,
2805 SourceLocation AnnotationLoc) {
2806 auto Annotations = AnnotationInfos.find(II);
2807 if (Annotations == AnnotationInfos.end())
2808 AnnotationInfos.insert(std::make_pair(
2809 II,
2810 MacroAnnotations::makeDeprecation(AnnotationLoc, std::move(Msg))));
2811 else
2812 Annotations->second.DeprecationInfo =
2813 MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
2814 }
2815
2816 void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg,
2817 SourceLocation AnnotationLoc) {
2818 auto Annotations = AnnotationInfos.find(II);
2819 if (Annotations == AnnotationInfos.end())
2820 AnnotationInfos.insert(
2821 std::make_pair(II, MacroAnnotations::makeRestrictExpansion(
2822 AnnotationLoc, std::move(Msg))));
2823 else
2824 Annotations->second.RestrictExpansionInfo =
2825 MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
2826 }
2827
2828 void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) {
2829 auto Annotations = AnnotationInfos.find(II);
2830 if (Annotations == AnnotationInfos.end())
2831 AnnotationInfos.insert(
2832 std::make_pair(II, MacroAnnotations::makeFinal(AnnotationLoc)));
2833 else
2834 Annotations->second.FinalAnnotationLoc = AnnotationLoc;
2835 }
2836
2837 const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const {
2838 return AnnotationInfos.find(II)->second;
2839 }
2840
2842 bool IsIfnDef = false) const {
2843 IdentifierInfo *Info = Identifier.getIdentifierInfo();
2844 if (Info->isDeprecatedMacro())
2845 emitMacroDeprecationWarning(Identifier);
2846
2847 if (Info->isRestrictExpansion() &&
2848 !SourceMgr.isInMainFile(Identifier.getLocation()))
2849 emitRestrictExpansionWarning(Identifier);
2850
2851 if (!IsIfnDef) {
2852 if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs)
2853 emitRestrictInfNaNWarning(Identifier, 0);
2854 if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs)
2855 emitRestrictInfNaNWarning(Identifier, 1);
2856 }
2857 }
2858
2860 const LangOptions &LangOpts,
2861 const TargetInfo &TI);
2862
2864 const PresumedLoc &PLoc,
2865 const LangOptions &LangOpts,
2866 const TargetInfo &TI);
2867
2868private:
2869 void emitMacroDeprecationWarning(const Token &Identifier) const;
2870 void emitRestrictExpansionWarning(const Token &Identifier) const;
2871 void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const;
2872 void emitRestrictInfNaNWarning(const Token &Identifier,
2873 unsigned DiagSelection) const;
2874
2875 /// This boolean state keeps track if the current scanned token (by this PP)
2876 /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a
2877 /// translation unit in a linear order.
2878 bool InSafeBufferOptOutRegion = false;
2879
2880 /// Hold the start location of the current "-Wunsafe-buffer-usage" opt-out
2881 /// region if PP is currently in such a region. Hold undefined value
2882 /// otherwise.
2883 SourceLocation CurrentSafeBufferOptOutStart; // It is used to report the start location of an never-closed region.
2884
2885 // An ordered sequence of "-Wunsafe-buffer-usage" opt-out regions in one
2886 // translation unit. Each region is represented by a pair of start and end
2887 // locations. A region is "open" if its' start and end locations are
2888 // identical.
2890
2891public:
2892 /// \return true iff the given `Loc` is in a "-Wunsafe-buffer-usage" opt-out
2893 /// region. This `Loc` must be a source location that has been pre-processed.
2894 bool isSafeBufferOptOut(const SourceManager&SourceMgr, const SourceLocation &Loc) const;
2895
2896 /// Alter the state of whether this PP currently is in a
2897 /// "-Wunsafe-buffer-usage" opt-out region.
2898 ///
2899 /// \param isEnter true if this PP is entering a region; otherwise, this PP
2900 /// is exiting a region
2901 /// \param Loc the location of the entry or exit of a
2902 /// region
2903 /// \return true iff it is INVALID to enter or exit a region, i.e.,
2904 /// attempt to enter a region before exiting a previous region, or exiting a
2905 /// region that PP is not currently in.
2906 bool enterOrExitSafeBufferOptOutRegion(bool isEnter,
2907 const SourceLocation &Loc);
2908
2909 /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2910 /// opt-out region
2912
2913 /// \param StartLoc output argument. It will be set to the start location of
2914 /// the current "-Wunsafe-buffer-usage" opt-out region iff this function
2915 /// returns true.
2916 /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2917 /// opt-out region
2919
2920private:
2921 /// Helper functions to forward lexing to the actual lexer. They all share the
2922 /// same signature.
2923 static bool CLK_Lexer(Preprocessor &P, Token &Result) {
2924 return P.CurLexer->Lex(Result);
2925 }
2926 static bool CLK_TokenLexer(Preprocessor &P, Token &Result) {
2927 return P.CurTokenLexer->Lex(Result);
2928 }
2929 static bool CLK_CachingLexer(Preprocessor &P, Token &Result) {
2930 P.CachingLex(Result);
2931 return true;
2932 }
2933 static bool CLK_DependencyDirectivesLexer(Preprocessor &P, Token &Result) {
2934 return P.CurLexer->LexDependencyDirectiveToken(Result);
2935 }
2936 static bool CLK_LexAfterModuleImport(Preprocessor &P, Token &Result) {
2937 return P.LexAfterModuleImport(Result);
2938 }
2939};
2940
2941/// Abstract base class that describes a handler that will receive
2942/// source ranges for each of the comments encountered in the source file.
2944public:
2946
2947 // The handler shall return true if it has pushed any tokens
2948 // to be read using e.g. EnterToken or EnterTokenStream.
2949 virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
2950};
2951
2952/// Abstract base class that describes a handler that will receive
2953/// source ranges for empty lines encountered in the source file.
2955public:
2957
2958 // The handler handles empty lines.
2959 virtual void HandleEmptyline(SourceRange Range) = 0;
2960};
2961
2962/// Registry of pragma handlers added by plugins
2963using PragmaHandlerRegistry = llvm::Registry<PragmaHandler>;
2964
2965} // namespace clang
2966
2967#endif // LLVM_CLANG_LEX_PREPROCESSOR_H
#define V(N, I)
Definition: ASTContext.h:3259
int Id
Definition: ASTDiff.cpp:190
StringRef P
#define SM(sm)
Definition: Cuda.cpp:82
Defines the Diagnostic-related interfaces.
static constexpr Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:32
Defines the Diagnostic IDs-related interfaces.
StringRef Filename
Definition: Format.cpp:2980
StringRef Identifier
Definition: Format.cpp:2988
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
Defines the clang::MacroInfo and clang::MacroDirective classes.
Defines the clang::Module class, which describes a module in the source code.
Defines the PPCallbacks interface.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Defines the clang::TokenKind enum and support functions.
__device__ __2f16 float __ockl_bool s
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:366
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition: Builtins.h:85
Callback handler that receives notifications when performing code completion within the preprocessor.
Abstract base class that describes a handler that will receive source ranges for each of the comments...
virtual bool HandleComment(Preprocessor &PP, SourceRange Comment)=0
A directive for a defined macro or a macro imported from a module.
Definition: MacroInfo.h:432
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1271
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1547
void setSuppressAllDiagnostics(bool Val)
Suppress all diagnostics, to silence the front end when we know that we don't want any more diagnosti...
Definition: Diagnostic.h:697
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Abstract base class that describes a handler that will receive source ranges for empty lines encounte...
virtual void HandleEmptyline(SourceRange Range)=0
Abstract interface for external sources of preprocessor information.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:300
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:232
HeaderFileInfo & getFileInfo(FileEntryRef FE)
Return the HeaderFileInfo structure for the specified FileEntry, in preparation for updating it in so...
One of these records is kept for each identifier that is lexed.
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
bool isDeprecatedMacro() const
bool isOutOfDate() const
Determine whether the information for this identifier is out of date with respect to the external sou...
StringRef getName() const
Return the actual identifier string.
bool isRestrictExpansion() const
Implements an efficient mapping from strings to IdentifierInfo nodes.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
FPEvalMethodKind
Possible float expression evaluation method choices.
Definition: LangOptions.h:276
@ FEM_UnsetOnCommandLine
Used only for FE option processing; this is only used to indicate that the user did not specify an ex...
Definition: LangOptions.h:290
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:418
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition: Lexer.cpp:1060
static bool isAtStartOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroBegin=nullptr)
Returns true if the given MacroID location points at the first token of the macro expansion.
Definition: Lexer.cpp:872
static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Characters, const SourceManager &SM, const LangOptions &LangOpts)
AdvanceToTokenCharacter - If the current SourceLocation specifies a location at the start of a token,...
Definition: Lexer.h:399
static bool isAtEndOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroEnd=nullptr)
Returns true if the given MacroID location points at the last token of the macro expansion.
Definition: Lexer.cpp:894
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer,...
Definition: Lexer.cpp:452
static bool getRawToken(SourceLocation Loc, Token &Result, const SourceManager &SM, const LangOptions &LangOpts, bool IgnoreWhiteSpace=false)
Relex the token at the specified location.
Definition: Lexer.cpp:510
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
Definition: Lexer.cpp:850
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition: MacroArgs.h:30
A description of the current definition of a macro.
Definition: MacroInfo.h:590
const DefMacroDirective * getDirective() const
Definition: MacroInfo.h:375
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
Definition: MacroInfo.h:313
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Definition: MacroInfo.h:125
Abstract interface for a module loader.
Definition: ModuleLoader.h:82
Represents a macro directive exported by a module.
Definition: MacroInfo.h:514
A header that is known to reside within a given module, whether it was included or excluded.
Definition: ModuleMap.h:159
Describes a module or submodule.
Definition: Module.h:105
bool isModuleMapModule() const
Definition: Module.h:212
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
PragmaHandler - Instances of this interface defined to handle the various pragmas that the language f...
Definition: Pragma.h:65
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
SourceLocation getLastFPEvalPragmaLocation() const
bool isMacroDefined(const IdentifierInfo *II)
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define'd and not #undef'...
bool markIncluded(FileEntryRef File)
Mark the file as included.
void HandlePragmaPushMacro(Token &Tok)
Handle #pragma push_macro.
Definition: Pragma.cpp:627
void FinalizeForModelFile()
Cleanup after model file parsing.
bool FinishLexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Complete the lexing of a string literal where the first token has already been lexed (see LexStringLi...
void HandlePragmaPoison()
HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
Definition: Pragma.cpp:442
void setCodeCompletionHandler(CodeCompletionHandler &Handler)
Set the code completion handler to the given object.
void dumpMacroInfo(const IdentifierInfo *II)
std::pair< IdentifierInfo *, SourceLocation > getPragmaARCCFCodeAuditedInfo() const
The location of the currently-active #pragma clang arc_cf_code_audited begin.
void HandlePragmaSystemHeader(Token &SysHeaderTok)
HandlePragmaSystemHeader - Implement #pragma GCC system_header.
Definition: Pragma.cpp:484
bool creatingPCHWithThroughHeader()
True if creating a PCH with a through header.
void DumpToken(const Token &Tok, bool DumpFlags=false) const
Print the token to stderr, used for debugging.
void MaybeHandlePoisonedIdentifier(Token &Identifier)
ModuleMacro * addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro, ArrayRef< ModuleMacro * > Overrides, bool &IsNew)
Register an exported macro for a module and identifier.
void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED, MacroDirective *MD)
Set a MacroDirective that was loaded from a PCH file.
MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II, SourceLocation Loc)
void markClangModuleAsAffecting(Module *M)
Mark the given clang module as affecting the current clang module or translation unit.
void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident, SourceLocation Loc)
Set the location of the currently-active #pragma clang arc_cf_code_audited begin.
void HandlePragmaModuleBuild(Token &Tok)
Definition: Pragma.cpp:807
void InitializeForModelFile()
Initialize the preprocessor to parse a model file.
SourceLocation getCodeCompletionLoc() const
Returns the location of the code-completion point.
ArrayRef< ModuleMacro * > getLeafModuleMacros(const IdentifierInfo *II) const
Get the list of leaf (non-overridden) module macros for a name.
void CollectPpImportSuffix(SmallVectorImpl< Token > &Toks)
Collect the tokens of a C++20 pp-import-suffix.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
void EnterToken(const Token &Tok, bool IsReinject)
Enters a token in the token stream to be lexed next.
void IgnorePragmas()
Install empty handlers for all pragmas (making them ignored).
Definition: Pragma.cpp:2195
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI)
PPCallbacks * getPPCallbacks() const
bool isInNamedInterfaceUnit() const
If we are proprocessing a named interface unit.
ArrayRef< PPConditionalInfo > getPreambleConditionalStack() const
void setPreambleRecordedPragmaAssumeNonNullLoc(SourceLocation Loc)
Record the location of the unterminated #pragma clang assume_nonnull begin in the preamble.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
ArrayRef< BuildingSubmoduleInfo > getBuildingSubmodules() const
Get the list of submodules that we're currently building.
SourceLocation getCodeCompletionFileLoc() const
Returns the start location of the file of code-completion point.
DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const
SourceRange getCodeCompletionTokenRange() const
SourceLocation getModuleImportLoc(Module *M) const
void overrideMaxTokens(unsigned Value, SourceLocation Loc)
void setCodeCompletionTokenRange(const SourceLocation Start, const SourceLocation End)
Set the code completion token range for detecting replacement range later on.
bool isRecordingPreamble() const
void HandleSkippedDirectiveWhileUsingPCH(Token &Result, SourceLocation HashLoc)
Process directives while skipping until the through header or #pragma hdrstop is found.
void setRecordedPreambleConditionalStack(ArrayRef< PPConditionalInfo > s)
void enableIncrementalProcessing(bool value=true)
Enables the incremental processing.
bool LexAfterModuleImport(Token &Result)
Lex a token following the 'import' contextual keyword.
void TypoCorrectToken(const Token &Tok)
Update the current token to represent the provided identifier, in order to cache an action performed ...
bool GetSuppressIncludeNotFoundError()
bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M)
Determine whether II is defined as a macro within the module M, if that is a module that we've alread...
void setPragmaAssumeNonNullLoc(SourceLocation Loc)
Set the location of the currently-active #pragma clang assume_nonnull begin.
bool isInPrimaryFile() const
Return true if we're in the top-level file, not in a #include.
macro_iterator macro_begin(bool IncludeExternalMacros=true) const
void CreateString(StringRef Str, Token &Tok, SourceLocation ExpansionLocStart=SourceLocation(), SourceLocation ExpansionLocEnd=SourceLocation())
Plop the specified string into a scratch buffer and set the specified token's location and length to ...
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
LangOptions::FPEvalMethodKind getCurrentFPEvalMethod() const
void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma)
bool isSafeBufferOptOut(const SourceManager &SourceMgr, const SourceLocation &Loc) const
void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg, SourceLocation AnnotationLoc)
const char * getCheckPoint(FileID FID, const char *Start) const
Returns a pointer into the given file's buffer that's guaranteed to be between tokens.
void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg, SourceLocation AnnotationLoc)
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
MacroDirective * getLocalMacroDirectiveHistory(const IdentifierInfo *II) const
Given an identifier, return the latest non-imported macro directive for that identifier.
void setPreprocessedOutput(bool IsPreprocessedOutput)
Sets whether the preprocessor is responsible for producing output or if it is producing tokens to be ...
void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc)
bool IsPreviousCachedToken(const Token &Tok) const
Whether Tok is the most recent token (CachedLexPos - 1) in CachedTokens.
Definition: PPCaching.cpp:139
bool SawDateOrTime() const
Returns true if the preprocessor has seen a use of DATE or TIME in the file so far.
const TargetInfo * getAuxTargetInfo() const
void CommitBacktrackedTokens()
Disable the last EnableBacktrackAtThisPos call.
Definition: PPCaching.cpp:32
friend class MacroArgs
Definition: Preprocessor.h:693
void DumpMacro(const MacroInfo &MI) const
bool HandleEndOfTokenLexer(Token &Result)
Callback invoked when the current TokenLexer hits the end of its token stream.
void setDiagnostics(DiagnosticsEngine &D)
IncludedFilesSet & getIncludedFiles()
Get the set of included files.
friend void TokenLexer::ExpandFunctionArguments()
void setCodeCompletionReached()
Note that we hit the code-completion point.
bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line, unsigned Column)
Specify the point at which code-completion will be performed.
void AnnotateCachedTokens(const Token &Tok)
We notify the Preprocessor that if it is caching tokens (because backtrack is enabled) it should repl...
bool isPreprocessedOutput() const
Returns true if the preprocessor is responsible for generating output, false if it is producing token...
StringRef getNamedModuleName() const
Get the named module name we're preprocessing.
void makeModuleVisible(Module *M, SourceLocation Loc)
unsigned getCounterValue() const
bool mightHavePendingAnnotationTokens()
Determine whether it's possible for a future call to Lex to produce an annotation token created by a ...
bool isInImportingCXXNamedModules() const
If we're importing a standard C++20 Named Modules.
void Lex(Token &Result)
Lex the next token for this preprocessor.
void EnterTokenStream(ArrayRef< Token > Toks, bool DisableMacroExpansion, bool IsReinject)
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
Definition: Preprocessor.h:285
bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, SourceLocation Loc, bool IsFirstIncludeOfFile=true)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
bool isParsingIfOrElifDirective() const
True if we are currently preprocessing a if or #elif directive.
unsigned getNumDirectives() const
Retrieve the number of Directives that have been processed by the Preprocessor.
bool isInImplementationUnit() const
If we are implementing an implementation module unit.
void addCommentHandler(CommentHandler *Handler)
Add the specified comment handler to the preprocessor.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
void LexNonComment(Token &Result)
Lex a token.
void removeCommentHandler(CommentHandler *Handler)
Remove the specified comment handler.
PreprocessorLexer * getCurrentLexer() const
Return the current lexer being lexed from.
bool LexOnOffSwitch(tok::OnOffSwitch &Result)
Lex an on-off-switch (C99 6.10.6p2) and verify that it is followed by EOD.
Definition: Pragma.cpp:968
StringRef getCodeCompletionFilter()
Get the code completion token for filtering purposes.
void setMainFileDir(DirectoryEntryRef Dir)
Set the directory in which the main file should be considered to have been found, if it is not a real...
const IdentifierTable & getIdentifierTable() const
void HandlePragmaDependency(Token &DependencyTok)
HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
Definition: Pragma.cpp:516
SourceLocation CheckEndOfDirective(const char *DirType, bool EnableMacros=false)
Ensure that the next token is a tok::eod token.
void HandlePoisonedIdentifier(Token &Identifier)
Display reason for poisoned identifier.
void Backtrack()
Make Preprocessor re-lex the tokens that were lexed since EnableBacktrackAtThisPos() was previously c...
Definition: PPCaching.cpp:40
bool isCurrentLexer(const PreprocessorLexer *L) const
Return true if we are lexing directly from the specified lexer.
bool HandleIdentifier(Token &Identifier)
Callback invoked when the lexer reads an identifier and has filled in the tokens IdentifierInfo membe...
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
bool enterOrExitSafeBufferOptOutRegion(bool isEnter, const SourceLocation &Loc)
Alter the state of whether this PP currently is in a "-Wunsafe-buffer-usage" opt-out region.
void IncrementPasteCounter(bool isFast)
Increment the counters for the number of token paste operations performed.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
void setReplayablePreambleConditionalStack(ArrayRef< PPConditionalInfo > s, std::optional< PreambleSkipInfo > SkipInfo)
const Token & LookAhead(unsigned N)
Peeks ahead N tokens and returns that token without consuming any tokens.
const MacroAnnotations & getMacroAnnotations(const IdentifierInfo *II) const
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
macro_iterator macro_end(bool IncludeExternalMacros=true) const
SourceManager & getSourceManager() const
bool isBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of tokens is on.
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
std::optional< PreambleSkipInfo > getPreambleSkipInfo() const
void setPreprocessToken(bool Preprocess)
void SetPoisonReason(IdentifierInfo *II, unsigned DiagID)
Specifies the reason for poisoning an identifier.
void HandlePragmaOnce(Token &OnceTok)
HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
Definition: Pragma.cpp:417
EmptylineHandler * getEmptylineHandler() const
bool getCommentRetentionState() const
bool isMacroDefined(StringRef Id)
static bool checkModuleIsAvailable(const LangOptions &LangOpts, const TargetInfo &TargetInfo, const Module &M, DiagnosticsEngine &Diags)
Check that the given module is available, producing a diagnostic if not.
Module * getCurrentModuleImplementation()
Retrieves the module whose implementation we're current compiling, if any.
void SetMacroExpansionOnlyInDirectives()
Disables macro expansion everywhere except for preprocessor directives.
bool hasRecordedPreamble() const
SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const
Given a location that specifies the start of a token, return a new location that specifies a characte...
SourceLocation getPragmaAssumeNonNullLoc() const
The location of the currently-active #pragma clang assume_nonnull begin.
MacroMap::const_iterator macro_iterator
void createPreprocessingRecord()
Create a new preprocessing record, which will keep track of all macro expansions, macro definitions,...
SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length)
Split the first Length characters out of the token starting at TokLoc and return a location pointing ...
void EnterTokenStream(std::unique_ptr< Token[]> Toks, unsigned NumToks, bool DisableMacroExpansion, bool IsReinject)
void RevertCachedTokens(unsigned N)
When backtracking is enabled and tokens are cached, this allows to revert a specific number of tokens...
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
void RemovePragmaHandler(PragmaHandler *Handler)
bool isPPInSafeBufferOptOutRegion()
unsigned getTokenCount() const
Get the number of tokens processed so far.
unsigned getMaxTokens() const
Get the max number of tokens before issuing a -Wmax-tokens warning.
SourceLocation getMaxTokensOverrideLoc() const
bool hadModuleLoaderFatalFailure() const
static void processPathToFileName(SmallVectorImpl< char > &FileName, const PresumedLoc &PLoc, const LangOptions &LangOpts, const TargetInfo &TI)
void setCurrentFPEvalMethod(SourceLocation PragmaLoc, LangOptions::FPEvalMethodKind Val)
const TargetInfo & getTargetInfo() const
FileManager & getFileManager() const
bool LexHeaderName(Token &Result, bool AllowMacroExpansion=true)
Lex a token, forming a header-name token if possible.
PreprocessorOptions & getPreprocessorOpts() const
Retrieve the preprocessor options used to initialize this preprocessor.
std::string getSpelling(const Token &Tok, bool *Invalid=nullptr) const
Return the 'spelling' of the Tok token.
bool isPCHThroughHeader(const FileEntry *FE)
Returns true if the FileEntry is the PCH through header.
void DumpLocation(SourceLocation Loc) const
Module * getCurrentLexerSubmodule() const
Return the submodule owning the file being lexed.
bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value)
Parses a simple integer literal to get its numeric value.
MacroInfo * AllocateMacroInfo(SourceLocation L)
Allocate a new MacroInfo object with the provided SourceLocation.
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
StringRef getImmediateMacroName(SourceLocation Loc)
Retrieve the name of the immediate macro expansion.
bool creatingPCHWithPragmaHdrStop()
True if creating a PCH with a #pragma hdrstop.
bool alreadyIncluded(FileEntryRef File) const
Return true if this header has already been included.
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
void Initialize(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize the preprocessor using information about the target.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
void LexUnexpandedNonComment(Token &Result)
Like LexNonComment, but this disables macro expansion of identifier tokens.
char getSpellingOfSingleCharacterNumericConstant(const Token &Tok, bool *Invalid=nullptr) const
Given a Token Tok that is a numeric constant with length 1, return the character.
void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler)
Add the specified pragma handler to this preprocessor.
Definition: Pragma.cpp:915
llvm::BumpPtrAllocator & getPreprocessorAllocator()
ModuleMacro * getModuleMacro(Module *Mod, const IdentifierInfo *II)
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
bool HandleComment(Token &result, SourceRange Comment)
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
bool GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)
Turn the specified lexer token into a fully checked and spelled filename, e.g.
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
HeaderSearch & getHeaderSearchInfo() const
void emitMacroExpansionWarnings(const Token &Identifier, bool IsIfnDef=false) const
void HandlePragmaPopMacro(Token &Tok)
Handle #pragma pop_macro.
Definition: Pragma.cpp:650
void ReplaceLastTokenWithAnnotation(const Token &Tok)
Replace the last token with an annotation token.
ExternalPreprocessorSource * getExternalSource() const
Module * LeaveSubmodule(bool ForPragma)
SourceRange DiscardUntilEndOfDirective()
Read and discard all tokens remaining on the current line until the tok::eod token is found.
const std::string & getPredefines() const
Get the predefines for this processor.
void HandleDirective(Token &Result)
Callback invoked when the lexer sees a # token at the start of a line.
CodeCompletionHandler * getCodeCompletionHandler() const
Retrieve the current code-completion handler.
void recomputeCurLexerKind()
Recompute the current lexer kind based on the CurLexer/ CurTokenLexer pointers.
void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind, void *AnnotationVal)
Enter an annotation token into the token stream.
void setTokenWatcher(llvm::unique_function< void(const clang::Token &)> F)
Register a function that would be called on each token in the final expanded token stream.
Preprocessor(std::shared_ptr< PreprocessorOptions > PPOpts, DiagnosticsEngine &diags, const LangOptions &LangOpts, SourceManager &SM, HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup=nullptr, bool OwnsHeaderSearch=false, TranslationUnitKind TUKind=TU_Complete)
MacroInfo * getMacroInfo(const IdentifierInfo *II)
void setPredefines(std::string P)
Set the predefines for this Preprocessor.
OptionalFileEntryRef LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, ConstSearchDirIterator FromDir, const FileEntry *FromFile, ConstSearchDirIterator *CurDir, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache=false, bool OpenFile=true, bool CacheFailures=true)
Given a "foo" or <foo> reference, look up the indicated file.
IdentifierTable & getIdentifierTable()
Builtin::Context & getBuiltinInfo()
void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine)
Instruct the preprocessor to skip part of the main source file.
void ReplacePreviousCachedToken(ArrayRef< Token > NewToks)
Replace token in CachedLexPos - 1 in CachedTokens by the tokens in NewToks.
Definition: PPCaching.cpp:157
LangOptions::FPEvalMethodKind getTUFPEvalMethod() const
const LangOptions & getLangOpts() const
void setTUFPEvalMethod(LangOptions::FPEvalMethodKind Val)
void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled)
Hook used by the lexer to invoke the "included file" code completion point.
void SetSuppressIncludeNotFoundError(bool Suppress)
static void processPathForFileMacro(SmallVectorImpl< char > &Path, const LangOptions &LangOpts, const TargetInfo &TI)
bool isInNamedModule() const
If we are preprocessing a named module.
void RemoveTopOfLexerStack()
Pop the current lexer/macro exp off the top of the lexer stack.
void PoisonSEHIdentifiers(bool Poison=true)
bool isAtStartOfMacroExpansion(SourceLocation loc, SourceLocation *MacroBegin=nullptr) const
Returns true if the given MacroID location points at the first token of the macro expansion.
size_t getTotalMemory() const
void setExternalSource(ExternalPreprocessorSource *Source)
void clearCodeCompletionHandler()
Clear out the code completion handler.
void AddPragmaHandler(PragmaHandler *Handler)
OptionalFileEntryRef getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, SourceLocation MLoc)
We want to produce a diagnostic at location IncLoc concerning an unreachable effect at location MLoc ...
void setCounterValue(unsigned V)
bool isCodeCompletionReached() const
Returns true if code-completion is enabled and we have hit the code-completion point.
IdentifierInfo * ParsePragmaPushOrPopMacro(Token &Tok)
ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Definition: Pragma.cpp:572
void LexTokensUntilEOF(std::vector< Token > *Tokens=nullptr)
Lex all tokens for this preprocessor until (and excluding) end of file.
bool getRawToken(SourceLocation Loc, Token &Result, bool IgnoreWhiteSpace=false)
Relex the token at the specified location.
bool usingPCHWithPragmaHdrStop()
True if using a PCH with a #pragma hdrstop.
void CodeCompleteNaturalLanguage()
Hook used by the lexer to invoke the "natural language" code completion point.
void EndSourceFile()
Inform the preprocessor callbacks that processing is complete.
bool HandleEndOfFile(Token &Result, bool isEndOfMacro=false)
Callback invoked when the lexer hits the end of the current file.
void setPragmasEnabled(bool Enabled)
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments)
Control whether the preprocessor retains comments in output.
bool isAtEndOfMacroExpansion(SourceLocation loc, SourceLocation *MacroEnd=nullptr) const
Returns true if the given MacroID location points at the last token of the macro expansion.
void HandlePragmaMark(Token &MarkTok)
Definition: Pragma.cpp:432
bool getPragmasEnabled() const
void HandlePragmaHdrstop(Token &Tok)
Definition: Pragma.cpp:881
PreprocessingRecord * getPreprocessingRecord() const
Retrieve the preprocessing record, or NULL if there is no preprocessing record.
void setEmptylineHandler(EmptylineHandler *Handler)
Set empty line handler.
DiagnosticsEngine & getDiagnostics() const
SourceLocation getLastCachedTokenLocation() const
Get the location of the last cached token, suitable for setting the end location of an annotation tok...
llvm::DenseSet< const FileEntry * > IncludedFilesSet
Definition: Preprocessor.h:689
unsigned getSpelling(const Token &Tok, const char *&Buffer, bool *Invalid=nullptr) const
Get the spelling of a token into a preallocated buffer, instead of as an std::string.
SelectorTable & getSelectorTable()
void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler)
Remove the specific pragma handler from this preprocessor.
Definition: Pragma.cpp:946
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
const llvm::SmallSetVector< Module *, 2 > & getAffectingClangModules() const
Get the set of top-level clang modules that affected preprocessing, but were not imported.
const IncludedFilesSet & getIncludedFiles() const
StringRef getLastMacroWithSpelling(SourceLocation Loc, ArrayRef< TokenValue > Tokens) const
Return the name of the macro defined before Loc that has spelling Tokens.
void HandlePragmaIncludeAlias(Token &Tok)
Definition: Pragma.cpp:685
Module * getModuleForLocation(SourceLocation Loc, bool AllowTextual)
Find the module that owns the source or header file that Loc points to.
void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter)
Set the code completion token for filtering purposes.
SourceLocation getPreambleRecordedPragmaAssumeNonNullLoc() const
Get the location of the recorded unterminated #pragma clang assume_nonnull begin in the preamble,...
void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, MacroArgs *Args)
Add a Macro to the top of the include stack and start lexing tokens from it instead of the current bu...
void EnableBacktrackAtThisPos()
From the point that this method is called, and until CommitBacktrackedTokens() or Backtrack() is call...
Definition: PPCaching.cpp:25
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
void SkipTokensWhileUsingPCH()
Skip tokens until after the #include of the through header or until after a #pragma hdrstop.
bool usingPCHWithThroughHeader()
True if using a PCH with a through header.
bool LexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Lex a string literal, which may be the concatenation of multiple string literals and may even come fr...
void HandleMicrosoftCommentPaste(Token &Tok)
When the macro expander pastes together a comment (/##/) in Microsoft mode, this method handles updat...
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD)
Add a directive to the macro directive history for this identifier.
Represents an unpacked "presumed" location which can be presented to the user.
This table allows us to fully hide how we implement multi-keyword caching.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Exposes information about the current target.
Definition: TargetInfo.h:212
Stores token information for comparing actual tokens with predefined values.
Definition: Preprocessor.h:89
TokenValue(IdentifierInfo *II)
Definition: Preprocessor.h:102
TokenValue(tok::TokenKind Kind)
Definition: Preprocessor.h:94
bool operator==(const Token &Tok) const
Definition: Preprocessor.h:104
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:187
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:132
unsigned getLength() const
Definition: Token.h:135
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition: Token.h:99
tok::TokenKind getKind() const
Definition: Token.h:94
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:121
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
Definition: Token.h:295
const char * getLiteralData() const
getLiteralData - For a literal token (numeric constant, string, etc), this returns a pointer to the s...
Definition: Token.h:225
A class for tracking whether we're inside a VA_OPT during a traversal of the tokens of a variadic mac...
An RAII class that tracks when the Preprocessor starts and stops lexing the definition of a (ISO C/C+...
Directive - Abstract class representing a parsed verify directive.
constexpr XRayInstrMask None
Definition: XRayInstr.h:38
StringRef getName(const HeaderType T)
Definition: HeaderFile.h:36
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
OnOffSwitch
Defines the possible values of an on-off-switch (C99 6.10.6p2).
Definition: TokenKinds.h:56
bool isLiteral(TokenKind K)
Return true if this is a "literal" kind, like a numeric constant, string, etc.
Definition: TokenKinds.h:97
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
Definition: TokenKinds.h:33
bool isAnnotation(TokenKind K)
Return true if this is any of tok::annot_* kinds.
Definition: TokenKinds.cpp:58
@ HeaderSearch
Remove unused header search paths including header maps.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef
detail::SearchDirIteratorImpl< true > ConstSearchDirIterator
Definition: HeaderSearch.h:224
MacroUse
Context in which macro name is used.
Definition: Preprocessor.h:111
@ MU_Undef
Definition: Preprocessor.h:119
@ MU_Other
Definition: Preprocessor.h:113
@ MU_Define
Definition: Preprocessor.h:116
llvm::Registry< PragmaHandler > PragmaHandlerRegistry
Registry of pragma handlers added by plugins.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
@ Result
The result type of a method or function.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition: FileEntry.h:202
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:987
@ TU_Complete
The translation unit is a complete translation unit.
Definition: LangOptions.h:989
YAML serialization mapping.
Definition: Dominators.h:30
Definition: Format.h:5304
#define bool
Definition: stdbool.h:20
Describes how and where the pragma was introduced.
Definition: Pragma.h:51
PreambleSkipInfo(SourceLocation HashTokenLoc, SourceLocation IfTokenLoc, bool FoundNonSkipPortion, bool FoundElse, SourceLocation ElseLoc)
Definition: Preprocessor.h:681