diff options
author | dim <dim@FreeBSD.org> | 2012-04-14 14:01:31 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2012-04-14 14:01:31 +0000 |
commit | 50b73317314e889cf39c7b1d6cbf419fa7502f22 (patch) | |
tree | be1815eb79b42ff482a8562b13c2dcbf0c5dcbee /include/clang/Lex/Preprocessor.h | |
parent | dc04cb328508e61aad809d9b53b12f9799a00e7d (diff) | |
download | FreeBSD-src-50b73317314e889cf39c7b1d6cbf419fa7502f22.zip FreeBSD-src-50b73317314e889cf39c7b1d6cbf419fa7502f22.tar.gz |
Vendor import of clang trunk r154661:
http://llvm.org/svn/llvm-project/cfe/trunk@r154661
Diffstat (limited to 'include/clang/Lex/Preprocessor.h')
-rw-r--r-- | include/clang/Lex/Preprocessor.h | 210 |
1 files changed, 137 insertions, 73 deletions
diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 8b77433..055008f 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -33,6 +33,10 @@ #include "llvm/Support/Allocator.h" #include <vector> +namespace llvm { + template<unsigned InternalLen> class SmallString; +} + namespace clang { class SourceManager; @@ -50,15 +54,15 @@ class CodeCompletionHandler; class DirectoryLookup; class PreprocessingRecord; class ModuleLoader; - + /// Preprocessor - This object engages in a tight little dance with the lexer to /// efficiently preprocess tokens. Lexers know only about tokens within a /// single source file, and don't know anything about preprocessor-level issues /// like the #include stack, token expansion, etc. /// -class Preprocessor : public llvm::RefCountedBase<Preprocessor> { +class Preprocessor : public RefCountedBase<Preprocessor> { DiagnosticsEngine *Diags; - LangOptions &Features; + LangOptions &LangOpts; const TargetInfo *Target; FileManager &FileMgr; SourceManager &SourceMgr; @@ -69,10 +73,10 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// \brief External source of macros. ExternalPreprocessorSource *ExternalSource; - + /// PTH - An optional PTHManager object used for getting tokens from /// a token cache rather than lexing the original source file. - llvm::OwningPtr<PTHManager> PTH; + OwningPtr<PTHManager> PTH; /// BP - A BumpPtrAllocator object used to quickly allocate and release /// objects internal to the Preprocessor. @@ -107,8 +111,7 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { bool KeepComments : 1; bool KeepMacroComments : 1; bool SuppressIncludeNotFoundError : 1; - bool AutoModuleImport : 1; - + // State that changes while the preprocessor runs: bool InMacroArgs : 1; // True if parsing fn macro invocation args. @@ -121,6 +124,9 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// \brief Whether we have already loaded macros from the external source. mutable bool ReadMacrosFromExternalSource : 1; + /// \brief True if we are pre-expanding macro arguments. + bool InMacroArgPreExpansion; + /// Identifiers - This is mapping/lookup information for all identifiers in /// the program, including program keywords. mutable IdentifierTable Identifiers; @@ -145,9 +151,13 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// with this preprocessor. std::vector<CommentHandler *> CommentHandlers; + /// \brief True if we want to ignore EOF token and continue later on (thus + /// avoid tearing the Lexer and etc. down). + bool IncrementalProcessing; + /// \brief The code-completion handler. CodeCompletionHandler *CodeComplete; - + /// \brief The file that we're performing code-completion for, if any. const FileEntry *CodeCompletionFile; @@ -163,14 +173,22 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// for preprocessing. SourceLocation CodeCompletionFileLoc; - /// \brief The source location of the __import_module__ keyword we just + /// \brief The source location of the 'import' contextual keyword we just /// lexed, if any. SourceLocation ModuleImportLoc; + /// \brief The module import path that we're currently processing. + llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> + ModuleImportPath; + + /// \brief Whether the module import expectes an identifier next. Otherwise, + /// it expects a '.' or ';'. + bool ModuleImportExpectsIdentifier; + /// \brief The source location of the currently-active /// #pragma clang arc_cf_code_audited begin. SourceLocation PragmaARCCFCodeAuditedLoc; - + /// \brief True if we hit the code-completion point. bool CodeCompletionReached; @@ -179,22 +197,22 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// with a flag that indicates whether skipping this number of bytes will /// place the lexer at the start of a line. std::pair<unsigned, bool> SkipMainFilePreamble; - + /// CurLexer - This is the current top of the stack that we're lexing from if /// not expanding a macro and we are lexing directly from source code. /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null. - llvm::OwningPtr<Lexer> CurLexer; + OwningPtr<Lexer> CurLexer; /// CurPTHLexer - This is the current top of stack that we're lexing from if /// not expanding from a macro and we are lexing from a PTH cache. /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null. - llvm::OwningPtr<PTHLexer> CurPTHLexer; + OwningPtr<PTHLexer> CurPTHLexer; /// CurPPLexer - This is the current top of the stack what we're lexing from /// if not expanding a macro. This is an alias for either CurLexer or /// CurPTHLexer. PreprocessorLexer *CurPPLexer; - + /// CurLookup - The DirectoryLookup structure used to find the current /// FileEntry, if CurLexer is non-null and if applicable. This allows us to /// implement #include_next and find directory-specific properties. @@ -202,13 +220,13 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// CurTokenLexer - This is the current macro we are expanding, if we are /// expanding a macro. One of CurLexer and CurTokenLexer must be null. - llvm::OwningPtr<TokenLexer> CurTokenLexer; + OwningPtr<TokenLexer> CurTokenLexer; /// \brief The kind of lexer we're currently working with. - enum CurLexerKind { - CLK_Lexer, - CLK_PTHLexer, - CLK_TokenLexer, + enum CurLexerKind { + CLK_Lexer, + CLK_PTHLexer, + CLK_TokenLexer, CLK_CachingLexer, CLK_LexAfterModuleImport } CurLexerKind; @@ -224,10 +242,10 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { TokenLexer *TheTokenLexer; const DirectoryLookup *TheDirLookup; - IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P, + IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P, PreprocessorLexer* PPL, TokenLexer* TL, const DirectoryLookup *D) - : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL), + : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL), TheTokenLexer(TL), TheDirLookup(D) {} }; std::vector<IncludeStackInfo> IncludeMacroStack; @@ -254,9 +272,9 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { /// reused for quick allocation. MacroArgs *MacroArgCache; friend class MacroArgs; - - /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma - /// push_macro directive, we keep a MacroInfo stack used to restore + + /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma + /// push_macro directive, we keep a MacroInfo stack used to restore /// previous macro value. llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo; @@ -286,12 +304,12 @@ class Preprocessor : public llvm::RefCountedBase<Preprocessor> { std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack; /// \brief A record of the macro definitions and expansions that - /// occurred during preprocessing. + /// occurred during preprocessing. /// /// This is an optional side structure that can be enabled with /// \c createPreprocessingRecord() prior to preprocessing. PreprocessingRecord *Record; - + private: // Cached tokens state. typedef SmallVector<Token, 1> CachedTokensTy; @@ -325,7 +343,7 @@ private: // Cached tokens state. MacroInfoChain *MICache; MacroInfo *getInfoForMacro(IdentifierInfo *II) const; - + public: Preprocessor(DiagnosticsEngine &diags, LangOptions &opts, const TargetInfo *target, @@ -333,7 +351,8 @@ public: ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup = 0, bool OwnsHeaderSearch = false, - bool DelayInitialization = false); + bool DelayInitialization = false, + bool IncrProcessing = false); ~Preprocessor(); @@ -342,11 +361,11 @@ public: /// /// \param Target Information about the target. void Initialize(const TargetInfo &Target); - + DiagnosticsEngine &getDiagnostics() const { return *Diags; } void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; } - const LangOptions &getLangOptions() const { return Features; } + const LangOptions &getLangOpts() const { return LangOpts; } const TargetInfo &getTargetInfo() const { return *Target; } FileManager &getFileManager() const { return FileMgr; } SourceManager &getSourceManager() const { return SourceMgr; } @@ -371,7 +390,7 @@ public: /// \brief Retrieve the module loader associated with this preprocessor. ModuleLoader &getModuleLoader() const { return TheModuleLoader; } - + /// SetCommentRetentionState - Control whether or not the preprocessor retains /// comments in output. void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) { @@ -389,11 +408,6 @@ public: return SuppressIncludeNotFoundError; } - /// \brief Specify whether automatic module imports are enabled. - void setAutoModuleImport(bool AutoModuleImport = true) { - this->AutoModuleImport = AutoModuleImport; - } - /// isCurrentLexer - Return true if we are lexing directly from the specified /// lexer. bool isCurrentLexer(const PreprocessorLexer *L) const { @@ -425,13 +439,14 @@ public: MacroInfo *getMacroInfo(IdentifierInfo *II) const { if (!II->hasMacroDefinition()) return 0; - + return getInfoForMacro(II); } /// setMacroInfo - Specify a macro for this identifier. /// - void setMacroInfo(IdentifierInfo *II, MacroInfo *MI); + void setMacroInfo(IdentifierInfo *II, MacroInfo *MI, + bool LoadedFromAST = false); /// macro_iterator/macro_begin/macro_end - This allows you to walk the current /// state of the macro table. This visits every currently-defined macro. @@ -484,29 +499,29 @@ public: void setCodeCompletionHandler(CodeCompletionHandler &Handler) { CodeComplete = &Handler; } - + /// \brief Retrieve the current code-completion handler. CodeCompletionHandler *getCodeCompletionHandler() const { return CodeComplete; } - + /// \brief Clear out the code completion handler. void clearCodeCompletionHandler() { CodeComplete = 0; } - + /// \brief Hook used by the lexer to invoke the "natural language" code /// completion point. void CodeCompleteNaturalLanguage(); - + /// \brief Retrieve the preprocessing record, or NULL if there is no /// preprocessing record. PreprocessingRecord *getPreprocessingRecord() const { return Record; } - - /// \brief Create a new preprocessing record, which will keep track of + + /// \brief Create a new preprocessing record, which will keep track of /// all macro expansions, macro definitions, etc. - void createPreprocessingRecord(bool IncludeNestedMacroExpansions); - + void createPreprocessingRecord(bool RecordConditionalDirectives); + /// EnterMainSourceFile - Enter the specified FileID as the main source file, /// which implicitly adds the builtin defines etc. void EnterMainSourceFile(); @@ -588,7 +603,7 @@ public: } void LexAfterModuleImport(Token &Result); - + /// LexNonComment - Lex a token. If it's a comment, keep lexing until we get /// something not a comment. This is useful in -E -C mode where comments /// would foul up preprocessor directive handling. @@ -681,6 +696,18 @@ public: CachedTokens[CachedLexPos-1] = Tok; } + /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/ + /// CurTokenLexer pointers. + void recomputeCurLexerKind(); + + /// \brief Returns true if incremental processing is enabled + bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; } + + /// \brief Enables the incremental processing + void enableIncrementalProcessing(bool value = true) { + IncrementalProcessing = value; + } + /// \brief Specify the point at which code-completion will be performed. /// /// \param File the file in which code completion should occur. If @@ -745,11 +772,11 @@ public: /// /// \brief StartOfLine Whether skipping these bytes puts the lexer at the /// start of a line. - void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) { + void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) { SkipMainFilePreamble.first = Bytes; SkipMainFilePreamble.second = StartOfLine; } - + /// Diag - Forwarding function for diagnostics. This emits a diagnostic at /// the specified Token's location, translating the token's start /// position in the current buffer into a SourcePosition object for rendering. @@ -771,7 +798,7 @@ public: StringRef getSpelling(SourceLocation loc, SmallVectorImpl<char> &buffer, bool *invalid = 0) const { - return Lexer::getSpelling(loc, buffer, SourceMgr, Features, invalid); + return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid); } /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a @@ -782,7 +809,7 @@ public: /// /// \param Invalid If non-null, will be set \c true if an error occurs. std::string getSpelling(const Token &Tok, bool *Invalid = 0) const { - return Lexer::getSpelling(Tok, SourceMgr, Features, Invalid); + return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid); } /// getSpelling - This method is used to get the spelling of a token into a @@ -795,21 +822,21 @@ public: /// to point to a constant buffer with the data already in it (avoiding a /// copy). The caller is not allowed to modify the returned buffer pointer /// if an internal buffer is returned. - unsigned getSpelling(const Token &Tok, const char *&Buffer, + unsigned getSpelling(const Token &Tok, const char *&Buffer, bool *Invalid = 0) const { - return Lexer::getSpelling(Tok, Buffer, SourceMgr, Features, Invalid); + return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid); } /// getSpelling - This method is used to get the spelling of a token into a /// SmallVector. Note that the returned StringRef may not point to the /// supplied buffer if a copy can be avoided. StringRef getSpelling(const Token &Tok, - SmallVectorImpl<char> &Buffer, - bool *Invalid = 0) const; + SmallVectorImpl<char> &Buffer, + bool *Invalid = 0) const; /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant /// with length 1, return the character. - char getSpellingOfSingleCharacterNumericConstant(const Token &Tok, + char getSpellingOfSingleCharacterNumericConstant(const Token &Tok, bool *Invalid = 0) const { assert(Tok.is(tok::numeric_constant) && Tok.getLength() == 1 && "Called on unsupported token"); @@ -824,6 +851,17 @@ public: return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid); } + /// \brief Retrieve the name of the immediate macro expansion. + /// + /// This routine starts from a source location, and finds the name of the macro + /// responsible for its immediate expansion. It looks through any intervening + /// macro argument expansions to compute this. It returns a StringRef which + /// refers to the SourceManager-owned buffer of the source where that macro + /// name is spelled. Thus, the result shouldn't out-live the SourceManager. + StringRef getImmediateMacroName(SourceLocation Loc) { + return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts()); + } + /// CreateString - Plop the specified string into a scratch buffer and set the /// specified token's location and length to it. If specified, the source /// location provides a location of the expansion point of the token. @@ -847,19 +885,28 @@ public: /// location pointing just past the end of the token; an offset of 1 produces /// a source location pointing to the last character in the token, etc. SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) { - return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, Features); + return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); } /// \brief Returns true if the given MacroID location points at the first /// token of the macro expansion. - bool isAtStartOfMacroExpansion(SourceLocation loc) const { - return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, Features); + /// + /// \param MacroBegin If non-null and function returns true, it is set to + /// begin location of the macro. + bool isAtStartOfMacroExpansion(SourceLocation loc, + SourceLocation *MacroBegin = 0) const { + return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts, + MacroBegin); } /// \brief Returns true if the given MacroID location points at the last /// token of the macro expansion. - bool isAtEndOfMacroExpansion(SourceLocation loc) const { - return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, Features); + /// + /// \param MacroBegin If non-null and function returns true, it is set to + /// end location of the macro. + bool isAtEndOfMacroExpansion(SourceLocation loc, + SourceLocation *MacroEnd = 0) const { + return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd); } /// DumpToken - Print the token to stderr, used for debugging. @@ -872,7 +919,7 @@ public: /// token, return a new location that specifies a character within the token. SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const { - return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, Features); + return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts); } /// IncrementPasteCounter - Increment the counters for the number of token @@ -930,9 +977,18 @@ public: private: /// Identifiers used for SEH handling in Borland. These are only /// allowed in particular circumstances - IdentifierInfo *Ident__exception_code, *Ident___exception_code, *Ident_GetExceptionCode; // __except block - IdentifierInfo *Ident__exception_info, *Ident___exception_info, *Ident_GetExceptionInfo; // __except filter expression - IdentifierInfo *Ident__abnormal_termination, *Ident___abnormal_termination, *Ident_AbnormalTermination; // __finally + // __except block + IdentifierInfo *Ident__exception_code, + *Ident___exception_code, + *Ident_GetExceptionCode; + // __except filter expression + IdentifierInfo *Ident__exception_info, + *Ident___exception_info, + *Ident_GetExceptionInfo; + // __finally + IdentifierInfo *Ident__abnormal_termination, + *Ident___abnormal_termination, + *Ident_AbnormalTermination; public: void PoisonSEHIdentifiers(bool Poison = true); // Borland @@ -976,6 +1032,9 @@ public: unsigned getCounterValue() const { return CounterValue; } void setCounterValue(unsigned V) { CounterValue = V; } + /// \brief Retrieves the module that we're currently building, if any. + Module *getCurrentModule(); + /// AllocateMacroInfo - Allocate a new MacroInfo object with the provide /// SourceLocation. MacroInfo *AllocateMacroInfo(SourceLocation L); @@ -999,7 +1058,8 @@ public: const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, - StringRef *SuggestedModule); + Module **SuggestedModule, + bool SkipCache = false); /// GetCurLookup - The DirectoryLookup structure used to find the current /// FileEntry, if CurLexer is non-null and if applicable. This allows us to @@ -1020,7 +1080,7 @@ public: /// This code concatenates and consumes tokens up to the '>' token. It /// returns false if the > was found, otherwise it returns true if it finds /// and consumes the EOD marker. - bool ConcatenateIncludeName(llvm::SmallString<128> &FilenameBuffer, + bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer, SourceLocation &End); /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is @@ -1063,9 +1123,10 @@ private: /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro /// definition has just been read. Lex the rest of the arguments and the - /// closing ), updating MI with what we learn. Return true if an error occurs - /// parsing the arg list. - bool ReadMacroDefinitionArgList(MacroInfo *MI); + /// closing ), updating MI with what we learn and saving in LastTok the + /// last token read. + /// Return true if an error occurs parsing the arg list. + bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok); /// SkipExcludedConditionalBlock - We just read a #if or related directive and /// decided that the subsequent tokens are in the #if'd out portion of the @@ -1163,7 +1224,7 @@ private: bool InCachingLexMode() const { // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means // that we are past EOF, not that we are in CachingLex mode. - return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 && + return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 && !IncludeMacroStack.empty(); } void EnterCachingLexMode(); @@ -1182,8 +1243,9 @@ private: void HandleDigitDirective(Token &Tok); void HandleUserDiagnosticDirective(Token &Tok, bool isWarning); void HandleIdentSCCSDirective(Token &Tok); - void HandleMacroExportDirective(Token &Tok); - + void HandleMacroPublicDirective(Token &Tok); + void HandleMacroPrivateDirective(Token &Tok); + // File inclusion. void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok, @@ -1192,6 +1254,7 @@ private: void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok); void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok); void HandleImportDirective(SourceLocation HashLoc, Token &Tok); + void HandleMicrosoftImportDirective(Token &Tok); // Macro handling. void HandleDefineDirective(Token &Tok); @@ -1217,6 +1280,7 @@ public: void HandlePragmaMessage(Token &MessageTok); void HandlePragmaPushMacro(Token &Tok); void HandlePragmaPopMacro(Token &Tok); + void HandlePragmaIncludeAlias(Token &Tok); IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok); // Return true and store the first token only if any CommentHandler |