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