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