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()), |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 56 | SourceMgr(SM), HeaderInfo(Headers), ExternalSource(0), |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 57 | Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0), |
| 58 | CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0), |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame^] | 59 | CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0), |
| 60 | MICache(0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 62 | CounterValue = 0; // __COUNTER__ starts at 0. |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 63 | OwnsHeaderSearch = OwnsHeaders; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | // Clear stats. |
| 66 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 67 | NumIf = NumElse = NumEndif = 0; |
| 68 | NumEnteredSourceFiles = 0; |
| 69 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 70 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | MaxIncludeStackDepth = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | NumSkipped = 0; |
| 73 | |
| 74 | // Default to discarding comments. |
| 75 | KeepComments = false; |
| 76 | KeepMacroComments = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | // Macro expansion is enabled. |
| 79 | DisableMacroExpansion = false; |
| 80 | InMacroArgs = false; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 81 | NumCachedTokenLexers = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 83 | CachedLexPos = 0; |
| 84 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 85 | // We haven't read anything from the external source. |
| 86 | ReadMacrosFromExternalSource = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 87 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 89 | // This gets unpoisoned where it is allowed. |
| 90 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | // Initialize the pragma handlers. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 93 | PragmaHandlers = new PragmaNamespace(llvm::StringRef()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | RegisterBuiltinPragmas(); |
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 | // Initialize builtin macros like __LINE__ and friends. |
| 97 | RegisterBuiltinMacros(); |
| 98 | } |
| 99 | |
| 100 | Preprocessor::~Preprocessor() { |
Argyrios Kyrtzidis | 2174a4f | 2008-08-23 12:12:06 +0000 | [diff] [blame] | 101 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | while (!IncludeMacroStack.empty()) { |
| 104 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 105 | delete IncludeMacroStack.back().TheTokenLexer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | IncludeMacroStack.pop_back(); |
| 107 | } |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 108 | |
| 109 | // Free any macro definitions. |
Ted Kremenek | 2a6b03a | 2010-10-19 21:30:11 +0000 | [diff] [blame] | 110 | for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) |
Ted Kremenek | af8fa25 | 2010-10-19 18:16:54 +0000 | [diff] [blame] | 111 | I->MI.Destroy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 113 | // Free any cached macro expanders. |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 114 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 115 | delete TokenLexerCache[i]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 117 | // Free any cached MacroArgs. |
| 118 | for (MacroArgs *ArgList = MacroArgCache; ArgList; ) |
| 119 | ArgList = ArgList->deallocate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | // Release pragma information. |
| 122 | delete PragmaHandlers; |
| 123 | |
| 124 | // Delete the scratch buffer info. |
| 125 | delete ScratchBuf; |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 127 | // Delete the header search info, if we own it. |
| 128 | if (OwnsHeaderSearch) |
| 129 | delete &HeaderInfo; |
| 130 | |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 131 | delete Callbacks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 134 | void Preprocessor::setPTHManager(PTHManager* pm) { |
| 135 | PTH.reset(pm); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 136 | FileMgr.addStatCache(PTH->createStatCache()); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 139 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 140 | llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" |
| 141 | << getSpelling(Tok) << "'"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | if (!DumpFlags) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 145 | llvm::errs() << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | if (Tok.isAtStartOfLine()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 147 | llvm::errs() << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | if (Tok.hasLeadingSpace()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 149 | llvm::errs() << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | if (Tok.isExpandDisabled()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 151 | llvm::errs() << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | if (Tok.needsCleaning()) { |
| 153 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 154 | llvm::errs() << " [UnClean='" << llvm::StringRef(Start, Tok.getLength()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 155 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 158 | llvm::errs() << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 159 | DumpLocation(Tok.getLocation()); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 160 | llvm::errs() << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 164 | Loc.dump(SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 168 | llvm::errs() << "MACRO: "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 170 | DumpToken(MI.getReplacementToken(i)); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 171 | llvm::errs() << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 173 | llvm::errs() << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void Preprocessor::PrintStats() { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 177 | llvm::errs() << "\n*** Preprocessor Stats:\n"; |
| 178 | llvm::errs() << NumDirectives << " directives found:\n"; |
| 179 | llvm::errs() << " " << NumDefined << " #define.\n"; |
| 180 | llvm::errs() << " " << NumUndefined << " #undef.\n"; |
| 181 | llvm::errs() << " #include/#include_next/#import:\n"; |
| 182 | llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 183 | llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 184 | llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 185 | llvm::errs() << " " << NumElse << " #else/#elif.\n"; |
| 186 | llvm::errs() << " " << NumEndif << " #endif.\n"; |
| 187 | llvm::errs() << " " << NumPragma << " #pragma.\n"; |
| 188 | llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 190 | llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 191 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 192 | << NumFastMacroExpanded << " on the fast path.\n"; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 193 | llvm::errs() << (NumFastTokenPaste+NumTokenPaste) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 194 | << " token paste (##) operations performed, " |
| 195 | << NumFastTokenPaste << " on the fast path.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 198 | Preprocessor::macro_iterator |
| 199 | Preprocessor::macro_begin(bool IncludeExternalMacros) const { |
| 200 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 201 | !ReadMacrosFromExternalSource) { |
| 202 | ReadMacrosFromExternalSource = true; |
| 203 | ExternalSource->ReadDefinedMacros(); |
| 204 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 205 | |
| 206 | return Macros.begin(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 209 | Preprocessor::macro_iterator |
| 210 | Preprocessor::macro_end(bool IncludeExternalMacros) const { |
| 211 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 212 | !ReadMacrosFromExternalSource) { |
| 213 | ReadMacrosFromExternalSource = true; |
| 214 | ExternalSource->ReadDefinedMacros(); |
| 215 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 216 | |
| 217 | return Macros.end(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 220 | bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, |
| 221 | unsigned TruncateAtLine, |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 222 | unsigned TruncateAtColumn) { |
| 223 | using llvm::MemoryBuffer; |
| 224 | |
| 225 | CodeCompletionFile = File; |
| 226 | |
| 227 | // Okay to clear out the code-completion point by passing NULL. |
| 228 | if (!CodeCompletionFile) |
| 229 | return false; |
| 230 | |
| 231 | // Load the actual file's contents. |
Douglas Gregor | aa38c3d | 2010-03-16 19:49:24 +0000 | [diff] [blame] | 232 | bool Invalid = false; |
| 233 | const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); |
| 234 | if (Invalid) |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 235 | return true; |
| 236 | |
| 237 | // Find the byte position of the truncation point. |
| 238 | const char *Position = Buffer->getBufferStart(); |
| 239 | for (unsigned Line = 1; Line < TruncateAtLine; ++Line) { |
| 240 | for (; *Position; ++Position) { |
| 241 | if (*Position != '\r' && *Position != '\n') |
| 242 | continue; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 244 | // Eat \r\n or \n\r as a single line. |
| 245 | if ((Position[1] == '\r' || Position[1] == '\n') && |
| 246 | Position[0] != Position[1]) |
| 247 | ++Position; |
| 248 | ++Position; |
| 249 | break; |
| 250 | } |
| 251 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | b760fe8 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 253 | Position += TruncateAtColumn - 1; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 255 | // Truncate the buffer. |
Douglas Gregor | b760fe8 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 256 | if (Position < Buffer->getBufferEnd()) { |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 257 | llvm::StringRef Data(Buffer->getBufferStart(), |
| 258 | Position-Buffer->getBufferStart()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 259 | MemoryBuffer *TruncatedBuffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 260 | = MemoryBuffer::getMemBufferCopy(Data, Buffer->getBufferIdentifier()); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 261 | SourceMgr.overrideFileContents(File, TruncatedBuffer); |
| 262 | } |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
Douglas Gregor | 109ae73 | 2009-12-03 17:05:59 +0000 | [diff] [blame] | 267 | bool Preprocessor::isCodeCompletionFile(SourceLocation FileLoc) const { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 268 | return CodeCompletionFile && FileLoc.isFileID() && |
| 269 | SourceMgr.getFileEntryForID(SourceMgr.getFileID(FileLoc)) |
| 270 | == CodeCompletionFile; |
| 271 | } |
| 272 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 273 | void Preprocessor::CodeCompleteNaturalLanguage() { |
| 274 | SetCodeCompletionPoint(0, 0, 0); |
| 275 | getDiagnostics().setSuppressAllDiagnostics(true); |
| 276 | if (CodeComplete) |
| 277 | CodeComplete->CodeCompleteNaturalLanguage(); |
| 278 | } |
| 279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | //===----------------------------------------------------------------------===// |
| 281 | // Token Spelling |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 285 | /// token are the characters used to represent the token in the source file |
| 286 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 287 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 288 | /// UCNs, etc. |
Daniel Dunbar | 0ff1042 | 2009-11-14 01:20:48 +0000 | [diff] [blame] | 289 | std::string Preprocessor::getSpelling(const Token &Tok, |
| 290 | const SourceManager &SourceMgr, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 291 | const LangOptions &Features, |
| 292 | bool *Invalid) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | // If this token contains nothing interesting, return it directly. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 296 | bool CharDataInvalid = false; |
| 297 | const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(), |
| 298 | &CharDataInvalid); |
| 299 | if (Invalid) |
| 300 | *Invalid = CharDataInvalid; |
| 301 | if (CharDataInvalid) |
| 302 | return std::string(); |
| 303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | if (!Tok.needsCleaning()) |
| 305 | return std::string(TokStart, TokStart+Tok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | std::string Result; |
| 308 | Result.reserve(Tok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | // Otherwise, hard case, relex the characters into the string. |
| 311 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 312 | Ptr != End; ) { |
| 313 | unsigned CharSize; |
| 314 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 315 | Ptr += CharSize; |
| 316 | } |
| 317 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 318 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 319 | return Result; |
| 320 | } |
| 321 | |
Daniel Dunbar | 0ff1042 | 2009-11-14 01:20:48 +0000 | [diff] [blame] | 322 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 323 | /// token are the characters used to represent the token in the source file |
| 324 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 325 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 326 | /// UCNs, etc. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 327 | std::string Preprocessor::getSpelling(const Token &Tok, bool *Invalid) const { |
| 328 | return getSpelling(Tok, SourceMgr, Features, Invalid); |
Daniel Dunbar | 0ff1042 | 2009-11-14 01:20:48 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 331 | /// getSpelling - This method is used to get the spelling of a token into a |
| 332 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 333 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 334 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 335 | /// |
| 336 | /// Note that this method may do two possible things: it may either fill in |
| 337 | /// the buffer specified with characters, or it may *change the input pointer* |
| 338 | /// to point to a constant buffer with the data already in it (avoiding a |
| 339 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 340 | /// if an internal buffer is returned. |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 341 | unsigned Preprocessor::getSpelling(const Token &Tok, |
| 342 | const char *&Buffer, bool *Invalid) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | // If this token is an identifier, just return the string from the identifier |
| 346 | // table, which is very quick. |
| 347 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 348 | Buffer = II->getNameStart(); |
| 349 | return II->getLength(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | } |
Ted Kremenek | b70e3da | 2009-01-08 02:47:16 +0000 | [diff] [blame] | 351 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | // Otherwise, compute the start of the token in the input lexer buffer. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 353 | const char *TokStart = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 355 | if (Tok.isLiteral()) |
| 356 | TokStart = Tok.getLiteralData(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 358 | if (TokStart == 0) { |
| 359 | bool CharDataInvalid = false; |
| 360 | TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); |
| 361 | if (Invalid) |
| 362 | *Invalid = CharDataInvalid; |
| 363 | if (CharDataInvalid) { |
| 364 | Buffer = ""; |
| 365 | return 0; |
| 366 | } |
| 367 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | |
| 369 | // If this token contains nothing interesting, return it directly. |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 370 | if (!Tok.needsCleaning()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | Buffer = TokStart; |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 372 | return Tok.getLength(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | // Otherwise, hard case, relex the characters into the string. |
| 376 | char *OutBuf = const_cast<char*>(Buffer); |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 377 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 378 | Ptr != End; ) { |
| 379 | unsigned CharSize; |
| 380 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 381 | Ptr += CharSize; |
| 382 | } |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 383 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | return OutBuf-Buffer; |
| 387 | } |
| 388 | |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 389 | /// getSpelling - This method is used to get the spelling of a token into a |
| 390 | /// SmallVector. Note that the returned StringRef may not point to the |
| 391 | /// supplied buffer if a copy can be avoided. |
| 392 | llvm::StringRef Preprocessor::getSpelling(const Token &Tok, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 393 | llvm::SmallVectorImpl<char> &Buffer, |
| 394 | bool *Invalid) const { |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 395 | // Try the fast path. |
| 396 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) |
| 397 | return II->getName(); |
| 398 | |
| 399 | // Resize the buffer if we need to copy into it. |
| 400 | if (Tok.needsCleaning()) |
| 401 | Buffer.resize(Tok.getLength()); |
| 402 | |
| 403 | const char *Ptr = Buffer.data(); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 404 | unsigned Len = getSpelling(Tok, Ptr, Invalid); |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 405 | return llvm::StringRef(Ptr, Len); |
| 406 | } |
| 407 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 409 | /// location for it. If specified, the source location provides a source |
| 410 | /// location for the token. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 411 | void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok, |
| 412 | SourceLocation InstantiationLoc) { |
| 413 | Tok.setLength(Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 415 | const char *DestPtr; |
| 416 | SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 418 | if (InstantiationLoc.isValid()) |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 419 | Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc, |
| 420 | InstantiationLoc, Len); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 421 | Tok.setLocation(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 423 | // If this is a literal token, set the pointer data. |
| 424 | if (Tok.isLiteral()) |
| 425 | Tok.setLiteralData(DestPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 429 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 430 | /// token, return a new location that specifies a character within the token. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 432 | unsigned CharNo) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 433 | // Figure out how many physical characters away the specified instantiation |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 434 | // character is. This needs to take into consideration newlines and |
| 435 | // trigraphs. |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 436 | bool Invalid = false; |
| 437 | const char *TokPtr = SourceMgr.getCharacterData(TokStart, &Invalid); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 88e2524 | 2009-04-18 22:28:58 +0000 | [diff] [blame] | 439 | // If they request the first char of the token, we're trivially done. |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 440 | if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) |
Chris Lattner | 88e2524 | 2009-04-18 22:28:58 +0000 | [diff] [blame] | 441 | return TokStart; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 443 | unsigned PhysOffset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 445 | // The usual case is that tokens don't contain anything interesting. Skip |
| 446 | // over the uninteresting characters. If a token only consists of simple |
| 447 | // chars, this method is extremely fast. |
Chris Lattner | 88e2524 | 2009-04-18 22:28:58 +0000 | [diff] [blame] | 448 | while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { |
| 449 | if (CharNo == 0) |
| 450 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 451 | ++TokPtr, --CharNo, ++PhysOffset; |
Chris Lattner | 88e2524 | 2009-04-18 22:28:58 +0000 | [diff] [blame] | 452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 28c90ad | 2009-01-17 07:57:25 +0000 | [diff] [blame] | 454 | // If we have a character that may be a trigraph or escaped newline, use a |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 455 | // lexer to parse it correctly. |
Chris Lattner | 88e2524 | 2009-04-18 22:28:58 +0000 | [diff] [blame] | 456 | for (; CharNo; --CharNo) { |
| 457 | unsigned Size; |
| 458 | Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features); |
| 459 | TokPtr += Size; |
| 460 | PhysOffset += Size; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 88e2524 | 2009-04-18 22:28:58 +0000 | [diff] [blame] | 463 | // Final detail: if we end up on an escaped newline, we want to return the |
| 464 | // location of the actual byte of the token. For example foo\<newline>bar |
| 465 | // advanced by 3 should return the location of b, not of \\. One compounding |
| 466 | // detail of this is that the escape may be made by a trigraph. |
| 467 | if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) |
Ted Kremenek | c0178e9 | 2010-01-29 19:38:24 +0000 | [diff] [blame] | 468 | PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 470 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 473 | SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 474 | unsigned Offset) { |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 475 | if (Loc.isInvalid() || !Loc.isFileID()) |
| 476 | return SourceLocation(); |
| 477 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 478 | unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager(), Features); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 479 | if (Len > Offset) |
| 480 | Len = Len - Offset; |
| 481 | else |
| 482 | return Loc; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 483 | |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 484 | return AdvanceToTokenCharacter(Loc, Len); |
| 485 | } |
| 486 | |
| 487 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 489 | //===----------------------------------------------------------------------===// |
| 490 | // Preprocessor Initialization Methods |
| 491 | //===----------------------------------------------------------------------===// |
| 492 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 493 | |
| 494 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 495 | /// which implicitly adds the builtin defines etc. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 496 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 05db427 | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 497 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 498 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 499 | // information) and predefined macros aren't guaranteed to be set properly. |
| 500 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 501 | FileID MainFileID = SourceMgr.getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 503 | // Enter the main file source buffer. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 504 | EnterSourceFile(MainFileID, 0, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 506 | // If we've been asked to skip bytes in the main file (e.g., as part of a |
| 507 | // precompiled preamble), do so now. |
| 508 | if (SkipMainFilePreamble.first > 0) |
| 509 | CurLexer->SkipBytes(SkipMainFilePreamble.first, |
| 510 | SkipMainFilePreamble.second); |
| 511 | |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 512 | // Tell the header info that the main file was entered. If the file is later |
| 513 | // #imported, it won't be re-entered. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 514 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 515 | HeaderInfo.IncrementIncludeCount(FE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Benjamin Kramer | ffd6e39 | 2009-12-31 15:33:09 +0000 | [diff] [blame] | 517 | // Preprocess Predefines to populate the initial preprocessor state. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | llvm::MemoryBuffer *SB = |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 519 | llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); |
Douglas Gregor | 043266b | 2010-08-26 14:07:34 +0000 | [diff] [blame] | 520 | assert(SB && "Cannot create predefined source buffer"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 521 | FileID FID = SourceMgr.createFileIDForMemBuffer(SB); |
| 522 | assert(!FID.isInvalid() && "Could not create FileID for predefines?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 524 | // Start parsing the predefines. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 525 | EnterSourceFile(FID, 0, SourceLocation()); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 526 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 527 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 528 | void Preprocessor::EndSourceFile() { |
| 529 | // Notify the client that we reached the end of the source file. |
| 530 | if (Callbacks) |
| 531 | Callbacks->EndOfMainFile(); |
| 532 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | |
| 534 | //===----------------------------------------------------------------------===// |
| 535 | // Lexer Event Handling. |
| 536 | //===----------------------------------------------------------------------===// |
| 537 | |
| 538 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 539 | /// identifier information for the token and install it into the token. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 540 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
Daniel Dunbar | c322209 | 2009-11-05 01:53:52 +0000 | [diff] [blame] | 541 | const char *BufPtr) const { |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 542 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 546 | IdentifierInfo *II; |
| 547 | if (BufPtr && !Identifier.needsCleaning()) { |
| 548 | // No cleaning needed, just use the characters from the lexed buffer. |
Daniel Dunbar | 3da736c | 2009-11-05 01:53:39 +0000 | [diff] [blame] | 549 | II = getIdentifierInfo(llvm::StringRef(BufPtr, Identifier.getLength())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 550 | } else { |
| 551 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 552 | llvm::SmallString<64> IdentifierBuffer; |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 553 | llvm::StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); |
| 554 | II = getIdentifierInfo(CleanedStr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 555 | } |
| 556 | Identifier.setIdentifierInfo(II); |
| 557 | return II; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 562 | /// identifier. This callback looks up the identifier in the map and/or |
| 563 | /// 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] | 564 | /// |
| 565 | /// Note that callers of this method are guarded by checking the |
| 566 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 567 | /// IdentifierInfo methods that compute these properties will need to change to |
| 568 | /// match. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 569 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 570 | assert(Identifier.getIdentifierInfo() && |
| 571 | "Can't handle identifiers without identifier info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 573 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 574 | |
| 575 | // If this identifier was poisoned, and if it was not produced from a macro |
| 576 | // expansion, emit an error. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 577 | if (II.isPoisoned() && CurPPLexer) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 579 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 580 | else |
| 581 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 584 | // If this is a macro to be expanded, do it. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 585 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 587 | if (MI->isEnabled()) { |
| 588 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 589 | return; |
| 590 | } else { |
| 591 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 592 | // expanded, even if it's in a context where it could be expanded in the |
| 593 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 594 | Identifier.setFlag(Token::DisableExpand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 600 | // then we act as if it is the actual operator and not the textual |
| 601 | // representation of it. |
Fariborz Jahanian | afbc681 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 602 | if (II.isCPlusPlusOperatorKeyword()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 603 | Identifier.setIdentifierInfo(0); |
| 604 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 605 | // If this is an extension token, diagnose its use. |
Steve Naroff | b4eaf9c | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 606 | // We avoid diagnosing tokens that originate from macro definitions. |
Eli Friedman | 2962f4d | 2009-04-28 03:59:15 +0000 | [diff] [blame] | 607 | // FIXME: This warning is disabled in cases where it shouldn't be, |
| 608 | // like "#define TY typeof", "TY(1) x". |
| 609 | if (II.isExtensionToken() && !DisableMacroExpansion) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 610 | Diag(Identifier, diag::ext_token_used); |
| 611 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 612 | |
| 613 | void Preprocessor::AddCommentHandler(CommentHandler *Handler) { |
| 614 | assert(Handler && "NULL comment handler"); |
| 615 | assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == |
| 616 | CommentHandlers.end() && "Comment handler already registered"); |
| 617 | CommentHandlers.push_back(Handler); |
| 618 | } |
| 619 | |
| 620 | void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) { |
| 621 | std::vector<CommentHandler *>::iterator Pos |
| 622 | = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); |
| 623 | assert(Pos != CommentHandlers.end() && "Comment handler not registered"); |
| 624 | CommentHandlers.erase(Pos); |
| 625 | } |
| 626 | |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 627 | bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { |
| 628 | bool AnyPendingTokens = false; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 629 | for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), |
| 630 | HEnd = CommentHandlers.end(); |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 631 | H != HEnd; ++H) { |
| 632 | if ((*H)->HandleComment(*this, Comment)) |
| 633 | AnyPendingTokens = true; |
| 634 | } |
| 635 | if (!AnyPendingTokens || getCommentRetentionState()) |
| 636 | return false; |
| 637 | Lex(result); |
| 638 | return true; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | CommentHandler::~CommentHandler() { } |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 643 | CodeCompletionHandler::~CodeCompletionHandler() { } |
| 644 | |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 645 | void Preprocessor::createPreprocessingRecord() { |
| 646 | if (Record) |
| 647 | return; |
| 648 | |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 649 | Record = new PreprocessingRecord; |
| 650 | addPPCallbacks(Record); |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 651 | } |