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" |
Chris Lattner | 568ec6b | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 29 | #include "MacroArgs.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | #include "clang/Lex/HeaderSearch.h" |
| 31 | #include "clang/Lex/MacroInfo.h" |
| 32 | #include "clang/Lex/PPCallbacks.h" |
| 33 | #include "clang/Lex/Pragma.h" |
| 34 | #include "clang/Lex/ScratchBuffer.h" |
| 35 | #include "clang/Basic/Diagnostic.h" |
| 36 | #include "clang/Basic/FileManager.h" |
| 37 | #include "clang/Basic/SourceManager.h" |
| 38 | #include "clang/Basic/TargetInfo.h" |
| 39 | #include "llvm/ADT/SmallVector.h" |
| 40 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Streams.h" |
Chris Lattner | 1b02318 | 2007-09-03 18:30:32 +0000 | [diff] [blame] | 42 | #include <ctime> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | using namespace clang; |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 48 | TargetInfo &target, SourceManager &SM, |
| 49 | HeaderSearch &Headers) |
| 50 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
| 51 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts), |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 52 | CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 53 | ScratchBuf = new ScratchBuffer(SourceMgr); |
| 54 | |
| 55 | // Clear stats. |
| 56 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 57 | NumIf = NumElse = NumEndif = 0; |
| 58 | NumEnteredSourceFiles = 0; |
| 59 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 60 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 61 | MaxIncludeStackDepth = 0; |
| 62 | NumSkipped = 0; |
| 63 | |
| 64 | // Default to discarding comments. |
| 65 | KeepComments = false; |
| 66 | KeepMacroComments = false; |
| 67 | |
| 68 | // Macro expansion is enabled. |
| 69 | DisableMacroExpansion = false; |
| 70 | InMacroArgs = false; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 71 | NumCachedTokenLexers = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | |
| 73 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 74 | // This gets unpoisoned where it is allowed. |
| 75 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 76 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 77 | Predefines = 0; |
| 78 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 79 | // Initialize the pragma handlers. |
| 80 | PragmaHandlers = new PragmaNamespace(0); |
| 81 | RegisterBuiltinPragmas(); |
| 82 | |
| 83 | // Initialize builtin macros like __LINE__ and friends. |
| 84 | RegisterBuiltinMacros(); |
| 85 | } |
| 86 | |
| 87 | Preprocessor::~Preprocessor() { |
| 88 | // Free any active lexers. |
| 89 | delete CurLexer; |
| 90 | |
| 91 | while (!IncludeMacroStack.empty()) { |
| 92 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 93 | delete IncludeMacroStack.back().TheTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 94 | IncludeMacroStack.pop_back(); |
| 95 | } |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 96 | |
| 97 | // Free any macro definitions. |
| 98 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 99 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 100 | // Free the macro definition. |
| 101 | delete I->second; |
| 102 | I->second = 0; |
| 103 | I->first->setHasMacroDefinition(false); |
| 104 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 105 | |
| 106 | // Free any cached macro expanders. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 107 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 108 | delete TokenLexerCache[i]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 109 | |
| 110 | // Release pragma information. |
| 111 | delete PragmaHandlers; |
| 112 | |
| 113 | // Delete the scratch buffer info. |
| 114 | delete ScratchBuf; |
| 115 | } |
| 116 | |
| 117 | PPCallbacks::~PPCallbacks() { |
| 118 | } |
| 119 | |
| 120 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
| 121 | /// the specified Token's location, translating the token's start |
| 122 | /// position in the current buffer into a SourcePosition object for rendering. |
| 123 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 124 | Diags.Report(getFullLoc(Loc), DiagID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 128 | const std::string &Msg) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 129 | Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 133 | llvm::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 134 | << getSpelling(Tok) << "'"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 135 | |
| 136 | if (!DumpFlags) return; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 138 | llvm::cerr << "\t"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 139 | if (Tok.isAtStartOfLine()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 140 | llvm::cerr << " [StartOfLine]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 141 | if (Tok.hasLeadingSpace()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 142 | llvm::cerr << " [LeadingSpace]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 143 | if (Tok.isExpandDisabled()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 144 | llvm::cerr << " [ExpandDisabled]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 145 | if (Tok.needsCleaning()) { |
| 146 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 147 | llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 148 | << "']"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 151 | llvm::cerr << "\tLoc=<"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 152 | DumpLocation(Tok.getLocation()); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 153 | llvm::cerr << ">"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
| 157 | SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 158 | llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':' |
| 159 | << SourceMgr.getLineNumber(LogLoc) << ':' |
| 160 | << SourceMgr.getLineNumber(LogLoc); |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 161 | |
| 162 | SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc); |
| 163 | if (PhysLoc != LogLoc) { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 164 | llvm::cerr << " <PhysLoc="; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 165 | DumpLocation(PhysLoc); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 166 | llvm::cerr << ">"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 171 | llvm::cerr << "MACRO: "; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 173 | DumpToken(MI.getReplacementToken(i)); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 174 | llvm::cerr << " "; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 175 | } |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 176 | llvm::cerr << "\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void Preprocessor::PrintStats() { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 180 | llvm::cerr << "\n*** Preprocessor Stats:\n"; |
| 181 | llvm::cerr << NumDirectives << " directives found:\n"; |
| 182 | llvm::cerr << " " << NumDefined << " #define.\n"; |
| 183 | llvm::cerr << " " << NumUndefined << " #undef.\n"; |
| 184 | llvm::cerr << " #include/#include_next/#import:\n"; |
| 185 | llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 186 | llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 187 | llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 188 | llvm::cerr << " " << NumElse << " #else/#elif.\n"; |
| 189 | llvm::cerr << " " << NumEndif << " #endif.\n"; |
| 190 | llvm::cerr << " " << NumPragma << " #pragma.\n"; |
| 191 | llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 193 | llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 194 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 195 | << NumFastMacroExpanded << " on the fast path.\n"; |
| 196 | llvm::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 197 | << " token paste (##) operations performed, " |
| 198 | << NumFastTokenPaste << " on the fast path.\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | //===----------------------------------------------------------------------===// |
| 202 | // Token Spelling |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | |
| 205 | |
| 206 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 207 | /// token are the characters used to represent the token in the source file |
| 208 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 209 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 210 | /// UCNs, etc. |
| 211 | std::string Preprocessor::getSpelling(const Token &Tok) const { |
| 212 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 213 | |
| 214 | // If this token contains nothing interesting, return it directly. |
| 215 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 216 | if (!Tok.needsCleaning()) |
| 217 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 218 | |
| 219 | std::string Result; |
| 220 | Result.reserve(Tok.getLength()); |
| 221 | |
| 222 | // Otherwise, hard case, relex the characters into the string. |
| 223 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 224 | Ptr != End; ) { |
| 225 | unsigned CharSize; |
| 226 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 227 | Ptr += CharSize; |
| 228 | } |
| 229 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 230 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 231 | return Result; |
| 232 | } |
| 233 | |
| 234 | /// getSpelling - This method is used to get the spelling of a token into a |
| 235 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 236 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 237 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 238 | /// |
| 239 | /// Note that this method may do two possible things: it may either fill in |
| 240 | /// the buffer specified with characters, or it may *change the input pointer* |
| 241 | /// to point to a constant buffer with the data already in it (avoiding a |
| 242 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 243 | /// if an internal buffer is returned. |
| 244 | unsigned Preprocessor::getSpelling(const Token &Tok, |
| 245 | const char *&Buffer) const { |
| 246 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 247 | |
| 248 | // If this token is an identifier, just return the string from the identifier |
| 249 | // table, which is very quick. |
| 250 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 251 | Buffer = II->getName(); |
| 252 | |
| 253 | // Return the length of the token. If the token needed cleaning, don't |
| 254 | // include the size of the newlines or trigraphs in it. |
| 255 | if (!Tok.needsCleaning()) |
| 256 | return Tok.getLength(); |
| 257 | else |
| 258 | return strlen(Buffer); |
| 259 | } |
| 260 | |
| 261 | // Otherwise, compute the start of the token in the input lexer buffer. |
| 262 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 263 | |
| 264 | // If this token contains nothing interesting, return it directly. |
| 265 | if (!Tok.needsCleaning()) { |
| 266 | Buffer = TokStart; |
| 267 | return Tok.getLength(); |
| 268 | } |
| 269 | // Otherwise, hard case, relex the characters into the string. |
| 270 | char *OutBuf = const_cast<char*>(Buffer); |
| 271 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 272 | Ptr != End; ) { |
| 273 | unsigned CharSize; |
| 274 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 275 | Ptr += CharSize; |
| 276 | } |
| 277 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 278 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 279 | |
| 280 | return OutBuf-Buffer; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 285 | /// location for it. If specified, the source location provides a source |
| 286 | /// location for the token. |
| 287 | SourceLocation Preprocessor:: |
| 288 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 289 | if (SLoc.isValid()) |
| 290 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 291 | return ScratchBuf->getToken(Buf, Len); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 296 | /// token, return a new location that specifies a character within the token. |
| 297 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 298 | unsigned CharNo) { |
| 299 | // If they request the first char of the token, we're trivially done. If this |
| 300 | // is a macro expansion, it doesn't make sense to point to a character within |
| 301 | // the instantiation point (the name). We could point to the source |
| 302 | // character, but without also pointing to instantiation info, this is |
| 303 | // confusing. |
| 304 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
| 305 | |
| 306 | // Figure out how many physical characters away the specified logical |
| 307 | // character is. This needs to take into consideration newlines and |
| 308 | // trigraphs. |
| 309 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 310 | unsigned PhysOffset = 0; |
| 311 | |
| 312 | // The usual case is that tokens don't contain anything interesting. Skip |
| 313 | // over the uninteresting characters. If a token only consists of simple |
| 314 | // chars, this method is extremely fast. |
| 315 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
| 316 | ++TokPtr, --CharNo, ++PhysOffset; |
| 317 | |
| 318 | // If we have a character that may be a trigraph or escaped newline, create a |
| 319 | // lexer to parse it correctly. |
| 320 | if (CharNo != 0) { |
| 321 | // Create a lexer starting at this token position. |
| 322 | Lexer TheLexer(TokStart, *this, TokPtr); |
| 323 | Token Tok; |
| 324 | // Skip over characters the remaining characters. |
| 325 | const char *TokStartPtr = TokPtr; |
| 326 | for (; CharNo; --CharNo) |
| 327 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
| 328 | |
| 329 | PhysOffset += TokPtr-TokStartPtr; |
| 330 | } |
| 331 | |
| 332 | return TokStart.getFileLocWithOffset(PhysOffset); |
| 333 | } |
| 334 | |
| 335 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 336 | //===----------------------------------------------------------------------===// |
| 337 | // Preprocessor Initialization Methods |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | |
| 340 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 341 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 342 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 343 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 344 | const char *Command = "#define ") { |
| 345 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 346 | if (const char *Equal = strchr(Macro, '=')) { |
| 347 | // Turn the = into ' '. |
| 348 | Buf.insert(Buf.end(), Macro, Equal); |
| 349 | Buf.push_back(' '); |
| 350 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 351 | } else { |
| 352 | // Push "macroname 1". |
| 353 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 354 | Buf.push_back(' '); |
| 355 | Buf.push_back('1'); |
| 356 | } |
| 357 | Buf.push_back('\n'); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 362 | std::vector<char> &Buf) { |
| 363 | // FIXME: Implement magic like cpp_init_builtins for things like __STDC__ |
| 364 | // and __DATE__ etc. |
| 365 | #if 0 |
| 366 | /* __STDC__ has the value 1 under normal circumstances. |
| 367 | However, if (a) we are in a system header, (b) the option |
| 368 | stdc_0_in_system_headers is true (set by target config), and |
| 369 | (c) we are not in strictly conforming mode, then it has the |
| 370 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
| 371 | //case BT_STDC: |
| 372 | if (cpp_in_system_header (pfile)) |
| 373 | number = 0; |
| 374 | else |
| 375 | number = 1; |
| 376 | break; |
| 377 | #endif |
| 378 | // These should all be defined in the preprocessor according to the |
| 379 | // current language configuration. |
| 380 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 381 | //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 382 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
| 383 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 384 | else if (0) // STDC94 ? |
| 385 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 386 | |
| 387 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 388 | if (PP.getLangOptions().ObjC1) |
| 389 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 390 | if (PP.getLangOptions().ObjC2) |
| 391 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 77cec47 | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 393 | // Add __builtin_va_list typedef. |
| 394 | { |
| 395 | const char *VAList = PP.getTargetInfo().getVAListDeclaration(); |
| 396 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 397 | Buf.push_back('\n'); |
| 398 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 399 | |
| 400 | // Get the target #defines. |
| 401 | PP.getTargetInfo().getTargetDefines(Buf); |
| 402 | |
| 403 | // Compiler set macros. |
| 404 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5250"); |
Steve Naroff | b5a086e | 2007-11-10 18:06:36 +0000 | [diff] [blame] | 405 | DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1050"); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 406 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0"); |
| 407 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 408 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 409 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 410 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. " |
| 411 | "build 5250)\""); |
| 412 | |
| 413 | // Build configuration options. |
| 414 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 415 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 416 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 417 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
| 418 | |
| 419 | |
| 420 | if (PP.getLangOptions().CPlusPlus) { |
| 421 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 422 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 423 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 424 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 425 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 426 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 427 | } |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 428 | if (PP.getLangOptions().Microsoft) { |
| 429 | DefineBuiltinMacro(Buf, "__stdcall="); |
| 430 | DefineBuiltinMacro(Buf, "__cdecl="); |
| 431 | DefineBuiltinMacro(Buf, "_cdecl="); |
| 432 | DefineBuiltinMacro(Buf, "__ptr64="); |
Steve Naroff | be880ec | 2008-02-07 23:24:32 +0000 | [diff] [blame] | 433 | DefineBuiltinMacro(Buf, "__w64="); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 434 | DefineBuiltinMacro(Buf, "__forceinline="); |
Steve Naroff | f9bba13 | 2008-02-07 15:26:07 +0000 | [diff] [blame] | 435 | DefineBuiltinMacro(Buf, "__int8=char"); |
| 436 | DefineBuiltinMacro(Buf, "__int16=short"); |
| 437 | DefineBuiltinMacro(Buf, "__int32=int"); |
Chris Lattner | d1a552b | 2008-02-10 21:12:45 +0000 | [diff] [blame] | 438 | DefineBuiltinMacro(Buf, "__int64=long long"); |
Steve Naroff | cfe7821 | 2008-02-11 22:29:58 +0000 | [diff] [blame] | 439 | DefineBuiltinMacro(Buf, "__declspec(X)="); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 441 | // FIXME: Should emit a #line directive here. |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 886bf13 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 446 | /// which implicitly adds the builtin defines etc. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 447 | void Preprocessor::EnterMainSourceFile() { |
| 448 | |
| 449 | unsigned MainFileID = SourceMgr.getMainFileID(); |
| 450 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 451 | // Enter the main file source buffer. |
| 452 | EnterSourceFile(MainFileID, 0); |
| 453 | |
Chris Lattner | b45f05c | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 454 | // Tell the header info that the main file was entered. If the file is later |
| 455 | // #imported, it won't be re-entered. |
| 456 | if (const FileEntry *FE = |
| 457 | SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0))) |
| 458 | HeaderInfo.IncrementIncludeCount(FE); |
| 459 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 460 | std::vector<char> PrologFile; |
| 461 | PrologFile.reserve(4080); |
| 462 | |
| 463 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 464 | InitializePredefinedMacros(*this, PrologFile); |
| 465 | |
| 466 | // Add on the predefines from the driver. |
| 467 | PrologFile.insert(PrologFile.end(), Predefines,Predefines+strlen(Predefines)); |
| 468 | |
| 469 | // Memory buffer must end with a null byte! |
| 470 | PrologFile.push_back(0); |
| 471 | |
| 472 | // Now that we have emitted the predefined macros, #includes, etc into |
| 473 | // PrologFile, preprocess it to populate the initial preprocessor state. |
| 474 | llvm::MemoryBuffer *SB = |
| 475 | llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(), |
| 476 | "<predefines>"); |
| 477 | assert(SB && "Cannot fail to create predefined source buffer"); |
| 478 | unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 479 | assert(FileID && "Could not create FileID for predefines?"); |
| 480 | |
| 481 | // Start parsing the predefines. |
| 482 | EnterSourceFile(FileID, 0); |
| 483 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 484 | |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | // Source File Location Methods. |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
| 489 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 490 | /// return null on failure. isAngled indicates whether the file reference is |
| 491 | /// for system #include's or not (i.e. using <> instead of ""). |
| 492 | const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, |
| 493 | const char *FilenameEnd, |
| 494 | bool isAngled, |
| 495 | const DirectoryLookup *FromDir, |
| 496 | const DirectoryLookup *&CurDir) { |
| 497 | // If the header lookup mechanism may be relative to the current file, pass in |
| 498 | // info about where the current file is. |
| 499 | const FileEntry *CurFileEnt = 0; |
| 500 | if (!FromDir) { |
| 501 | SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc(); |
| 502 | CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc); |
| 503 | } |
| 504 | |
| 505 | // Do a standard file entry lookup. |
| 506 | CurDir = CurDirLookup; |
| 507 | const FileEntry *FE = |
| 508 | HeaderInfo.LookupFile(FilenameStart, FilenameEnd, |
| 509 | isAngled, FromDir, CurDir, CurFileEnt); |
| 510 | if (FE) return FE; |
| 511 | |
| 512 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 513 | // to one of the headers on the #include stack. Walk the list of the current |
| 514 | // headers on the #include stack and pass them to HeaderInfo. |
| 515 | if (CurLexer && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 017d65b | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 516 | if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))) |
| 517 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 518 | CurFileEnt))) |
| 519 | return FE; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 523 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
| 524 | if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) { |
Chris Lattner | 017d65b | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 525 | if ((CurFileEnt = |
| 526 | SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc()))) |
| 527 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, |
| 528 | FilenameEnd, CurFileEnt))) |
| 529 | return FE; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | // Otherwise, we really couldn't find the file. |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
| 538 | /// #include. |
| 539 | bool Preprocessor::isInPrimaryFile() const { |
| 540 | if (CurLexer && !CurLexer->Is_PragmaLexer) |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 541 | return IncludeMacroStack.empty(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 542 | |
| 543 | // If there are any stacked lexers, we're in a #include. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 544 | assert(IncludeMacroStack[0].TheLexer && |
| 545 | !IncludeMacroStack[0].TheLexer->Is_PragmaLexer && |
| 546 | "Top level include stack isn't our primary lexer?"); |
| 547 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 548 | if (IncludeMacroStack[i].TheLexer && |
| 549 | !IncludeMacroStack[i].TheLexer->Is_PragmaLexer) |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 550 | return false; |
| 551 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 555 | /// that this ignores any potentially active macro expansions and _Pragma |
| 556 | /// expansions going on at the time. |
| 557 | Lexer *Preprocessor::getCurrentFileLexer() const { |
| 558 | if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer; |
| 559 | |
| 560 | // Look for a stacked lexer. |
| 561 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 562 | Lexer *L = IncludeMacroStack[i-1].TheLexer; |
| 563 | if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions. |
| 564 | return L; |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | |
| 570 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 571 | /// start lexing tokens from it instead of the current buffer. Return true |
| 572 | /// on failure. |
| 573 | void Preprocessor::EnterSourceFile(unsigned FileID, |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 574 | const DirectoryLookup *CurDir) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 575 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 576 | ++NumEnteredSourceFiles; |
| 577 | |
| 578 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 579 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 580 | |
| 581 | Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 582 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 583 | } |
| 584 | |
| 585 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 586 | /// start lexing tokens from it instead of the current buffer. |
| 587 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 588 | const DirectoryLookup *CurDir) { |
| 589 | |
| 590 | // Add the current lexer to the include stack. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 591 | if (CurLexer || CurTokenLexer) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 592 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 593 | CurTokenLexer)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 594 | |
| 595 | CurLexer = TheLexer; |
| 596 | CurDirLookup = CurDir; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 597 | CurTokenLexer = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 598 | |
| 599 | // Notify the client, if desired, that we are in a new source file. |
| 600 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
| 601 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 602 | |
| 603 | // Get the file entry for the current file. |
| 604 | if (const FileEntry *FE = |
| 605 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 606 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 607 | |
| 608 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 609 | PPCallbacks::EnterFile, FileType); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 616 | /// tokens from it instead of the current buffer. |
| 617 | void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) { |
| 618 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 619 | CurTokenLexer)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 620 | CurLexer = 0; |
| 621 | CurDirLookup = 0; |
| 622 | |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 623 | if (NumCachedTokenLexers == 0) { |
| 624 | CurTokenLexer = new TokenLexer(Tok, Args, *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 625 | } else { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 626 | CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 627 | CurTokenLexer->Init(Tok, Args); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
| 632 | /// which will cause the lexer to start returning the specified tokens. Note |
| 633 | /// that these tokens will be re-macro-expanded when/if expansion is enabled. |
| 634 | /// This method assumes that the specified stream of tokens has a permanent |
| 635 | /// owner somewhere, so they do not need to be copied. |
| 636 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) { |
| 637 | // Save our current state. |
| 638 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 639 | CurTokenLexer)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 640 | CurLexer = 0; |
| 641 | CurDirLookup = 0; |
| 642 | |
| 643 | // Create a macro expander to expand from the specified token stream. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 644 | if (NumCachedTokenLexers == 0) { |
| 645 | CurTokenLexer = new TokenLexer(Toks, NumToks, *this); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 646 | } else { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 647 | CurTokenLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 648 | CurTokenLexer->Init(Toks, NumToks); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
| 652 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 653 | /// lexer stack. This should only be used in situations where the current |
| 654 | /// state of the top-of-stack lexer is known. |
| 655 | void Preprocessor::RemoveTopOfLexerStack() { |
| 656 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
| 657 | |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 658 | if (CurTokenLexer) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 659 | // Delete or cache the now-dead macro expander. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 660 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 661 | delete CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 662 | else |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 663 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 664 | } else { |
| 665 | delete CurLexer; |
| 666 | } |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 667 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 668 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 669 | CurTokenLexer = IncludeMacroStack.back().TheTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 670 | IncludeMacroStack.pop_back(); |
| 671 | } |
| 672 | |
| 673 | //===----------------------------------------------------------------------===// |
| 674 | // Macro Expansion Handling. |
| 675 | //===----------------------------------------------------------------------===// |
| 676 | |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 677 | /// setMacroInfo - Specify a macro for this identifier. |
| 678 | /// |
| 679 | void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) { |
| 680 | if (MI == 0) { |
| 681 | if (II->hasMacroDefinition()) { |
| 682 | Macros.erase(II); |
| 683 | II->setHasMacroDefinition(false); |
| 684 | } |
| 685 | } else { |
| 686 | Macros[II] = MI; |
| 687 | II->setHasMacroDefinition(true); |
| 688 | } |
| 689 | } |
| 690 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 691 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 692 | /// table and mark it as a builtin macro to be expanded. |
| 693 | IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) { |
| 694 | // Get the identifier. |
| 695 | IdentifierInfo *Id = getIdentifierInfo(Name); |
| 696 | |
| 697 | // Mark it as being a macro that is builtin. |
| 698 | MacroInfo *MI = new MacroInfo(SourceLocation()); |
| 699 | MI->setIsBuiltinMacro(); |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 700 | setMacroInfo(Id, MI); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 701 | return Id; |
| 702 | } |
| 703 | |
| 704 | |
| 705 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 706 | /// identifier table. |
| 707 | void Preprocessor::RegisterBuiltinMacros() { |
| 708 | Ident__LINE__ = RegisterBuiltinMacro("__LINE__"); |
| 709 | Ident__FILE__ = RegisterBuiltinMacro("__FILE__"); |
| 710 | Ident__DATE__ = RegisterBuiltinMacro("__DATE__"); |
| 711 | Ident__TIME__ = RegisterBuiltinMacro("__TIME__"); |
| 712 | Ident_Pragma = RegisterBuiltinMacro("_Pragma"); |
| 713 | |
| 714 | // GCC Extensions. |
| 715 | Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__"); |
| 716 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__"); |
| 717 | Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__"); |
| 718 | } |
| 719 | |
| 720 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 721 | /// in its expansion, currently expands to that token literally. |
| 722 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 723 | const IdentifierInfo *MacroIdent, |
| 724 | Preprocessor &PP) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 725 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 726 | |
| 727 | // If the token isn't an identifier, it's always literally expanded. |
| 728 | if (II == 0) return true; |
| 729 | |
| 730 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 731 | // expanded so it's not a trivial expansion. |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 732 | if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 733 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 734 | II != MacroIdent) |
| 735 | return false; |
| 736 | |
| 737 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 738 | // it. |
| 739 | if (MI->isObjectLike()) return true; |
| 740 | |
| 741 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 742 | // as long as the identifier is not a macro argument. |
| 743 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 744 | I != E; ++I) |
| 745 | if (*I == II) |
| 746 | return false; // Identifier is a macro argument. |
| 747 | |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | |
| 752 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 753 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 754 | /// method should have no observable side-effect on the lexed tokens. |
| 755 | bool Preprocessor::isNextPPTokenLParen() { |
| 756 | // Do some quick tests for rejection cases. |
| 757 | unsigned Val; |
| 758 | if (CurLexer) |
| 759 | Val = CurLexer->isNextPPTokenLParen(); |
| 760 | else |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 761 | Val = CurTokenLexer->isNextTokenLParen(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 762 | |
| 763 | if (Val == 2) { |
| 764 | // We have run off the end. If it's a source file we don't |
| 765 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 766 | // macro stack. |
| 767 | if (CurLexer) |
| 768 | return false; |
| 769 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 770 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 771 | if (Entry.TheLexer) |
| 772 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
| 773 | else |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 774 | Val = Entry.TheTokenLexer->isNextTokenLParen(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 775 | |
| 776 | if (Val != 2) |
| 777 | break; |
| 778 | |
| 779 | // Ran off the end of a source file? |
| 780 | if (Entry.TheLexer) |
| 781 | return false; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 786 | // have found something that isn't a '(' or we found the end of the |
| 787 | // translation unit. In either case, return false. |
| 788 | if (Val != 1) |
| 789 | return false; |
| 790 | |
| 791 | Token Tok; |
| 792 | LexUnexpandedToken(Tok); |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 793 | assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 794 | return true; |
| 795 | } |
| 796 | |
| 797 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 798 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 799 | bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, |
| 800 | MacroInfo *MI) { |
Chris Lattner | c834ea6 | 2008-01-07 19:50:27 +0000 | [diff] [blame] | 801 | // If this is a macro exapnsion in the "#if !defined(x)" line for the file, |
| 802 | // then the macro could expand to different things in other contexts, we need |
| 803 | // to disable the optimization in this case. |
| 804 | if (CurLexer) CurLexer->MIOpt.ExpandedMacro(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 805 | |
| 806 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 807 | if (MI->isBuiltinMacro()) { |
| 808 | ExpandBuiltinMacro(Identifier); |
| 809 | return false; |
| 810 | } |
| 811 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 812 | /// Args - If this is a function-like macro expansion, this contains, |
| 813 | /// for each macro argument, the list of tokens that were provided to the |
| 814 | /// invocation. |
| 815 | MacroArgs *Args = 0; |
| 816 | |
| 817 | // If this is a function-like macro, read the arguments. |
| 818 | if (MI->isFunctionLike()) { |
| 819 | // C99 6.10.3p10: If the preprocessing token immediately after the the macro |
| 820 | // name isn't a '(', this macro should not be expanded. Otherwise, consume |
| 821 | // it. |
| 822 | if (!isNextPPTokenLParen()) |
| 823 | return true; |
| 824 | |
| 825 | // Remember that we are now parsing the arguments to a macro invocation. |
| 826 | // Preprocessor directives used inside macro arguments are not portable, and |
| 827 | // this enables the warning. |
| 828 | InMacroArgs = true; |
| 829 | Args = ReadFunctionLikeMacroArgs(Identifier, MI); |
| 830 | |
| 831 | // Finished parsing args. |
| 832 | InMacroArgs = false; |
| 833 | |
| 834 | // If there was an error parsing the arguments, bail out. |
| 835 | if (Args == 0) return false; |
| 836 | |
| 837 | ++NumFnMacroExpanded; |
| 838 | } else { |
| 839 | ++NumMacroExpanded; |
| 840 | } |
| 841 | |
| 842 | // Notice that this macro has been used. |
| 843 | MI->setIsUsed(true); |
| 844 | |
| 845 | // If we started lexing a macro, enter the macro expansion body. |
| 846 | |
| 847 | // If this macro expands to no tokens, don't bother to push it onto the |
| 848 | // expansion stack, only to take it right back off. |
| 849 | if (MI->getNumTokens() == 0) { |
| 850 | // No need for arg info. |
| 851 | if (Args) Args->destroy(); |
| 852 | |
| 853 | // Ignore this macro use, just return the next token in the current |
| 854 | // buffer. |
| 855 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 856 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 857 | |
| 858 | Lex(Identifier); |
| 859 | |
| 860 | // If the identifier isn't on some OTHER line, inherit the leading |
| 861 | // whitespace/first-on-a-line property of this token. This handles |
| 862 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 863 | // empty. |
| 864 | if (!Identifier.isAtStartOfLine()) { |
| 865 | if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine); |
| 866 | if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace); |
| 867 | } |
| 868 | ++NumFastMacroExpanded; |
| 869 | return false; |
| 870 | |
| 871 | } else if (MI->getNumTokens() == 1 && |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 872 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), |
| 873 | *this)){ |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 874 | // Otherwise, if this macro expands into a single trivially-expanded |
| 875 | // token: expand it now. This handles common cases like |
| 876 | // "#define VAL 42". |
| 877 | |
| 878 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 879 | // identifier to the expanded token. |
| 880 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 881 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 882 | |
| 883 | // Remember where the token is instantiated. |
| 884 | SourceLocation InstantiateLoc = Identifier.getLocation(); |
| 885 | |
| 886 | // Replace the result token. |
| 887 | Identifier = MI->getReplacementToken(0); |
| 888 | |
| 889 | // Restore the StartOfLine/LeadingSpace markers. |
| 890 | Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); |
| 891 | Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); |
| 892 | |
| 893 | // Update the tokens location to include both its logical and physical |
| 894 | // locations. |
| 895 | SourceLocation Loc = |
| 896 | SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc); |
| 897 | Identifier.setLocation(Loc); |
| 898 | |
| 899 | // If this is #define X X, we must mark the result as unexpandible. |
| 900 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 901 | if (getMacroInfo(NewII) == MI) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 902 | Identifier.setFlag(Token::DisableExpand); |
| 903 | |
| 904 | // Since this is not an identifier token, it can't be macro expanded, so |
| 905 | // we're done. |
| 906 | ++NumFastMacroExpanded; |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | // Start expanding the macro. |
| 911 | EnterMacro(Identifier, Args); |
| 912 | |
| 913 | // Now that the macro is at the top of the include stack, ask the |
| 914 | // preprocessor to read the next token from it. |
| 915 | Lex(Identifier); |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is |
| 920 | /// invoked to read all of the actual arguments specified for the macro |
| 921 | /// invocation. This returns null on error. |
| 922 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, |
| 923 | MacroInfo *MI) { |
| 924 | // The number of fixed arguments to parse. |
| 925 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 926 | bool isVariadic = MI->isVariadic(); |
| 927 | |
| 928 | // Outer loop, while there are more arguments, keep reading them. |
| 929 | Token Tok; |
| 930 | Tok.setKind(tok::comma); |
| 931 | --NumFixedArgsLeft; // Start reading the first arg. |
| 932 | |
| 933 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 934 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 935 | // heap allocations in the common case. |
| 936 | llvm::SmallVector<Token, 64> ArgTokens; |
| 937 | |
| 938 | unsigned NumActuals = 0; |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 939 | while (Tok.is(tok::comma)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 940 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 941 | // that we already consumed the first one. |
| 942 | unsigned NumParens = 0; |
| 943 | |
| 944 | while (1) { |
| 945 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 946 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 947 | LexUnexpandedToken(Tok); |
| 948 | |
Chris Lattner | ecdf4f0 | 2008-01-22 19:34:51 +0000 | [diff] [blame] | 949 | if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 950 | Diag(MacroName, diag::err_unterm_macro_invoc); |
Chris Lattner | ecdf4f0 | 2008-01-22 19:34:51 +0000 | [diff] [blame] | 951 | // Do not lose the EOF/EOM. Return it to the client. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 952 | MacroName = Tok; |
| 953 | return 0; |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 954 | } else if (Tok.is(tok::r_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 955 | // If we found the ) token, the macro arg list is done. |
| 956 | if (NumParens-- == 0) |
| 957 | break; |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 958 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 959 | ++NumParens; |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 960 | } else if (Tok.is(tok::comma) && NumParens == 0) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 961 | // Comma ends this argument if there are more fixed arguments expected. |
| 962 | if (NumFixedArgsLeft) |
| 963 | break; |
| 964 | |
| 965 | // If this is not a variadic macro, too many args were specified. |
| 966 | if (!isVariadic) { |
| 967 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 968 | // Emitting it at the , could be far away from the macro name. |
| 969 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
| 970 | return 0; |
| 971 | } |
| 972 | // Otherwise, continue to add the tokens to this variable argument. |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 973 | } else if (Tok.is(tok::comment) && !KeepMacroComments) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 974 | // If this is a comment token in the argument list and we're just in |
| 975 | // -C mode (not -CC mode), discard the comment. |
| 976 | continue; |
Chris Lattner | e373b05 | 2007-11-23 06:50:21 +0000 | [diff] [blame] | 977 | } else if (Tok.is(tok::identifier)) { |
| 978 | // Reading macro arguments can cause macros that we are currently |
| 979 | // expanding from to be popped off the expansion stack. Doing so causes |
| 980 | // them to be reenabled for expansion. Here we record whether any |
| 981 | // identifiers we lex as macro arguments correspond to disabled macros. |
| 982 | // If so, we mark the token as noexpand. This is a subtle aspect of |
| 983 | // C99 6.10.3.4p2. |
| 984 | if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) |
| 985 | if (!MI->isEnabled()) |
| 986 | Tok.setFlag(Token::DisableExpand); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | ArgTokens.push_back(Tok); |
| 990 | } |
| 991 | |
| 992 | // Empty arguments are standard in C99 and supported as an extension in |
| 993 | // other modes. |
| 994 | if (ArgTokens.empty() && !Features.C99) |
| 995 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
| 996 | |
| 997 | // Add a marker EOF token to the end of the token list for this argument. |
| 998 | Token EOFTok; |
| 999 | EOFTok.startToken(); |
| 1000 | EOFTok.setKind(tok::eof); |
| 1001 | EOFTok.setLocation(Tok.getLocation()); |
| 1002 | EOFTok.setLength(0); |
| 1003 | ArgTokens.push_back(EOFTok); |
| 1004 | ++NumActuals; |
| 1005 | --NumFixedArgsLeft; |
| 1006 | }; |
| 1007 | |
| 1008 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 1009 | // arguments. |
| 1010 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 1011 | |
| 1012 | // See MacroArgs instance var for description of this. |
| 1013 | bool isVarargsElided = false; |
| 1014 | |
| 1015 | if (NumActuals < MinArgsExpected) { |
| 1016 | // There are several cases where too few arguments is ok, handle them now. |
| 1017 | if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) { |
| 1018 | // Varargs where the named vararg parameter is missing: ok as extension. |
| 1019 | // #define A(x, ...) |
| 1020 | // A("blah") |
| 1021 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 1022 | |
| 1023 | // Remember this occurred if this is a C99 macro invocation with at least |
| 1024 | // one actual argument. |
| 1025 | isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1; |
| 1026 | } else if (MI->getNumArgs() == 1) { |
| 1027 | // #define A(x) |
| 1028 | // A() |
| 1029 | // is ok because it is an empty argument. |
| 1030 | |
| 1031 | // Empty arguments are standard in C99 and supported as an extension in |
| 1032 | // other modes. |
| 1033 | if (ArgTokens.empty() && !Features.C99) |
| 1034 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
| 1035 | } else { |
| 1036 | // Otherwise, emit the error. |
| 1037 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | // Add a marker EOF token to the end of the token list for this argument. |
| 1042 | SourceLocation EndLoc = Tok.getLocation(); |
| 1043 | Tok.startToken(); |
| 1044 | Tok.setKind(tok::eof); |
| 1045 | Tok.setLocation(EndLoc); |
| 1046 | Tok.setLength(0); |
| 1047 | ArgTokens.push_back(Tok); |
| 1048 | } |
| 1049 | |
| 1050 | return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided); |
| 1051 | } |
| 1052 | |
| 1053 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 1054 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 1055 | /// the identifier tokens inserted. |
| 1056 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 1057 | Preprocessor &PP) { |
| 1058 | time_t TT = time(0); |
| 1059 | struct tm *TM = localtime(&TT); |
| 1060 | |
| 1061 | static const char * const Months[] = { |
| 1062 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 1063 | }; |
| 1064 | |
| 1065 | char TmpBuffer[100]; |
| 1066 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 1067 | TM->tm_year+1900); |
| 1068 | DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer)); |
| 1069 | |
| 1070 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 1071 | TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer)); |
| 1072 | } |
| 1073 | |
| 1074 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 1075 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 1076 | void Preprocessor::ExpandBuiltinMacro(Token &Tok) { |
| 1077 | // Figure out which token this is. |
| 1078 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1079 | assert(II && "Can't be a macro without id info!"); |
| 1080 | |
| 1081 | // If this is an _Pragma directive, expand it, invoke the pragma handler, then |
| 1082 | // lex the token after it. |
| 1083 | if (II == Ident_Pragma) |
| 1084 | return Handle_Pragma(Tok); |
| 1085 | |
| 1086 | ++NumBuiltinMacroExpanded; |
| 1087 | |
| 1088 | char TmpBuffer[100]; |
| 1089 | |
| 1090 | // Set up the return result. |
| 1091 | Tok.setIdentifierInfo(0); |
| 1092 | Tok.clearFlag(Token::NeedsCleaning); |
| 1093 | |
| 1094 | if (II == Ident__LINE__) { |
| 1095 | // __LINE__ expands to a simple numeric value. |
| 1096 | sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation())); |
| 1097 | unsigned Length = strlen(TmpBuffer); |
| 1098 | Tok.setKind(tok::numeric_constant); |
| 1099 | Tok.setLength(Length); |
| 1100 | Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); |
| 1101 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
| 1102 | SourceLocation Loc = Tok.getLocation(); |
| 1103 | if (II == Ident__BASE_FILE__) { |
| 1104 | Diag(Tok, diag::ext_pp_base_file); |
| 1105 | SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc); |
| 1106 | while (NextLoc.isValid()) { |
| 1107 | Loc = NextLoc; |
| 1108 | NextLoc = SourceMgr.getIncludeLoc(Loc); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
| 1113 | std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc)); |
| 1114 | FN = '"' + Lexer::Stringify(FN) + '"'; |
| 1115 | Tok.setKind(tok::string_literal); |
| 1116 | Tok.setLength(FN.size()); |
| 1117 | Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation())); |
| 1118 | } else if (II == Ident__DATE__) { |
| 1119 | if (!DATELoc.isValid()) |
| 1120 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 1121 | Tok.setKind(tok::string_literal); |
| 1122 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
| 1123 | Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation())); |
| 1124 | } else if (II == Ident__TIME__) { |
| 1125 | if (!TIMELoc.isValid()) |
| 1126 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 1127 | Tok.setKind(tok::string_literal); |
| 1128 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
| 1129 | Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation())); |
| 1130 | } else if (II == Ident__INCLUDE_LEVEL__) { |
| 1131 | Diag(Tok, diag::ext_pp_include_level); |
| 1132 | |
| 1133 | // Compute the include depth of this token. |
| 1134 | unsigned Depth = 0; |
| 1135 | SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation()); |
| 1136 | for (; Loc.isValid(); ++Depth) |
| 1137 | Loc = SourceMgr.getIncludeLoc(Loc); |
| 1138 | |
| 1139 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 1140 | sprintf(TmpBuffer, "%u", Depth); |
| 1141 | unsigned Length = strlen(TmpBuffer); |
| 1142 | Tok.setKind(tok::numeric_constant); |
| 1143 | Tok.setLength(Length); |
| 1144 | Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); |
| 1145 | } else if (II == Ident__TIMESTAMP__) { |
| 1146 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 1147 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 1148 | Diag(Tok, diag::ext_pp_timestamp); |
| 1149 | |
| 1150 | // Get the file that we are lexing out of. If we're currently lexing from |
| 1151 | // a macro, dig into the include stack. |
| 1152 | const FileEntry *CurFile = 0; |
| 1153 | Lexer *TheLexer = getCurrentFileLexer(); |
| 1154 | |
| 1155 | if (TheLexer) |
| 1156 | CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc()); |
| 1157 | |
| 1158 | // If this file is older than the file it depends on, emit a diagnostic. |
| 1159 | const char *Result; |
| 1160 | if (CurFile) { |
| 1161 | time_t TT = CurFile->getModificationTime(); |
| 1162 | struct tm *TM = localtime(&TT); |
| 1163 | Result = asctime(TM); |
| 1164 | } else { |
| 1165 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 1166 | } |
| 1167 | TmpBuffer[0] = '"'; |
| 1168 | strcpy(TmpBuffer+1, Result); |
| 1169 | unsigned Len = strlen(TmpBuffer); |
| 1170 | TmpBuffer[Len-1] = '"'; // Replace the newline with a quote. |
| 1171 | Tok.setKind(tok::string_literal); |
| 1172 | Tok.setLength(Len); |
| 1173 | Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation())); |
| 1174 | } else { |
| 1175 | assert(0 && "Unknown identifier!"); |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 1176 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | //===----------------------------------------------------------------------===// |
| 1180 | // Lexer Event Handling. |
| 1181 | //===----------------------------------------------------------------------===// |
| 1182 | |
| 1183 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 1184 | /// identifier information for the token and install it into the token. |
| 1185 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
| 1186 | const char *BufPtr) { |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 1187 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1188 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 1189 | |
| 1190 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 1191 | IdentifierInfo *II; |
| 1192 | if (BufPtr && !Identifier.needsCleaning()) { |
| 1193 | // No cleaning needed, just use the characters from the lexed buffer. |
| 1194 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 1195 | } else { |
| 1196 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
| 1197 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 1198 | IdentifierBuffer.resize(Identifier.getLength()); |
| 1199 | const char *TmpBuf = &IdentifierBuffer[0]; |
| 1200 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 1201 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 1202 | } |
| 1203 | Identifier.setIdentifierInfo(II); |
| 1204 | return II; |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 1209 | /// identifier. This callback looks up the identifier in the map and/or |
| 1210 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 1211 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
| 1212 | assert(Identifier.getIdentifierInfo() && |
| 1213 | "Can't handle identifiers without identifier info!"); |
| 1214 | |
| 1215 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 1216 | |
| 1217 | // If this identifier was poisoned, and if it was not produced from a macro |
| 1218 | // expansion, emit an error. |
| 1219 | if (II.isPoisoned() && CurLexer) { |
| 1220 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 1221 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 1222 | else |
| 1223 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 1224 | } |
| 1225 | |
| 1226 | // If this is a macro to be expanded, do it. |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 1227 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1228 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 1229 | if (MI->isEnabled()) { |
| 1230 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 1231 | return; |
| 1232 | } else { |
| 1233 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 1234 | // expanded, even if it's in a context where it could be expanded in the |
| 1235 | // future. |
| 1236 | Identifier.setFlag(Token::DisableExpand); |
| 1237 | } |
| 1238 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 1242 | // then we act as if it is the actual operator and not the textual |
| 1243 | // representation of it. |
| 1244 | if (II.isCPlusPlusOperatorKeyword()) |
| 1245 | Identifier.setIdentifierInfo(0); |
| 1246 | |
| 1247 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 1248 | // turning "for" into a keyword. |
| 1249 | Identifier.setKind(II.getTokenID()); |
| 1250 | |
| 1251 | // If this is an extension token, diagnose its use. |
| 1252 | // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99 |
| 1253 | // For now, I'm just commenting it out (while I work on attributes). |
| 1254 | if (II.isExtensionToken() && Features.C99) |
| 1255 | Diag(Identifier, diag::ext_token_used); |
| 1256 | } |
| 1257 | |
| 1258 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 1259 | /// the current file. This either returns the EOF token or pops a level off |
| 1260 | /// the include stack and keeps going. |
| 1261 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 1262 | assert(!CurTokenLexer && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1263 | "Ending a file when currently in a macro!"); |
| 1264 | |
| 1265 | // See if this file had a controlling macro. |
| 1266 | if (CurLexer) { // Not ending a macro, ignore it. |
| 1267 | if (const IdentifierInfo *ControllingMacro = |
| 1268 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
| 1269 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 1270 | if (const FileEntry *FE = |
| 1271 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 1272 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | // If this is a #include'd file, pop it off the include stack and continue |
| 1277 | // lexing the #includer file. |
| 1278 | if (!IncludeMacroStack.empty()) { |
| 1279 | // We're done with the #included file. |
| 1280 | RemoveTopOfLexerStack(); |
| 1281 | |
| 1282 | // Notify the client, if desired, that we are in a new source file. |
| 1283 | if (Callbacks && !isEndOfMacro && CurLexer) { |
| 1284 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 1285 | |
| 1286 | // Get the file entry for the current file. |
| 1287 | if (const FileEntry *FE = |
| 1288 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
| 1289 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 1290 | |
| 1291 | Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 1292 | PPCallbacks::ExitFile, FileType); |
| 1293 | } |
| 1294 | |
| 1295 | // Client should lex another token. |
| 1296 | return false; |
| 1297 | } |
Chris Lattner | 1d34a7c | 2008-01-25 00:00:30 +0000 | [diff] [blame] | 1298 | |
| 1299 | // If the file ends with a newline, form the EOF token on the newline itself, |
| 1300 | // rather than "on the line following it", which doesn't exist. This makes |
| 1301 | // diagnostics relating to the end of file include the last file that the user |
| 1302 | // actually typed, which is goodness. |
| 1303 | const char *EndPos = CurLexer->BufferEnd; |
| 1304 | if (EndPos != CurLexer->BufferStart && |
| 1305 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
| 1306 | --EndPos; |
| 1307 | |
| 1308 | // Handle \n\r and \r\n: |
| 1309 | if (EndPos != CurLexer->BufferStart && |
| 1310 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 1311 | EndPos[-1] != EndPos[0]) |
| 1312 | --EndPos; |
| 1313 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1314 | |
| 1315 | Result.startToken(); |
Chris Lattner | 1d34a7c | 2008-01-25 00:00:30 +0000 | [diff] [blame] | 1316 | CurLexer->BufferPtr = EndPos; |
| 1317 | CurLexer->FormTokenWithChars(Result, EndPos); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1318 | Result.setKind(tok::eof); |
| 1319 | |
| 1320 | // We're done with the #included file. |
| 1321 | delete CurLexer; |
| 1322 | CurLexer = 0; |
| 1323 | |
| 1324 | // 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] | 1325 | // diagnostic is enabled, look for macros that have not been used. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1326 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 1327 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 1328 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 1329 | if (!I->second->isUsed()) |
| 1330 | Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1333 | return true; |
| 1334 | } |
| 1335 | |
Chris Lattner | 325c197 | 2008-03-09 03:04:16 +0000 | [diff] [blame] | 1336 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 1337 | /// hits the end of its token stream. |
| 1338 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 1339 | assert(CurTokenLexer && !CurLexer && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1340 | "Ending a macro when currently in a #include file!"); |
| 1341 | |
| 1342 | // Delete or cache the now-dead macro expander. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 1343 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 1344 | delete CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1345 | else |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 1346 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1347 | |
| 1348 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 1349 | CurTokenLexer = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1350 | return HandleEndOfFile(Result, true); |
| 1351 | } |
| 1352 | |
Chris Lattner | 64b32ec | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 1353 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 1354 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 1355 | /// state, returning the token on the next source line. |
| 1356 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 1357 | assert(CurTokenLexer && !CurLexer && |
Chris Lattner | 64b32ec | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 1358 | "Pasted comment can only be formed from macro"); |
| 1359 | |
| 1360 | // We handle this by scanning for the closest real lexer, switching it to |
| 1361 | // raw mode and preprocessor mode. This will cause it to return \n as an |
| 1362 | // explicit EOM token. |
| 1363 | Lexer *FoundLexer = 0; |
| 1364 | bool LexerWasInPPMode = false; |
| 1365 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 1366 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
| 1367 | if (ISI.TheLexer == 0) continue; // Scan for a real lexer. |
| 1368 | |
| 1369 | // Once we find a real lexer, mark it as raw mode (disabling macro |
| 1370 | // expansions) and preprocessor mode (return EOM). We know that the lexer |
| 1371 | // was *not* in raw mode before, because the macro that the comment came |
| 1372 | // from was expanded. However, it could have already been in preprocessor |
| 1373 | // mode (#if COMMENT) in which case we have to return it to that mode and |
| 1374 | // return EOM. |
| 1375 | FoundLexer = ISI.TheLexer; |
| 1376 | FoundLexer->LexingRawMode = true; |
| 1377 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 1378 | FoundLexer->ParsingPreprocessorDirective = true; |
| 1379 | break; |
| 1380 | } |
| 1381 | |
| 1382 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 1383 | // lexer. In either case, finish off the macro the comment came from, getting |
| 1384 | // the next token. |
Chris Lattner | 325c197 | 2008-03-09 03:04:16 +0000 | [diff] [blame] | 1385 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
Chris Lattner | 64b32ec | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Discarding comments as long as we don't have EOF or EOM. This 'comments |
| 1388 | // out' the rest of the line, including any tokens that came from other macros |
| 1389 | // that were active, as in: |
| 1390 | // #define submacro a COMMENT b |
| 1391 | // submacro c |
| 1392 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
| 1393 | while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof)) |
| 1394 | Lex(Tok); |
| 1395 | |
| 1396 | // If we got an eom token, then we successfully found the end of the line. |
| 1397 | if (Tok.is(tok::eom)) { |
| 1398 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 1399 | // Restore the lexer back to normal mode instead of raw mode. |
| 1400 | FoundLexer->LexingRawMode = false; |
| 1401 | |
| 1402 | // If the lexer was already in preprocessor mode, just return the EOM token |
| 1403 | // to finish the preprocessor line. |
| 1404 | if (LexerWasInPPMode) return; |
| 1405 | |
| 1406 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 1407 | FoundLexer->ParsingPreprocessorDirective = false; |
| 1408 | return Lex(Tok); |
| 1409 | } |
| 1410 | |
| 1411 | // If we got an EOF token, then we reached the end of the token stream but |
| 1412 | // didn't find an explicit \n. This can only happen if there was no lexer |
| 1413 | // active (an active lexer would return EOM at EOF if there was no \n in |
| 1414 | // preprocessor directive mode), so just return EOF as our token. |
| 1415 | assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode"); |
| 1416 | return; |
| 1417 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1418 | |