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. |
| 16 | // -d[MDNI] - Dump various things. |
| 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" |
| 29 | #include "clang/Lex/HeaderSearch.h" |
| 30 | #include "clang/Lex/MacroInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Pragma.h" |
| 32 | #include "clang/Lex/ScratchBuffer.h" |
| 33 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceManager.h" |
| 35 | #include "clang/Basic/TargetInfo.h" |
| 36 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Streams.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 44 | TargetInfo &target, SourceManager &SM, |
| 45 | HeaderSearch &Headers) |
| 46 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
| 47 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts), |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 48 | CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 50 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | // Clear stats. |
| 52 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 53 | NumIf = NumElse = NumEndif = 0; |
| 54 | NumEnteredSourceFiles = 0; |
| 55 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 56 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 57 | MaxIncludeStackDepth = 0; |
| 58 | NumSkipped = 0; |
| 59 | |
| 60 | // Default to discarding comments. |
| 61 | KeepComments = false; |
| 62 | KeepMacroComments = false; |
| 63 | |
| 64 | // Macro expansion is enabled. |
| 65 | DisableMacroExpansion = false; |
| 66 | InMacroArgs = false; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 67 | NumCachedTokenLexers = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | |
| 69 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 70 | // This gets unpoisoned where it is allowed. |
| 71 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 72 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 73 | Predefines = 0; |
| 74 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | // Initialize the pragma handlers. |
| 76 | PragmaHandlers = new PragmaNamespace(0); |
| 77 | RegisterBuiltinPragmas(); |
| 78 | |
| 79 | // Initialize builtin macros like __LINE__ and friends. |
| 80 | RegisterBuiltinMacros(); |
| 81 | } |
| 82 | |
| 83 | Preprocessor::~Preprocessor() { |
| 84 | // Free any active lexers. |
| 85 | delete CurLexer; |
| 86 | |
| 87 | while (!IncludeMacroStack.empty()) { |
| 88 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 89 | delete IncludeMacroStack.back().TheTokenLexer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | IncludeMacroStack.pop_back(); |
| 91 | } |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 92 | |
| 93 | // Free any macro definitions. |
| 94 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 95 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 96 | // Free the macro definition. |
| 97 | delete I->second; |
| 98 | I->second = 0; |
| 99 | I->first->setHasMacroDefinition(false); |
| 100 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 102 | // Free any cached macro expanders. |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 103 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 104 | delete TokenLexerCache[i]; |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 105 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | // Release pragma information. |
| 107 | delete PragmaHandlers; |
| 108 | |
| 109 | // Delete the scratch buffer info. |
| 110 | delete ScratchBuf; |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 111 | |
| 112 | delete Callbacks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 116 | /// the specified Token's location, translating the token's start |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | /// position in the current buffer into a SourcePosition object for rendering. |
| 118 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 119 | Diags.Report(getFullLoc(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 123 | const std::string &Msg) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 124 | Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 127 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 128 | llvm::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 129 | << getSpelling(Tok) << "'"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | |
| 131 | if (!DumpFlags) return; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 133 | llvm::cerr << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | if (Tok.isAtStartOfLine()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 135 | llvm::cerr << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | if (Tok.hasLeadingSpace()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 137 | llvm::cerr << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | if (Tok.isExpandDisabled()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 139 | llvm::cerr << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | if (Tok.needsCleaning()) { |
| 141 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 142 | llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 143 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 146 | llvm::cerr << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 147 | DumpLocation(Tok.getLocation()); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 148 | llvm::cerr << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
| 152 | SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 153 | llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':' |
| 154 | << SourceMgr.getLineNumber(LogLoc) << ':' |
| 155 | << SourceMgr.getLineNumber(LogLoc); |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 156 | |
| 157 | SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc); |
| 158 | if (PhysLoc != LogLoc) { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 159 | llvm::cerr << " <PhysLoc="; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 160 | DumpLocation(PhysLoc); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 161 | llvm::cerr << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 162 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 166 | llvm::cerr << "MACRO: "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 168 | DumpToken(MI.getReplacementToken(i)); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 169 | llvm::cerr << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 171 | llvm::cerr << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void Preprocessor::PrintStats() { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 175 | llvm::cerr << "\n*** Preprocessor Stats:\n"; |
| 176 | llvm::cerr << NumDirectives << " directives found:\n"; |
| 177 | llvm::cerr << " " << NumDefined << " #define.\n"; |
| 178 | llvm::cerr << " " << NumUndefined << " #undef.\n"; |
| 179 | llvm::cerr << " #include/#include_next/#import:\n"; |
| 180 | llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 181 | llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 182 | llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 183 | llvm::cerr << " " << NumElse << " #else/#elif.\n"; |
| 184 | llvm::cerr << " " << NumEndif << " #endif.\n"; |
| 185 | llvm::cerr << " " << NumPragma << " #pragma.\n"; |
| 186 | llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 188 | llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 189 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 190 | << NumFastMacroExpanded << " on the fast path.\n"; |
| 191 | llvm::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 192 | << " token paste (##) operations performed, " |
| 193 | << NumFastTokenPaste << " on the fast path.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | // Token Spelling |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | |
| 200 | |
| 201 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 202 | /// token are the characters used to represent the token in the source file |
| 203 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 204 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 205 | /// UCNs, etc. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 206 | std::string Preprocessor::getSpelling(const Token &Tok) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 208 | |
| 209 | // If this token contains nothing interesting, return it directly. |
| 210 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 211 | if (!Tok.needsCleaning()) |
| 212 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 213 | |
| 214 | std::string Result; |
| 215 | Result.reserve(Tok.getLength()); |
| 216 | |
| 217 | // Otherwise, hard case, relex the characters into the string. |
| 218 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 219 | Ptr != End; ) { |
| 220 | unsigned CharSize; |
| 221 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 222 | Ptr += CharSize; |
| 223 | } |
| 224 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 225 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 226 | return Result; |
| 227 | } |
| 228 | |
| 229 | /// getSpelling - This method is used to get the spelling of a token into a |
| 230 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 231 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 232 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 233 | /// |
| 234 | /// Note that this method may do two possible things: it may either fill in |
| 235 | /// the buffer specified with characters, or it may *change the input pointer* |
| 236 | /// to point to a constant buffer with the data already in it (avoiding a |
| 237 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 238 | /// if an internal buffer is returned. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 239 | unsigned Preprocessor::getSpelling(const Token &Tok, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | const char *&Buffer) const { |
| 241 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 242 | |
| 243 | // If this token is an identifier, just return the string from the identifier |
| 244 | // table, which is very quick. |
| 245 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 246 | Buffer = II->getName(); |
Chris Lattner | 0f67032 | 2007-07-22 22:50:09 +0000 | [diff] [blame] | 247 | |
| 248 | // Return the length of the token. If the token needed cleaning, don't |
| 249 | // include the size of the newlines or trigraphs in it. |
| 250 | if (!Tok.needsCleaning()) |
| 251 | return Tok.getLength(); |
| 252 | else |
| 253 | return strlen(Buffer); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Otherwise, compute the start of the token in the input lexer buffer. |
| 257 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 258 | |
| 259 | // If this token contains nothing interesting, return it directly. |
| 260 | if (!Tok.needsCleaning()) { |
| 261 | Buffer = TokStart; |
| 262 | return Tok.getLength(); |
| 263 | } |
| 264 | // Otherwise, hard case, relex the characters into the string. |
| 265 | char *OutBuf = const_cast<char*>(Buffer); |
| 266 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 267 | Ptr != End; ) { |
| 268 | unsigned CharSize; |
| 269 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 270 | Ptr += CharSize; |
| 271 | } |
| 272 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 273 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 274 | |
| 275 | return OutBuf-Buffer; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 280 | /// location for it. If specified, the source location provides a source |
| 281 | /// location for the token. |
| 282 | SourceLocation Preprocessor:: |
| 283 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 284 | if (SLoc.isValid()) |
| 285 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 286 | return ScratchBuf->getToken(Buf, Len); |
| 287 | } |
| 288 | |
| 289 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 290 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 291 | /// token, return a new location that specifies a character within the token. |
| 292 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 293 | unsigned CharNo) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 294 | // If they request the first char of the token, we're trivially done. If this |
| 295 | // is a macro expansion, it doesn't make sense to point to a character within |
| 296 | // the instantiation point (the name). We could point to the source |
| 297 | // character, but without also pointing to instantiation info, this is |
| 298 | // confusing. |
| 299 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 300 | |
| 301 | // Figure out how many physical characters away the specified logical |
| 302 | // character is. This needs to take into consideration newlines and |
| 303 | // trigraphs. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 304 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 305 | unsigned PhysOffset = 0; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 306 | |
| 307 | // The usual case is that tokens don't contain anything interesting. Skip |
| 308 | // over the uninteresting characters. If a token only consists of simple |
| 309 | // chars, this method is extremely fast. |
| 310 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 311 | ++TokPtr, --CharNo, ++PhysOffset; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 312 | |
| 313 | // If we have a character that may be a trigraph or escaped newline, create a |
| 314 | // lexer to parse it correctly. |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 315 | if (CharNo != 0) { |
| 316 | // Create a lexer starting at this token position. |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame] | 317 | Lexer TheLexer(TokStart, *this, TokPtr); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 318 | Token Tok; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 319 | // Skip over characters the remaining characters. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 320 | const char *TokStartPtr = TokPtr; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 321 | for (; CharNo; --CharNo) |
| 322 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 323 | |
| 324 | PhysOffset += TokPtr-TokStartPtr; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 325 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 326 | |
| 327 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 331 | //===----------------------------------------------------------------------===// |
| 332 | // Preprocessor Initialization Methods |
| 333 | //===----------------------------------------------------------------------===// |
| 334 | |
| 335 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 336 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 337 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 338 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 339 | const char *Command = "#define ") { |
| 340 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 341 | if (const char *Equal = strchr(Macro, '=')) { |
| 342 | // Turn the = into ' '. |
| 343 | Buf.insert(Buf.end(), Macro, Equal); |
| 344 | Buf.push_back(' '); |
| 345 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 346 | } else { |
| 347 | // Push "macroname 1". |
| 348 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 349 | Buf.push_back(' '); |
| 350 | Buf.push_back('1'); |
| 351 | } |
| 352 | Buf.push_back('\n'); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 357 | std::vector<char> &Buf) { |
| 358 | // FIXME: Implement magic like cpp_init_builtins for things like __STDC__ |
| 359 | // and __DATE__ etc. |
| 360 | #if 0 |
| 361 | /* __STDC__ has the value 1 under normal circumstances. |
| 362 | However, if (a) we are in a system header, (b) the option |
| 363 | stdc_0_in_system_headers is true (set by target config), and |
| 364 | (c) we are not in strictly conforming mode, then it has the |
| 365 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
| 366 | //case BT_STDC: |
| 367 | if (cpp_in_system_header (pfile)) |
| 368 | number = 0; |
| 369 | else |
| 370 | number = 1; |
| 371 | break; |
| 372 | #endif |
| 373 | // These should all be defined in the preprocessor according to the |
| 374 | // current language configuration. |
| 375 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 376 | //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 377 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
| 378 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 379 | else if (0) // STDC94 ? |
| 380 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 381 | |
| 382 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 383 | if (PP.getLangOptions().ObjC1) |
| 384 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 385 | if (PP.getLangOptions().ObjC2) |
| 386 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 387 | |
Chris Lattner | d19144b | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 388 | // Add __builtin_va_list typedef. |
| 389 | { |
| 390 | const char *VAList = PP.getTargetInfo().getVAListDeclaration(); |
| 391 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 392 | Buf.push_back('\n'); |
| 393 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 394 | |
| 395 | // Get the target #defines. |
| 396 | PP.getTargetInfo().getTargetDefines(Buf); |
| 397 | |
| 398 | // Compiler set macros. |
| 399 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5250"); |
Steve Naroff | 39d0a27 | 2007-11-10 18:06:36 +0000 | [diff] [blame] | 400 | DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050"); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 401 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0"); |
| 402 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 403 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 404 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 405 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. " |
| 406 | "build 5250)\""); |
| 407 | |
| 408 | // Build configuration options. |
| 409 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 410 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 411 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 412 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
| 413 | |
| 414 | |
| 415 | if (PP.getLangOptions().CPlusPlus) { |
| 416 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 417 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 418 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 419 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 420 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 421 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 422 | } |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 423 | if (PP.getLangOptions().Microsoft) { |
| 424 | DefineBuiltinMacro(Buf, "__stdcall="); |
| 425 | DefineBuiltinMacro(Buf, "__cdecl="); |
| 426 | DefineBuiltinMacro(Buf, "_cdecl="); |
| 427 | DefineBuiltinMacro(Buf, "__ptr64="); |
Steve Naroff | b746ce8 | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 428 | DefineBuiltinMacro(Buf, "__w64="); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 429 | DefineBuiltinMacro(Buf, "__forceinline="); |
Steve Naroff | 419154d | 2008-02-07 15:26:07 +0000 | [diff] [blame] | 430 | DefineBuiltinMacro(Buf, "__int8=char"); |
| 431 | DefineBuiltinMacro(Buf, "__int16=short"); |
| 432 | DefineBuiltinMacro(Buf, "__int32=int"); |
Chris Lattner | 9880ba9 | 2008-02-10 21:12:45 +0000 | [diff] [blame] | 433 | DefineBuiltinMacro(Buf, "__int64=long long"); |
Steve Naroff | 705b5b5 | 2008-02-11 22:29:58 +0000 | [diff] [blame] | 434 | DefineBuiltinMacro(Buf, "__declspec(X)="); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 435 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 436 | // FIXME: Should emit a #line directive here. |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 441 | /// which implicitly adds the builtin defines etc. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 442 | void Preprocessor::EnterMainSourceFile() { |
| 443 | |
| 444 | unsigned MainFileID = SourceMgr.getMainFileID(); |
| 445 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 446 | // Enter the main file source buffer. |
| 447 | EnterSourceFile(MainFileID, 0); |
| 448 | |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 449 | // Tell the header info that the main file was entered. If the file is later |
| 450 | // #imported, it won't be re-entered. |
| 451 | if (const FileEntry *FE = |
| 452 | SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0))) |
| 453 | HeaderInfo.IncrementIncludeCount(FE); |
| 454 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 455 | std::vector<char> PrologFile; |
| 456 | PrologFile.reserve(4080); |
| 457 | |
| 458 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 459 | InitializePredefinedMacros(*this, PrologFile); |
| 460 | |
| 461 | // Add on the predefines from the driver. |
| 462 | PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines)); |
| 463 | |
| 464 | // Memory buffer must end with a null byte! |
| 465 | PrologFile.push_back(0); |
| 466 | |
| 467 | // Now that we have emitted the predefined macros, #includes, etc into |
| 468 | // PrologFile, preprocess it to populate the initial preprocessor state. |
| 469 | llvm::MemoryBuffer *SB = |
| 470 | llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(), |
| 471 | "<predefines>"); |
| 472 | assert(SB && "Cannot fail to create predefined source buffer"); |
| 473 | unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 474 | assert(FileID && "Could not create FileID for predefines?"); |
| 475 | |
| 476 | // Start parsing the predefines. |
| 477 | EnterSourceFile(FileID, 0); |
| 478 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 479 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 480 | |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | // Lexer Event Handling. |
| 483 | //===----------------------------------------------------------------------===// |
| 484 | |
| 485 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 486 | /// identifier information for the token and install it into the token. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 487 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 488 | const char *BufPtr) { |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 489 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 491 | |
| 492 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 493 | IdentifierInfo *II; |
| 494 | if (BufPtr && !Identifier.needsCleaning()) { |
| 495 | // No cleaning needed, just use the characters from the lexed buffer. |
| 496 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 497 | } else { |
| 498 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | c35717a | 2007-07-13 17:10:38 +0000 | [diff] [blame] | 499 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 500 | IdentifierBuffer.resize(Identifier.getLength()); |
| 501 | const char *TmpBuf = &IdentifierBuffer[0]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 503 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 504 | } |
| 505 | Identifier.setIdentifierInfo(II); |
| 506 | return II; |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 511 | /// identifier. This callback looks up the identifier in the map and/or |
| 512 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 513 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | assert(Identifier.getIdentifierInfo() && |
| 515 | "Can't handle identifiers without identifier info!"); |
| 516 | |
| 517 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 518 | |
| 519 | // If this identifier was poisoned, and if it was not produced from a macro |
| 520 | // expansion, emit an error. |
| 521 | if (II.isPoisoned() && CurLexer) { |
| 522 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 523 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 524 | else |
| 525 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 526 | } |
| 527 | |
| 528 | // If this is a macro to be expanded, do it. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 529 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 530 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 531 | if (MI->isEnabled()) { |
| 532 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 533 | return; |
| 534 | } else { |
| 535 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 536 | // expanded, even if it's in a context where it could be expanded in the |
| 537 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 538 | Identifier.setFlag(Token::DisableExpand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 544 | // then we act as if it is the actual operator and not the textual |
| 545 | // representation of it. |
| 546 | if (II.isCPlusPlusOperatorKeyword()) |
| 547 | Identifier.setIdentifierInfo(0); |
| 548 | |
| 549 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 550 | // turning "for" into a keyword. |
| 551 | Identifier.setKind(II.getTokenID()); |
| 552 | |
| 553 | // If this is an extension token, diagnose its use. |
| 554 | // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99 |
| 555 | // For now, I'm just commenting it out (while I work on attributes). |
| 556 | if (II.isExtensionToken() && Features.C99) |
| 557 | Diag(Identifier, diag::ext_token_used); |
| 558 | } |
| 559 | |