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