Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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" |
| 31 | #include "clang/Lex/PPCallbacks.h" |
| 32 | #include "clang/Lex/Pragma.h" |
| 33 | #include "clang/Lex/ScratchBuffer.h" |
| 34 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 35 | #include "clang/Basic/SourceManager.h" |
| 36 | #include "clang/Basic/TargetInfo.h" |
| 37 | #include "llvm/ADT/SmallVector.h" |
| 38 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Streams.h" |
Chris Lattner | 1b02318 | 2007-09-03 18:30:32 +0000 | [diff] [blame] | 40 | #include <ctime> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 46 | TargetInfo &target, SourceManager &SM, |
| 47 | HeaderSearch &Headers) |
| 48 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
| 49 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts), |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 50 | CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | ScratchBuf = new ScratchBuffer(SourceMgr); |
| 52 | |
| 53 | // Clear stats. |
| 54 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 55 | NumIf = NumElse = NumEndif = 0; |
| 56 | NumEnteredSourceFiles = 0; |
| 57 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 58 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 59 | MaxIncludeStackDepth = 0; |
| 60 | NumSkipped = 0; |
| 61 | |
| 62 | // Default to discarding comments. |
| 63 | KeepComments = false; |
| 64 | KeepMacroComments = false; |
| 65 | |
| 66 | // Macro expansion is enabled. |
| 67 | DisableMacroExpansion = false; |
| 68 | InMacroArgs = false; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 69 | NumCachedTokenLexers = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | |
| 71 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 72 | // This gets unpoisoned where it is allowed. |
| 73 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 74 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 75 | Predefines = 0; |
| 76 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 77 | // Initialize the pragma handlers. |
| 78 | PragmaHandlers = new PragmaNamespace(0); |
| 79 | RegisterBuiltinPragmas(); |
| 80 | |
| 81 | // Initialize builtin macros like __LINE__ and friends. |
| 82 | RegisterBuiltinMacros(); |
| 83 | } |
| 84 | |
| 85 | Preprocessor::~Preprocessor() { |
| 86 | // Free any active lexers. |
| 87 | delete CurLexer; |
| 88 | |
| 89 | while (!IncludeMacroStack.empty()) { |
| 90 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 91 | delete IncludeMacroStack.back().TheTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 92 | IncludeMacroStack.pop_back(); |
| 93 | } |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 94 | |
| 95 | // Free any macro definitions. |
| 96 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 97 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 98 | // Free the macro definition. |
| 99 | delete I->second; |
| 100 | I->second = 0; |
| 101 | I->first->setHasMacroDefinition(false); |
| 102 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 103 | |
| 104 | // Free any cached macro expanders. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 105 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 106 | delete TokenLexerCache[i]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 107 | |
| 108 | // Release pragma information. |
| 109 | delete PragmaHandlers; |
| 110 | |
| 111 | // Delete the scratch buffer info. |
| 112 | delete ScratchBuf; |
| 113 | } |
| 114 | |
| 115 | PPCallbacks::~PPCallbacks() { |
| 116 | } |
| 117 | |
| 118 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
| 119 | /// the specified Token's location, translating the token's start |
| 120 | /// position in the current buffer into a SourcePosition object for rendering. |
| 121 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 122 | Diags.Report(getFullLoc(Loc), DiagID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 126 | const std::string &Msg) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 127 | Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 131 | llvm::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 132 | << getSpelling(Tok) << "'"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 133 | |
| 134 | if (!DumpFlags) return; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 136 | llvm::cerr << "\t"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 137 | if (Tok.isAtStartOfLine()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 138 | llvm::cerr << " [StartOfLine]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 139 | if (Tok.hasLeadingSpace()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 140 | llvm::cerr << " [LeadingSpace]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 141 | if (Tok.isExpandDisabled()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 142 | llvm::cerr << " [ExpandDisabled]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 143 | if (Tok.needsCleaning()) { |
| 144 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 145 | llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 146 | << "']"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 149 | llvm::cerr << "\tLoc=<"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 150 | DumpLocation(Tok.getLocation()); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 151 | llvm::cerr << ">"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
| 155 | SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 156 | llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':' |
| 157 | << SourceMgr.getLineNumber(LogLoc) << ':' |
| 158 | << SourceMgr.getLineNumber(LogLoc); |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 159 | |
| 160 | SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc); |
| 161 | if (PhysLoc != LogLoc) { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 162 | llvm::cerr << " <PhysLoc="; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 163 | DumpLocation(PhysLoc); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 164 | llvm::cerr << ">"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 165 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 169 | llvm::cerr << "MACRO: "; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 170 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 171 | DumpToken(MI.getReplacementToken(i)); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 172 | llvm::cerr << " "; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 174 | llvm::cerr << "\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void Preprocessor::PrintStats() { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 178 | llvm::cerr << "\n*** Preprocessor Stats:\n"; |
| 179 | llvm::cerr << NumDirectives << " directives found:\n"; |
| 180 | llvm::cerr << " " << NumDefined << " #define.\n"; |
| 181 | llvm::cerr << " " << NumUndefined << " #undef.\n"; |
| 182 | llvm::cerr << " #include/#include_next/#import:\n"; |
| 183 | llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 184 | llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 185 | llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 186 | llvm::cerr << " " << NumElse << " #else/#elif.\n"; |
| 187 | llvm::cerr << " " << NumEndif << " #endif.\n"; |
| 188 | llvm::cerr << " " << NumPragma << " #pragma.\n"; |
| 189 | llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 191 | llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 192 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 193 | << NumFastMacroExpanded << " on the fast path.\n"; |
| 194 | llvm::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 195 | << " token paste (##) operations performed, " |
| 196 | << NumFastTokenPaste << " on the fast path.\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | // Token Spelling |
| 201 | //===----------------------------------------------------------------------===// |
| 202 | |
| 203 | |
| 204 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 205 | /// token are the characters used to represent the token in the source file |
| 206 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 207 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 208 | /// UCNs, etc. |
| 209 | std::string Preprocessor::getSpelling(const Token &Tok) const { |
| 210 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 211 | |
| 212 | // If this token contains nothing interesting, return it directly. |
| 213 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 214 | if (!Tok.needsCleaning()) |
| 215 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 216 | |
| 217 | std::string Result; |
| 218 | Result.reserve(Tok.getLength()); |
| 219 | |
| 220 | // Otherwise, hard case, relex the characters into the string. |
| 221 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 222 | Ptr != End; ) { |
| 223 | unsigned CharSize; |
| 224 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 225 | Ptr += CharSize; |
| 226 | } |
| 227 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 228 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 229 | return Result; |
| 230 | } |
| 231 | |
| 232 | /// getSpelling - This method is used to get the spelling of a token into a |
| 233 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 234 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 235 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 236 | /// |
| 237 | /// Note that this method may do two possible things: it may either fill in |
| 238 | /// the buffer specified with characters, or it may *change the input pointer* |
| 239 | /// to point to a constant buffer with the data already in it (avoiding a |
| 240 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 241 | /// if an internal buffer is returned. |
| 242 | unsigned Preprocessor::getSpelling(const Token &Tok, |
| 243 | const char *&Buffer) const { |
| 244 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 245 | |
| 246 | // If this token is an identifier, just return the string from the identifier |
| 247 | // table, which is very quick. |
| 248 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 249 | Buffer = II->getName(); |
| 250 | |
| 251 | // Return the length of the token. If the token needed cleaning, don't |
| 252 | // include the size of the newlines or trigraphs in it. |
| 253 | if (!Tok.needsCleaning()) |
| 254 | return Tok.getLength(); |
| 255 | else |
| 256 | return strlen(Buffer); |
| 257 | } |
| 258 | |
| 259 | // Otherwise, compute the start of the token in the input lexer buffer. |
| 260 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 261 | |
| 262 | // If this token contains nothing interesting, return it directly. |
| 263 | if (!Tok.needsCleaning()) { |
| 264 | Buffer = TokStart; |
| 265 | return Tok.getLength(); |
| 266 | } |
| 267 | // Otherwise, hard case, relex the characters into the string. |
| 268 | char *OutBuf = const_cast<char*>(Buffer); |
| 269 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 270 | Ptr != End; ) { |
| 271 | unsigned CharSize; |
| 272 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 273 | Ptr += CharSize; |
| 274 | } |
| 275 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 276 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 277 | |
| 278 | return OutBuf-Buffer; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 283 | /// location for it. If specified, the source location provides a source |
| 284 | /// location for the token. |
| 285 | SourceLocation Preprocessor:: |
| 286 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 287 | if (SLoc.isValid()) |
| 288 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 289 | return ScratchBuf->getToken(Buf, Len); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 294 | /// token, return a new location that specifies a character within the token. |
| 295 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 296 | unsigned CharNo) { |
| 297 | // If they request the first char of the token, we're trivially done. If this |
| 298 | // is a macro expansion, it doesn't make sense to point to a character within |
| 299 | // the instantiation point (the name). We could point to the source |
| 300 | // character, but without also pointing to instantiation info, this is |
| 301 | // confusing. |
| 302 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
| 303 | |
| 304 | // Figure out how many physical characters away the specified logical |
| 305 | // character is. This needs to take into consideration newlines and |
| 306 | // trigraphs. |
| 307 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 308 | unsigned PhysOffset = 0; |
| 309 | |
| 310 | // The usual case is that tokens don't contain anything interesting. Skip |
| 311 | // over the uninteresting characters. If a token only consists of simple |
| 312 | // chars, this method is extremely fast. |
| 313 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
| 314 | ++TokPtr, --CharNo, ++PhysOffset; |
| 315 | |
| 316 | // If we have a character that may be a trigraph or escaped newline, create a |
| 317 | // lexer to parse it correctly. |
| 318 | if (CharNo != 0) { |
| 319 | // Create a lexer starting at this token position. |
| 320 | Lexer TheLexer(TokStart, *this, TokPtr); |
| 321 | Token Tok; |
| 322 | // Skip over characters the remaining characters. |
| 323 | const char *TokStartPtr = TokPtr; |
| 324 | for (; CharNo; --CharNo) |
| 325 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
| 326 | |
| 327 | PhysOffset += TokPtr-TokStartPtr; |
| 328 | } |
| 329 | |
| 330 | return TokStart.getFileLocWithOffset(PhysOffset); |
| 331 | } |
| 332 | |
| 333 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 334 | //===----------------------------------------------------------------------===// |
| 335 | // Preprocessor Initialization Methods |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | |
| 338 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 339 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 340 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 341 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 342 | const char *Command = "#define ") { |
| 343 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 344 | if (const char *Equal = strchr(Macro, '=')) { |
| 345 | // Turn the = into ' '. |
| 346 | Buf.insert(Buf.end(), Macro, Equal); |
| 347 | Buf.push_back(' '); |
| 348 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 349 | } else { |
| 350 | // Push "macroname 1". |
| 351 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 352 | Buf.push_back(' '); |
| 353 | Buf.push_back('1'); |
| 354 | } |
| 355 | Buf.push_back('\n'); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 360 | std::vector<char> &Buf) { |
| 361 | // FIXME: Implement magic like cpp_init_builtins for things like __STDC__ |
| 362 | // and __DATE__ etc. |
| 363 | #if 0 |
| 364 | /* __STDC__ has the value 1 under normal circumstances. |
| 365 | However, if (a) we are in a system header, (b) the option |
| 366 | stdc_0_in_system_headers is true (set by target config), and |
| 367 | (c) we are not in strictly conforming mode, then it has the |
| 368 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
| 369 | //case BT_STDC: |
| 370 | if (cpp_in_system_header (pfile)) |
| 371 | number = 0; |
| 372 | else |
| 373 | number = 1; |
| 374 | break; |
| 375 | #endif |
| 376 | // These should all be defined in the preprocessor according to the |
| 377 | // current language configuration. |
| 378 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 379 | //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 380 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
| 381 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 382 | else if (0) // STDC94 ? |
| 383 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 384 | |
| 385 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 386 | if (PP.getLangOptions().ObjC1) |
| 387 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 388 | if (PP.getLangOptions().ObjC2) |
| 389 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 77cec47 | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 391 | // Add __builtin_va_list typedef. |
| 392 | { |
| 393 | const char *VAList = PP.getTargetInfo().getVAListDeclaration(); |
| 394 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 395 | Buf.push_back('\n'); |
| 396 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 397 | |
| 398 | // Get the target #defines. |
| 399 | PP.getTargetInfo().getTargetDefines(Buf); |
| 400 | |
| 401 | // Compiler set macros. |
| 402 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5250"); |
Steve Naroff | b5a086e | 2007-11-10 18:06:36 +0000 | [diff] [blame] | 403 | DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050"); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 404 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0"); |
| 405 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 406 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 407 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 408 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. " |
| 409 | "build 5250)\""); |
| 410 | |
| 411 | // Build configuration options. |
| 412 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 413 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 414 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 415 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
| 416 | |
| 417 | |
| 418 | if (PP.getLangOptions().CPlusPlus) { |
| 419 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 420 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 421 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 422 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 423 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 424 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 425 | } |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 426 | if (PP.getLangOptions().Microsoft) { |
| 427 | DefineBuiltinMacro(Buf, "__stdcall="); |
| 428 | DefineBuiltinMacro(Buf, "__cdecl="); |
| 429 | DefineBuiltinMacro(Buf, "_cdecl="); |
| 430 | DefineBuiltinMacro(Buf, "__ptr64="); |
Steve Naroff | be880ec | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 431 | DefineBuiltinMacro(Buf, "__w64="); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 432 | DefineBuiltinMacro(Buf, "__forceinline="); |
Steve Naroff | f9bba13 | 2008-02-07 15:26:07 +0000 | [diff] [blame] | 433 | DefineBuiltinMacro(Buf, "__int8=char"); |
| 434 | DefineBuiltinMacro(Buf, "__int16=short"); |
| 435 | DefineBuiltinMacro(Buf, "__int32=int"); |
Chris Lattner | d1a552b | 2008-02-10 21:12:45 +0000 | [diff] [blame] | 436 | DefineBuiltinMacro(Buf, "__int64=long long"); |
Steve Naroff | cfe7821 | 2008-02-11 22:29:58 +0000 | [diff] [blame] | 437 | DefineBuiltinMacro(Buf, "__declspec(X)="); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 438 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 439 | // FIXME: Should emit a #line directive here. |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 886bf13 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 444 | /// which implicitly adds the builtin defines etc. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 445 | void Preprocessor::EnterMainSourceFile() { |
| 446 | |
| 447 | unsigned MainFileID = SourceMgr.getMainFileID(); |
| 448 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 449 | // Enter the main file source buffer. |
| 450 | EnterSourceFile(MainFileID, 0); |
| 451 | |
Chris Lattner | b45f05c | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 452 | // Tell the header info that the main file was entered. If the file is later |
| 453 | // #imported, it won't be re-entered. |
| 454 | if (const FileEntry *FE = |
| 455 | SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0))) |
| 456 | HeaderInfo.IncrementIncludeCount(FE); |
| 457 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 458 | std::vector<char> PrologFile; |
| 459 | PrologFile.reserve(4080); |
| 460 | |
| 461 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 462 | InitializePredefinedMacros(*this, PrologFile); |
| 463 | |
| 464 | // Add on the predefines from the driver. |
| 465 | PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines)); |
| 466 | |
| 467 | // Memory buffer must end with a null byte! |
| 468 | PrologFile.push_back(0); |
| 469 | |
| 470 | // Now that we have emitted the predefined macros, #includes, etc into |
| 471 | // PrologFile, preprocess it to populate the initial preprocessor state. |
| 472 | llvm::MemoryBuffer *SB = |
| 473 | llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(), |
| 474 | "<predefines>"); |
| 475 | assert(SB && "Cannot fail to create predefined source buffer"); |
| 476 | unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 477 | assert(FileID && "Could not create FileID for predefines?"); |
| 478 | |
| 479 | // Start parsing the predefines. |
| 480 | EnterSourceFile(FileID, 0); |
| 481 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | |
| 483 | //===----------------------------------------------------------------------===// |
| 484 | // Source File Location Methods. |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | |
| 487 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 488 | /// return null on failure. isAngled indicates whether the file reference is |
| 489 | /// for system #include's or not (i.e. using <> instead of ""). |
| 490 | const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, |
| 491 | const char *FilenameEnd, |
| 492 | bool isAngled, |
| 493 | const DirectoryLookup *FromDir, |
| 494 | const DirectoryLookup *&CurDir) { |
| 495 | // If the header lookup mechanism may be relative to the current file, pass in |
| 496 | // info about where the current file is. |
| 497 | const FileEntry *CurFileEnt = 0; |
| 498 | if (!FromDir) { |
| 499 | SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc(); |
| 500 | CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc); |
| 501 | } |
| 502 | |
| 503 | // Do a standard file entry lookup. |
| 504 | CurDir = CurDirLookup; |
| 505 | const FileEntry *FE = |
| 506 | HeaderInfo.LookupFile(FilenameStart, FilenameEnd, |
| 507 | isAngled, FromDir, CurDir, CurFileEnt); |
| 508 | if (FE) return FE; |
| 509 | |
| 510 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 511 | // to one of the headers on the #include stack. Walk the list of the current |
| 512 | // headers on the #include stack and pass them to HeaderInfo. |
| 513 | if (CurLexer && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 017d65b | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 514 | if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))) |
| 515 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 516 | CurFileEnt))) |
| 517 | return FE; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 521 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
| 522 | if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) { |
Chris Lattner | 017d65b | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 523 | if ((CurFileEnt = |
| 524 | SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc()))) |
| 525 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, |
| 526 | FilenameEnd, CurFileEnt))) |
| 527 | return FE; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | // Otherwise, we really couldn't find the file. |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
| 536 | /// #include. |
| 537 | bool Preprocessor::isInPrimaryFile() const { |
| 538 | if (CurLexer && !CurLexer->Is_PragmaLexer) |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 539 | return IncludeMacroStack.empty(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 540 | |
| 541 | // If there are any stacked lexers, we're in a #include. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 542 | assert(IncludeMacroStack[0].TheLexer && |
| 543 | !IncludeMacroStack[0].TheLexer->Is_PragmaLexer && |
| 544 | "Top level include stack isn't our primary lexer?"); |
| 545 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 546 | if (IncludeMacroStack[i].TheLexer && |
| 547 | !IncludeMacroStack[i].TheLexer->Is_PragmaLexer) |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 548 | return false; |
| 549 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 553 | /// that this ignores any potentially active macro expansions and _Pragma |
| 554 | /// expansions going on at the time. |
| 555 | Lexer *Preprocessor::getCurrentFileLexer() const { |
| 556 | if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer; |
| 557 | |
| 558 | // Look for a stacked lexer. |
| 559 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 560 | Lexer *L = IncludeMacroStack[i-1].TheLexer; |
| 561 | if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions. |
| 562 | return L; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 569 | /// start lexing tokens from it instead of the current buffer. Return true |
| 570 | /// on failure. |
| 571 | void Preprocessor::EnterSourceFile(unsigned FileID, |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 572 | const DirectoryLookup *CurDir) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 573 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 574 | ++NumEnteredSourceFiles; |
| 575 | |
| 576 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 577 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 578 | |
| 579 | Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 580 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 581 | } |
| 582 | |
| 583 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 584 | /// start lexing tokens from it instead of the current buffer. |
| 585 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 586 | const DirectoryLookup *CurDir) { |
| 587 | |
| 588 | // Add the current lexer to the include stack. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 589 | if (CurLexer || CurTokenLexer) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 590 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 591 | CurTokenLexer)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 592 | |
| 593 | CurLexer = TheLexer; |
| 594 | CurDirLookup = CurDir; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 595 | CurTokenLexer = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 596 | |
| 597 | // Notify the client, if desired, that we are in a new source file. |
| 598 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
| 599 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 600 | |
| 601 | // Get the file entry for the current file. |
| 602 | if (const FileEntry *FE = |
| 603 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 604 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 605 | |
| 606 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 607 | PPCallbacks::EnterFile, FileType); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | |
| 612 | |
| 613 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 614 | /// tokens from it instead of the current buffer. |
| 615 | void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) { |
| 616 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 617 | CurTokenLexer)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 618 | CurLexer = 0; |
| 619 | CurDirLookup = 0; |
| 620 | |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 621 | if (NumCachedTokenLexers == 0) { |
| 622 | CurTokenLexer = new TokenLexer(Tok, Args, *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 623 | } else { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 624 | CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 625 | CurTokenLexer->Init(Tok, Args); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
| 630 | /// which will cause the lexer to start returning the specified tokens. Note |
| 631 | /// that these tokens will be re-macro-expanded when/if expansion is enabled. |
| 632 | /// This method assumes that the specified stream of tokens has a permanent |
| 633 | /// owner somewhere, so they do not need to be copied. |
| 634 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) { |
| 635 | // Save our current state. |
| 636 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 637 | CurTokenLexer)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 638 | CurLexer = 0; |
| 639 | CurDirLookup = 0; |
| 640 | |
| 641 | // Create a macro expander to expand from the specified token stream. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 642 | if (NumCachedTokenLexers == 0) { |
| 643 | CurTokenLexer = new TokenLexer(Toks, NumToks, *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 644 | } else { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 645 | CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 646 | CurTokenLexer->Init(Toks, NumToks); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 651 | /// lexer stack. This should only be used in situations where the current |
| 652 | /// state of the top-of-stack lexer is known. |
| 653 | void Preprocessor::RemoveTopOfLexerStack() { |
| 654 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
| 655 | |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 656 | if (CurTokenLexer) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 657 | // Delete or cache the now-dead macro expander. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 658 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 659 | delete CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 660 | else |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 661 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 662 | } else { |
| 663 | delete CurLexer; |
| 664 | } |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 665 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 666 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 667 | CurTokenLexer = IncludeMacroStack.back().TheTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 668 | IncludeMacroStack.pop_back(); |
| 669 | } |
| 670 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 671 | |
| 672 | //===----------------------------------------------------------------------===// |
| 673 | // Lexer Event Handling. |
| 674 | //===----------------------------------------------------------------------===// |
| 675 | |
| 676 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 677 | /// identifier information for the token and install it into the token. |
| 678 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
| 679 | const char *BufPtr) { |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 680 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 681 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 682 | |
| 683 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 684 | IdentifierInfo *II; |
| 685 | if (BufPtr && !Identifier.needsCleaning()) { |
| 686 | // No cleaning needed, just use the characters from the lexed buffer. |
| 687 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 688 | } else { |
| 689 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
| 690 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 691 | IdentifierBuffer.resize(Identifier.getLength()); |
| 692 | const char *TmpBuf = &IdentifierBuffer[0]; |
| 693 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 694 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 695 | } |
| 696 | Identifier.setIdentifierInfo(II); |
| 697 | return II; |
| 698 | } |
| 699 | |
| 700 | |
| 701 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 702 | /// identifier. This callback looks up the identifier in the map and/or |
| 703 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 704 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
| 705 | assert(Identifier.getIdentifierInfo() && |
| 706 | "Can't handle identifiers without identifier info!"); |
| 707 | |
| 708 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 709 | |
| 710 | // If this identifier was poisoned, and if it was not produced from a macro |
| 711 | // expansion, emit an error. |
| 712 | if (II.isPoisoned() && CurLexer) { |
| 713 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 714 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 715 | else |
| 716 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 717 | } |
| 718 | |
| 719 | // If this is a macro to be expanded, do it. |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 720 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 721 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 722 | if (MI->isEnabled()) { |
| 723 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 724 | return; |
| 725 | } else { |
| 726 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 727 | // expanded, even if it's in a context where it could be expanded in the |
| 728 | // future. |
| 729 | Identifier.setFlag(Token::DisableExpand); |
| 730 | } |
| 731 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 735 | // then we act as if it is the actual operator and not the textual |
| 736 | // representation of it. |
| 737 | if (II.isCPlusPlusOperatorKeyword()) |
| 738 | Identifier.setIdentifierInfo(0); |
| 739 | |
| 740 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 741 | // turning "for" into a keyword. |
| 742 | Identifier.setKind(II.getTokenID()); |
| 743 | |
| 744 | // If this is an extension token, diagnose its use. |
| 745 | // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99 |
| 746 | // For now, I'm just commenting it out (while I work on attributes). |
| 747 | if (II.isExtensionToken() && Features.C99) |
| 748 | Diag(Identifier, diag::ext_token_used); |
| 749 | } |
| 750 | |
| 751 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 752 | /// the current file. This either returns the EOF token or pops a level off |
| 753 | /// the include stack and keeps going. |
| 754 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 755 | assert(!CurTokenLexer && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 756 | "Ending a file when currently in a macro!"); |
| 757 | |
| 758 | // See if this file had a controlling macro. |
| 759 | if (CurLexer) { // Not ending a macro, ignore it. |
| 760 | if (const IdentifierInfo *ControllingMacro = |
| 761 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
| 762 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 763 | if (const FileEntry *FE = |
| 764 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 765 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // If this is a #include'd file, pop it off the include stack and continue |
| 770 | // lexing the #includer file. |
| 771 | if (!IncludeMacroStack.empty()) { |
| 772 | // We're done with the #included file. |
| 773 | RemoveTopOfLexerStack(); |
| 774 | |
| 775 | // Notify the client, if desired, that we are in a new source file. |
| 776 | if (Callbacks && !isEndOfMacro && CurLexer) { |
| 777 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 778 | |
| 779 | // Get the file entry for the current file. |
| 780 | if (const FileEntry *FE = |
| 781 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 782 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 783 | |
| 784 | Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 785 | PPCallbacks::ExitFile, FileType); |
| 786 | } |
| 787 | |
| 788 | // Client should lex another token. |
| 789 | return false; |
| 790 | } |
Chris Lattner | 1d34a7c | 2008-01-25 00:00:30 +0000 | [diff] [blame] | 791 | |
| 792 | // If the file ends with a newline, form the EOF token on the newline itself, |
| 793 | // rather than "on the line following it", which doesn't exist. This makes |
| 794 | // diagnostics relating to the end of file include the last file that the user |
| 795 | // actually typed, which is goodness. |
| 796 | const char *EndPos = CurLexer->BufferEnd; |
| 797 | if (EndPos != CurLexer->BufferStart && |
| 798 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
| 799 | --EndPos; |
| 800 | |
| 801 | // Handle \n\r and \r\n: |
| 802 | if (EndPos != CurLexer->BufferStart && |
| 803 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 804 | EndPos[-1] != EndPos[0]) |
| 805 | --EndPos; |
| 806 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 807 | |
| 808 | Result.startToken(); |
Chris Lattner | 1d34a7c | 2008-01-25 00:00:30 +0000 | [diff] [blame] | 809 | CurLexer->BufferPtr = EndPos; |
| 810 | CurLexer->FormTokenWithChars(Result, EndPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 811 | Result.setKind(tok::eof); |
| 812 | |
| 813 | // We're done with the #included file. |
| 814 | delete CurLexer; |
| 815 | CurLexer = 0; |
| 816 | |
| 817 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 818 | // diagnostic is enabled, look for macros that have not been used. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 819 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 820 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 821 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 822 | if (!I->second->isUsed()) |
| 823 | Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 826 | return true; |
| 827 | } |
| 828 | |
Chris Lattner | 325c197 | 2008-03-09 03:04:16 +0000 | [diff] [blame] | 829 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 830 | /// hits the end of its token stream. |
| 831 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 832 | assert(CurTokenLexer && !CurLexer && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 833 | "Ending a macro when currently in a #include file!"); |
| 834 | |
| 835 | // Delete or cache the now-dead macro expander. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 836 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 837 | delete CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 838 | else |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 839 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 840 | |
| 841 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 842 | CurTokenLexer = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 843 | return HandleEndOfFile(Result, true); |
| 844 | } |
| 845 | |
Chris Lattner | 64b32ec | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 846 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 847 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 848 | /// state, returning the token on the next source line. |
| 849 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 850 | assert(CurTokenLexer && !CurLexer && |
Chris Lattner | 64b32ec | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 851 | "Pasted comment can only be formed from macro"); |
| 852 | |
| 853 | // We handle this by scanning for the closest real lexer, switching it to |
| 854 | // raw mode and preprocessor mode. This will cause it to return \n as an |
| 855 | // explicit EOM token. |
| 856 | Lexer *FoundLexer = 0; |
| 857 | bool LexerWasInPPMode = false; |
| 858 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 859 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
| 860 | if (ISI.TheLexer == 0) continue; // Scan for a real lexer. |
| 861 | |
| 862 | // Once we find a real lexer, mark it as raw mode (disabling macro |
| 863 | // expansions) and preprocessor mode (return EOM). We know that the lexer |
| 864 | // was *not* in raw mode before, because the macro that the comment came |
| 865 | // from was expanded. However, it could have already been in preprocessor |
| 866 | // mode (#if COMMENT) in which case we have to return it to that mode and |
| 867 | // return EOM. |
| 868 | FoundLexer = ISI.TheLexer; |
| 869 | FoundLexer->LexingRawMode = true; |
| 870 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 871 | FoundLexer->ParsingPreprocessorDirective = true; |
| 872 | break; |
| 873 | } |
| 874 | |
| 875 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 876 | // lexer. In either case, finish off the macro the comment came from, getting |
| 877 | // the next token. |
Chris Lattner | 325c197 | 2008-03-09 03:04:16 +0000 | [diff] [blame] | 878 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
Chris Lattner | 64b32ec | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 879 | |
| 880 | // Discarding comments as long as we don't have EOF or EOM. This 'comments |
| 881 | // out' the rest of the line, including any tokens that came from other macros |
| 882 | // that were active, as in: |
| 883 | // #define submacro a COMMENT b |
| 884 | // submacro c |
| 885 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
| 886 | while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof)) |
| 887 | Lex(Tok); |
| 888 | |
| 889 | // If we got an eom token, then we successfully found the end of the line. |
| 890 | if (Tok.is(tok::eom)) { |
| 891 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 892 | // Restore the lexer back to normal mode instead of raw mode. |
| 893 | FoundLexer->LexingRawMode = false; |
| 894 | |
| 895 | // If the lexer was already in preprocessor mode, just return the EOM token |
| 896 | // to finish the preprocessor line. |
| 897 | if (LexerWasInPPMode) return; |
| 898 | |
| 899 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 900 | FoundLexer->ParsingPreprocessorDirective = false; |
| 901 | return Lex(Tok); |
| 902 | } |
| 903 | |
| 904 | // If we got an EOF token, then we reached the end of the token stream but |
| 905 | // didn't find an explicit \n. This can only happen if there was no lexer |
| 906 | // active (an active lexer would return EOM at EOF if there was no \n in |
| 907 | // preprocessor directive mode), so just return EOF as our token. |
| 908 | assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode"); |
| 909 | return; |
| 910 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 911 | |