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 
17 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/Module.h"
25 #include "clang/Basic/TokenKinds.h"
26 #include "clang/Lex/HeaderSearch.h"
27 #include "clang/Lex/Lexer.h"
28 #include "clang/Lex/MacroInfo.h"
29 #include "clang/Lex/ModuleLoader.h"
30 #include "clang/Lex/ModuleMap.h"
31 #include "clang/Lex/PPCallbacks.h"
32 #include "clang/Lex/Token.h"
33 #include "clang/Lex/TokenLexer.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 
58 namespace llvm {
59 
60 template<unsigned InternalLen> class SmallString;
61 
62 } // namespace llvm
63 
64 namespace clang {
65 
66 class CodeCompletionHandler;
67 class CommentHandler;
68 class DirectoryEntry;
69 class EmptylineHandler;
70 class ExternalPreprocessorSource;
71 class FileEntry;
72 class FileManager;
73 class HeaderSearch;
74 class MacroArgs;
75 class PragmaHandler;
76 class PragmaNamespace;
77 class PreprocessingRecord;
78 class PreprocessorLexer;
79 class PreprocessorOptions;
80 class ScratchBuffer;
81 class TargetInfo;
82 
83 namespace Builtin {
84 class Context;
85 }
86 
87 /// Stores token information for comparing actual tokens with
88 /// predefined values. Only handles simple tokens and identifiers.
89 class TokenValue {
90  tok::TokenKind Kind;
91  IdentifierInfo *II;
92 
93 public:
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.
111 enum MacroUse {
112  // other than #define or #undef
113  MU_Other = 0,
114 
115  // macro name specified in #define
117 
118  // macro name specified in #undef
119  MU_Undef = 2
120 };
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 =
194  LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
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 =
202  LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
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 
283 public:
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 
294 private:
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 
673 public:
678  bool FoundElse;
680 
682  bool FoundNonSkipPortion, bool FoundElse,
686  ElseLoc(ElseLoc) {}
687  };
688 
690 
691 private:
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(*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<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(const IdentifierInfo &II) const;
1166 
1167 public:
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.
1193  void InitializeForModelFile();
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 
1255  void SetSuppressIncludeNotFoundError(bool Suppress) {
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  }
1336  bool isMacroDefined(const IdentifierInfo *II) {
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  MacroInfo *Macro,
1437  ArrayRef<ModuleMacro *> Overrides, bool &IsNew);
1438  ModuleMacro *getModuleMacro(Module *Mod, const IdentifierInfo *II);
1439 
1440  /// Get the list of leaf (non-overridden) module macros for a name.
1442  if (II->isOutOfDate())
1443  updateOutOfDateIdentifier(*II);
1444  auto I = LeafModuleMacros.find(II);
1445  if (I != LeafModuleMacros.end())
1446  return I->second;
1447  return std::nullopt;
1448  }
1449 
1450  /// Get the list of submodules that we're currently building.
1452  return BuildingSubmoduleStack;
1453  }
1454 
1455  /// \{
1456  /// Iterators for the macro history table. Currently defined macros have
1457  /// IdentifierInfo::hasMacroDefinition() set and an empty
1458  /// MacroInfo::getUndefLoc() at the head of the list.
1459  using macro_iterator = MacroMap::const_iterator;
1460 
1461  macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
1462  macro_iterator macro_end(bool IncludeExternalMacros = true) const;
1463 
1464  llvm::iterator_range<macro_iterator>
1465  macros(bool IncludeExternalMacros = true) const {
1466  macro_iterator begin = macro_begin(IncludeExternalMacros);
1467  macro_iterator end = macro_end(IncludeExternalMacros);
1468  return llvm::make_range(begin, end);
1469  }
1470 
1471  /// \}
1472 
1473  /// Mark the given clang module as affecting the current clang module or translation unit.
1475  assert(M->isModuleMapModule());
1476  if (!BuildingSubmoduleStack.empty()) {
1477  if (M != BuildingSubmoduleStack.back().M)
1478  BuildingSubmoduleStack.back().M->AffectingClangModules.insert(M);
1479  } else {
1480  AffectingClangModules.insert(M);
1481  }
1482  }
1483 
1484  /// Get the set of top-level clang modules that affected preprocessing, but were not
1485  /// imported.
1487  return AffectingClangModules;
1488  }
1489 
1490  /// Mark the file as included.
1491  /// Returns true if this is the first time the file was included.
1493  HeaderInfo.getFileInfo(File);
1494  return IncludedFiles.insert(File).second;
1495  }
1496 
1497  /// Return true if this header has already been included.
1498  bool alreadyIncluded(FileEntryRef File) const {
1499  HeaderInfo.getFileInfo(File);
1500  return IncludedFiles.count(File);
1501  }
1502 
1503  /// Get the set of included files.
1504  IncludedFilesSet &getIncludedFiles() { return IncludedFiles; }
1505  const IncludedFilesSet &getIncludedFiles() const { return IncludedFiles; }
1506 
1507  /// Return the name of the macro defined before \p Loc that has
1508  /// spelling \p Tokens. If there are multiple macros with same spelling,
1509  /// return the last one defined.
1511  ArrayRef<TokenValue> Tokens) const;
1512 
1513  /// Get the predefines for this processor.
1514  /// Used by some third-party tools to inspect and add predefines (see
1515  /// https://github.com/llvm/llvm-project/issues/57483).
1516  const std::string &getPredefines() const { return Predefines; }
1517 
1518  /// Set the predefines for this Preprocessor.
1519  ///
1520  /// These predefines are automatically injected when parsing the main file.
1521  void setPredefines(std::string P) { Predefines = std::move(P); }
1522 
1523  /// Return information about the specified preprocessor
1524  /// identifier token.
1525  IdentifierInfo *getIdentifierInfo(StringRef Name) const {
1526  return &Identifiers.get(Name);
1527  }
1528 
1529  /// Add the specified pragma handler to this preprocessor.
1530  ///
1531  /// If \p Namespace is non-null, then it is a token required to exist on the
1532  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1533  void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1535  AddPragmaHandler(StringRef(), Handler);
1536  }
1537 
1538  /// Remove the specific pragma handler from this preprocessor.
1539  ///
1540  /// If \p Namespace is non-null, then it should be the namespace that
1541  /// \p Handler was added to. It is an error to remove a handler that
1542  /// has not been registered.
1543  void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1545  RemovePragmaHandler(StringRef(), Handler);
1546  }
1547 
1548  /// Install empty handlers for all pragmas (making them ignored).
1549  void IgnorePragmas();
1550 
1551  /// Set empty line handler.
1552  void setEmptylineHandler(EmptylineHandler *Handler) { Emptyline = Handler; }
1553 
1554  EmptylineHandler *getEmptylineHandler() const { return Emptyline; }
1555 
1556  /// Add the specified comment handler to the preprocessor.
1557  void addCommentHandler(CommentHandler *Handler);
1558 
1559  /// Remove the specified comment handler.
1560  ///
1561  /// It is an error to remove a handler that has not been registered.
1562  void removeCommentHandler(CommentHandler *Handler);
1563 
1564  /// Set the code completion handler to the given object.
1566  CodeComplete = &Handler;
1567  }
1568 
1569  /// Retrieve the current code-completion handler.
1571  return CodeComplete;
1572  }
1573 
1574  /// Clear out the code completion handler.
1576  CodeComplete = nullptr;
1577  }
1578 
1579  /// Hook used by the lexer to invoke the "included file" code
1580  /// completion point.
1581  void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled);
1582 
1583  /// Hook used by the lexer to invoke the "natural language" code
1584  /// completion point.
1586 
1587  /// Set the code completion token for filtering purposes.
1589  CodeCompletionII = Filter;
1590  }
1591 
1592  /// Set the code completion token range for detecting replacement range later
1593  /// on.
1595  const SourceLocation End) {
1596  CodeCompletionTokenRange = {Start, End};
1597  }
1599  return CodeCompletionTokenRange;
1600  }
1601 
1602  /// Get the code completion token for filtering purposes.
1604  if (CodeCompletionII)
1605  return CodeCompletionII->getName();
1606  return {};
1607  }
1608 
1609  /// Retrieve the preprocessing record, or NULL if there is no
1610  /// preprocessing record.
1612 
1613  /// Create a new preprocessing record, which will keep track of
1614  /// all macro expansions, macro definitions, etc.
1616 
1617  /// Returns true if the FileEntry is the PCH through header.
1618  bool isPCHThroughHeader(const FileEntry *FE);
1619 
1620  /// True if creating a PCH with a through header.
1622 
1623  /// True if using a PCH with a through header.
1625 
1626  /// True if creating a PCH with a #pragma hdrstop.
1628 
1629  /// True if using a PCH with a #pragma hdrstop.
1631 
1632  /// Skip tokens until after the #include of the through header or
1633  /// until after a #pragma hdrstop.
1634  void SkipTokensWhileUsingPCH();
1635 
1636  /// Process directives while skipping until the through header or
1637  /// #pragma hdrstop is found.
1639  SourceLocation HashLoc);
1640 
1641  /// Enter the specified FileID as the main source file,
1642  /// which implicitly adds the builtin defines etc.
1643  void EnterMainSourceFile();
1644 
1645  /// Inform the preprocessor callbacks that processing is complete.
1646  void EndSourceFile();
1647 
1648  /// Add a source file to the top of the include stack and
1649  /// start lexing tokens from it instead of the current buffer.
1650  ///
1651  /// Emits a diagnostic, doesn't enter the file, and returns true on error.
1653  SourceLocation Loc, bool IsFirstIncludeOfFile = true);
1654 
1655  /// Add a Macro to the top of the include stack and start lexing
1656  /// tokens from it instead of the current buffer.
1657  ///
1658  /// \param Args specifies the tokens input to a function-like macro.
1659  /// \param ILEnd specifies the location of the ')' for a function-like macro
1660  /// or the identifier for an object-like macro.
1661  void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro,
1662  MacroArgs *Args);
1663 
1664 private:
1665  /// Add a "macro" context to the top of the include stack,
1666  /// which will cause the lexer to start returning the specified tokens.
1667  ///
1668  /// If \p DisableMacroExpansion is true, tokens lexed from the token stream
1669  /// will not be subject to further macro expansion. Otherwise, these tokens
1670  /// will be re-macro-expanded when/if expansion is enabled.
1671  ///
1672  /// If \p OwnsTokens is false, this method assumes that the specified stream
1673  /// of tokens has a permanent owner somewhere, so they do not need to be
1674  /// copied. If it is true, it assumes the array of tokens is allocated with
1675  /// \c new[] and the Preprocessor will delete[] it.
1676  ///
1677  /// If \p IsReinject the resulting tokens will have Token::IsReinjected flag
1678  /// set, see the flag documentation for details.
1679  void EnterTokenStream(const Token *Toks, unsigned NumToks,
1680  bool DisableMacroExpansion, bool OwnsTokens,
1681  bool IsReinject);
1682 
1683 public:
1684  void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1685  bool DisableMacroExpansion, bool IsReinject) {
1686  EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true,
1687  IsReinject);
1688  }
1689 
1690  void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion,
1691  bool IsReinject) {
1692  EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false,
1693  IsReinject);
1694  }
1695 
1696  /// Pop the current lexer/macro exp off the top of the lexer stack.
1697  ///
1698  /// This should only be used in situations where the current state of the
1699  /// top-of-stack lexer is known.
1700  void RemoveTopOfLexerStack();
1701 
1702  /// From the point that this method is called, and until
1703  /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
1704  /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
1705  /// make the Preprocessor re-lex the same tokens.
1706  ///
1707  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
1708  /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
1709  /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
1710  ///
1711  /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
1712  /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
1713  /// tokens will continue indefinitely.
1714  ///
1715  void EnableBacktrackAtThisPos();
1716 
1717  /// Disable the last EnableBacktrackAtThisPos call.
1718  void CommitBacktrackedTokens();
1719 
1720  /// Make Preprocessor re-lex the tokens that were lexed since
1721  /// EnableBacktrackAtThisPos() was previously called.
1722  void Backtrack();
1723 
1724  /// True if EnableBacktrackAtThisPos() was called and
1725  /// caching of tokens is on.
1726  bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
1727 
1728  /// Lex the next token for this preprocessor.
1729  void Lex(Token &Result);
1730 
1731  /// Lex all tokens for this preprocessor until (and excluding) end of file.
1732  void LexTokensUntilEOF(std::vector<Token> *Tokens = nullptr);
1733 
1734  /// Lex a token, forming a header-name token if possible.
1735  bool LexHeaderName(Token &Result, bool AllowMacroExpansion = true);
1736 
1737  bool LexAfterModuleImport(Token &Result);
1739 
1741 
1743  return CurSubmoduleState->VisibleModules.getImportLoc(M);
1744  }
1745 
1746  /// Lex a string literal, which may be the concatenation of multiple
1747  /// string literals and may even come from macro expansion.
1748  /// \returns true on success, false if a error diagnostic has been generated.
1749  bool LexStringLiteral(Token &Result, std::string &String,
1750  const char *DiagnosticTag, bool AllowMacroExpansion) {
1751  if (AllowMacroExpansion)
1752  Lex(Result);
1753  else
1754  LexUnexpandedToken(Result);
1755  return FinishLexStringLiteral(Result, String, DiagnosticTag,
1756  AllowMacroExpansion);
1757  }
1758 
1759  /// Complete the lexing of a string literal where the first token has
1760  /// already been lexed (see LexStringLiteral).
1761  bool FinishLexStringLiteral(Token &Result, std::string &String,
1762  const char *DiagnosticTag,
1763  bool AllowMacroExpansion);
1764 
1765  /// Lex a token. If it's a comment, keep lexing until we get
1766  /// something not a comment.
1767  ///
1768  /// This is useful in -E -C mode where comments would foul up preprocessor
1769  /// directive handling.
1770  void LexNonComment(Token &Result) {
1771  do
1772  Lex(Result);
1773  while (Result.getKind() == tok::comment);
1774  }
1775 
1776  /// Just like Lex, but disables macro expansion of identifier tokens.
1777  void LexUnexpandedToken(Token &Result) {
1778  // Disable macro expansion.
1779  bool OldVal = DisableMacroExpansion;
1780  DisableMacroExpansion = true;
1781  // Lex the token.
1782  Lex(Result);
1783 
1784  // Reenable it.
1785  DisableMacroExpansion = OldVal;
1786  }
1787 
1788  /// Like LexNonComment, but this disables macro expansion of
1789  /// identifier tokens.
1791  do
1792  LexUnexpandedToken(Result);
1793  while (Result.getKind() == tok::comment);
1794  }
1795 
1796  /// Parses a simple integer literal to get its numeric value. Floating
1797  /// point literals and user defined literals are rejected. Used primarily to
1798  /// handle pragmas that accept integer arguments.
1800 
1801  /// Disables macro expansion everywhere except for preprocessor directives.
1803  DisableMacroExpansion = true;
1804  MacroExpansionInDirectivesOverride = true;
1805  }
1806 
1807  /// Peeks ahead N tokens and returns that token without consuming any
1808  /// tokens.
1809  ///
1810  /// LookAhead(0) returns the next token that would be returned by Lex(),
1811  /// LookAhead(1) returns the token after it, etc. This returns normal
1812  /// tokens after phase 5. As such, it is equivalent to using
1813  /// 'Lex', not 'LexUnexpandedToken'.
1814  const Token &LookAhead(unsigned N) {
1815  assert(LexLevel == 0 && "cannot use lookahead while lexing");
1816  if (CachedLexPos + N < CachedTokens.size())
1817  return CachedTokens[CachedLexPos+N];
1818  else
1819  return PeekAhead(N+1);
1820  }
1821 
1822  /// When backtracking is enabled and tokens are cached,
1823  /// this allows to revert a specific number of tokens.
1824  ///
1825  /// Note that the number of tokens being reverted should be up to the last
1826  /// backtrack position, not more.
1827  void RevertCachedTokens(unsigned N) {
1828  assert(isBacktrackEnabled() &&
1829  "Should only be called when tokens are cached for backtracking");
1830  assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
1831  && "Should revert tokens up to the last backtrack position, not more");
1832  assert(signed(CachedLexPos) - signed(N) >= 0 &&
1833  "Corrupted backtrack positions ?");
1834  CachedLexPos -= N;
1835  }
1836 
1837  /// Enters a token in the token stream to be lexed next.
1838  ///
1839  /// If BackTrack() is called afterwards, the token will remain at the
1840  /// insertion point.
1841  /// If \p IsReinject is true, resulting token will have Token::IsReinjected
1842  /// flag set. See the flag documentation for details.
1843  void EnterToken(const Token &Tok, bool IsReinject) {
1844  if (LexLevel) {
1845  // It's not correct in general to enter caching lex mode while in the
1846  // middle of a nested lexing action.
1847  auto TokCopy = std::make_unique<Token[]>(1);
1848  TokCopy[0] = Tok;
1849  EnterTokenStream(std::move(TokCopy), 1, true, IsReinject);
1850  } else {
1851  EnterCachingLexMode();
1852  assert(IsReinject && "new tokens in the middle of cached stream");
1853  CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
1854  }
1855  }
1856 
1857  /// We notify the Preprocessor that if it is caching tokens (because
1858  /// backtrack is enabled) it should replace the most recent cached tokens
1859  /// with the given annotation token. This function has no effect if
1860  /// backtracking is not enabled.
1861  ///
1862  /// Note that the use of this function is just for optimization, so that the
1863  /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
1864  /// invoked.
1865  void AnnotateCachedTokens(const Token &Tok) {
1866  assert(Tok.isAnnotation() && "Expected annotation token");
1867  if (CachedLexPos != 0 && isBacktrackEnabled())
1868  AnnotatePreviousCachedTokens(Tok);
1869  }
1870 
1871  /// Get the location of the last cached token, suitable for setting the end
1872  /// location of an annotation token.
1874  assert(CachedLexPos != 0);
1875  return CachedTokens[CachedLexPos-1].getLastLoc();
1876  }
1877 
1878  /// Whether \p Tok is the most recent token (`CachedLexPos - 1`) in
1879  /// CachedTokens.
1880  bool IsPreviousCachedToken(const Token &Tok) const;
1881 
1882  /// Replace token in `CachedLexPos - 1` in CachedTokens by the tokens
1883  /// in \p NewToks.
1884  ///
1885  /// Useful when a token needs to be split in smaller ones and CachedTokens
1886  /// most recent token must to be updated to reflect that.
1888 
1889  /// Replace the last token with an annotation token.
1890  ///
1891  /// Like AnnotateCachedTokens(), this routine replaces an
1892  /// already-parsed (and resolved) token with an annotation
1893  /// token. However, this routine only replaces the last token with
1894  /// the annotation token; it does not affect any other cached
1895  /// tokens. This function has no effect if backtracking is not
1896  /// enabled.
1898  assert(Tok.isAnnotation() && "Expected annotation token");
1899  if (CachedLexPos != 0 && isBacktrackEnabled())
1900  CachedTokens[CachedLexPos-1] = Tok;
1901  }
1902 
1903  /// Enter an annotation token into the token stream.
1905  void *AnnotationVal);
1906 
1907  /// Determine whether it's possible for a future call to Lex to produce an
1908  /// annotation token created by a previous call to EnterAnnotationToken.
1910  return CurLexerCallback != CLK_Lexer;
1911  }
1912 
1913  /// Update the current token to represent the provided
1914  /// identifier, in order to cache an action performed by typo correction.
1915  void TypoCorrectToken(const Token &Tok) {
1916  assert(Tok.getIdentifierInfo() && "Expected identifier token");
1917  if (CachedLexPos != 0 && isBacktrackEnabled())
1918  CachedTokens[CachedLexPos-1] = Tok;
1919  }
1920 
1921  /// Recompute the current lexer kind based on the CurLexer/
1922  /// CurTokenLexer pointers.
1923  void recomputeCurLexerKind();
1924 
1925  /// Returns true if incremental processing is enabled
1926  bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
1927 
1928  /// Enables the incremental processing
1929  void enableIncrementalProcessing(bool value = true) {
1930  IncrementalProcessing = value;
1931  }
1932 
1933  /// Specify the point at which code-completion will be performed.
1934  ///
1935  /// \param File the file in which code completion should occur. If
1936  /// this file is included multiple times, code-completion will
1937  /// perform completion the first time it is included. If NULL, this
1938  /// function clears out the code-completion point.
1939  ///
1940  /// \param Line the line at which code completion should occur
1941  /// (1-based).
1942  ///
1943  /// \param Column the column at which code completion should occur
1944  /// (1-based).
1945  ///
1946  /// \returns true if an error occurred, false otherwise.
1947  bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line,
1948  unsigned Column);
1949 
1950  /// Determine if we are performing code completion.
1951  bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }
1952 
1953  /// Returns the location of the code-completion point.
1954  ///
1955  /// Returns an invalid location if code-completion is not enabled or the file
1956  /// containing the code-completion point has not been lexed yet.
1957  SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
1958 
1959  /// Returns the start location of the file of code-completion point.
1960  ///
1961  /// Returns an invalid location if code-completion is not enabled or the file
1962  /// containing the code-completion point has not been lexed yet.
1964  return CodeCompletionFileLoc;
1965  }
1966 
1967  /// Returns true if code-completion is enabled and we have hit the
1968  /// code-completion point.
1969  bool isCodeCompletionReached() const { return CodeCompletionReached; }
1970 
1971  /// Note that we hit the code-completion point.
1973  assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
1974  CodeCompletionReached = true;
1975  // Silence any diagnostics that occur after we hit the code-completion.
1977  }
1978 
1979  /// The location of the currently-active \#pragma clang
1980  /// arc_cf_code_audited begin.
1981  ///
1982  /// Returns an invalid location if there is no such pragma active.
1983  std::pair<IdentifierInfo *, SourceLocation>
1985  return PragmaARCCFCodeAuditedInfo;
1986  }
1987 
1988  /// Set the location of the currently-active \#pragma clang
1989  /// arc_cf_code_audited begin. An invalid location ends the pragma.
1991  SourceLocation Loc) {
1992  PragmaARCCFCodeAuditedInfo = {Ident, Loc};
1993  }
1994 
1995  /// The location of the currently-active \#pragma clang
1996  /// assume_nonnull begin.
1997  ///
1998  /// Returns an invalid location if there is no such pragma active.
2000  return PragmaAssumeNonNullLoc;
2001  }
2002 
2003  /// Set the location of the currently-active \#pragma clang
2004  /// assume_nonnull begin. An invalid location ends the pragma.
2006  PragmaAssumeNonNullLoc = Loc;
2007  }
2008 
2009  /// Get the location of the recorded unterminated \#pragma clang
2010  /// assume_nonnull begin in the preamble, if one exists.
2011  ///
2012  /// Returns an invalid location if the premable did not end with
2013  /// such a pragma active or if there is no recorded preamble.
2015  return PreambleRecordedPragmaAssumeNonNullLoc;
2016  }
2017 
2018  /// Record the location of the unterminated \#pragma clang
2019  /// assume_nonnull begin in the preamble.
2021  PreambleRecordedPragmaAssumeNonNullLoc = Loc;
2022  }
2023 
2024  /// Set the directory in which the main file should be considered
2025  /// to have been found, if it is not a real file.
2026  void setMainFileDir(DirectoryEntryRef Dir) { MainFileDir = Dir; }
2027 
2028  /// Instruct the preprocessor to skip part of the main source file.
2029  ///
2030  /// \param Bytes The number of bytes in the preamble to skip.
2031  ///
2032  /// \param StartOfLine Whether skipping these bytes puts the lexer at the
2033  /// start of a line.
2034  void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
2035  SkipMainFilePreamble.first = Bytes;
2036  SkipMainFilePreamble.second = StartOfLine;
2037  }
2038 
2039  /// Forwarding function for diagnostics. This emits a diagnostic at
2040  /// the specified Token's location, translating the token's start
2041  /// position in the current buffer into a SourcePosition object for rendering.
2042  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
2043  return Diags->Report(Loc, DiagID);
2044  }
2045 
2046  DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
2047  return Diags->Report(Tok.getLocation(), DiagID);
2048  }
2049 
2050  /// Return the 'spelling' of the token at the given
2051  /// location; does not go up to the spelling location or down to the
2052  /// expansion location.
2053  ///
2054  /// \param buffer A buffer which will be used only if the token requires
2055  /// "cleaning", e.g. if it contains trigraphs or escaped newlines
2056  /// \param invalid If non-null, will be set \c true if an error occurs.
2058  SmallVectorImpl<char> &buffer,
2059  bool *invalid = nullptr) const {
2060  return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
2061  }
2062 
2063  /// Return the 'spelling' of the Tok token.
2064  ///
2065  /// The spelling of a token is the characters used to represent the token in
2066  /// the source file after trigraph expansion and escaped-newline folding. In
2067  /// particular, this wants to get the true, uncanonicalized, spelling of
2068  /// things like digraphs, UCNs, etc.
2069  ///
2070  /// \param Invalid If non-null, will be set \c true if an error occurs.
2071  std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const {
2072  return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
2073  }
2074 
2075  /// Get the spelling of a token into a preallocated buffer, instead
2076  /// of as an std::string.
2077  ///
2078  /// The caller is required to allocate enough space for the token, which is
2079  /// guaranteed to be at least Tok.getLength() bytes long. The length of the
2080  /// actual result is returned.
2081  ///
2082  /// Note that this method may do two possible things: it may either fill in
2083  /// the buffer specified with characters, or it may *change the input pointer*
2084  /// to point to a constant buffer with the data already in it (avoiding a
2085  /// copy). The caller is not allowed to modify the returned buffer pointer
2086  /// if an internal buffer is returned.
2087  unsigned getSpelling(const Token &Tok, const char *&Buffer,
2088  bool *Invalid = nullptr) const {
2089  return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
2090  }
2091 
2092  /// Get the spelling of a token into a SmallVector.
2093  ///
2094  /// Note that the returned StringRef may not point to the
2095  /// supplied buffer if a copy can be avoided.
2096  StringRef getSpelling(const Token &Tok,
2097  SmallVectorImpl<char> &Buffer,
2098  bool *Invalid = nullptr) const;
2099 
2100  /// Relex the token at the specified location.
2101  /// \returns true if there was a failure, false on success.
2103  bool IgnoreWhiteSpace = false) {
2104  return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace);
2105  }
2106 
2107  /// Given a Token \p Tok that is a numeric constant with length 1,
2108  /// return the character.
2109  char
2111  bool *Invalid = nullptr) const {
2112  assert(Tok.is(tok::numeric_constant) &&
2113  Tok.getLength() == 1 && "Called on unsupported token");
2114  assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
2115 
2116  // If the token is carrying a literal data pointer, just use it.
2117  if (const char *D = Tok.getLiteralData())
2118  return *D;
2119 
2120  // Otherwise, fall back on getCharacterData, which is slower, but always
2121  // works.
2122  return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
2123  }
2124 
2125  /// Retrieve the name of the immediate macro expansion.
2126  ///
2127  /// This routine starts from a source location, and finds the name of the
2128  /// macro responsible for its immediate expansion. It looks through any
2129  /// intervening macro argument expansions to compute this. It returns a
2130  /// StringRef that refers to the SourceManager-owned buffer of the source
2131  /// where that macro name is spelled. Thus, the result shouldn't out-live
2132  /// the SourceManager.
2134  return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
2135  }
2136 
2137  /// Plop the specified string into a scratch buffer and set the
2138  /// specified token's location and length to it.
2139  ///
2140  /// If specified, the source location provides a location of the expansion
2141  /// point of the token.
2142  void CreateString(StringRef Str, Token &Tok,
2143  SourceLocation ExpansionLocStart = SourceLocation(),
2144  SourceLocation ExpansionLocEnd = SourceLocation());
2145 
2146  /// Split the first Length characters out of the token starting at TokLoc
2147  /// and return a location pointing to the split token. Re-lexing from the
2148  /// split token will return the split token rather than the original.
2149  SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length);
2150 
2151  /// Computes the source location just past the end of the
2152  /// token at this source location.
2153  ///
2154  /// This routine can be used to produce a source location that
2155  /// points just past the end of the token referenced by \p Loc, and
2156  /// is generally used when a diagnostic needs to point just after a
2157  /// token where it expected something different that it received. If
2158  /// the returned source location would not be meaningful (e.g., if
2159  /// it points into a macro), this routine returns an invalid
2160  /// source location.
2161  ///
2162  /// \param Offset an offset from the end of the token, where the source
2163  /// location should refer to. The default offset (0) produces a source
2164  /// location pointing just past the end of the token; an offset of 1 produces
2165  /// a source location pointing to the last character in the token, etc.
2167  return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
2168  }
2169 
2170  /// Returns true if the given MacroID location points at the first
2171  /// token of the macro expansion.
2172  ///
2173  /// \param MacroBegin If non-null and function returns true, it is set to
2174  /// begin location of the macro.
2176  SourceLocation *MacroBegin = nullptr) const {
2177  return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
2178  MacroBegin);
2179  }
2180 
2181  /// Returns true if the given MacroID location points at the last
2182  /// token of the macro expansion.
2183  ///
2184  /// \param MacroEnd If non-null and function returns true, it is set to
2185  /// end location of the macro.
2187  SourceLocation *MacroEnd = nullptr) const {
2188  return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
2189  }
2190 
2191  /// Print the token to stderr, used for debugging.
2192  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
2193  void DumpLocation(SourceLocation Loc) const;
2194  void DumpMacro(const MacroInfo &MI) const;
2195  void dumpMacroInfo(const IdentifierInfo *II);
2196 
2197  /// Given a location that specifies the start of a
2198  /// token, return a new location that specifies a character within the token.
2200  unsigned Char) const {
2201  return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
2202  }
2203 
2204  /// Increment the counters for the number of token paste operations
2205  /// performed.
2206  ///
2207  /// If fast was specified, this is a 'fast paste' case we handled.
2208  void IncrementPasteCounter(bool isFast) {
2209  if (isFast)
2210  ++NumFastTokenPaste;
2211  else
2212  ++NumTokenPaste;
2213  }
2214 
2215  void PrintStats();
2216 
2217  size_t getTotalMemory() const;
2218 
2219  /// When the macro expander pastes together a comment (/##/) in Microsoft
2220  /// mode, this method handles updating the current state, returning the
2221  /// token on the next source line.
2223 
2224  //===--------------------------------------------------------------------===//
2225  // Preprocessor callback methods. These are invoked by a lexer as various
2226  // directives and events are found.
2227 
2228  /// Given a tok::raw_identifier token, look up the
2229  /// identifier information for the token and install it into the token,
2230  /// updating the token kind accordingly.
2232 
2233 private:
2234  llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
2235 
2236 public:
2237  /// Specifies the reason for poisoning an identifier.
2238  ///
2239  /// If that identifier is accessed while poisoned, then this reason will be
2240  /// used instead of the default "poisoned" diagnostic.
2241  void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
2242 
2243  /// Display reason for poisoned identifier.
2245 
2247  if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
2248  if(II->isPoisoned()) {
2250  }
2251  }
2252  }
2253 
2254 private:
2255  /// Identifiers used for SEH handling in Borland. These are only
2256  /// allowed in particular circumstances
2257  // __except block
2258  IdentifierInfo *Ident__exception_code,
2259  *Ident___exception_code,
2260  *Ident_GetExceptionCode;
2261  // __except filter expression
2262  IdentifierInfo *Ident__exception_info,
2263  *Ident___exception_info,
2264  *Ident_GetExceptionInfo;
2265  // __finally
2266  IdentifierInfo *Ident__abnormal_termination,
2267  *Ident___abnormal_termination,
2268  *Ident_AbnormalTermination;
2269 
2270  const char *getCurLexerEndPos();
2271  void diagnoseMissingHeaderInUmbrellaDir(const Module &Mod);
2272 
2273 public:
2274  void PoisonSEHIdentifiers(bool Poison = true); // Borland
2275 
2276  /// Callback invoked when the lexer reads an identifier and has
2277  /// filled in the tokens IdentifierInfo member.
2278  ///
2279  /// This callback potentially macro expands it or turns it into a named
2280  /// token (like 'for').
2281  ///
2282  /// \returns true if we actually computed a token, false if we need to
2283  /// lex again.
2285 
2286  /// Callback invoked when the lexer hits the end of the current file.
2287  ///
2288  /// This either returns the EOF token and returns true, or
2289  /// pops a level off the include stack and returns false, at which point the
2290  /// client should call lex again.
2291  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
2292 
2293  /// Callback invoked when the current TokenLexer hits the end of its
2294  /// token stream.
2295  bool HandleEndOfTokenLexer(Token &Result);
2296 
2297  /// Callback invoked when the lexer sees a # token at the start of a
2298  /// line.
2299  ///
2300  /// This consumes the directive, modifies the lexer/preprocessor state, and
2301  /// advances the lexer(s) so that the next token read is the correct one.
2302  void HandleDirective(Token &Result);
2303 
2304  /// Ensure that the next token is a tok::eod token.
2305  ///
2306  /// If not, emit a diagnostic and consume up until the eod.
2307  /// If \p EnableMacros is true, then we consider macros that expand to zero
2308  /// tokens as being ok.
2309  ///
2310  /// \return The location of the end of the directive (the terminating
2311  /// newline).
2312  SourceLocation CheckEndOfDirective(const char *DirType,
2313  bool EnableMacros = false);
2314 
2315  /// Read and discard all tokens remaining on the current line until
2316  /// the tok::eod token is found. Returns the range of the skipped tokens.
2318 
2319  /// Returns true if the preprocessor has seen a use of
2320  /// __DATE__ or __TIME__ in the file so far.
2321  bool SawDateOrTime() const {
2322  return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
2323  }
2324  unsigned getCounterValue() const { return CounterValue; }
2325  void setCounterValue(unsigned V) { CounterValue = V; }
2326 
2328  assert(CurrentFPEvalMethod != LangOptions::FEM_UnsetOnCommandLine &&
2329  "FPEvalMethod should be set either from command line or from the "
2330  "target info");
2331  return CurrentFPEvalMethod;
2332  }
2333 
2335  return TUFPEvalMethod;
2336  }
2337 
2339  return LastFPEvalPragmaLocation;
2340  }
2341 
2344  assert(Val != LangOptions::FEM_UnsetOnCommandLine &&
2345  "FPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2346  // This is the location of the '#pragma float_control" where the
2347  // execution state is modifed.
2348  LastFPEvalPragmaLocation = PragmaLoc;
2349  CurrentFPEvalMethod = Val;
2350  TUFPEvalMethod = Val;
2351  }
2352 
2354  assert(Val != LangOptions::FEM_UnsetOnCommandLine &&
2355  "TUPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2356  TUFPEvalMethod = Val;
2357  }
2358 
2359  /// Retrieves the module that we're currently building, if any.
2361 
2362  /// Retrieves the module whose implementation we're current compiling, if any.
2364 
2365  /// If we are preprocessing a named module.
2366  bool isInNamedModule() const { return ModuleDeclState.isNamedModule(); }
2367 
2368  /// If we are proprocessing a named interface unit.
2369  /// Note that a module implementation partition is not considered as an
2370  /// named interface unit here although it is importable
2371  /// to ease the parsing.
2372  bool isInNamedInterfaceUnit() const {
2373  return ModuleDeclState.isNamedInterface();
2374  }
2375 
2376  /// Get the named module name we're preprocessing.
2377  /// Requires we're preprocessing a named module.
2378  StringRef getNamedModuleName() const { return ModuleDeclState.getName(); }
2379 
2380  /// If we are implementing an implementation module unit.
2381  /// Note that the module implementation partition is not considered as an
2382  /// implementation unit.
2383  bool isInImplementationUnit() const {
2384  return ModuleDeclState.isImplementationUnit();
2385  }
2386 
2387  /// If we're importing a standard C++20 Named Modules.
2389  // NamedModuleImportPath will be non-empty only if we're importing
2390  // Standard C++ named modules.
2391  return !NamedModuleImportPath.empty() && getLangOpts().CPlusPlusModules &&
2392  !IsAtImport;
2393  }
2394 
2395  /// Allocate a new MacroInfo object with the provided SourceLocation.
2397 
2398  /// Turn the specified lexer token into a fully checked and spelled
2399  /// filename, e.g. as an operand of \#include.
2400  ///
2401  /// The caller is expected to provide a buffer that is large enough to hold
2402  /// the spelling of the filename, but is also expected to handle the case
2403  /// when this method decides to use a different buffer.
2404  ///
2405  /// \returns true if the input filename was in <>'s or false if it was
2406  /// in ""'s.
2407  bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Buffer);
2408 
2409  /// Given a "foo" or <foo> reference, look up the indicated file.
2410  ///
2411  /// Returns std::nullopt on failure. \p isAngled indicates whether the file
2412  /// reference is for system \#include's or not (i.e. using <> instead of "").
2414  LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
2415  ConstSearchDirIterator FromDir, const FileEntry *FromFile,
2416  ConstSearchDirIterator *CurDir, SmallVectorImpl<char> *SearchPath,
2417  SmallVectorImpl<char> *RelativePath,
2418  ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
2419  bool *IsFrameworkFound, bool SkipCache = false,
2420  bool OpenFile = true, bool CacheFailures = true);
2421 
2422  /// Return true if we're in the top-level file, not in a \#include.
2423  bool isInPrimaryFile() const;
2424 
2425  /// Lex an on-off-switch (C99 6.10.6p2) and verify that it is
2426  /// followed by EOD. Return true if the token is not a valid on-off-switch.
2427  bool LexOnOffSwitch(tok::OnOffSwitch &Result);
2428 
2429  bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
2430  bool *ShadowFlag = nullptr);
2431 
2432  void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma);
2433  Module *LeaveSubmodule(bool ForPragma);
2434 
2435 private:
2437 
2438  void PushIncludeMacroStack() {
2439  assert(CurLexerCallback != CLK_CachingLexer &&
2440  "cannot push a caching lexer");
2441  IncludeMacroStack.emplace_back(CurLexerCallback, CurLexerSubmodule,
2442  std::move(CurLexer), CurPPLexer,
2443  std::move(CurTokenLexer), CurDirLookup);
2444  CurPPLexer = nullptr;
2445  }
2446 
2447  void PopIncludeMacroStack() {
2448  CurLexer = std::move(IncludeMacroStack.back().TheLexer);
2449  CurPPLexer = IncludeMacroStack.back().ThePPLexer;
2450  CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
2451  CurDirLookup = IncludeMacroStack.back().TheDirLookup;
2452  CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule;
2453  CurLexerCallback = IncludeMacroStack.back().CurLexerCallback;
2454  IncludeMacroStack.pop_back();
2455  }
2456 
2457  void PropagateLineStartLeadingSpaceInfo(Token &Result);
2458 
2459  /// Determine whether we need to create module macros for #defines in the
2460  /// current context.
2461  bool needModuleMacros() const;
2462 
2463  /// Update the set of active module macros and ambiguity flag for a module
2464  /// macro name.
2465  void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
2466 
2467  DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
2468  SourceLocation Loc);
2469  UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
2470  VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
2471  bool isPublic);
2472 
2473  /// Lex and validate a macro name, which occurs after a
2474  /// \#define or \#undef.
2475  ///
2476  /// \param MacroNameTok Token that represents the name defined or undefined.
2477  /// \param IsDefineUndef Kind if preprocessor directive.
2478  /// \param ShadowFlag Points to flag that is set if macro name shadows
2479  /// a keyword.
2480  ///
2481  /// This emits a diagnostic, sets the token kind to eod,
2482  /// and discards the rest of the macro line if the macro name is invalid.
2483  void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other,
2484  bool *ShadowFlag = nullptr);
2485 
2486  /// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2487  /// entire line) of the macro's tokens and adds them to MacroInfo, and while
2488  /// doing so performs certain validity checks including (but not limited to):
2489  /// - # (stringization) is followed by a macro parameter
2490  /// \param MacroNameTok - Token that represents the macro name
2491  /// \param ImmediatelyAfterHeaderGuard - Macro follows an #ifdef header guard
2492  ///
2493  /// Either returns a pointer to a MacroInfo object OR emits a diagnostic and
2494  /// returns a nullptr if an invalid sequence of tokens is encountered.
2495  MacroInfo *ReadOptionalMacroParameterListAndBody(
2496  const Token &MacroNameTok, bool ImmediatelyAfterHeaderGuard);
2497 
2498  /// The ( starting an argument list of a macro definition has just been read.
2499  /// Lex the rest of the parameters and the closing ), updating \p MI with
2500  /// what we learn and saving in \p LastTok the last token read.
2501  /// Return true if an error occurs parsing the arg list.
2502  bool ReadMacroParameterList(MacroInfo *MI, Token& LastTok);
2503 
2504  /// Provide a suggestion for a typoed directive. If there is no typo, then
2505  /// just skip suggesting.
2506  ///
2507  /// \param Tok - Token that represents the directive
2508  /// \param Directive - String reference for the directive name
2509  void SuggestTypoedDirective(const Token &Tok, StringRef Directive) const;
2510 
2511  /// We just read a \#if or related directive and decided that the
2512  /// subsequent tokens are in the \#if'd out portion of the
2513  /// file. Lex the rest of the file, until we see an \#endif. If \p
2514  /// FoundNonSkipPortion is true, then we have already emitted code for part of
2515  /// this \#if directive, so \#else/\#elif blocks should never be entered. If
2516  /// \p FoundElse is false, then \#else directives are ok, if not, then we have
2517  /// already seen one so a \#else directive is a duplicate. When this returns,
2518  /// the caller can lex the first valid token.
2519  void SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
2520  SourceLocation IfTokenLoc,
2521  bool FoundNonSkipPortion, bool FoundElse,
2522  SourceLocation ElseLoc = SourceLocation());
2523 
2524  /// Information about the result for evaluating an expression for a
2525  /// preprocessor directive.
2526  struct DirectiveEvalResult {
2527  /// Whether the expression was evaluated as true or not.
2528  bool Conditional;
2529 
2530  /// True if the expression contained identifiers that were undefined.
2531  bool IncludedUndefinedIds;
2532 
2533  /// The source range for the expression.
2534  SourceRange ExprRange;
2535  };
2536 
2537  /// Evaluate an integer constant expression that may occur after a
2538  /// \#if or \#elif directive and return a \p DirectiveEvalResult object.
2539  ///
2540  /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
2541  DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
2542 
2543  /// Process a '__has_include("path")' expression.
2544  ///
2545  /// Returns true if successful.
2546  bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II);
2547 
2548  /// Process '__has_include_next("path")' expression.
2549  ///
2550  /// Returns true if successful.
2551  bool EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II);
2552 
2553  /// Get the directory and file from which to start \#include_next lookup.
2554  std::pair<ConstSearchDirIterator, const FileEntry *>
2555  getIncludeNextStart(const Token &IncludeNextTok) const;
2556 
2557  /// Install the standard preprocessor pragmas:
2558  /// \#pragma GCC poison/system_header/dependency and \#pragma once.
2559  void RegisterBuiltinPragmas();
2560 
2561  /// Register builtin macros such as __LINE__ with the identifier table.
2562  void RegisterBuiltinMacros();
2563 
2564  /// If an identifier token is read that is to be expanded as a macro, handle
2565  /// it and return the next token as 'Tok'. If we lexed a token, return true;
2566  /// otherwise the caller should lex again.
2567  bool HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &MD);
2568 
2569  /// Cache macro expanded tokens for TokenLexers.
2570  //
2571  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
2572  /// going to lex in the cache and when it finishes the tokens are removed
2573  /// from the end of the cache.
2574  Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
2575  ArrayRef<Token> tokens);
2576 
2577  void removeCachedMacroExpandedTokensOfLastLexer();
2578 
2579  /// Determine whether the next preprocessor token to be
2580  /// lexed is a '('. If so, consume the token and return true, if not, this
2581  /// method should have no observable side-effect on the lexed tokens.
2582  bool isNextPPTokenLParen();
2583 
2584  /// After reading "MACRO(", this method is invoked to read all of the formal
2585  /// arguments specified for the macro invocation. Returns null on error.
2586  MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI,
2587  SourceLocation &MacroEnd);
2588 
2589  /// If an identifier token is read that is to be expanded
2590  /// as a builtin macro, handle it and return the next token as 'Tok'.
2591  void ExpandBuiltinMacro(Token &Tok);
2592 
2593  /// Read a \c _Pragma directive, slice it up, process it, then
2594  /// return the first token after the directive.
2595  /// This assumes that the \c _Pragma token has just been read into \p Tok.
2596  void Handle_Pragma(Token &Tok);
2597 
2598  /// Like Handle_Pragma except the pragma text is not enclosed within
2599  /// a string literal.
2600  void HandleMicrosoft__pragma(Token &Tok);
2601 
2602  /// Add a lexer to the top of the include stack and
2603  /// start lexing tokens from it instead of the current buffer.
2604  void EnterSourceFileWithLexer(Lexer *TheLexer, ConstSearchDirIterator Dir);
2605 
2606  /// Set the FileID for the preprocessor predefines.
2607  void setPredefinesFileID(FileID FID) {
2608  assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
2609  PredefinesFileID = FID;
2610  }
2611 
2612  /// Set the FileID for the PCH through header.
2613  void setPCHThroughHeaderFileID(FileID FID);
2614 
2615  /// Returns true if we are lexing from a file and not a
2616  /// pragma or a macro.
2617  static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
2618  return L ? !L->isPragmaLexer() : P != nullptr;
2619  }
2620 
2621  static bool IsFileLexer(const IncludeStackInfo& I) {
2622  return IsFileLexer(I.TheLexer.get(), I.ThePPLexer);
2623  }
2624 
2625  bool IsFileLexer() const {
2626  return IsFileLexer(CurLexer.get(), CurPPLexer);
2627  }
2628 
2629  //===--------------------------------------------------------------------===//
2630  // Caching stuff.
2631  void CachingLex(Token &Result);
2632 
2633  bool InCachingLexMode() const {
2634  // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
2635  // that we are past EOF, not that we are in CachingLex mode.
2636  return !CurPPLexer && !CurTokenLexer && !IncludeMacroStack.empty();
2637  }
2638 
2639  void EnterCachingLexMode();
2640  void EnterCachingLexModeUnchecked();
2641 
2642  void ExitCachingLexMode() {
2643  if (InCachingLexMode())
2645  }
2646 
2647  const Token &PeekAhead(unsigned N);
2648  void AnnotatePreviousCachedTokens(const Token &Tok);
2649 
2650  //===--------------------------------------------------------------------===//
2651  /// Handle*Directive - implement the various preprocessor directives. These
2652  /// should side-effect the current preprocessor object so that the next call
2653  /// to Lex() will return the appropriate token next.
2654  void HandleLineDirective();
2655  void HandleDigitDirective(Token &Tok);
2656  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
2657  void HandleIdentSCCSDirective(Token &Tok);
2658  void HandleMacroPublicDirective(Token &Tok);
2659  void HandleMacroPrivateDirective();
2660 
2661  /// An additional notification that can be produced by a header inclusion or
2662  /// import to tell the parser what happened.
2663  struct ImportAction {
2664  enum ActionKind {
2665  None,
2666  ModuleBegin,
2667  ModuleImport,
2668  HeaderUnitImport,
2669  SkippedModuleImport,
2670  Failure,
2671  } Kind;
2672  Module *ModuleForHeader = nullptr;
2673 
2674  ImportAction(ActionKind AK, Module *Mod = nullptr)
2675  : Kind(AK), ModuleForHeader(Mod) {
2676  assert((AK == None || Mod || AK == Failure) &&
2677  "no module for module action");
2678  }
2679  };
2680 
2681  OptionalFileEntryRef LookupHeaderIncludeOrImport(
2682  ConstSearchDirIterator *CurDir, StringRef &Filename,
2683  SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2684  const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2685  bool &IsMapped, ConstSearchDirIterator LookupFrom,
2686  const FileEntry *LookupFromFile, StringRef &LookupFilename,
2687  SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2688  ModuleMap::KnownHeader &SuggestedModule, bool isAngled);
2689 
2690  // File inclusion.
2691  void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok,
2692  ConstSearchDirIterator LookupFrom = nullptr,
2693  const FileEntry *LookupFromFile = nullptr);
2694  ImportAction
2695  HandleHeaderIncludeOrImport(SourceLocation HashLoc, Token &IncludeTok,
2696  Token &FilenameTok, SourceLocation EndLoc,
2697  ConstSearchDirIterator LookupFrom = nullptr,
2698  const FileEntry *LookupFromFile = nullptr);
2699  void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
2700  void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
2701  void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
2702  void HandleMicrosoftImportDirective(Token &Tok);
2703 
2704 public:
2705  /// Check that the given module is available, producing a diagnostic if not.
2706  /// \return \c true if the check failed (because the module is not available).
2707  /// \c false if the module appears to be usable.
2708  static bool checkModuleIsAvailable(const LangOptions &LangOpts,
2709  const TargetInfo &TargetInfo,
2710  const Module &M, DiagnosticsEngine &Diags);
2711 
2712  // Module inclusion testing.
2713  /// Find the module that owns the source or header file that
2714  /// \p Loc points to. If the location is in a file that was included
2715  /// into a module, or is outside any module, returns nullptr.
2716  Module *getModuleForLocation(SourceLocation Loc, bool AllowTextual);
2717 
2718  /// We want to produce a diagnostic at location IncLoc concerning an
2719  /// unreachable effect at location MLoc (eg, where a desired entity was
2720  /// declared or defined). Determine whether the right way to make MLoc
2721  /// reachable is by #include, and if so, what header should be included.
2722  ///
2723  /// This is not necessarily fast, and might load unexpected module maps, so
2724  /// should only be called by code that intends to produce an error.
2725  ///
2726  /// \param IncLoc The location at which the missing effect was detected.
2727  /// \param MLoc A location within an unimported module at which the desired
2728  /// effect occurred.
2729  /// \return A file that can be #included to provide the desired effect. Null
2730  /// if no such file could be determined or if a #include is not
2731  /// appropriate (eg, if a module should be imported instead).
2733  SourceLocation MLoc);
2734 
2735  bool isRecordingPreamble() const {
2736  return PreambleConditionalStack.isRecording();
2737  }
2738 
2739  bool hasRecordedPreamble() const {
2740  return PreambleConditionalStack.hasRecordedPreamble();
2741  }
2742 
2744  return PreambleConditionalStack.getStack();
2745  }
2746 
2748  PreambleConditionalStack.setStack(s);
2749  }
2750 
2752  ArrayRef<PPConditionalInfo> s, std::optional<PreambleSkipInfo> SkipInfo) {
2753  PreambleConditionalStack.startReplaying();
2754  PreambleConditionalStack.setStack(s);
2755  PreambleConditionalStack.SkipInfo = SkipInfo;
2756  }
2757 
2758  std::optional<PreambleSkipInfo> getPreambleSkipInfo() const {
2759  return PreambleConditionalStack.SkipInfo;
2760  }
2761 
2762 private:
2763  /// After processing predefined file, initialize the conditional stack from
2764  /// the preamble.
2765  void replayPreambleConditionalStack();
2766 
2767  // Macro handling.
2768  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
2769  void HandleUndefDirective();
2770 
2771  // Conditional Inclusion.
2772  void HandleIfdefDirective(Token &Result, const Token &HashToken,
2773  bool isIfndef, bool ReadAnyTokensBeforeDirective);
2774  void HandleIfDirective(Token &IfToken, const Token &HashToken,
2775  bool ReadAnyTokensBeforeDirective);
2776  void HandleEndifDirective(Token &EndifToken);
2777  void HandleElseDirective(Token &Result, const Token &HashToken);
2778  void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken,
2780 
2781  // Pragmas.
2782  void HandlePragmaDirective(PragmaIntroducer Introducer);
2783 
2784 public:
2785  void HandlePragmaOnce(Token &OnceTok);
2786  void HandlePragmaMark(Token &MarkTok);
2787  void HandlePragmaPoison();
2788  void HandlePragmaSystemHeader(Token &SysHeaderTok);
2789  void HandlePragmaDependency(Token &DependencyTok);
2790  void HandlePragmaPushMacro(Token &Tok);
2791  void HandlePragmaPopMacro(Token &Tok);
2792  void HandlePragmaIncludeAlias(Token &Tok);
2793  void HandlePragmaModuleBuild(Token &Tok);
2794  void HandlePragmaHdrstop(Token &Tok);
2796 
2797  // Return true and store the first token only if any CommentHandler
2798  // has inserted some tokens and getCommentRetentionState() is false.
2799  bool HandleComment(Token &result, SourceRange Comment);
2800 
2801  /// A macro is used, update information about macros that need unused
2802  /// warnings.
2803  void markMacroAsUsed(MacroInfo *MI);
2804 
2805  void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg,
2806  SourceLocation AnnotationLoc) {
2807  auto Annotations = AnnotationInfos.find(II);
2808  if (Annotations == AnnotationInfos.end())
2809  AnnotationInfos.insert(std::make_pair(
2810  II,
2811  MacroAnnotations::makeDeprecation(AnnotationLoc, std::move(Msg))));
2812  else
2813  Annotations->second.DeprecationInfo =
2814  MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
2815  }
2816 
2817  void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg,
2818  SourceLocation AnnotationLoc) {
2819  auto Annotations = AnnotationInfos.find(II);
2820  if (Annotations == AnnotationInfos.end())
2821  AnnotationInfos.insert(
2822  std::make_pair(II, MacroAnnotations::makeRestrictExpansion(
2823  AnnotationLoc, std::move(Msg))));
2824  else
2825  Annotations->second.RestrictExpansionInfo =
2826  MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
2827  }
2828 
2829  void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) {
2830  auto Annotations = AnnotationInfos.find(II);
2831  if (Annotations == AnnotationInfos.end())
2832  AnnotationInfos.insert(
2833  std::make_pair(II, MacroAnnotations::makeFinal(AnnotationLoc)));
2834  else
2835  Annotations->second.FinalAnnotationLoc = AnnotationLoc;
2836  }
2837 
2838  const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const {
2839  return AnnotationInfos.find(II)->second;
2840  }
2841 
2843  bool IsIfnDef = false) const {
2844  IdentifierInfo *Info = Identifier.getIdentifierInfo();
2845  if (Info->isDeprecatedMacro())
2846  emitMacroDeprecationWarning(Identifier);
2847 
2848  if (Info->isRestrictExpansion() &&
2849  !SourceMgr.isInMainFile(Identifier.getLocation()))
2850  emitRestrictExpansionWarning(Identifier);
2851 
2852  if (!IsIfnDef) {
2853  if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs)
2854  emitRestrictInfNaNWarning(Identifier, 0);
2855  if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs)
2856  emitRestrictInfNaNWarning(Identifier, 1);
2857  }
2858  }
2859 
2861  const LangOptions &LangOpts,
2862  const TargetInfo &TI);
2863 
2864  static void processPathToFileName(SmallVectorImpl<char> &FileName,
2865  const PresumedLoc &PLoc,
2866  const LangOptions &LangOpts,
2867  const TargetInfo &TI);
2868 
2869 private:
2870  void emitMacroDeprecationWarning(const Token &Identifier) const;
2871  void emitRestrictExpansionWarning(const Token &Identifier) const;
2872  void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const;
2873  void emitRestrictInfNaNWarning(const Token &Identifier,
2874  unsigned DiagSelection) const;
2875 
2876  /// This boolean state keeps track if the current scanned token (by this PP)
2877  /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a
2878  /// translation unit in a linear order.
2879  bool InSafeBufferOptOutRegion = false;
2880 
2881  /// Hold the start location of the current "-Wunsafe-buffer-usage" opt-out
2882  /// region if PP is currently in such a region. Hold undefined value
2883  /// otherwise.
2884  SourceLocation CurrentSafeBufferOptOutStart; // It is used to report the start location of an never-closed region.
2885 
2886  // An ordered sequence of "-Wunsafe-buffer-usage" opt-out regions in one
2887  // translation unit. Each region is represented by a pair of start and end
2888  // locations. A region is "open" if its' start and end locations are
2889  // identical.
2891 
2892 public:
2893  /// \return true iff the given `Loc` is in a "-Wunsafe-buffer-usage" opt-out
2894  /// region. This `Loc` must be a source location that has been pre-processed.
2895  bool isSafeBufferOptOut(const SourceManager&SourceMgr, const SourceLocation &Loc) const;
2896 
2897  /// Alter the state of whether this PP currently is in a
2898  /// "-Wunsafe-buffer-usage" opt-out region.
2899  ///
2900  /// \param isEnter true if this PP is entering a region; otherwise, this PP
2901  /// is exiting a region
2902  /// \param Loc the location of the entry or exit of a
2903  /// region
2904  /// \return true iff it is INVALID to enter or exit a region, i.e.,
2905  /// attempt to enter a region before exiting a previous region, or exiting a
2906  /// region that PP is not currently in.
2907  bool enterOrExitSafeBufferOptOutRegion(bool isEnter,
2908  const SourceLocation &Loc);
2909 
2910  /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2911  /// opt-out region
2913 
2914  /// \param StartLoc output argument. It will be set to the start location of
2915  /// the current "-Wunsafe-buffer-usage" opt-out region iff this function
2916  /// returns true.
2917  /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2918  /// opt-out region
2920 
2921 private:
2922  /// Helper functions to forward lexing to the actual lexer. They all share the
2923  /// same signature.
2924  static bool CLK_Lexer(Preprocessor &P, Token &Result) {
2925  return P.CurLexer->Lex(Result);
2926  }
2927  static bool CLK_TokenLexer(Preprocessor &P, Token &Result) {
2928  return P.CurTokenLexer->Lex(Result);
2929  }
2930  static bool CLK_CachingLexer(Preprocessor &P, Token &Result) {
2931  P.CachingLex(Result);
2932  return true;
2933  }
2934  static bool CLK_DependencyDirectivesLexer(Preprocessor &P, Token &Result) {
2935  return P.CurLexer->LexDependencyDirectiveToken(Result);
2936  }
2937  static bool CLK_LexAfterModuleImport(Preprocessor &P, Token &Result) {
2938  return P.LexAfterModuleImport(Result);
2939  }
2940 };
2941 
2942 /// Abstract base class that describes a handler that will receive
2943 /// source ranges for each of the comments encountered in the source file.
2945 public:
2946  virtual ~CommentHandler();
2947 
2948  // The handler shall return true if it has pushed any tokens
2949  // to be read using e.g. EnterToken or EnterTokenStream.
2950  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
2951 };
2952 
2953 /// Abstract base class that describes a handler that will receive
2954 /// source ranges for empty lines encountered in the source file.
2956 public:
2958 
2959  // The handler handles empty lines.
2960  virtual void HandleEmptyline(SourceRange Range) = 0;
2961 };
2962 
2963 /// Registry of pragma handlers added by plugins
2964 using PragmaHandlerRegistry = llvm::Registry<PragmaHandler>;
2965 
2966 } // namespace clang
2967 
2968 #endif // LLVM_CLANG_LEX_PREPROCESSOR_H
#define V(N, I)
Definition: ASTContext.h:3299
int Id
Definition: ASTDiff.cpp:190
StringRef P
#define SM(sm)
Definition: Cuda.cpp:83
Defines the Diagnostic-related interfaces.
static constexpr Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:32
Defines the Diagnostic IDs-related interfaces.
unsigned Offset
Definition: Format.cpp:2978
StringRef Filename
Definition: Format.cpp:2976
StringRef Identifier
Definition: Format.cpp:2984
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.
llvm::MachO::Target Target
Definition: MachO.h:50
llvm::MachO::Record Record
Definition: MachO.h:31
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.
static std::string getName(const CallEvent &Call)
SourceRange Range
Definition: SemaObjC.cpp:754
SourceLocation Loc
Definition: SemaObjC.cpp:755
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Defines the clang::TokenKind enum and support functions.
SourceLocation End
LineState State
__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:1277
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:193
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1553
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:703
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:253
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:299
@ 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:313
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:482
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
PreprocessorOptions & getPreprocessorOpts() const
Retrieve the preprocessor options used to initialize this preprocessor.
SourceLocation getLastFPEvalPragmaLocation() const
bool isMacroDefined(const IdentifierInfo *II)
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)
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.
void CollectPpImportSuffix(SmallVectorImpl< Token > &Toks)
Collect the tokens of a C++20 pp-import-suffix.
IncludedFilesSet & getIncludedFiles()
Get the set of included files.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define'd and not #undef'...
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:2196
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI)
bool isInNamedInterfaceUnit() const
If we are proprocessing a named interface unit.
void setPreambleRecordedPragmaAssumeNonNullLoc(SourceLocation Loc)
Record the location of the unterminated #pragma clang assume_nonnull begin in the preamble.
const llvm::SmallSetVector< Module *, 2 > & getAffectingClangModules() const
Get the set of top-level clang modules that affected preprocessing, but were not imported.
SourceManager & getSourceManager() const
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
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.
ExternalPreprocessorSource * getExternalSource() const
void setRecordedPreambleConditionalStack(ArrayRef< PPConditionalInfo > s)
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
void enableIncrementalProcessing(bool value=true)
Enables the incremental processing.
bool LexAfterModuleImport(Token &Result)
Lex a token following the 'import' contextual keyword.
HeaderSearch & getHeaderSearchInfo() const
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)
const std::string & getPredefines() const
Get the predefines for this processor.
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.
void CommitBacktrackedTokens()
Disable the last EnableBacktrackAtThisPos call.
Definition: PPCaching.cpp:32
friend class MacroArgs
Definition: Preprocessor.h:693
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
void DumpMacro(const MacroInfo &MI) const
const IncludedFilesSet & getIncludedFiles() const
CodeCompletionHandler * getCodeCompletionHandler() const
Retrieve the current code-completion handler.
bool HandleEndOfTokenLexer(Token &Result)
Callback invoked when the current TokenLexer hits the end of its token stream.
void setDiagnostics(DiagnosticsEngine &D)
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 ...
const Token & LookAhead(unsigned N)
Peeks ahead N tokens and returns that token without consuming any tokens.
bool isInImportingCXXNamedModules() const
If we're importing a standard C++20 Named Modules.
PPCallbacks * getPPCallbacks() const
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.
void LexNonComment(Token &Result)
Lex a token.
void removeCommentHandler(CommentHandler *Handler)
Remove the specified comment handler.
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...
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...
ArrayRef< PPConditionalInfo > getPreambleConditionalStack() const
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.
ArrayRef< BuildingSubmoduleInfo > getBuildingSubmodules() const
Get the list of submodules that we're currently building.
void setReplayablePreambleConditionalStack(ArrayRef< PPConditionalInfo > s, std::optional< PreambleSkipInfo > SkipInfo)
DiagnosticsEngine & getDiagnostics() const
std::optional< PreambleSkipInfo > getPreambleSkipInfo() const
Module * getCurrentLexerSubmodule() const
Return the submodule owning the file being lexed.
macro_iterator macro_end(bool IncludeExternalMacros=true) 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)
void setPreprocessToken(bool Preprocess)
IdentifierTable & getIdentifierTable()
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
const MacroAnnotations & getMacroAnnotations(const IdentifierInfo *II) 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
MacroInfo * getMacroInfo(const IdentifierInfo *II)
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
FileManager & getFileManager() const
bool hadModuleLoaderFatalFailure() const
SelectorTable & getSelectorTable()
static void processPathToFileName(SmallVectorImpl< char > &FileName, const PresumedLoc &PLoc, const LangOptions &LangOpts, const TargetInfo &TI)
void setCurrentFPEvalMethod(SourceLocation PragmaLoc, LangOptions::FPEvalMethodKind Val)
bool LexHeaderName(Token &Result, bool AllowMacroExpansion=true)
Lex a token, forming a header-name token if possible.
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
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.
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
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 ...
ArrayRef< ModuleMacro * > getLeafModuleMacros(const IdentifierInfo *II) const
Get the list of leaf (non-overridden) module macros for a name.
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.
std::pair< IdentifierInfo *, SourceLocation > getPragmaARCCFCodeAuditedInfo() const
The location of the currently-active #pragma clang arc_cf_code_audited begin.
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
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.
Module * LeaveSubmodule(bool ForPragma)
SourceRange DiscardUntilEndOfDirective()
Read and discard all tokens remaining on the current line until the tok::eod token is found.
void HandleDirective(Token &Result)
Callback invoked when the lexer sees a # token at the start of a line.
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)
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.
const IdentifierTable & getIdentifierTable() const
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
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.
const TargetInfo * getAuxTargetInfo() const
void SetSuppressIncludeNotFoundError(bool Suppress)
EmptylineHandler * getEmptylineHandler() const
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.
const LangOptions & getLangOpts() const
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)
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.
Builtin::Context & getBuiltinInfo()
void HandlePragmaMark(Token &MarkTok)
Definition: Pragma.cpp:432
bool getPragmasEnabled() const
void HandlePragmaHdrstop(Token &Tok)
Definition: Pragma.cpp:881
void setEmptylineHandler(EmptylineHandler *Handler)
Set empty line handler.
SourceLocation getLastCachedTokenLocation() const
Get the location of the last cached token, suitable for setting the end location of an annotation tok...
PreprocessorLexer * getCurrentLexer() const
Return the current lexer being lexed from.
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.
PreprocessingRecord * getPreprocessingRecord() const
Retrieve the preprocessing record, or NULL if there is no preprocessing record.
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.
llvm::BumpPtrAllocator & getPreprocessorAllocator()
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.
const TargetInfo & getTargetInfo() const
void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter)
Set the code completion token for filtering purposes.
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
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...
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
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:218
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
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
const char * getLiteralData() const
getLiteralData - For a literal token (numeric constant, string, etc), this returns a pointer to the s...
Definition: Token.h:225
tok::TokenKind getKind() const
Definition: Token.h:94
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:187
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
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
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.
llvm::cl::opt< std::string > Filter
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef
detail::SearchDirIteratorImpl< true > ConstSearchDirIterator
Definition: HeaderSearch.h:245
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...
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition: FileEntry.h:202
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:1074
@ TU_Complete
The translation unit is a complete translation unit.
Definition: LangOptions.h:1076
unsigned long uint64_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
Definition: Format.h:5433
#define bool
Definition: stdbool.h:24
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