1 //===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Preprocessor interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
15 #define LLVM_CLANG_LEX_PREPROCESSOR_H
17 #include "clang/Lex/MacroInfo.h"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Lex/PTHLexer.h"
20 #include "clang/Lex/PPCallbacks.h"
21 #include "clang/Lex/TokenLexer.h"
22 #include "clang/Lex/PTHManager.h"
23 #include "clang/Basic/Builtins.h"
24 #include "clang/Basic/Diagnostic.h"
25 #include "clang/Basic/IdentifierTable.h"
26 #include "clang/Basic/SourceLocation.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/IntrusiveRefCntPtr.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/OwningPtr.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/Support/Allocator.h"
39 class ExternalPreprocessorSource;
43 class PragmaNamespace;
49 class CodeCompletionHandler;
50 class DirectoryLookup;
51 class PreprocessingRecord;
54 /// Preprocessor - This object engages in a tight little dance with the lexer to
55 /// efficiently preprocess tokens. Lexers know only about tokens within a
56 /// single source file, and don't know anything about preprocessor-level issues
57 /// like the #include stack, token expansion, etc.
59 class Preprocessor : public llvm::RefCountedBase<Preprocessor> {
60 DiagnosticsEngine *Diags;
61 LangOptions &Features;
62 const TargetInfo *Target;
64 SourceManager &SourceMgr;
65 ScratchBuffer *ScratchBuf;
66 HeaderSearch &HeaderInfo;
67 ModuleLoader &TheModuleLoader;
69 /// \brief External source of macros.
70 ExternalPreprocessorSource *ExternalSource;
73 /// PTH - An optional PTHManager object used for getting tokens from
74 /// a token cache rather than lexing the original source file.
75 llvm::OwningPtr<PTHManager> PTH;
77 /// BP - A BumpPtrAllocator object used to quickly allocate and release
78 /// objects internal to the Preprocessor.
79 llvm::BumpPtrAllocator BP;
81 /// Identifiers for builtin macros and other builtins.
82 IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__
83 IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__
84 IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__
85 IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__
86 IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__
87 IdentifierInfo *Ident__COUNTER__; // __COUNTER__
88 IdentifierInfo *Ident_Pragma, *Ident__pragma; // _Pragma, __pragma
89 IdentifierInfo *Ident__VA_ARGS__; // __VA_ARGS__
90 IdentifierInfo *Ident__has_feature; // __has_feature
91 IdentifierInfo *Ident__has_extension; // __has_extension
92 IdentifierInfo *Ident__has_builtin; // __has_builtin
93 IdentifierInfo *Ident__has_attribute; // __has_attribute
94 IdentifierInfo *Ident__has_include; // __has_include
95 IdentifierInfo *Ident__has_include_next; // __has_include_next
96 IdentifierInfo *Ident__has_warning; // __has_warning
98 SourceLocation DATELoc, TIMELoc;
99 unsigned CounterValue; // Next __COUNTER__ value.
102 /// MaxIncludeStackDepth - Maximum depth of #includes.
103 MaxAllowedIncludeStackDepth = 200
106 // State that is set before the preprocessor begins.
107 bool KeepComments : 1;
108 bool KeepMacroComments : 1;
109 bool SuppressIncludeNotFoundError : 1;
110 bool AutoModuleImport : 1;
112 // State that changes while the preprocessor runs:
113 bool InMacroArgs : 1; // True if parsing fn macro invocation args.
115 /// Whether the preprocessor owns the header search object.
116 bool OwnsHeaderSearch : 1;
118 /// DisableMacroExpansion - True if macro expansion is disabled.
119 bool DisableMacroExpansion : 1;
121 /// \brief Whether we have already loaded macros from the external source.
122 mutable bool ReadMacrosFromExternalSource : 1;
124 /// Identifiers - This is mapping/lookup information for all identifiers in
125 /// the program, including program keywords.
126 mutable IdentifierTable Identifiers;
128 /// Selectors - This table contains all the selectors in the program. Unlike
129 /// IdentifierTable above, this table *isn't* populated by the preprocessor.
130 /// It is declared/expanded here because it's role/lifetime is
131 /// conceptually similar the IdentifierTable. In addition, the current control
132 /// flow (in clang::ParseAST()), make it convenient to put here.
133 /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
134 /// the lifetime of the preprocessor.
135 SelectorTable Selectors;
137 /// BuiltinInfo - Information about builtins.
138 Builtin::Context BuiltinInfo;
140 /// PragmaHandlers - This tracks all of the pragmas that the client registered
141 /// with this preprocessor.
142 PragmaNamespace *PragmaHandlers;
144 /// \brief Tracks all of the comment handlers that the client registered
145 /// with this preprocessor.
146 std::vector<CommentHandler *> CommentHandlers;
148 /// \brief The code-completion handler.
149 CodeCompletionHandler *CodeComplete;
151 /// \brief The file that we're performing code-completion for, if any.
152 const FileEntry *CodeCompletionFile;
154 /// \brief The offset in file for the code-completion point.
155 unsigned CodeCompletionOffset;
157 /// \brief The location for the code-completion point. This gets instantiated
158 /// when the CodeCompletionFile gets #include'ed for preprocessing.
159 SourceLocation CodeCompletionLoc;
161 /// \brief The start location for the file of the code-completion point.
162 /// This gets instantiated when the CodeCompletionFile gets #include'ed
163 /// for preprocessing.
164 SourceLocation CodeCompletionFileLoc;
166 /// \brief The source location of the __import_module__ keyword we just
168 SourceLocation ModuleImportLoc;
170 /// \brief The source location of the currently-active
171 /// #pragma clang arc_cf_code_audited begin.
172 SourceLocation PragmaARCCFCodeAuditedLoc;
174 /// \brief True if we hit the code-completion point.
175 bool CodeCompletionReached;
177 /// \brief The number of bytes that we will initially skip when entering the
178 /// main file, which is used when loading a precompiled preamble, along
179 /// with a flag that indicates whether skipping this number of bytes will
180 /// place the lexer at the start of a line.
181 std::pair<unsigned, bool> SkipMainFilePreamble;
183 /// CurLexer - This is the current top of the stack that we're lexing from if
184 /// not expanding a macro and we are lexing directly from source code.
185 /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
186 llvm::OwningPtr<Lexer> CurLexer;
188 /// CurPTHLexer - This is the current top of stack that we're lexing from if
189 /// not expanding from a macro and we are lexing from a PTH cache.
190 /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
191 llvm::OwningPtr<PTHLexer> CurPTHLexer;
193 /// CurPPLexer - This is the current top of the stack what we're lexing from
194 /// if not expanding a macro. This is an alias for either CurLexer or
196 PreprocessorLexer *CurPPLexer;
198 /// CurLookup - The DirectoryLookup structure used to find the current
199 /// FileEntry, if CurLexer is non-null and if applicable. This allows us to
200 /// implement #include_next and find directory-specific properties.
201 const DirectoryLookup *CurDirLookup;
203 /// CurTokenLexer - This is the current macro we are expanding, if we are
204 /// expanding a macro. One of CurLexer and CurTokenLexer must be null.
205 llvm::OwningPtr<TokenLexer> CurTokenLexer;
207 /// \brief The kind of lexer we're currently working with.
213 CLK_LexAfterModuleImport
216 /// IncludeMacroStack - This keeps track of the stack of files currently
217 /// #included, and macros currently being expanded from, not counting
218 /// CurLexer/CurTokenLexer.
219 struct IncludeStackInfo {
220 enum CurLexerKind CurLexerKind;
222 PTHLexer *ThePTHLexer;
223 PreprocessorLexer *ThePPLexer;
224 TokenLexer *TheTokenLexer;
225 const DirectoryLookup *TheDirLookup;
227 IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P,
228 PreprocessorLexer* PPL,
229 TokenLexer* TL, const DirectoryLookup *D)
230 : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL),
231 TheTokenLexer(TL), TheDirLookup(D) {}
233 std::vector<IncludeStackInfo> IncludeMacroStack;
235 /// Callbacks - These are actions invoked when some preprocessor activity is
236 /// encountered (e.g. a file is #included, etc).
237 PPCallbacks *Callbacks;
239 /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
240 /// to the actual definition of the macro.
241 llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
243 /// \brief Macros that we want to warn because they are not used at the end
244 /// of the translation unit; we store just their SourceLocations instead
245 /// something like MacroInfo*. The benefit of this is that when we are
246 /// deserializing from PCH, we don't need to deserialize identifier & macros
247 /// just so that we can report that they are unused, we just warn using
248 /// the SourceLocations of this set (that will be filled by the ASTReader).
249 /// We are using SmallPtrSet instead of a vector for faster removal.
250 typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
251 WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
253 /// MacroArgCache - This is a "freelist" of MacroArg objects that can be
254 /// reused for quick allocation.
255 MacroArgs *MacroArgCache;
256 friend class MacroArgs;
258 /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma
259 /// push_macro directive, we keep a MacroInfo stack used to restore
260 /// previous macro value.
261 llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
263 // Various statistics we track for performance analysis.
264 unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
265 unsigned NumIf, NumElse, NumEndif;
266 unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
267 unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
268 unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
271 /// Predefines - This string is the predefined macros that preprocessor
272 /// should use from the command line etc.
273 std::string Predefines;
275 /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
276 enum { TokenLexerCacheSize = 8 };
277 unsigned NumCachedTokenLexers;
278 TokenLexer *TokenLexerCache[TokenLexerCacheSize];
280 /// \brief Keeps macro expanded tokens for TokenLexers.
282 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
283 /// going to lex in the cache and when it finishes the tokens are removed
284 /// from the end of the cache.
285 SmallVector<Token, 16> MacroExpandedTokens;
286 std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
288 /// \brief A record of the macro definitions and expansions that
289 /// occurred during preprocessing.
291 /// This is an optional side structure that can be enabled with
292 /// \c createPreprocessingRecord() prior to preprocessing.
293 PreprocessingRecord *Record;
295 private: // Cached tokens state.
296 typedef SmallVector<Token, 1> CachedTokensTy;
298 /// CachedTokens - Cached tokens are stored here when we do backtracking or
299 /// lookahead. They are "lexed" by the CachingLex() method.
300 CachedTokensTy CachedTokens;
302 /// CachedLexPos - The position of the cached token that CachingLex() should
303 /// "lex" next. If it points beyond the CachedTokens vector, it means that
304 /// a normal Lex() should be invoked.
305 CachedTokensTy::size_type CachedLexPos;
307 /// BacktrackPositions - Stack of backtrack positions, allowing nested
308 /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
309 /// indicate where CachedLexPos should be set when the BackTrack() method is
310 /// invoked (at which point the last position is popped).
311 std::vector<CachedTokensTy::size_type> BacktrackPositions;
313 struct MacroInfoChain {
315 MacroInfoChain *Next;
316 MacroInfoChain *Prev;
319 /// MacroInfos are managed as a chain for easy disposal. This is the head
321 MacroInfoChain *MIChainHead;
323 /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
325 MacroInfoChain *MICache;
327 MacroInfo *getInfoForMacro(IdentifierInfo *II) const;
330 Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
331 const TargetInfo *target,
332 SourceManager &SM, HeaderSearch &Headers,
333 ModuleLoader &TheModuleLoader,
334 IdentifierInfoLookup *IILookup = 0,
335 bool OwnsHeaderSearch = false,
336 bool DelayInitialization = false);
340 /// \brief Initialize the preprocessor, if the constructor did not already
341 /// perform the initialization.
343 /// \param Target Information about the target.
344 void Initialize(const TargetInfo &Target);
346 DiagnosticsEngine &getDiagnostics() const { return *Diags; }
347 void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
349 const LangOptions &getLangOptions() const { return Features; }
350 const TargetInfo &getTargetInfo() const { return *Target; }
351 FileManager &getFileManager() const { return FileMgr; }
352 SourceManager &getSourceManager() const { return SourceMgr; }
353 HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
355 IdentifierTable &getIdentifierTable() { return Identifiers; }
356 SelectorTable &getSelectorTable() { return Selectors; }
357 Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
358 llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
360 void setPTHManager(PTHManager* pm);
362 PTHManager *getPTHManager() { return PTH.get(); }
364 void setExternalSource(ExternalPreprocessorSource *Source) {
365 ExternalSource = Source;
368 ExternalPreprocessorSource *getExternalSource() const {
369 return ExternalSource;
372 /// \brief Retrieve the module loader associated with this preprocessor.
373 ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
375 /// SetCommentRetentionState - Control whether or not the preprocessor retains
376 /// comments in output.
377 void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
378 this->KeepComments = KeepComments | KeepMacroComments;
379 this->KeepMacroComments = KeepMacroComments;
382 bool getCommentRetentionState() const { return KeepComments; }
384 void SetSuppressIncludeNotFoundError(bool Suppress) {
385 SuppressIncludeNotFoundError = Suppress;
388 bool GetSuppressIncludeNotFoundError() {
389 return SuppressIncludeNotFoundError;
392 /// \brief Specify whether automatic module imports are enabled.
393 void setAutoModuleImport(bool AutoModuleImport = true) {
394 this->AutoModuleImport = AutoModuleImport;
397 /// isCurrentLexer - Return true if we are lexing directly from the specified
399 bool isCurrentLexer(const PreprocessorLexer *L) const {
400 return CurPPLexer == L;
403 /// getCurrentLexer - Return the current lexer being lexed from. Note
404 /// that this ignores any potentially active macro expansions and _Pragma
405 /// expansions going on at the time.
406 PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
408 /// getCurrentFileLexer - Return the current file lexer being lexed from.
409 /// Note that this ignores any potentially active macro expansions and _Pragma
410 /// expansions going on at the time.
411 PreprocessorLexer *getCurrentFileLexer() const;
413 /// getPPCallbacks/addPPCallbacks - Accessors for preprocessor callbacks.
414 /// Note that this class takes ownership of any PPCallbacks object given to
416 PPCallbacks *getPPCallbacks() const { return Callbacks; }
417 void addPPCallbacks(PPCallbacks *C) {
419 C = new PPChainedCallbacks(C, Callbacks);
423 /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to
424 /// or null if it isn't #define'd.
425 MacroInfo *getMacroInfo(IdentifierInfo *II) const {
426 if (!II->hasMacroDefinition())
429 return getInfoForMacro(II);
432 /// setMacroInfo - Specify a macro for this identifier.
434 void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
436 /// macro_iterator/macro_begin/macro_end - This allows you to walk the current
437 /// state of the macro table. This visits every currently-defined macro.
438 typedef llvm::DenseMap<IdentifierInfo*,
439 MacroInfo*>::const_iterator macro_iterator;
440 macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
441 macro_iterator macro_end(bool IncludeExternalMacros = true) const;
443 const std::string &getPredefines() const { return Predefines; }
444 /// setPredefines - Set the predefines for this Preprocessor. These
445 /// predefines are automatically injected when parsing the main file.
446 void setPredefines(const char *P) { Predefines = P; }
447 void setPredefines(const std::string &P) { Predefines = P; }
449 /// getIdentifierInfo - Return information about the specified preprocessor
450 /// identifier token. The version of this method that takes two character
451 /// pointers is preferred unless the identifier is already available as a
452 /// string (this avoids allocation and copying of memory to construct an
454 IdentifierInfo *getIdentifierInfo(StringRef Name) const {
455 return &Identifiers.get(Name);
458 /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
459 /// If 'Namespace' is non-null, then it is a token required to exist on the
460 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
461 void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
462 void AddPragmaHandler(PragmaHandler *Handler) {
463 AddPragmaHandler(StringRef(), Handler);
466 /// RemovePragmaHandler - Remove the specific pragma handler from
467 /// the preprocessor. If \arg Namespace is non-null, then it should
468 /// be the namespace that \arg Handler was added to. It is an error
469 /// to remove a handler that has not been registered.
470 void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
471 void RemovePragmaHandler(PragmaHandler *Handler) {
472 RemovePragmaHandler(StringRef(), Handler);
475 /// \brief Add the specified comment handler to the preprocessor.
476 void AddCommentHandler(CommentHandler *Handler);
478 /// \brief Remove the specified comment handler.
480 /// It is an error to remove a handler that has not been registered.
481 void RemoveCommentHandler(CommentHandler *Handler);
483 /// \brief Set the code completion handler to the given object.
484 void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
485 CodeComplete = &Handler;
488 /// \brief Retrieve the current code-completion handler.
489 CodeCompletionHandler *getCodeCompletionHandler() const {
493 /// \brief Clear out the code completion handler.
494 void clearCodeCompletionHandler() {
498 /// \brief Hook used by the lexer to invoke the "natural language" code
499 /// completion point.
500 void CodeCompleteNaturalLanguage();
502 /// \brief Retrieve the preprocessing record, or NULL if there is no
503 /// preprocessing record.
504 PreprocessingRecord *getPreprocessingRecord() const { return Record; }
506 /// \brief Create a new preprocessing record, which will keep track of
507 /// all macro expansions, macro definitions, etc.
508 void createPreprocessingRecord(bool IncludeNestedMacroExpansions);
510 /// EnterMainSourceFile - Enter the specified FileID as the main source file,
511 /// which implicitly adds the builtin defines etc.
512 void EnterMainSourceFile();
514 /// EndSourceFile - Inform the preprocessor callbacks that processing is
516 void EndSourceFile();
518 /// EnterSourceFile - Add a source file to the top of the include stack and
519 /// start lexing tokens from it instead of the current buffer. Emit an error
520 /// and don't enter the file on error.
521 void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
524 /// EnterMacro - Add a Macro to the top of the include stack and start lexing
525 /// tokens from it instead of the current buffer. Args specifies the
526 /// tokens input to a function-like macro.
528 /// ILEnd specifies the location of the ')' for a function-like macro or the
529 /// identifier for an object-like macro.
530 void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroArgs *Args);
532 /// EnterTokenStream - Add a "macro" context to the top of the include stack,
533 /// which will cause the lexer to start returning the specified tokens.
535 /// If DisableMacroExpansion is true, tokens lexed from the token stream will
536 /// not be subject to further macro expansion. Otherwise, these tokens will
537 /// be re-macro-expanded when/if expansion is enabled.
539 /// If OwnsTokens is false, this method assumes that the specified stream of
540 /// tokens has a permanent owner somewhere, so they do not need to be copied.
541 /// If it is true, it assumes the array of tokens is allocated with new[] and
544 void EnterTokenStream(const Token *Toks, unsigned NumToks,
545 bool DisableMacroExpansion, bool OwnsTokens);
547 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
548 /// lexer stack. This should only be used in situations where the current
549 /// state of the top-of-stack lexer is known.
550 void RemoveTopOfLexerStack();
552 /// EnableBacktrackAtThisPos - From the point that this method is called, and
553 /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
554 /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
555 /// make the Preprocessor re-lex the same tokens.
557 /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
558 /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
559 /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
561 /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
562 /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
563 /// tokens will continue indefinitely.
565 void EnableBacktrackAtThisPos();
567 /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
568 void CommitBacktrackedTokens();
570 /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
571 /// EnableBacktrackAtThisPos() was previously called.
574 /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
575 /// caching of tokens is on.
576 bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
578 /// Lex - To lex a token from the preprocessor, just pull a token from the
579 /// current lexer or macro object.
580 void Lex(Token &Result) {
581 switch (CurLexerKind) {
582 case CLK_Lexer: CurLexer->Lex(Result); break;
583 case CLK_PTHLexer: CurPTHLexer->Lex(Result); break;
584 case CLK_TokenLexer: CurTokenLexer->Lex(Result); break;
585 case CLK_CachingLexer: CachingLex(Result); break;
586 case CLK_LexAfterModuleImport: LexAfterModuleImport(Result); break;
590 void LexAfterModuleImport(Token &Result);
592 /// LexNonComment - Lex a token. If it's a comment, keep lexing until we get
593 /// something not a comment. This is useful in -E -C mode where comments
594 /// would foul up preprocessor directive handling.
595 void LexNonComment(Token &Result) {
598 while (Result.getKind() == tok::comment);
601 /// LexUnexpandedToken - This is just like Lex, but this disables macro
602 /// expansion of identifier tokens.
603 void LexUnexpandedToken(Token &Result) {
604 // Disable macro expansion.
605 bool OldVal = DisableMacroExpansion;
606 DisableMacroExpansion = true;
611 DisableMacroExpansion = OldVal;
614 /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
615 /// expansion of identifier tokens.
616 void LexUnexpandedNonComment(Token &Result) {
618 LexUnexpandedToken(Result);
619 while (Result.getKind() == tok::comment);
622 /// LookAhead - This peeks ahead N tokens and returns that token without
623 /// consuming any tokens. LookAhead(0) returns the next token that would be
624 /// returned by Lex(), LookAhead(1) returns the token after it, etc. This
625 /// returns normal tokens after phase 5. As such, it is equivalent to using
626 /// 'Lex', not 'LexUnexpandedToken'.
627 const Token &LookAhead(unsigned N) {
628 if (CachedLexPos + N < CachedTokens.size())
629 return CachedTokens[CachedLexPos+N];
631 return PeekAhead(N+1);
634 /// RevertCachedTokens - When backtracking is enabled and tokens are cached,
635 /// this allows to revert a specific number of tokens.
636 /// Note that the number of tokens being reverted should be up to the last
637 /// backtrack position, not more.
638 void RevertCachedTokens(unsigned N) {
639 assert(isBacktrackEnabled() &&
640 "Should only be called when tokens are cached for backtracking");
641 assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
642 && "Should revert tokens up to the last backtrack position, not more");
643 assert(signed(CachedLexPos) - signed(N) >= 0 &&
644 "Corrupted backtrack positions ?");
648 /// EnterToken - Enters a token in the token stream to be lexed next. If
649 /// BackTrack() is called afterwards, the token will remain at the insertion
651 void EnterToken(const Token &Tok) {
652 EnterCachingLexMode();
653 CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
656 /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching
657 /// tokens (because backtrack is enabled) it should replace the most recent
658 /// cached tokens with the given annotation token. This function has no effect
659 /// if backtracking is not enabled.
661 /// Note that the use of this function is just for optimization; so that the
662 /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
664 void AnnotateCachedTokens(const Token &Tok) {
665 assert(Tok.isAnnotation() && "Expected annotation token");
666 if (CachedLexPos != 0 && isBacktrackEnabled())
667 AnnotatePreviousCachedTokens(Tok);
670 /// \brief Replace the last token with an annotation token.
672 /// Like AnnotateCachedTokens(), this routine replaces an
673 /// already-parsed (and resolved) token with an annotation
674 /// token. However, this routine only replaces the last token with
675 /// the annotation token; it does not affect any other cached
676 /// tokens. This function has no effect if backtracking is not
678 void ReplaceLastTokenWithAnnotation(const Token &Tok) {
679 assert(Tok.isAnnotation() && "Expected annotation token");
680 if (CachedLexPos != 0 && isBacktrackEnabled())
681 CachedTokens[CachedLexPos-1] = Tok;
684 /// \brief Specify the point at which code-completion will be performed.
686 /// \param File the file in which code completion should occur. If
687 /// this file is included multiple times, code-completion will
688 /// perform completion the first time it is included. If NULL, this
689 /// function clears out the code-completion point.
691 /// \param Line the line at which code completion should occur
694 /// \param Column the column at which code completion should occur
697 /// \returns true if an error occurred, false otherwise.
698 bool SetCodeCompletionPoint(const FileEntry *File,
699 unsigned Line, unsigned Column);
701 /// \brief Determine if we are performing code completion.
702 bool isCodeCompletionEnabled() const { return CodeCompletionFile != 0; }
704 /// \brief Returns the location of the code-completion point.
705 /// Returns an invalid location if code-completion is not enabled or the file
706 /// containing the code-completion point has not been lexed yet.
707 SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
709 /// \brief Returns the start location of the file of code-completion point.
710 /// Returns an invalid location if code-completion is not enabled or the file
711 /// containing the code-completion point has not been lexed yet.
712 SourceLocation getCodeCompletionFileLoc() const {
713 return CodeCompletionFileLoc;
716 /// \brief Returns true if code-completion is enabled and we have hit the
717 /// code-completion point.
718 bool isCodeCompletionReached() const { return CodeCompletionReached; }
720 /// \brief Note that we hit the code-completion point.
721 void setCodeCompletionReached() {
722 assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
723 CodeCompletionReached = true;
724 // Silence any diagnostics that occur after we hit the code-completion.
725 getDiagnostics().setSuppressAllDiagnostics(true);
728 /// \brief The location of the currently-active #pragma clang
729 /// arc_cf_code_audited begin. Returns an invalid location if there
730 /// is no such pragma active.
731 SourceLocation getPragmaARCCFCodeAuditedLoc() const {
732 return PragmaARCCFCodeAuditedLoc;
735 /// \brief Set the location of the currently-active #pragma clang
736 /// arc_cf_code_audited begin. An invalid location ends the pragma.
737 void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
738 PragmaARCCFCodeAuditedLoc = Loc;
741 /// \brief Instruct the preprocessor to skip part of the main
742 /// the main source file.
744 /// \brief Bytes The number of bytes in the preamble to skip.
746 /// \brief StartOfLine Whether skipping these bytes puts the lexer at the
748 void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
749 SkipMainFilePreamble.first = Bytes;
750 SkipMainFilePreamble.second = StartOfLine;
753 /// Diag - Forwarding function for diagnostics. This emits a diagnostic at
754 /// the specified Token's location, translating the token's start
755 /// position in the current buffer into a SourcePosition object for rendering.
756 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
757 return Diags->Report(Loc, DiagID);
760 DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
761 return Diags->Report(Tok.getLocation(), DiagID);
764 /// getSpelling() - Return the 'spelling' of the token at the given
765 /// location; does not go up to the spelling location or down to the
766 /// expansion location.
768 /// \param buffer A buffer which will be used only if the token requires
769 /// "cleaning", e.g. if it contains trigraphs or escaped newlines
770 /// \param invalid If non-null, will be set \c true if an error occurs.
771 StringRef getSpelling(SourceLocation loc,
772 SmallVectorImpl<char> &buffer,
773 bool *invalid = 0) const {
774 return Lexer::getSpelling(loc, buffer, SourceMgr, Features, invalid);
777 /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a
778 /// token is the characters used to represent the token in the source file
779 /// after trigraph expansion and escaped-newline folding. In particular, this
780 /// wants to get the true, uncanonicalized, spelling of things like digraphs
783 /// \param Invalid If non-null, will be set \c true if an error occurs.
784 std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
785 return Lexer::getSpelling(Tok, SourceMgr, Features, Invalid);
788 /// getSpelling - This method is used to get the spelling of a token into a
789 /// preallocated buffer, instead of as an std::string. The caller is required
790 /// to allocate enough space for the token, which is guaranteed to be at least
791 /// Tok.getLength() bytes long. The length of the actual result is returned.
793 /// Note that this method may do two possible things: it may either fill in
794 /// the buffer specified with characters, or it may *change the input pointer*
795 /// to point to a constant buffer with the data already in it (avoiding a
796 /// copy). The caller is not allowed to modify the returned buffer pointer
797 /// if an internal buffer is returned.
798 unsigned getSpelling(const Token &Tok, const char *&Buffer,
799 bool *Invalid = 0) const {
800 return Lexer::getSpelling(Tok, Buffer, SourceMgr, Features, Invalid);
803 /// getSpelling - This method is used to get the spelling of a token into a
804 /// SmallVector. Note that the returned StringRef may not point to the
805 /// supplied buffer if a copy can be avoided.
806 StringRef getSpelling(const Token &Tok,
807 SmallVectorImpl<char> &Buffer,
808 bool *Invalid = 0) const;
810 /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
811 /// with length 1, return the character.
812 char getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
813 bool *Invalid = 0) const {
814 assert(Tok.is(tok::numeric_constant) &&
815 Tok.getLength() == 1 && "Called on unsupported token");
816 assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
818 // If the token is carrying a literal data pointer, just use it.
819 if (const char *D = Tok.getLiteralData())
822 // Otherwise, fall back on getCharacterData, which is slower, but always
824 return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
827 /// CreateString - Plop the specified string into a scratch buffer and set the
828 /// specified token's location and length to it. If specified, the source
829 /// location provides a location of the expansion point of the token.
830 void CreateString(const char *Buf, unsigned Len, Token &Tok,
831 SourceLocation ExpansionLocStart = SourceLocation(),
832 SourceLocation ExpansionLocEnd = SourceLocation());
834 /// \brief Computes the source location just past the end of the
835 /// token at this source location.
837 /// This routine can be used to produce a source location that
838 /// points just past the end of the token referenced by \p Loc, and
839 /// is generally used when a diagnostic needs to point just after a
840 /// token where it expected something different that it received. If
841 /// the returned source location would not be meaningful (e.g., if
842 /// it points into a macro), this routine returns an invalid
845 /// \param Offset an offset from the end of the token, where the source
846 /// location should refer to. The default offset (0) produces a source
847 /// location pointing just past the end of the token; an offset of 1 produces
848 /// a source location pointing to the last character in the token, etc.
849 SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
850 return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, Features);
853 /// \brief Returns true if the given MacroID location points at the first
854 /// token of the macro expansion.
855 bool isAtStartOfMacroExpansion(SourceLocation loc) const {
856 return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, Features);
859 /// \brief Returns true if the given MacroID location points at the last
860 /// token of the macro expansion.
861 bool isAtEndOfMacroExpansion(SourceLocation loc) const {
862 return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, Features);
865 /// DumpToken - Print the token to stderr, used for debugging.
867 void DumpToken(const Token &Tok, bool DumpFlags = false) const;
868 void DumpLocation(SourceLocation Loc) const;
869 void DumpMacro(const MacroInfo &MI) const;
871 /// AdvanceToTokenCharacter - Given a location that specifies the start of a
872 /// token, return a new location that specifies a character within the token.
873 SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
874 unsigned Char) const {
875 return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, Features);
878 /// IncrementPasteCounter - Increment the counters for the number of token
879 /// paste operations performed. If fast was specified, this is a 'fast paste'
882 void IncrementPasteCounter(bool isFast) {
891 size_t getTotalMemory() const;
893 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
894 /// comment (/##/) in microsoft mode, this method handles updating the current
895 /// state, returning the token on the next source line.
896 void HandleMicrosoftCommentPaste(Token &Tok);
898 //===--------------------------------------------------------------------===//
899 // Preprocessor callback methods. These are invoked by a lexer as various
900 // directives and events are found.
902 /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
903 /// identifier information for the token and install it into the token,
904 /// updating the token kind accordingly.
905 IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
908 llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
912 // SetPoisonReason - Call this function to indicate the reason for
913 // poisoning an identifier. If that identifier is accessed while
914 // poisoned, then this reason will be used instead of the default
915 // "poisoned" diagnostic.
916 void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
918 // HandlePoisonedIdentifier - Display reason for poisoned
920 void HandlePoisonedIdentifier(Token & Tok);
922 void MaybeHandlePoisonedIdentifier(Token & Identifier) {
923 if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
924 if(II->isPoisoned()) {
925 HandlePoisonedIdentifier(Identifier);
931 /// Identifiers used for SEH handling in Borland. These are only
932 /// allowed in particular circumstances
933 IdentifierInfo *Ident__exception_code, *Ident___exception_code, *Ident_GetExceptionCode; // __except block
934 IdentifierInfo *Ident__exception_info, *Ident___exception_info, *Ident_GetExceptionInfo; // __except filter expression
935 IdentifierInfo *Ident__abnormal_termination, *Ident___abnormal_termination, *Ident_AbnormalTermination; // __finally
937 void PoisonSEHIdentifiers(bool Poison = true); // Borland
939 /// HandleIdentifier - This callback is invoked when the lexer reads an
940 /// identifier and has filled in the tokens IdentifierInfo member. This
941 /// callback potentially macro expands it or turns it into a named token (like
943 void HandleIdentifier(Token &Identifier);
946 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
947 /// the current file. This either returns the EOF token and returns true, or
948 /// pops a level off the include stack and returns false, at which point the
949 /// client should call lex again.
950 bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
952 /// HandleEndOfTokenLexer - This callback is invoked when the current
953 /// TokenLexer hits the end of its token stream.
954 bool HandleEndOfTokenLexer(Token &Result);
956 /// HandleDirective - This callback is invoked when the lexer sees a # token
957 /// at the start of a line. This consumes the directive, modifies the
958 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
959 /// read is the correct one.
960 void HandleDirective(Token &Result);
962 /// CheckEndOfDirective - Ensure that the next token is a tok::eod token. If
963 /// not, emit a diagnostic and consume up until the eod. If EnableMacros is
964 /// true, then we consider macros that expand to zero tokens as being ok.
965 void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
967 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
968 /// current line until the tok::eod token is found.
969 void DiscardUntilEndOfDirective();
971 /// SawDateOrTime - This returns true if the preprocessor has seen a use of
972 /// __DATE__ or __TIME__ in the file so far.
973 bool SawDateOrTime() const {
974 return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
976 unsigned getCounterValue() const { return CounterValue; }
977 void setCounterValue(unsigned V) { CounterValue = V; }
979 /// AllocateMacroInfo - Allocate a new MacroInfo object with the provide
981 MacroInfo *AllocateMacroInfo(SourceLocation L);
983 /// CloneMacroInfo - Allocate a new MacroInfo object which is clone of MI.
984 MacroInfo *CloneMacroInfo(const MacroInfo &MI);
986 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
987 /// checked and spelled filename, e.g. as an operand of #include. This returns
988 /// true if the input filename was in <>'s or false if it were in ""'s. The
989 /// caller is expected to provide a buffer that is large enough to hold the
990 /// spelling of the filename, but is also expected to handle the case when
991 /// this method decides to use a different buffer.
992 bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
994 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
995 /// return null on failure. isAngled indicates whether the file reference is
996 /// for system #include's or not (i.e. using <> instead of "").
997 const FileEntry *LookupFile(StringRef Filename,
998 bool isAngled, const DirectoryLookup *FromDir,
999 const DirectoryLookup *&CurDir,
1000 SmallVectorImpl<char> *SearchPath,
1001 SmallVectorImpl<char> *RelativePath,
1002 StringRef *SuggestedModule);
1004 /// GetCurLookup - The DirectoryLookup structure used to find the current
1005 /// FileEntry, if CurLexer is non-null and if applicable. This allows us to
1006 /// implement #include_next and find directory-specific properties.
1007 const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1009 /// isInPrimaryFile - Return true if we're in the top-level file, not in a
1011 bool isInPrimaryFile() const;
1013 /// ConcatenateIncludeName - Handle cases where the #include name is expanded
1014 /// from a macro as multiple tokens, which need to be glued together. This
1015 /// occurs for code like:
1016 /// #define FOO <a/b.h>
1018 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1020 /// This code concatenates and consumes tokens up to the '>' token. It
1021 /// returns false if the > was found, otherwise it returns true if it finds
1022 /// and consumes the EOD marker.
1023 bool ConcatenateIncludeName(llvm::SmallString<128> &FilenameBuffer,
1024 SourceLocation &End);
1026 /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is
1027 /// followed by EOD. Return true if the token is not a valid on-off-switch.
1028 bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
1032 void PushIncludeMacroStack() {
1033 IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind,
1037 CurTokenLexer.take(),
1042 void PopIncludeMacroStack() {
1043 CurLexer.reset(IncludeMacroStack.back().TheLexer);
1044 CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer);
1045 CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1046 CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
1047 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
1048 CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1049 IncludeMacroStack.pop_back();
1052 /// AllocateMacroInfo - Allocate a new MacroInfo object.
1053 MacroInfo *AllocateMacroInfo();
1055 /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will
1056 /// be reused for allocating new MacroInfo objects.
1057 void ReleaseMacroInfo(MacroInfo* MI);
1059 /// ReadMacroName - Lex and validate a macro name, which occurs after a
1060 /// #define or #undef. This emits a diagnostic, sets the token kind to eod,
1061 /// and discards the rest of the macro line if the macro name is invalid.
1062 void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
1064 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1065 /// definition has just been read. Lex the rest of the arguments and the
1066 /// closing ), updating MI with what we learn. Return true if an error occurs
1067 /// parsing the arg list.
1068 bool ReadMacroDefinitionArgList(MacroInfo *MI);
1070 /// SkipExcludedConditionalBlock - We just read a #if or related directive and
1071 /// decided that the subsequent tokens are in the #if'd out portion of the
1072 /// file. Lex the rest of the file, until we see an #endif. If
1073 /// FoundNonSkipPortion is true, then we have already emitted code for part of
1074 /// this #if directive, so #else/#elif blocks should never be entered. If
1075 /// FoundElse is false, then #else directives are ok, if not, then we have
1076 /// already seen one so a #else directive is a duplicate. When this returns,
1077 /// the caller can lex the first valid token.
1078 void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1079 bool FoundNonSkipPortion, bool FoundElse,
1080 SourceLocation ElseLoc = SourceLocation());
1082 /// PTHSkipExcludedConditionalBlock - A fast PTH version of
1083 /// SkipExcludedConditionalBlock.
1084 void PTHSkipExcludedConditionalBlock();
1086 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
1087 /// may occur after a #if or #elif directive and return it as a bool. If the
1088 /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
1089 bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
1091 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1092 /// #pragma GCC poison/system_header/dependency and #pragma once.
1093 void RegisterBuiltinPragmas();
1095 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
1096 /// identifier table.
1097 void RegisterBuiltinMacros();
1099 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
1100 /// be expanded as a macro, handle it and return the next token as 'Tok'. If
1101 /// the macro should not be expanded return true, otherwise return false.
1102 bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
1104 /// \brief Cache macro expanded tokens for TokenLexers.
1106 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1107 /// going to lex in the cache and when it finishes the tokens are removed
1108 /// from the end of the cache.
1109 Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
1110 ArrayRef<Token> tokens);
1111 void removeCachedMacroExpandedTokensOfLastLexer();
1112 friend void TokenLexer::ExpandFunctionArguments();
1114 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
1115 /// lexed is a '('. If so, consume the token and return true, if not, this
1116 /// method should have no observable side-effect on the lexed tokens.
1117 bool isNextPPTokenLParen();
1119 /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
1120 /// invoked to read all of the formal arguments specified for the macro
1121 /// invocation. This returns null on error.
1122 MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
1123 SourceLocation &ExpansionEnd);
1125 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1126 /// as a builtin macro, handle it and return the next token as 'Tok'.
1127 void ExpandBuiltinMacro(Token &Tok);
1129 /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
1130 /// return the first token after the directive. The _Pragma token has just
1131 /// been read into 'Tok'.
1132 void Handle_Pragma(Token &Tok);
1134 /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
1135 /// is not enclosed within a string literal.
1136 void HandleMicrosoft__pragma(Token &Tok);
1138 /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
1139 /// start lexing tokens from it instead of the current buffer.
1140 void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
1142 /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and
1143 /// start getting tokens from it using the PTH cache.
1144 void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
1146 /// IsFileLexer - Returns true if we are lexing from a file and not a
1147 /// pragma or a macro.
1148 static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
1149 return L ? !L->isPragmaLexer() : P != 0;
1152 static bool IsFileLexer(const IncludeStackInfo& I) {
1153 return IsFileLexer(I.TheLexer, I.ThePPLexer);
1156 bool IsFileLexer() const {
1157 return IsFileLexer(CurLexer.get(), CurPPLexer);
1160 //===--------------------------------------------------------------------===//
1162 void CachingLex(Token &Result);
1163 bool InCachingLexMode() const {
1164 // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
1165 // that we are past EOF, not that we are in CachingLex mode.
1166 return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 &&
1167 !IncludeMacroStack.empty();
1169 void EnterCachingLexMode();
1170 void ExitCachingLexMode() {
1171 if (InCachingLexMode())
1172 RemoveTopOfLexerStack();
1174 const Token &PeekAhead(unsigned N);
1175 void AnnotatePreviousCachedTokens(const Token &Tok);
1177 //===--------------------------------------------------------------------===//
1178 /// Handle*Directive - implement the various preprocessor directives. These
1179 /// should side-effect the current preprocessor object so that the next call
1180 /// to Lex() will return the appropriate token next.
1181 void HandleLineDirective(Token &Tok);
1182 void HandleDigitDirective(Token &Tok);
1183 void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1184 void HandleIdentSCCSDirective(Token &Tok);
1185 void HandleMacroExportDirective(Token &Tok);
1188 void HandleIncludeDirective(SourceLocation HashLoc,
1190 const DirectoryLookup *LookupFrom = 0,
1191 bool isImport = false);
1192 void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1193 void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1194 void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
1197 void HandleDefineDirective(Token &Tok);
1198 void HandleUndefDirective(Token &Tok);
1200 // Conditional Inclusion.
1201 void HandleIfdefDirective(Token &Tok, bool isIfndef,
1202 bool ReadAnyTokensBeforeDirective);
1203 void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1204 void HandleEndifDirective(Token &Tok);
1205 void HandleElseDirective(Token &Tok);
1206 void HandleElifDirective(Token &Tok);
1209 void HandlePragmaDirective(unsigned Introducer);
1211 void HandlePragmaOnce(Token &OnceTok);
1212 void HandlePragmaMark();
1213 void HandlePragmaPoison(Token &PoisonTok);
1214 void HandlePragmaSystemHeader(Token &SysHeaderTok);
1215 void HandlePragmaDependency(Token &DependencyTok);
1216 void HandlePragmaComment(Token &CommentTok);
1217 void HandlePragmaMessage(Token &MessageTok);
1218 void HandlePragmaPushMacro(Token &Tok);
1219 void HandlePragmaPopMacro(Token &Tok);
1220 IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1222 // Return true and store the first token only if any CommentHandler
1223 // has inserted some tokens and getCommentRetentionState() is false.
1224 bool HandleComment(Token &Token, SourceRange Comment);
1226 /// \brief A macro is used, update information about macros that need unused
1228 void markMacroAsUsed(MacroInfo *MI);
1231 /// \brief Abstract base class that describes a handler that will receive
1232 /// source ranges for each of the comments encountered in the source file.
1233 class CommentHandler {
1235 virtual ~CommentHandler();
1237 // The handler shall return true if it has pushed any tokens
1238 // to be read using e.g. EnterToken or EnterTokenStream.
1239 virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
1242 } // end namespace clang