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" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Pragma.h" |
| 32 | #include "clang/Lex/ScratchBuffer.h" |
| 33 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceManager.h" |
| 35 | #include "clang/Basic/TargetInfo.h" |
| 36 | #include "llvm/ADT/SmallVector.h" |
| 37 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Streams.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Ted Kremenek | 5ab36b0 | 2008-04-17 21:23:07 +0000 | [diff] [blame] | 43 | PreprocessorFactory::~PreprocessorFactory() {} |
| 44 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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 | |
Argiris Kirtzidis | f55b110 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 71 | CacheTokens = false; |
| 72 | CachedLexPos = 0; |
| 73 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 74 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 75 | // This gets unpoisoned where it is allowed. |
| 76 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 77 | |
| 78 | // Initialize the pragma handlers. |
| 79 | PragmaHandlers = new PragmaNamespace(0); |
| 80 | RegisterBuiltinPragmas(); |
| 81 | |
| 82 | // Initialize builtin macros like __LINE__ and friends. |
| 83 | RegisterBuiltinMacros(); |
| 84 | } |
| 85 | |
| 86 | Preprocessor::~Preprocessor() { |
Argiris Kirtzidis | 1370cf1 | 2008-08-23 12:12:06 +0000 | [diff] [blame] | 87 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 88 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | // Free any active lexers. |
| 90 | delete CurLexer; |
| 91 | |
| 92 | while (!IncludeMacroStack.empty()) { |
| 93 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 94 | delete IncludeMacroStack.back().TheTokenLexer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 95 | IncludeMacroStack.pop_back(); |
| 96 | } |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 97 | |
| 98 | // Free any macro definitions. |
| 99 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 100 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 101 | // Free the macro definition. |
| 102 | delete I->second; |
| 103 | I->second = 0; |
| 104 | I->first->setHasMacroDefinition(false); |
| 105 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 106 | |
| 107 | // Free any cached macro expanders. |
Chris Lattner | 5b54ed9 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 108 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 109 | delete TokenLexerCache[i]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 110 | |
| 111 | // Release pragma information. |
| 112 | delete PragmaHandlers; |
| 113 | |
| 114 | // Delete the scratch buffer info. |
| 115 | delete ScratchBuf; |
Chris Lattner | 6582981 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 116 | |
| 117 | delete Callbacks; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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 | |
Chris Lattner | bef45c5 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 132 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 133 | const std::string &Msg, |
| 134 | const SourceRange &R1, const SourceRange &R2) { |
| 135 | SourceRange R[] = {R1, R2}; |
| 136 | Diags.Report(getFullLoc(Loc), DiagID, &Msg, 1, R, 2); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 141 | const SourceRange &R) { |
| 142 | Diags.Report(getFullLoc(Loc), DiagID, 0, 0, &R, 1); |
| 143 | } |
| 144 | |
| 145 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 146 | const SourceRange &R1, const SourceRange &R2) { |
| 147 | SourceRange R[] = {R1, R2}; |
| 148 | Diags.Report(getFullLoc(Loc), DiagID, 0, 0, R, 2); |
| 149 | } |
| 150 | |
| 151 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 152 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 153 | llvm::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 154 | << getSpelling(Tok) << "'"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 155 | |
| 156 | if (!DumpFlags) return; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 158 | llvm::cerr << "\t"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 159 | if (Tok.isAtStartOfLine()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 160 | llvm::cerr << " [StartOfLine]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | if (Tok.hasLeadingSpace()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 162 | llvm::cerr << " [LeadingSpace]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | if (Tok.isExpandDisabled()) |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 164 | llvm::cerr << " [ExpandDisabled]"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 165 | if (Tok.needsCleaning()) { |
| 166 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 167 | llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 168 | << "']"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 171 | llvm::cerr << "\tLoc=<"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 172 | DumpLocation(Tok.getLocation()); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 173 | llvm::cerr << ">"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
| 177 | SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 178 | llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':' |
| 179 | << SourceMgr.getLineNumber(LogLoc) << ':' |
Ted Kremenek | 7988274 | 2008-07-19 19:10:04 +0000 | [diff] [blame] | 180 | << SourceMgr.getColumnNumber(LogLoc); |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 181 | |
| 182 | SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc); |
| 183 | if (PhysLoc != LogLoc) { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 184 | llvm::cerr << " <PhysLoc="; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 185 | DumpLocation(PhysLoc); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 186 | llvm::cerr << ">"; |
Chris Lattner | c0f7c51 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 191 | llvm::cerr << "MACRO: "; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 192 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 193 | DumpToken(MI.getReplacementToken(i)); |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 194 | llvm::cerr << " "; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 195 | } |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 196 | llvm::cerr << "\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void Preprocessor::PrintStats() { |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 200 | llvm::cerr << "\n*** Preprocessor Stats:\n"; |
| 201 | llvm::cerr << NumDirectives << " directives found:\n"; |
| 202 | llvm::cerr << " " << NumDefined << " #define.\n"; |
| 203 | llvm::cerr << " " << NumUndefined << " #undef.\n"; |
| 204 | llvm::cerr << " #include/#include_next/#import:\n"; |
| 205 | llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 206 | llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 207 | llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 208 | llvm::cerr << " " << NumElse << " #else/#elif.\n"; |
| 209 | llvm::cerr << " " << NumEndif << " #endif.\n"; |
| 210 | llvm::cerr << " " << NumPragma << " #pragma.\n"; |
| 211 | llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | ce4c64e | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 213 | llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 214 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 215 | << NumFastMacroExpanded << " on the fast path.\n"; |
| 216 | llvm::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 217 | << " token paste (##) operations performed, " |
| 218 | << NumFastTokenPaste << " on the fast path.\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | // Token Spelling |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | |
| 225 | |
| 226 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 227 | /// token are the characters used to represent the token in the source file |
| 228 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 229 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 230 | /// UCNs, etc. |
| 231 | std::string Preprocessor::getSpelling(const Token &Tok) const { |
| 232 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 233 | |
| 234 | // If this token contains nothing interesting, return it directly. |
| 235 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 236 | if (!Tok.needsCleaning()) |
| 237 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 238 | |
| 239 | std::string Result; |
| 240 | Result.reserve(Tok.getLength()); |
| 241 | |
| 242 | // Otherwise, hard case, relex the characters into the string. |
| 243 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 244 | Ptr != End; ) { |
| 245 | unsigned CharSize; |
| 246 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 247 | Ptr += CharSize; |
| 248 | } |
| 249 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 250 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 251 | return Result; |
| 252 | } |
| 253 | |
| 254 | /// getSpelling - This method is used to get the spelling of a token into a |
| 255 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 256 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 257 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 258 | /// |
| 259 | /// Note that this method may do two possible things: it may either fill in |
| 260 | /// the buffer specified with characters, or it may *change the input pointer* |
| 261 | /// to point to a constant buffer with the data already in it (avoiding a |
| 262 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 263 | /// if an internal buffer is returned. |
| 264 | unsigned Preprocessor::getSpelling(const Token &Tok, |
| 265 | const char *&Buffer) const { |
| 266 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 267 | |
| 268 | // If this token is an identifier, just return the string from the identifier |
| 269 | // table, which is very quick. |
| 270 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 271 | Buffer = II->getName(); |
| 272 | |
| 273 | // Return the length of the token. If the token needed cleaning, don't |
| 274 | // include the size of the newlines or trigraphs in it. |
| 275 | if (!Tok.needsCleaning()) |
| 276 | return Tok.getLength(); |
| 277 | else |
| 278 | return strlen(Buffer); |
| 279 | } |
| 280 | |
| 281 | // Otherwise, compute the start of the token in the input lexer buffer. |
| 282 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 283 | |
| 284 | // If this token contains nothing interesting, return it directly. |
| 285 | if (!Tok.needsCleaning()) { |
| 286 | Buffer = TokStart; |
| 287 | return Tok.getLength(); |
| 288 | } |
| 289 | // Otherwise, hard case, relex the characters into the string. |
| 290 | char *OutBuf = const_cast<char*>(Buffer); |
| 291 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 292 | Ptr != End; ) { |
| 293 | unsigned CharSize; |
| 294 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 295 | Ptr += CharSize; |
| 296 | } |
| 297 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 298 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 299 | |
| 300 | return OutBuf-Buffer; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 305 | /// location for it. If specified, the source location provides a source |
| 306 | /// location for the token. |
| 307 | SourceLocation Preprocessor:: |
| 308 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 309 | if (SLoc.isValid()) |
| 310 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 311 | return ScratchBuf->getToken(Buf, Len); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 316 | /// token, return a new location that specifies a character within the token. |
| 317 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 318 | unsigned CharNo) { |
| 319 | // If they request the first char of the token, we're trivially done. If this |
| 320 | // is a macro expansion, it doesn't make sense to point to a character within |
| 321 | // the instantiation point (the name). We could point to the source |
| 322 | // character, but without also pointing to instantiation info, this is |
| 323 | // confusing. |
| 324 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
| 325 | |
| 326 | // Figure out how many physical characters away the specified logical |
| 327 | // character is. This needs to take into consideration newlines and |
| 328 | // trigraphs. |
| 329 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 330 | unsigned PhysOffset = 0; |
| 331 | |
| 332 | // The usual case is that tokens don't contain anything interesting. Skip |
| 333 | // over the uninteresting characters. If a token only consists of simple |
| 334 | // chars, this method is extremely fast. |
| 335 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
| 336 | ++TokPtr, --CharNo, ++PhysOffset; |
| 337 | |
| 338 | // If we have a character that may be a trigraph or escaped newline, create a |
| 339 | // lexer to parse it correctly. |
| 340 | if (CharNo != 0) { |
| 341 | // Create a lexer starting at this token position. |
| 342 | Lexer TheLexer(TokStart, *this, TokPtr); |
| 343 | Token Tok; |
| 344 | // Skip over characters the remaining characters. |
| 345 | const char *TokStartPtr = TokPtr; |
| 346 | for (; CharNo; --CharNo) |
| 347 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
| 348 | |
| 349 | PhysOffset += TokPtr-TokStartPtr; |
| 350 | } |
| 351 | |
| 352 | return TokStart.getFileLocWithOffset(PhysOffset); |
| 353 | } |
| 354 | |
| 355 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 356 | //===----------------------------------------------------------------------===// |
| 357 | // Preprocessor Initialization Methods |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | |
| 360 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 361 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 362 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 363 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 364 | const char *Command = "#define ") { |
| 365 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 366 | if (const char *Equal = strchr(Macro, '=')) { |
| 367 | // Turn the = into ' '. |
| 368 | Buf.insert(Buf.end(), Macro, Equal); |
| 369 | Buf.push_back(' '); |
| 370 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 371 | } else { |
| 372 | // Push "macroname 1". |
| 373 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 374 | Buf.push_back(' '); |
| 375 | Buf.push_back('1'); |
| 376 | } |
| 377 | Buf.push_back('\n'); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 382 | std::vector<char> &Buf) { |
Chris Lattner | a023ec5 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 383 | // Compiler version introspection macros. |
| 384 | DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend |
| 385 | DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend |
| 386 | |
| 387 | // Currently claim to be compatible with GCC 4.2.1-5621. |
| 388 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5621"); |
| 389 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); |
| 390 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 391 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 392 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 393 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 (Apple Computer, Inc. " |
| 394 | "build 5621) (dot 3)\""); |
| 395 | |
| 396 | |
| 397 | // Initialize language-specific preprocessor defines. |
| 398 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 399 | // FIXME: Implement magic like cpp_init_builtins for things like __STDC__ |
| 400 | // and __DATE__ etc. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 401 | // These should all be defined in the preprocessor according to the |
| 402 | // current language configuration. |
| 403 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 404 | //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 405 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
| 406 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 407 | else if (0) // STDC94 ? |
| 408 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 409 | |
| 410 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
Daniel Dunbar | 428e676 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 411 | if (PP.getLangOptions().ObjC1) { |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 412 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
Daniel Dunbar | 428e676 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 413 | |
| 414 | if (PP.getLangOptions().getGCMode() == LangOptions::NonGC) { |
| 415 | DefineBuiltinMacro(Buf, "__weak="); |
| 416 | DefineBuiltinMacro(Buf, "__strong="); |
| 417 | } else { |
| 418 | DefineBuiltinMacro(Buf, "__weak=__attribute__((objc_gc(weak)))"); |
| 419 | DefineBuiltinMacro(Buf, "__strong=__attribute__((objc_gc(strong)))"); |
| 420 | DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); |
| 421 | } |
| 422 | |
| 423 | if (PP.getLangOptions().NeXTRuntime) |
| 424 | DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); |
| 425 | |
| 426 | // darwin_constant_cfstrings controls this. This is also dependent |
| 427 | // on other things like the runtime I believe. |
| 428 | DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); |
| 429 | } |
Chris Lattner | 5edfe01 | 2008-10-05 19:44:25 +0000 | [diff] [blame^] | 430 | |
Steve Naroff | b3cd9ac | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 431 | if (PP.getLangOptions().ObjC2) |
| 432 | DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 9b96b15 | 2008-09-30 00:48:48 +0000 | [diff] [blame] | 434 | if (PP.getLangOptions().PascalStrings) |
| 435 | DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); |
| 436 | |
Chris Lattner | a023ec5 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 437 | if (PP.getLangOptions().Blocks) { |
| 438 | DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); |
| 439 | DefineBuiltinMacro(Buf, "__BLOCKS__=1"); |
Chris Lattner | ce296f8 | 2008-10-05 19:32:52 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | a023ec5 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 441 | |
| 442 | if (PP.getLangOptions().CPlusPlus) { |
| 443 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 444 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 445 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 446 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 447 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 448 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 449 | } |
| 450 | |
| 451 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 452 | // mode. |
| 453 | if (PP.getLangOptions().Microsoft) { |
| 454 | DefineBuiltinMacro(Buf, "__stdcall="); |
| 455 | DefineBuiltinMacro(Buf, "__cdecl="); |
| 456 | DefineBuiltinMacro(Buf, "_cdecl="); |
| 457 | DefineBuiltinMacro(Buf, "__ptr64="); |
| 458 | DefineBuiltinMacro(Buf, "__w64="); |
| 459 | DefineBuiltinMacro(Buf, "__forceinline="); |
| 460 | DefineBuiltinMacro(Buf, "__int8=char"); |
| 461 | DefineBuiltinMacro(Buf, "__int16=short"); |
| 462 | DefineBuiltinMacro(Buf, "__int32=int"); |
| 463 | DefineBuiltinMacro(Buf, "__int64=long long"); |
| 464 | DefineBuiltinMacro(Buf, "__declspec(X)="); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | // Initialize target-specific preprocessor defines. |
Chris Lattner | 5edfe01 | 2008-10-05 19:44:25 +0000 | [diff] [blame^] | 469 | const TargetInfo &TI = PP.getTargetInfo(); |
| 470 | |
| 471 | // Define type sizing macros based on the target properties. |
| 472 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
| 473 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
| 474 | DefineBuiltinMacro(Buf, "__SCHAR_MAX__=127"); |
| 475 | |
| 476 | assert(TI.getShortWidth() == 16 && "Only support 16-bit short so far"); |
| 477 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
| 478 | DefineBuiltinMacro(Buf, "__SHRT_MAX__=32767"); |
| 479 | |
| 480 | |
| 481 | assert(TI.getLongLongWidth() == 64 && "Only support 64-bit long long so far"); |
| 482 | DefineBuiltinMacro(Buf, "__LONG_LONG_MAX__=9223372036854775807LL"); |
| 483 | |
Chris Lattner | 9b96b15 | 2008-09-30 00:48:48 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 77cec47 | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 485 | // Add __builtin_va_list typedef. |
| 486 | { |
Chris Lattner | 5edfe01 | 2008-10-05 19:44:25 +0000 | [diff] [blame^] | 487 | const char *VAList = TI.getVAListDeclaration(); |
Chris Lattner | 77cec47 | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 488 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 489 | Buf.push_back('\n'); |
| 490 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 5edfe01 | 2008-10-05 19:44:25 +0000 | [diff] [blame^] | 492 | if (const char *Prefix = TI.getUserLabelPrefix()) { |
Chris Lattner | 3f6d8cf | 2008-10-05 19:22:37 +0000 | [diff] [blame] | 493 | llvm::SmallString<20> TmpStr; |
| 494 | TmpStr += "__USER_LABEL_PREFIX__="; |
| 495 | TmpStr += Prefix; |
| 496 | DefineBuiltinMacro(Buf, TmpStr.c_str()); |
| 497 | } |
| 498 | |
Chris Lattner | 5edfe01 | 2008-10-05 19:44:25 +0000 | [diff] [blame^] | 499 | // Build configuration options. FIXME: these should be controlled by |
| 500 | // command line options or something. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 501 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 502 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 503 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 504 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
Chris Lattner | 5edfe01 | 2008-10-05 19:44:25 +0000 | [diff] [blame^] | 505 | |
| 506 | // Get other target #defines. |
| 507 | TI.getTargetDefines(Buf); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 508 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 509 | // FIXME: Should emit a #line directive here. |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 886bf13 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 514 | /// which implicitly adds the builtin defines etc. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 515 | void Preprocessor::EnterMainSourceFile() { |
| 516 | |
| 517 | unsigned MainFileID = SourceMgr.getMainFileID(); |
| 518 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 519 | // Enter the main file source buffer. |
| 520 | EnterSourceFile(MainFileID, 0); |
| 521 | |
Chris Lattner | b45f05c | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 522 | // Tell the header info that the main file was entered. If the file is later |
| 523 | // #imported, it won't be re-entered. |
| 524 | if (const FileEntry *FE = |
| 525 | SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0))) |
| 526 | HeaderInfo.IncrementIncludeCount(FE); |
| 527 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 528 | std::vector<char> PrologFile; |
| 529 | PrologFile.reserve(4080); |
| 530 | |
| 531 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 532 | InitializePredefinedMacros(*this, PrologFile); |
| 533 | |
| 534 | // Add on the predefines from the driver. |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 535 | PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end()); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 536 | |
| 537 | // Memory buffer must end with a null byte! |
| 538 | PrologFile.push_back(0); |
| 539 | |
| 540 | // Now that we have emitted the predefined macros, #includes, etc into |
| 541 | // PrologFile, preprocess it to populate the initial preprocessor state. |
| 542 | llvm::MemoryBuffer *SB = |
| 543 | llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(), |
| 544 | "<predefines>"); |
| 545 | assert(SB && "Cannot fail to create predefined source buffer"); |
| 546 | unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 547 | assert(FileID && "Could not create FileID for predefines?"); |
| 548 | |
| 549 | // Start parsing the predefines. |
| 550 | EnterSourceFile(FileID, 0); |
| 551 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 553 | |
| 554 | //===----------------------------------------------------------------------===// |
| 555 | // Lexer Event Handling. |
| 556 | //===----------------------------------------------------------------------===// |
| 557 | |
| 558 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 559 | /// identifier information for the token and install it into the token. |
| 560 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
| 561 | const char *BufPtr) { |
Chris Lattner | cb8e41c | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 562 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 563 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 564 | |
| 565 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 566 | IdentifierInfo *II; |
| 567 | if (BufPtr && !Identifier.needsCleaning()) { |
| 568 | // No cleaning needed, just use the characters from the lexed buffer. |
| 569 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 570 | } else { |
| 571 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
| 572 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 573 | IdentifierBuffer.resize(Identifier.getLength()); |
| 574 | const char *TmpBuf = &IdentifierBuffer[0]; |
| 575 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 576 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 577 | } |
| 578 | Identifier.setIdentifierInfo(II); |
| 579 | return II; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 584 | /// identifier. This callback looks up the identifier in the map and/or |
| 585 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 586 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
| 587 | assert(Identifier.getIdentifierInfo() && |
| 588 | "Can't handle identifiers without identifier info!"); |
| 589 | |
| 590 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 591 | |
| 592 | // If this identifier was poisoned, and if it was not produced from a macro |
| 593 | // expansion, emit an error. |
| 594 | if (II.isPoisoned() && CurLexer) { |
| 595 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 596 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 597 | else |
| 598 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 599 | } |
| 600 | |
| 601 | // If this is a macro to be expanded, do it. |
Chris Lattner | 7a1b088 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 602 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 603 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 604 | if (MI->isEnabled()) { |
| 605 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 606 | return; |
| 607 | } else { |
| 608 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 609 | // expanded, even if it's in a context where it could be expanded in the |
| 610 | // future. |
| 611 | Identifier.setFlag(Token::DisableExpand); |
| 612 | } |
| 613 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 617 | // then we act as if it is the actual operator and not the textual |
| 618 | // representation of it. |
| 619 | if (II.isCPlusPlusOperatorKeyword()) |
| 620 | Identifier.setIdentifierInfo(0); |
| 621 | |
| 622 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 623 | // turning "for" into a keyword. |
| 624 | Identifier.setKind(II.getTokenID()); |
| 625 | |
| 626 | // If this is an extension token, diagnose its use. |
Steve Naroff | 892bc0e | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 627 | // We avoid diagnosing tokens that originate from macro definitions. |
| 628 | if (II.isExtensionToken() && Features.C99 && !DisableMacroExpansion) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 629 | Diag(Identifier, diag::ext_token_used); |
| 630 | } |