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