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