Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
Chris Lattner | f73903a | 2009-02-06 06:45:26 +0000 | [diff] [blame] | 16 | // -d[DNI] - Dump various things. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | // -fworking-directory - #line's with preprocessor's working dir. |
| 18 | // -fpreprocessed |
| 19 | // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD |
| 20 | // -W* |
| 21 | // -w |
| 22 | // |
| 23 | // Messages to emit: |
| 24 | // "Multiple include guards may be useful for:\n" |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 29 | #include "MacroArgs.h" |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 30 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" |
| 32 | #include "clang/Lex/MacroInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Pragma.h" |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 34 | #include "clang/Lex/PreprocessingRecord.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 36 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 37 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 38 | #include "clang/Lex/ModuleLoader.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 40 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/APFloat.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Capacity.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | using namespace clang; |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 50 | ExternalPreprocessorSource::~ExternalPreprocessorSource() { } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 52 | Preprocessor::Preprocessor(DiagnosticsEngine &diags, LangOptions &opts, |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 53 | const TargetInfo *target, SourceManager &SM, |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 54 | HeaderSearch &Headers, ModuleLoader &TheModuleLoader, |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 55 | IdentifierInfoLookup* IILookup, |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 56 | bool OwnsHeaders, |
Axel Naumann | e55329d | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 57 | bool DelayInitialization, |
| 58 | bool IncrProcessing) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 59 | : Diags(&diags), LangOpts(opts), Target(target),FileMgr(Headers.getFileMgr()), |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 60 | SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader), |
Axel Naumann | e55329d | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 61 | ExternalSource(0), Identifiers(opts, IILookup), |
| 62 | IncrementalProcessing(IncrProcessing), CodeComplete(0), |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 63 | CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0), |
| 64 | SkipMainFilePreamble(0, true), CurPPLexer(0), |
Zhongxing Xu | 7e7b503 | 2011-09-26 03:37:43 +0000 | [diff] [blame] | 65 | CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), MacroArgCache(0), |
| 66 | Record(0), MIChainHead(0), MICache(0) |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 67 | { |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 68 | OwnsHeaderSearch = OwnsHeaders; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 69 | |
| 70 | ScratchBuf = new ScratchBuffer(SourceMgr); |
| 71 | CounterValue = 0; // __COUNTER__ starts at 0. |
| 72 | |
| 73 | // Clear stats. |
| 74 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 75 | NumIf = NumElse = NumEndif = 0; |
| 76 | NumEnteredSourceFiles = 0; |
| 77 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 78 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 79 | MaxIncludeStackDepth = 0; |
| 80 | NumSkipped = 0; |
| 81 | |
| 82 | // Default to discarding comments. |
| 83 | KeepComments = false; |
| 84 | KeepMacroComments = false; |
| 85 | SuppressIncludeNotFoundError = false; |
| 86 | |
| 87 | // Macro expansion is enabled. |
| 88 | DisableMacroExpansion = false; |
David Blaikie | 8c0b378 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 89 | MacroExpansionInDirectivesOverride = false; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 90 | InMacroArgs = false; |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 91 | InMacroArgPreExpansion = false; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 92 | NumCachedTokenLexers = 0; |
Jordan Rose | 6fe6a49 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 93 | PragmasEnabled = true; |
| 94 | |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 95 | CachedLexPos = 0; |
| 96 | |
| 97 | // We haven't read anything from the external source. |
| 98 | ReadMacrosFromExternalSource = false; |
| 99 | |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 100 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 101 | // This gets unpoisoned where it is allowed. |
| 102 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 103 | SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use); |
| 104 | |
| 105 | // Initialize the pragma handlers. |
| 106 | PragmaHandlers = new PragmaNamespace(StringRef()); |
| 107 | RegisterBuiltinPragmas(); |
| 108 | |
| 109 | // Initialize builtin macros like __LINE__ and friends. |
| 110 | RegisterBuiltinMacros(); |
| 111 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 112 | if(LangOpts.Borland) { |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 113 | Ident__exception_info = getIdentifierInfo("_exception_info"); |
| 114 | Ident___exception_info = getIdentifierInfo("__exception_info"); |
| 115 | Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation"); |
| 116 | Ident__exception_code = getIdentifierInfo("_exception_code"); |
| 117 | Ident___exception_code = getIdentifierInfo("__exception_code"); |
| 118 | Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode"); |
| 119 | Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination"); |
| 120 | Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination"); |
| 121 | Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination"); |
| 122 | } else { |
| 123 | Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0; |
| 124 | Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0; |
| 125 | Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0; |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 126 | } |
Argyrios Kyrtzidis | 25c2596 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 127 | |
| 128 | if (!DelayInitialization) { |
| 129 | assert(Target && "Must provide target information for PP initialization"); |
| 130 | Initialize(*Target); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Preprocessor::~Preprocessor() { |
| 135 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 136 | |
| 137 | while (!IncludeMacroStack.empty()) { |
| 138 | delete IncludeMacroStack.back().TheLexer; |
| 139 | delete IncludeMacroStack.back().TheTokenLexer; |
| 140 | IncludeMacroStack.pop_back(); |
| 141 | } |
| 142 | |
| 143 | // Free any macro definitions. |
| 144 | for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) |
| 145 | I->MI.Destroy(); |
| 146 | |
| 147 | // Free any cached macro expanders. |
| 148 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 149 | delete TokenLexerCache[i]; |
| 150 | |
| 151 | // Free any cached MacroArgs. |
| 152 | for (MacroArgs *ArgList = MacroArgCache; ArgList; ) |
| 153 | ArgList = ArgList->deallocate(); |
| 154 | |
| 155 | // Release pragma information. |
| 156 | delete PragmaHandlers; |
| 157 | |
| 158 | // Delete the scratch buffer info. |
| 159 | delete ScratchBuf; |
| 160 | |
| 161 | // Delete the header search info, if we own it. |
| 162 | if (OwnsHeaderSearch) |
| 163 | delete &HeaderInfo; |
| 164 | |
| 165 | delete Callbacks; |
| 166 | } |
| 167 | |
| 168 | void Preprocessor::Initialize(const TargetInfo &Target) { |
| 169 | assert((!this->Target || this->Target == &Target) && |
| 170 | "Invalid override of target information"); |
| 171 | this->Target = &Target; |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 172 | |
Argyrios Kyrtzidis | 25c2596 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 173 | // Initialize information about built-ins. |
| 174 | BuiltinInfo.InitializeTarget(Target); |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 175 | HeaderInfo.setTarget(Target); |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 178 | void Preprocessor::setPTHManager(PTHManager* pm) { |
| 179 | PTH.reset(pm); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 180 | FileMgr.addStatCache(PTH->createStatCache()); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 183 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 184 | llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" |
| 185 | << getSpelling(Tok) << "'"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | if (!DumpFlags) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 189 | llvm::errs() << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | if (Tok.isAtStartOfLine()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 191 | llvm::errs() << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | if (Tok.hasLeadingSpace()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 193 | llvm::errs() << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | if (Tok.isExpandDisabled()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 195 | llvm::errs() << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | if (Tok.needsCleaning()) { |
| 197 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 198 | llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 199 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 202 | llvm::errs() << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 203 | DumpLocation(Tok.getLocation()); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 204 | llvm::errs() << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 208 | Loc.dump(SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 212 | llvm::errs() << "MACRO: "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 214 | DumpToken(MI.getReplacementToken(i)); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 215 | llvm::errs() << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | } |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 217 | llvm::errs() << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void Preprocessor::PrintStats() { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 221 | llvm::errs() << "\n*** Preprocessor Stats:\n"; |
| 222 | llvm::errs() << NumDirectives << " directives found:\n"; |
| 223 | llvm::errs() << " " << NumDefined << " #define.\n"; |
| 224 | llvm::errs() << " " << NumUndefined << " #undef.\n"; |
| 225 | llvm::errs() << " #include/#include_next/#import:\n"; |
| 226 | llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 227 | llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 228 | llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 229 | llvm::errs() << " " << NumElse << " #else/#elif.\n"; |
| 230 | llvm::errs() << " " << NumEndif << " #endif.\n"; |
| 231 | llvm::errs() << " " << NumPragma << " #pragma.\n"; |
| 232 | llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 234 | llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 235 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 236 | << NumFastMacroExpanded << " on the fast path.\n"; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 237 | llvm::errs() << (NumFastTokenPaste+NumTokenPaste) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 238 | << " token paste (##) operations performed, " |
| 239 | << NumFastTokenPaste << " on the fast path.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 242 | Preprocessor::macro_iterator |
| 243 | Preprocessor::macro_begin(bool IncludeExternalMacros) const { |
| 244 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 245 | !ReadMacrosFromExternalSource) { |
| 246 | ReadMacrosFromExternalSource = true; |
| 247 | ExternalSource->ReadDefinedMacros(); |
| 248 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 249 | |
| 250 | return Macros.begin(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 253 | size_t Preprocessor::getTotalMemory() const { |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 254 | return BP.getTotalMemory() |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 255 | + llvm::capacity_in_bytes(MacroExpandedTokens) |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 256 | + Predefines.capacity() /* Predefines buffer. */ |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 257 | + llvm::capacity_in_bytes(Macros) |
| 258 | + llvm::capacity_in_bytes(PragmaPushMacroInfo) |
| 259 | + llvm::capacity_in_bytes(PoisonReasons) |
| 260 | + llvm::capacity_in_bytes(CommentHandlers); |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 263 | Preprocessor::macro_iterator |
| 264 | Preprocessor::macro_end(bool IncludeExternalMacros) const { |
| 265 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 266 | !ReadMacrosFromExternalSource) { |
| 267 | ReadMacrosFromExternalSource = true; |
| 268 | ExternalSource->ReadDefinedMacros(); |
| 269 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 270 | |
| 271 | return Macros.end(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Douglas Gregor | d6aba06 | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 274 | void Preprocessor::recomputeCurLexerKind() { |
| 275 | if (CurLexer) |
| 276 | CurLexerKind = CLK_Lexer; |
| 277 | else if (CurPTHLexer) |
| 278 | CurLexerKind = CLK_PTHLexer; |
| 279 | else if (CurTokenLexer) |
| 280 | CurLexerKind = CLK_TokenLexer; |
| 281 | else |
| 282 | CurLexerKind = CLK_CachingLexer; |
| 283 | } |
| 284 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 285 | bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 286 | unsigned CompleteLine, |
| 287 | unsigned CompleteColumn) { |
| 288 | assert(File); |
| 289 | assert(CompleteLine && CompleteColumn && "Starts from 1:1"); |
| 290 | assert(!CodeCompletionFile && "Already set"); |
| 291 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 292 | using llvm::MemoryBuffer; |
| 293 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 294 | // Load the actual file's contents. |
Douglas Gregor | aa38c3d | 2010-03-16 19:49:24 +0000 | [diff] [blame] | 295 | bool Invalid = false; |
| 296 | const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); |
| 297 | if (Invalid) |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 298 | return true; |
| 299 | |
| 300 | // Find the byte position of the truncation point. |
| 301 | const char *Position = Buffer->getBufferStart(); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 302 | for (unsigned Line = 1; Line < CompleteLine; ++Line) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 303 | for (; *Position; ++Position) { |
| 304 | if (*Position != '\r' && *Position != '\n') |
| 305 | continue; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 307 | // Eat \r\n or \n\r as a single line. |
| 308 | if ((Position[1] == '\r' || Position[1] == '\n') && |
| 309 | Position[0] != Position[1]) |
| 310 | ++Position; |
| 311 | ++Position; |
| 312 | break; |
| 313 | } |
| 314 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 315 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 316 | Position += CompleteColumn - 1; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 317 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 318 | // Insert '\0' at the code-completion point. |
Douglas Gregor | b760fe8 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 319 | if (Position < Buffer->getBufferEnd()) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 320 | CodeCompletionFile = File; |
| 321 | CodeCompletionOffset = Position - Buffer->getBufferStart(); |
| 322 | |
| 323 | MemoryBuffer *NewBuffer = |
| 324 | MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1, |
| 325 | Buffer->getBufferIdentifier()); |
Benjamin Kramer | 41a50a9 | 2011-09-04 20:26:28 +0000 | [diff] [blame] | 326 | char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 327 | char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf); |
| 328 | *NewPos = '\0'; |
| 329 | std::copy(Position, Buffer->getBufferEnd(), NewPos+1); |
| 330 | SourceMgr.overrideFileContents(File, NewBuffer); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 336 | void Preprocessor::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 337 | if (CodeComplete) |
| 338 | CodeComplete->CodeCompleteNaturalLanguage(); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 339 | setCodeCompletionReached(); |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 342 | /// getSpelling - This method is used to get the spelling of a token into a |
| 343 | /// SmallVector. Note that the returned StringRef may not point to the |
| 344 | /// supplied buffer if a copy can be avoided. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 345 | StringRef Preprocessor::getSpelling(const Token &Tok, |
| 346 | SmallVectorImpl<char> &Buffer, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 347 | bool *Invalid) const { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 348 | // NOTE: this has to be checked *before* testing for an IdentifierInfo. |
| 349 | if (Tok.isNot(tok::raw_identifier)) { |
| 350 | // Try the fast path. |
| 351 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) |
| 352 | return II->getName(); |
| 353 | } |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 354 | |
| 355 | // Resize the buffer if we need to copy into it. |
| 356 | if (Tok.needsCleaning()) |
| 357 | Buffer.resize(Tok.getLength()); |
| 358 | |
| 359 | const char *Ptr = Buffer.data(); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 360 | unsigned Len = getSpelling(Tok, Ptr, Invalid); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 361 | return StringRef(Ptr, Len); |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 365 | /// location for it. If specified, the source location provides a source |
| 366 | /// location for the token. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 367 | void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok, |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 368 | SourceLocation ExpansionLocStart, |
| 369 | SourceLocation ExpansionLocEnd) { |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 370 | Tok.setLength(Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 372 | const char *DestPtr; |
| 373 | SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 375 | if (ExpansionLocStart.isValid()) |
| 376 | Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart, |
| 377 | ExpansionLocEnd, Len); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 378 | Tok.setLocation(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 380 | // If this is a raw identifier or a literal token, set the pointer data. |
| 381 | if (Tok.is(tok::raw_identifier)) |
| 382 | Tok.setRawIdentifierData(DestPtr); |
| 383 | else if (Tok.isLiteral()) |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 384 | Tok.setLiteralData(DestPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 387 | Module *Preprocessor::getCurrentModule() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 388 | if (getLangOpts().CurrentModule.empty()) |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 389 | return 0; |
| 390 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 391 | return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 394 | //===----------------------------------------------------------------------===// |
| 395 | // Preprocessor Initialization Methods |
| 396 | //===----------------------------------------------------------------------===// |
| 397 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 398 | |
| 399 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 400 | /// which implicitly adds the builtin defines etc. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 401 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 05db427 | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 402 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 403 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 404 | // information) and predefined macros aren't guaranteed to be set properly. |
| 405 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 406 | FileID MainFileID = SourceMgr.getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Argyrios Kyrtzidis | b8c879a | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 408 | // If MainFileID is loaded it means we loaded an AST file, no need to enter |
| 409 | // a main file. |
| 410 | if (!SourceMgr.isLoadedFileID(MainFileID)) { |
| 411 | // Enter the main file source buffer. |
| 412 | EnterSourceFile(MainFileID, 0, SourceLocation()); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 413 | |
Argyrios Kyrtzidis | b8c879a | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 414 | // If we've been asked to skip bytes in the main file (e.g., as part of a |
| 415 | // precompiled preamble), do so now. |
| 416 | if (SkipMainFilePreamble.first > 0) |
| 417 | CurLexer->SkipBytes(SkipMainFilePreamble.first, |
| 418 | SkipMainFilePreamble.second); |
| 419 | |
| 420 | // Tell the header info that the main file was entered. If the file is later |
| 421 | // #imported, it won't be re-entered. |
| 422 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
| 423 | HeaderInfo.IncrementIncludeCount(FE); |
| 424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Benjamin Kramer | ffd6e39 | 2009-12-31 15:33:09 +0000 | [diff] [blame] | 426 | // Preprocess Predefines to populate the initial preprocessor state. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | llvm::MemoryBuffer *SB = |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 428 | llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); |
Douglas Gregor | 043266b | 2010-08-26 14:07:34 +0000 | [diff] [blame] | 429 | assert(SB && "Cannot create predefined source buffer"); |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame] | 430 | FileID FID = SourceMgr.createPredefinesFileIDForMemBuffer(SB); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 431 | assert(!FID.isInvalid() && "Could not create FileID for predefines?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 433 | // Start parsing the predefines. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 434 | EnterSourceFile(FID, 0, SourceLocation()); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 435 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 436 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 437 | void Preprocessor::EndSourceFile() { |
| 438 | // Notify the client that we reached the end of the source file. |
| 439 | if (Callbacks) |
| 440 | Callbacks->EndOfMainFile(); |
| 441 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 442 | |
| 443 | //===----------------------------------------------------------------------===// |
| 444 | // Lexer Event Handling. |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 447 | /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the |
| 448 | /// identifier information for the token and install it into the token, |
| 449 | /// updating the token kind accordingly. |
| 450 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const { |
| 451 | assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 454 | IdentifierInfo *II; |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 455 | if (!Identifier.needsCleaning()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | // No cleaning needed, just use the characters from the lexed buffer. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 457 | II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(), |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 458 | Identifier.getLength())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | } else { |
| 460 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 461 | SmallString<64> IdentifierBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 462 | StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 463 | II = getIdentifierInfo(CleanedStr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | } |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 465 | |
| 466 | // Update the token info (identifier info and appropriate token kind). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | Identifier.setIdentifierInfo(II); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 468 | Identifier.setKind(II->getTokenID()); |
| 469 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 470 | return II; |
| 471 | } |
| 472 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 473 | void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) { |
| 474 | PoisonReasons[II] = DiagID; |
| 475 | } |
| 476 | |
| 477 | void Preprocessor::PoisonSEHIdentifiers(bool Poison) { |
| 478 | assert(Ident__exception_code && Ident__exception_info); |
| 479 | assert(Ident___exception_code && Ident___exception_info); |
| 480 | Ident__exception_code->setIsPoisoned(Poison); |
| 481 | Ident___exception_code->setIsPoisoned(Poison); |
| 482 | Ident_GetExceptionCode->setIsPoisoned(Poison); |
| 483 | Ident__exception_info->setIsPoisoned(Poison); |
| 484 | Ident___exception_info->setIsPoisoned(Poison); |
| 485 | Ident_GetExceptionInfo->setIsPoisoned(Poison); |
| 486 | Ident__abnormal_termination->setIsPoisoned(Poison); |
| 487 | Ident___abnormal_termination->setIsPoisoned(Poison); |
| 488 | Ident_AbnormalTermination->setIsPoisoned(Poison); |
| 489 | } |
| 490 | |
| 491 | void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) { |
| 492 | assert(Identifier.getIdentifierInfo() && |
| 493 | "Can't handle identifiers without identifier info!"); |
| 494 | llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it = |
| 495 | PoisonReasons.find(Identifier.getIdentifierInfo()); |
| 496 | if(it == PoisonReasons.end()) |
| 497 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 498 | else |
| 499 | Diag(Identifier,it->second) << Identifier.getIdentifierInfo(); |
| 500 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | |
| 502 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 503 | /// identifier. This callback looks up the identifier in the map and/or |
| 504 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 505 | /// |
| 506 | /// Note that callers of this method are guarded by checking the |
| 507 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 508 | /// IdentifierInfo methods that compute these properties will need to change to |
| 509 | /// match. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 510 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | assert(Identifier.getIdentifierInfo() && |
| 512 | "Can't handle identifiers without identifier info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 515 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 516 | // If the information about this identifier is out of date, update it from |
| 517 | // the external source. |
| 518 | if (II.isOutOfDate()) { |
| 519 | ExternalSource->updateOutOfDateIdentifier(II); |
| 520 | Identifier.setKind(II.getTokenID()); |
| 521 | } |
| 522 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | // If this identifier was poisoned, and if it was not produced from a macro |
| 524 | // expansion, emit an error. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 525 | if (II.isPoisoned() && CurPPLexer) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 526 | HandlePoisonedIdentifier(Identifier); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 527 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | // If this is a macro to be expanded, do it. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 530 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Abramo Bagnara | 163ada8 | 2012-01-01 22:01:04 +0000 | [diff] [blame] | 531 | if (!DisableMacroExpansion) { |
| 532 | if (Identifier.isExpandDisabled()) { |
| 533 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
| 534 | } else if (MI->isEnabled()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 535 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
Douglas Gregor | 6be16fe | 2011-08-27 06:37:51 +0000 | [diff] [blame] | 536 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 537 | } else { |
| 538 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 539 | // expanded, even if it's in a context where it could be expanded in the |
| 540 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 541 | Identifier.setFlag(Token::DisableExpand); |
Abramo Bagnara | 163ada8 | 2012-01-01 22:01:04 +0000 | [diff] [blame] | 542 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Richard Smith | 98d86b9 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 547 | // If this identifier is a keyword in C++11, produce a warning. Don't warn if |
| 548 | // we're not considering macro expansion, since this identifier might be the |
| 549 | // name of a macro. |
| 550 | // FIXME: This warning is disabled in cases where it shouldn't be, like |
| 551 | // "#define constexpr constexpr", "int constexpr;" |
| 552 | if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) { |
| 553 | Diag(Identifier, diag::warn_cxx11_keyword) << II.getName(); |
| 554 | // Don't diagnose this keyword again in this translation unit. |
| 555 | II.setIsCXX11CompatKeyword(false); |
| 556 | } |
| 557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 559 | // then we act as if it is the actual operator and not the textual |
| 560 | // representation of it. |
Fariborz Jahanian | afbc681 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 561 | if (II.isCPlusPlusOperatorKeyword()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 562 | Identifier.setIdentifierInfo(0); |
| 563 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 564 | // If this is an extension token, diagnose its use. |
Steve Naroff | b4eaf9c | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 565 | // We avoid diagnosing tokens that originate from macro definitions. |
Eli Friedman | 2962f4d | 2009-04-28 03:59:15 +0000 | [diff] [blame] | 566 | // FIXME: This warning is disabled in cases where it shouldn't be, |
| 567 | // like "#define TY typeof", "TY(1) x". |
| 568 | if (II.isExtensionToken() && !DisableMacroExpansion) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | Diag(Identifier, diag::ext_token_used); |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 570 | |
Ted Kremenek | 32ad2ee | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 571 | // If this is the '__experimental_modules_import' contextual keyword, note |
| 572 | // that the next token indicates a module name. |
Douglas Gregor | d6aba06 | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 573 | // |
Ted Kremenek | 32ad2ee | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 574 | // Note that we do not treat '__experimental_modules_import' as a contextual |
| 575 | // keyword when we're in a caching lexer, because caching lexers only get |
| 576 | // used in contexts where import declarations are disallowed. |
| 577 | if (II.isModulesImport() && !InMacroArgs && !DisableMacroExpansion && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 578 | getLangOpts().Modules && CurLexerKind != CLK_CachingLexer) { |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 579 | ModuleImportLoc = Identifier.getLocation(); |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 580 | ModuleImportPath.clear(); |
| 581 | ModuleImportExpectsIdentifier = true; |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 582 | CurLexerKind = CLK_LexAfterModuleImport; |
| 583 | } |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Douglas Gregor | c13a34b | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 586 | /// \brief Lex a token following the 'import' contextual keyword. |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 587 | /// |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 588 | void Preprocessor::LexAfterModuleImport(Token &Result) { |
| 589 | // Figure out what kind of lexer we actually have. |
Douglas Gregor | d6aba06 | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 590 | recomputeCurLexerKind(); |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 591 | |
| 592 | // Lex the next token. |
| 593 | Lex(Result); |
| 594 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 595 | // The token sequence |
| 596 | // |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 597 | // import identifier (. identifier)* |
| 598 | // |
Douglas Gregor | c13a34b | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 599 | // indicates a module import directive. We already saw the 'import' |
| 600 | // contextual keyword, so now we're looking for the identifiers. |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 601 | if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) { |
| 602 | // We expected to see an identifier here, and we did; continue handling |
| 603 | // identifiers. |
| 604 | ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(), |
| 605 | Result.getLocation())); |
| 606 | ModuleImportExpectsIdentifier = false; |
| 607 | CurLexerKind = CLK_LexAfterModuleImport; |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 608 | return; |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 609 | } |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 611 | // If we're expecting a '.' or a ';', and we got a '.', then wait until we |
| 612 | // see the next identifier. |
| 613 | if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) { |
| 614 | ModuleImportExpectsIdentifier = true; |
| 615 | CurLexerKind = CLK_LexAfterModuleImport; |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | // If we have a non-empty module path, load the named module. |
| 620 | if (!ModuleImportPath.empty()) |
Douglas Gregor | 5e35693 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 621 | (void)TheModuleLoader.loadModule(ModuleImportLoc, ModuleImportPath, |
Douglas Gregor | 93ebfa6 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 622 | Module::MacrosVisible, |
| 623 | /*IsIncludeDirective=*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 624 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 625 | |
| 626 | void Preprocessor::AddCommentHandler(CommentHandler *Handler) { |
| 627 | assert(Handler && "NULL comment handler"); |
| 628 | assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == |
| 629 | CommentHandlers.end() && "Comment handler already registered"); |
| 630 | CommentHandlers.push_back(Handler); |
| 631 | } |
| 632 | |
| 633 | void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) { |
| 634 | std::vector<CommentHandler *>::iterator Pos |
| 635 | = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); |
| 636 | assert(Pos != CommentHandlers.end() && "Comment handler not registered"); |
| 637 | CommentHandlers.erase(Pos); |
| 638 | } |
| 639 | |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 640 | bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { |
| 641 | bool AnyPendingTokens = false; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 642 | for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), |
| 643 | HEnd = CommentHandlers.end(); |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 644 | H != HEnd; ++H) { |
| 645 | if ((*H)->HandleComment(*this, Comment)) |
| 646 | AnyPendingTokens = true; |
| 647 | } |
| 648 | if (!AnyPendingTokens || getCommentRetentionState()) |
| 649 | return false; |
| 650 | Lex(result); |
| 651 | return true; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 654 | ModuleLoader::~ModuleLoader() { } |
| 655 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 656 | CommentHandler::~CommentHandler() { } |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 658 | CodeCompletionHandler::~CodeCompletionHandler() { } |
| 659 | |
Argyrios Kyrtzidis | c6c5452 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 660 | void Preprocessor::createPreprocessingRecord(bool RecordConditionalDirectives) { |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 661 | if (Record) |
| 662 | return; |
| 663 | |
Argyrios Kyrtzidis | c6c5452 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 664 | Record = new PreprocessingRecord(getSourceManager(), |
| 665 | RecordConditionalDirectives); |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 666 | addPPCallbacks(Record); |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 667 | } |