Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
| 16 | // -d[MDNI] - Dump various things. |
| 17 | // -fworking-directory - #line's with preprocessor's working dir. |
| 18 | // -fpreprocessed |
| 19 | // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD |
| 20 | // -W* |
| 21 | // -w |
| 22 | // |
| 23 | // Messages to emit: |
| 24 | // "Multiple include guards may be useful for:\n" |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
| 29 | #include "clang/Lex/HeaderSearch.h" |
| 30 | #include "clang/Lex/MacroInfo.h" |
| 31 | #include "clang/Lex/PPCallbacks.h" |
| 32 | #include "clang/Lex/Pragma.h" |
| 33 | #include "clang/Lex/ScratchBuffer.h" |
| 34 | #include "clang/Basic/Diagnostic.h" |
| 35 | #include "clang/Basic/FileManager.h" |
| 36 | #include "clang/Basic/SourceManager.h" |
| 37 | #include "clang/Basic/TargetInfo.h" |
| 38 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | #include <iostream> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 46 | TargetInfo &target, SourceManager &SM, |
| 47 | HeaderSearch &Headers) |
| 48 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
| 49 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts), |
| 50 | CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) { |
| 51 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 52 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 69 | NumCachedMacroExpanders = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | |
| 71 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 72 | // This gets unpoisoned where it is allowed. |
| 73 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 74 | |
| 75 | // Initialize the pragma handlers. |
| 76 | PragmaHandlers = new PragmaNamespace(0); |
| 77 | RegisterBuiltinPragmas(); |
| 78 | |
| 79 | // Initialize builtin macros like __LINE__ and friends. |
| 80 | RegisterBuiltinMacros(); |
| 81 | } |
| 82 | |
| 83 | Preprocessor::~Preprocessor() { |
| 84 | // Free any active lexers. |
| 85 | delete CurLexer; |
| 86 | |
| 87 | while (!IncludeMacroStack.empty()) { |
| 88 | delete IncludeMacroStack.back().TheLexer; |
| 89 | delete IncludeMacroStack.back().TheMacroExpander; |
| 90 | IncludeMacroStack.pop_back(); |
| 91 | } |
| 92 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 93 | // Free any cached macro expanders. |
| 94 | for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i) |
| 95 | delete MacroExpanderCache[i]; |
| 96 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | // Release pragma information. |
| 98 | delete PragmaHandlers; |
| 99 | |
| 100 | // Delete the scratch buffer info. |
| 101 | delete ScratchBuf; |
| 102 | } |
| 103 | |
| 104 | PPCallbacks::~PPCallbacks() { |
| 105 | } |
| 106 | |
| 107 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
| 108 | /// the specified LexerToken's location, translating the token's start |
| 109 | /// position in the current buffer into a SourcePosition object for rendering. |
| 110 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) { |
| 111 | Diags.Report(Loc, DiagID); |
| 112 | } |
| 113 | |
| 114 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 115 | const std::string &Msg) { |
| 116 | Diags.Report(Loc, DiagID, &Msg, 1); |
| 117 | } |
| 118 | |
| 119 | void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const { |
| 120 | std::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 121 | << getSpelling(Tok) << "'"; |
| 122 | |
| 123 | if (!DumpFlags) return; |
| 124 | std::cerr << "\t"; |
| 125 | if (Tok.isAtStartOfLine()) |
| 126 | std::cerr << " [StartOfLine]"; |
| 127 | if (Tok.hasLeadingSpace()) |
| 128 | std::cerr << " [LeadingSpace]"; |
| 129 | if (Tok.isExpandDisabled()) |
| 130 | std::cerr << " [ExpandDisabled]"; |
| 131 | if (Tok.needsCleaning()) { |
| 132 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
| 133 | std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 134 | << "']"; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
| 139 | std::cerr << "MACRO: "; |
| 140 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 141 | DumpToken(MI.getReplacementToken(i)); |
| 142 | std::cerr << " "; |
| 143 | } |
| 144 | std::cerr << "\n"; |
| 145 | } |
| 146 | |
| 147 | void Preprocessor::PrintStats() { |
| 148 | std::cerr << "\n*** Preprocessor Stats:\n"; |
| 149 | std::cerr << NumDirectives << " directives found:\n"; |
| 150 | std::cerr << " " << NumDefined << " #define.\n"; |
| 151 | std::cerr << " " << NumUndefined << " #undef.\n"; |
| 152 | std::cerr << " #include/#include_next/#import:\n"; |
| 153 | std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 154 | std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 155 | std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 156 | std::cerr << " " << NumElse << " #else/#elif.\n"; |
| 157 | std::cerr << " " << NumEndif << " #endif.\n"; |
| 158 | std::cerr << " " << NumPragma << " #pragma.\n"; |
| 159 | std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
| 160 | |
| 161 | std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 162 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 163 | << NumFastMacroExpanded << " on the fast path.\n"; |
| 164 | std::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 165 | << " token paste (##) operations performed, " |
| 166 | << NumFastTokenPaste << " on the fast path.\n"; |
| 167 | } |
| 168 | |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | // Token Spelling |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | |
| 173 | |
| 174 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 175 | /// token are the characters used to represent the token in the source file |
| 176 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 177 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 178 | /// UCNs, etc. |
| 179 | std::string Preprocessor::getSpelling(const LexerToken &Tok) const { |
| 180 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 181 | |
| 182 | // If this token contains nothing interesting, return it directly. |
| 183 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 184 | if (!Tok.needsCleaning()) |
| 185 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 186 | |
| 187 | std::string Result; |
| 188 | Result.reserve(Tok.getLength()); |
| 189 | |
| 190 | // Otherwise, hard case, relex the characters into the string. |
| 191 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 192 | Ptr != End; ) { |
| 193 | unsigned CharSize; |
| 194 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 195 | Ptr += CharSize; |
| 196 | } |
| 197 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 198 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 199 | return Result; |
| 200 | } |
| 201 | |
| 202 | /// getSpelling - This method is used to get the spelling of a token into a |
| 203 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 204 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 205 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 206 | /// |
| 207 | /// Note that this method may do two possible things: it may either fill in |
| 208 | /// the buffer specified with characters, or it may *change the input pointer* |
| 209 | /// to point to a constant buffer with the data already in it (avoiding a |
| 210 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 211 | /// if an internal buffer is returned. |
| 212 | unsigned Preprocessor::getSpelling(const LexerToken &Tok, |
| 213 | const char *&Buffer) const { |
| 214 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 215 | |
| 216 | // If this token is an identifier, just return the string from the identifier |
| 217 | // table, which is very quick. |
| 218 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 219 | Buffer = II->getName(); |
| 220 | return Tok.getLength(); |
| 221 | } |
| 222 | |
| 223 | // Otherwise, compute the start of the token in the input lexer buffer. |
| 224 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 225 | |
| 226 | // If this token contains nothing interesting, return it directly. |
| 227 | if (!Tok.needsCleaning()) { |
| 228 | Buffer = TokStart; |
| 229 | return Tok.getLength(); |
| 230 | } |
| 231 | // Otherwise, hard case, relex the characters into the string. |
| 232 | char *OutBuf = const_cast<char*>(Buffer); |
| 233 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 234 | Ptr != End; ) { |
| 235 | unsigned CharSize; |
| 236 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 237 | Ptr += CharSize; |
| 238 | } |
| 239 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 240 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 241 | |
| 242 | return OutBuf-Buffer; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 247 | /// location for it. If specified, the source location provides a source |
| 248 | /// location for the token. |
| 249 | SourceLocation Preprocessor:: |
| 250 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 251 | if (SLoc.isValid()) |
| 252 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 253 | return ScratchBuf->getToken(Buf, Len); |
| 254 | } |
| 255 | |
| 256 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 257 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 258 | /// token, return a new location that specifies a character within the token. |
| 259 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 260 | unsigned CharNo) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 261 | // If they request the first char of the token, we're trivially done. If this |
| 262 | // is a macro expansion, it doesn't make sense to point to a character within |
| 263 | // the instantiation point (the name). We could point to the source |
| 264 | // character, but without also pointing to instantiation info, this is |
| 265 | // confusing. |
| 266 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 267 | |
| 268 | // Figure out how many physical characters away the specified logical |
| 269 | // character is. This needs to take into consideration newlines and |
| 270 | // trigraphs. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 271 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 272 | unsigned PhysOffset = 0; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 273 | |
| 274 | // The usual case is that tokens don't contain anything interesting. Skip |
| 275 | // over the uninteresting characters. If a token only consists of simple |
| 276 | // chars, this method is extremely fast. |
| 277 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 278 | ++TokPtr, --CharNo, ++PhysOffset; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 279 | |
| 280 | // If we have a character that may be a trigraph or escaped newline, create a |
| 281 | // lexer to parse it correctly. |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 282 | if (CharNo != 0) { |
| 283 | // Create a lexer starting at this token position. |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame^] | 284 | Lexer TheLexer(TokStart, *this, TokPtr); |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 285 | LexerToken Tok; |
| 286 | // Skip over characters the remaining characters. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 287 | const char *TokStartPtr = TokPtr; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 288 | for (; CharNo; --CharNo) |
| 289 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 290 | |
| 291 | PhysOffset += TokPtr-TokStartPtr; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 293 | |
| 294 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
| 298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | //===----------------------------------------------------------------------===// |
| 300 | // Source File Location Methods. |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
| 303 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 304 | /// return null on failure. isAngled indicates whether the file reference is |
| 305 | /// for system #include's or not (i.e. using <> instead of ""). |
| 306 | const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, |
| 307 | const char *FilenameEnd, |
| 308 | bool isAngled, |
| 309 | const DirectoryLookup *FromDir, |
| 310 | const DirectoryLookup *&CurDir) { |
| 311 | // If the header lookup mechanism may be relative to the current file, pass in |
| 312 | // info about where the current file is. |
| 313 | const FileEntry *CurFileEnt = 0; |
| 314 | if (!FromDir) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 315 | SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc(); |
| 316 | CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Do a standard file entry lookup. |
| 320 | CurDir = CurDirLookup; |
| 321 | const FileEntry *FE = |
| 322 | HeaderInfo.LookupFile(FilenameStart, FilenameEnd, |
| 323 | isAngled, FromDir, CurDir, CurFileEnt); |
| 324 | if (FE) return FE; |
| 325 | |
| 326 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 327 | // to one of the headers on the #include stack. Walk the list of the current |
| 328 | // headers on the #include stack and pass them to HeaderInfo. |
| 329 | if (CurLexer && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 330 | CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 331 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 332 | CurFileEnt))) |
| 333 | return FE; |
| 334 | } |
| 335 | |
| 336 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 337 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
| 338 | if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 339 | CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 341 | CurFileEnt))) |
| 342 | return FE; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Otherwise, we really couldn't find the file. |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
| 351 | /// #include. |
| 352 | bool Preprocessor::isInPrimaryFile() const { |
| 353 | if (CurLexer && !CurLexer->Is_PragmaLexer) |
| 354 | return CurLexer->isMainFile(); |
| 355 | |
| 356 | // If there are any stacked lexers, we're in a #include. |
| 357 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) |
| 358 | if (IncludeMacroStack[i].TheLexer && |
| 359 | !IncludeMacroStack[i].TheLexer->Is_PragmaLexer) |
| 360 | return IncludeMacroStack[i].TheLexer->isMainFile(); |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 365 | /// that this ignores any potentially active macro expansions and _Pragma |
| 366 | /// expansions going on at the time. |
| 367 | Lexer *Preprocessor::getCurrentFileLexer() const { |
| 368 | if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer; |
| 369 | |
| 370 | // Look for a stacked lexer. |
| 371 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 372 | Lexer *L = IncludeMacroStack[i-1].TheLexer; |
| 373 | if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions. |
| 374 | return L; |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 381 | /// start lexing tokens from it instead of the current buffer. Return true |
| 382 | /// on failure. |
| 383 | void Preprocessor::EnterSourceFile(unsigned FileID, |
| 384 | const DirectoryLookup *CurDir, |
| 385 | bool isMainFile) { |
| 386 | assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!"); |
| 387 | ++NumEnteredSourceFiles; |
| 388 | |
| 389 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 390 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 391 | |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame^] | 392 | Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | if (isMainFile) TheLexer->setIsMainFile(); |
| 394 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 395 | } |
| 396 | |
| 397 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 398 | /// start lexing tokens from it instead of the current buffer. |
| 399 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 400 | const DirectoryLookup *CurDir) { |
| 401 | |
| 402 | // Add the current lexer to the include stack. |
| 403 | if (CurLexer || CurMacroExpander) |
| 404 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 405 | CurMacroExpander)); |
| 406 | |
| 407 | CurLexer = TheLexer; |
| 408 | CurDirLookup = CurDir; |
| 409 | CurMacroExpander = 0; |
| 410 | |
| 411 | // Notify the client, if desired, that we are in a new source file. |
| 412 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
| 413 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 414 | |
| 415 | // Get the file entry for the current file. |
| 416 | if (const FileEntry *FE = |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 417 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 418 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 419 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 420 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | PPCallbacks::EnterFile, FileType); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | |
| 426 | |
| 427 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 428 | /// tokens from it instead of the current buffer. |
| 429 | void Preprocessor::EnterMacro(LexerToken &Tok, MacroArgs *Args) { |
| 430 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 431 | CurMacroExpander)); |
| 432 | CurLexer = 0; |
| 433 | CurDirLookup = 0; |
| 434 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 435 | if (NumCachedMacroExpanders == 0) { |
| 436 | CurMacroExpander = new MacroExpander(Tok, Args, *this); |
| 437 | } else { |
| 438 | CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders]; |
| 439 | CurMacroExpander->Init(Tok, Args); |
| 440 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
| 444 | /// which will cause the lexer to start returning the specified tokens. Note |
| 445 | /// that these tokens will be re-macro-expanded when/if expansion is enabled. |
| 446 | /// This method assumes that the specified stream of tokens has a permanent |
| 447 | /// owner somewhere, so they do not need to be copied. |
| 448 | void Preprocessor::EnterTokenStream(const LexerToken *Toks, unsigned NumToks) { |
| 449 | // Save our current state. |
| 450 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 451 | CurMacroExpander)); |
| 452 | CurLexer = 0; |
| 453 | CurDirLookup = 0; |
| 454 | |
| 455 | // Create a macro expander to expand from the specified token stream. |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 456 | if (NumCachedMacroExpanders == 0) { |
| 457 | CurMacroExpander = new MacroExpander(Toks, NumToks, *this); |
| 458 | } else { |
| 459 | CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders]; |
| 460 | CurMacroExpander->Init(Toks, NumToks); |
| 461 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 465 | /// lexer stack. This should only be used in situations where the current |
| 466 | /// state of the top-of-stack lexer is known. |
| 467 | void Preprocessor::RemoveTopOfLexerStack() { |
| 468 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 469 | |
| 470 | if (CurMacroExpander) { |
| 471 | // Delete or cache the now-dead macro expander. |
| 472 | if (NumCachedMacroExpanders == MacroExpanderCacheSize) |
| 473 | delete CurMacroExpander; |
| 474 | else |
| 475 | MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander; |
| 476 | } else { |
| 477 | delete CurLexer; |
| 478 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 479 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 480 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 481 | CurMacroExpander = IncludeMacroStack.back().TheMacroExpander; |
| 482 | IncludeMacroStack.pop_back(); |
| 483 | } |
| 484 | |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | // Macro Expansion Handling. |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
| 489 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 490 | /// table and mark it as a builtin macro to be expanded. |
| 491 | IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) { |
| 492 | // Get the identifier. |
| 493 | IdentifierInfo *Id = getIdentifierInfo(Name); |
| 494 | |
| 495 | // Mark it as being a macro that is builtin. |
| 496 | MacroInfo *MI = new MacroInfo(SourceLocation()); |
| 497 | MI->setIsBuiltinMacro(); |
| 498 | Id->setMacroInfo(MI); |
| 499 | return Id; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 504 | /// identifier table. |
| 505 | void Preprocessor::RegisterBuiltinMacros() { |
| 506 | Ident__LINE__ = RegisterBuiltinMacro("__LINE__"); |
| 507 | Ident__FILE__ = RegisterBuiltinMacro("__FILE__"); |
| 508 | Ident__DATE__ = RegisterBuiltinMacro("__DATE__"); |
| 509 | Ident__TIME__ = RegisterBuiltinMacro("__TIME__"); |
| 510 | Ident_Pragma = RegisterBuiltinMacro("_Pragma"); |
| 511 | |
| 512 | // GCC Extensions. |
| 513 | Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__"); |
| 514 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__"); |
| 515 | Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__"); |
| 516 | } |
| 517 | |
| 518 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 519 | /// in its expansion, currently expands to that token literally. |
| 520 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
| 521 | const IdentifierInfo *MacroIdent) { |
| 522 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 523 | |
| 524 | // If the token isn't an identifier, it's always literally expanded. |
| 525 | if (II == 0) return true; |
| 526 | |
| 527 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 528 | // expanded so it's not a trivial expansion. |
| 529 | if (II->getMacroInfo() && II->getMacroInfo()->isEnabled() && |
| 530 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 531 | II != MacroIdent) |
| 532 | return false; |
| 533 | |
| 534 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 535 | // it. |
| 536 | if (MI->isObjectLike()) return true; |
| 537 | |
| 538 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 539 | // as long as the identifier is not a macro argument. |
| 540 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 541 | I != E; ++I) |
| 542 | if (*I == II) |
| 543 | return false; // Identifier is a macro argument. |
| 544 | |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 550 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 551 | /// method should have no observable side-effect on the lexed tokens. |
| 552 | bool Preprocessor::isNextPPTokenLParen() { |
| 553 | // Do some quick tests for rejection cases. |
| 554 | unsigned Val; |
| 555 | if (CurLexer) |
| 556 | Val = CurLexer->isNextPPTokenLParen(); |
| 557 | else |
| 558 | Val = CurMacroExpander->isNextTokenLParen(); |
| 559 | |
| 560 | if (Val == 2) { |
Chris Lattner | 0ea793e | 2007-07-19 00:07:36 +0000 | [diff] [blame] | 561 | // We have run off the end. If it's a source file we don't |
| 562 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 563 | // macro stack. |
| 564 | if (CurLexer) |
| 565 | return false; |
| 566 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 567 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 568 | if (Entry.TheLexer) |
| 569 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
| 570 | else |
| 571 | Val = Entry.TheMacroExpander->isNextTokenLParen(); |
Chris Lattner | 0ea793e | 2007-07-19 00:07:36 +0000 | [diff] [blame] | 572 | |
| 573 | if (Val != 2) |
| 574 | break; |
| 575 | |
| 576 | // Ran off the end of a source file? |
| 577 | if (Entry.TheLexer) |
| 578 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 583 | // have found something that isn't a '(' or we found the end of the |
| 584 | // translation unit. In either case, return false. |
| 585 | if (Val != 1) |
| 586 | return false; |
| 587 | |
| 588 | LexerToken Tok; |
| 589 | LexUnexpandedToken(Tok); |
| 590 | assert(Tok.getKind() == tok::l_paren && "Error computing l-paren-ness?"); |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 595 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 596 | bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier, |
| 597 | MacroInfo *MI) { |
| 598 | |
| 599 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 600 | if (MI->isBuiltinMacro()) { |
| 601 | ExpandBuiltinMacro(Identifier); |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | // If this is the first use of a target-specific macro, warn about it. |
| 606 | if (MI->isTargetSpecific()) { |
| 607 | MI->setIsTargetSpecific(false); // Don't warn on second use. |
| 608 | getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(), |
| 609 | diag::port_target_macro_use); |
| 610 | } |
| 611 | |
| 612 | /// Args - If this is a function-like macro expansion, this contains, |
| 613 | /// for each macro argument, the list of tokens that were provided to the |
| 614 | /// invocation. |
| 615 | MacroArgs *Args = 0; |
| 616 | |
| 617 | // If this is a function-like macro, read the arguments. |
| 618 | if (MI->isFunctionLike()) { |
| 619 | // C99 6.10.3p10: If the preprocessing token immediately after the the macro |
Chris Lattner | 2b64fdc | 2007-07-19 16:11:58 +0000 | [diff] [blame] | 620 | // name isn't a '(', this macro should not be expanded. Otherwise, consume |
| 621 | // it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 622 | if (!isNextPPTokenLParen()) |
| 623 | return true; |
| 624 | |
| 625 | // Remember that we are now parsing the arguments to a macro invocation. |
| 626 | // Preprocessor directives used inside macro arguments are not portable, and |
| 627 | // this enables the warning. |
| 628 | InMacroArgs = true; |
| 629 | Args = ReadFunctionLikeMacroArgs(Identifier, MI); |
| 630 | |
| 631 | // Finished parsing args. |
| 632 | InMacroArgs = false; |
| 633 | |
| 634 | // If there was an error parsing the arguments, bail out. |
| 635 | if (Args == 0) return false; |
| 636 | |
| 637 | ++NumFnMacroExpanded; |
| 638 | } else { |
| 639 | ++NumMacroExpanded; |
| 640 | } |
| 641 | |
| 642 | // Notice that this macro has been used. |
| 643 | MI->setIsUsed(true); |
| 644 | |
| 645 | // If we started lexing a macro, enter the macro expansion body. |
| 646 | |
| 647 | // If this macro expands to no tokens, don't bother to push it onto the |
| 648 | // expansion stack, only to take it right back off. |
| 649 | if (MI->getNumTokens() == 0) { |
| 650 | // No need for arg info. |
| 651 | if (Args) Args->destroy(); |
| 652 | |
| 653 | // Ignore this macro use, just return the next token in the current |
| 654 | // buffer. |
| 655 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 656 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 657 | |
| 658 | Lex(Identifier); |
| 659 | |
| 660 | // If the identifier isn't on some OTHER line, inherit the leading |
| 661 | // whitespace/first-on-a-line property of this token. This handles |
| 662 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 663 | // empty. |
| 664 | if (!Identifier.isAtStartOfLine()) { |
| 665 | if (IsAtStartOfLine) Identifier.setFlag(LexerToken::StartOfLine); |
| 666 | if (HadLeadingSpace) Identifier.setFlag(LexerToken::LeadingSpace); |
| 667 | } |
| 668 | ++NumFastMacroExpanded; |
| 669 | return false; |
| 670 | |
| 671 | } else if (MI->getNumTokens() == 1 && |
| 672 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){ |
| 673 | // Otherwise, if this macro expands into a single trivially-expanded |
| 674 | // token: expand it now. This handles common cases like |
| 675 | // "#define VAL 42". |
| 676 | |
| 677 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 678 | // identifier to the expanded token. |
| 679 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 680 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 681 | |
| 682 | // Remember where the token is instantiated. |
| 683 | SourceLocation InstantiateLoc = Identifier.getLocation(); |
| 684 | |
| 685 | // Replace the result token. |
| 686 | Identifier = MI->getReplacementToken(0); |
| 687 | |
| 688 | // Restore the StartOfLine/LeadingSpace markers. |
| 689 | Identifier.setFlagValue(LexerToken::StartOfLine , isAtStartOfLine); |
| 690 | Identifier.setFlagValue(LexerToken::LeadingSpace, hasLeadingSpace); |
| 691 | |
| 692 | // Update the tokens location to include both its logical and physical |
| 693 | // locations. |
| 694 | SourceLocation Loc = |
| 695 | SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc); |
| 696 | Identifier.setLocation(Loc); |
| 697 | |
| 698 | // If this is #define X X, we must mark the result as unexpandible. |
| 699 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) |
| 700 | if (NewII->getMacroInfo() == MI) |
| 701 | Identifier.setFlag(LexerToken::DisableExpand); |
| 702 | |
| 703 | // Since this is not an identifier token, it can't be macro expanded, so |
| 704 | // we're done. |
| 705 | ++NumFastMacroExpanded; |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | // Start expanding the macro. |
| 710 | EnterMacro(Identifier, Args); |
| 711 | |
| 712 | // Now that the macro is at the top of the include stack, ask the |
| 713 | // preprocessor to read the next token from it. |
| 714 | Lex(Identifier); |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is |
| 719 | /// invoked to read all of the actual arguments specified for the macro |
| 720 | /// invocation. This returns null on error. |
| 721 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName, |
| 722 | MacroInfo *MI) { |
| 723 | // The number of fixed arguments to parse. |
| 724 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 725 | bool isVariadic = MI->isVariadic(); |
| 726 | |
| 727 | // Outer loop, while there are more arguments, keep reading them. |
| 728 | LexerToken Tok; |
| 729 | Tok.setKind(tok::comma); |
| 730 | --NumFixedArgsLeft; // Start reading the first arg. |
| 731 | |
| 732 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 733 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 734 | // heap allocations in the common case. |
| 735 | llvm::SmallVector<LexerToken, 64> ArgTokens; |
| 736 | |
| 737 | unsigned NumActuals = 0; |
| 738 | while (Tok.getKind() == tok::comma) { |
Chris Lattner | 2b64fdc | 2007-07-19 16:11:58 +0000 | [diff] [blame] | 739 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 740 | // that we already consumed the first one. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | unsigned NumParens = 0; |
| 742 | |
| 743 | while (1) { |
| 744 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 745 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 746 | LexUnexpandedToken(Tok); |
| 747 | |
| 748 | if (Tok.getKind() == tok::eof) { |
| 749 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 750 | // Do not lose the EOF. Return it to the client. |
| 751 | MacroName = Tok; |
| 752 | return 0; |
| 753 | } else if (Tok.getKind() == tok::r_paren) { |
| 754 | // If we found the ) token, the macro arg list is done. |
| 755 | if (NumParens-- == 0) |
| 756 | break; |
| 757 | } else if (Tok.getKind() == tok::l_paren) { |
| 758 | ++NumParens; |
| 759 | } else if (Tok.getKind() == tok::comma && NumParens == 0) { |
| 760 | // Comma ends this argument if there are more fixed arguments expected. |
| 761 | if (NumFixedArgsLeft) |
| 762 | break; |
| 763 | |
| 764 | // If this is not a variadic macro, too many args were specified. |
| 765 | if (!isVariadic) { |
| 766 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 767 | // Emitting it at the , could be far away from the macro name. |
| 768 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
| 769 | return 0; |
| 770 | } |
| 771 | // Otherwise, continue to add the tokens to this variable argument. |
| 772 | } else if (Tok.getKind() == tok::comment && !KeepMacroComments) { |
| 773 | // If this is a comment token in the argument list and we're just in |
| 774 | // -C mode (not -CC mode), discard the comment. |
| 775 | continue; |
| 776 | } |
| 777 | |
| 778 | ArgTokens.push_back(Tok); |
| 779 | } |
| 780 | |
| 781 | // Empty arguments are standard in C99 and supported as an extension in |
| 782 | // other modes. |
| 783 | if (ArgTokens.empty() && !Features.C99) |
| 784 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
| 785 | |
| 786 | // Add a marker EOF token to the end of the token list for this argument. |
| 787 | LexerToken EOFTok; |
| 788 | EOFTok.startToken(); |
| 789 | EOFTok.setKind(tok::eof); |
| 790 | EOFTok.setLocation(Tok.getLocation()); |
| 791 | EOFTok.setLength(0); |
| 792 | ArgTokens.push_back(EOFTok); |
| 793 | ++NumActuals; |
| 794 | --NumFixedArgsLeft; |
| 795 | }; |
| 796 | |
| 797 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 798 | // arguments. |
| 799 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 800 | |
| 801 | // See MacroArgs instance var for description of this. |
| 802 | bool isVarargsElided = false; |
| 803 | |
| 804 | if (NumActuals < MinArgsExpected) { |
| 805 | // There are several cases where too few arguments is ok, handle them now. |
| 806 | if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) { |
| 807 | // Varargs where the named vararg parameter is missing: ok as extension. |
| 808 | // #define A(x, ...) |
| 809 | // A("blah") |
| 810 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 811 | |
| 812 | // Remember this occurred if this is a C99 macro invocation with at least |
| 813 | // one actual argument. |
| 814 | isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1; |
| 815 | } else if (MI->getNumArgs() == 1) { |
| 816 | // #define A(x) |
| 817 | // A() |
| 818 | // is ok because it is an empty argument. |
| 819 | |
| 820 | // Empty arguments are standard in C99 and supported as an extension in |
| 821 | // other modes. |
| 822 | if (ArgTokens.empty() && !Features.C99) |
| 823 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
| 824 | } else { |
| 825 | // Otherwise, emit the error. |
| 826 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | // Add a marker EOF token to the end of the token list for this argument. |
| 831 | SourceLocation EndLoc = Tok.getLocation(); |
| 832 | Tok.startToken(); |
| 833 | Tok.setKind(tok::eof); |
| 834 | Tok.setLocation(EndLoc); |
| 835 | Tok.setLength(0); |
| 836 | ArgTokens.push_back(Tok); |
| 837 | } |
| 838 | |
| 839 | return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided); |
| 840 | } |
| 841 | |
| 842 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 843 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 844 | /// the identifier tokens inserted. |
| 845 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 846 | Preprocessor &PP) { |
| 847 | time_t TT = time(0); |
| 848 | struct tm *TM = localtime(&TT); |
| 849 | |
| 850 | static const char * const Months[] = { |
| 851 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 852 | }; |
| 853 | |
| 854 | char TmpBuffer[100]; |
| 855 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 856 | TM->tm_year+1900); |
| 857 | DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer)); |
| 858 | |
| 859 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
| 860 | TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer)); |
| 861 | } |
| 862 | |
| 863 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 864 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 865 | void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) { |
| 866 | // Figure out which token this is. |
| 867 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 868 | assert(II && "Can't be a macro without id info!"); |
| 869 | |
| 870 | // If this is an _Pragma directive, expand it, invoke the pragma handler, then |
| 871 | // lex the token after it. |
| 872 | if (II == Ident_Pragma) |
| 873 | return Handle_Pragma(Tok); |
| 874 | |
| 875 | ++NumBuiltinMacroExpanded; |
| 876 | |
| 877 | char TmpBuffer[100]; |
| 878 | |
| 879 | // Set up the return result. |
| 880 | Tok.setIdentifierInfo(0); |
| 881 | Tok.clearFlag(LexerToken::NeedsCleaning); |
| 882 | |
| 883 | if (II == Ident__LINE__) { |
| 884 | // __LINE__ expands to a simple numeric value. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 885 | sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | unsigned Length = strlen(TmpBuffer); |
| 887 | Tok.setKind(tok::numeric_constant); |
| 888 | Tok.setLength(Length); |
| 889 | Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); |
| 890 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
| 891 | SourceLocation Loc = Tok.getLocation(); |
| 892 | if (II == Ident__BASE_FILE__) { |
| 893 | Diag(Tok, diag::ext_pp_base_file); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 894 | SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc); |
| 895 | while (NextLoc.isValid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | Loc = NextLoc; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 897 | NextLoc = SourceMgr.getIncludeLoc(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| 901 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 902 | std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 903 | FN = '"' + Lexer::Stringify(FN) + '"'; |
| 904 | Tok.setKind(tok::string_literal); |
| 905 | Tok.setLength(FN.size()); |
| 906 | Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation())); |
| 907 | } else if (II == Ident__DATE__) { |
| 908 | if (!DATELoc.isValid()) |
| 909 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 910 | Tok.setKind(tok::string_literal); |
| 911 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
| 912 | Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation())); |
| 913 | } else if (II == Ident__TIME__) { |
| 914 | if (!TIMELoc.isValid()) |
| 915 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 916 | Tok.setKind(tok::string_literal); |
| 917 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
| 918 | Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation())); |
| 919 | } else if (II == Ident__INCLUDE_LEVEL__) { |
| 920 | Diag(Tok, diag::ext_pp_include_level); |
| 921 | |
| 922 | // Compute the include depth of this token. |
| 923 | unsigned Depth = 0; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 924 | SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation()); |
| 925 | for (; Loc.isValid(); ++Depth) |
| 926 | Loc = SourceMgr.getIncludeLoc(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 927 | |
| 928 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 929 | sprintf(TmpBuffer, "%u", Depth); |
| 930 | unsigned Length = strlen(TmpBuffer); |
| 931 | Tok.setKind(tok::numeric_constant); |
| 932 | Tok.setLength(Length); |
| 933 | Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); |
| 934 | } else if (II == Ident__TIMESTAMP__) { |
| 935 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 936 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 937 | Diag(Tok, diag::ext_pp_timestamp); |
| 938 | |
| 939 | // Get the file that we are lexing out of. If we're currently lexing from |
| 940 | // a macro, dig into the include stack. |
| 941 | const FileEntry *CurFile = 0; |
| 942 | Lexer *TheLexer = getCurrentFileLexer(); |
| 943 | |
| 944 | if (TheLexer) |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 945 | CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 946 | |
| 947 | // If this file is older than the file it depends on, emit a diagnostic. |
| 948 | const char *Result; |
| 949 | if (CurFile) { |
| 950 | time_t TT = CurFile->getModificationTime(); |
| 951 | struct tm *TM = localtime(&TT); |
| 952 | Result = asctime(TM); |
| 953 | } else { |
| 954 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 955 | } |
| 956 | TmpBuffer[0] = '"'; |
| 957 | strcpy(TmpBuffer+1, Result); |
| 958 | unsigned Len = strlen(TmpBuffer); |
| 959 | TmpBuffer[Len-1] = '"'; // Replace the newline with a quote. |
| 960 | Tok.setKind(tok::string_literal); |
| 961 | Tok.setLength(Len); |
| 962 | Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation())); |
| 963 | } else { |
| 964 | assert(0 && "Unknown identifier!"); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | //===----------------------------------------------------------------------===// |
| 969 | // Lexer Event Handling. |
| 970 | //===----------------------------------------------------------------------===// |
| 971 | |
| 972 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 973 | /// identifier information for the token and install it into the token. |
| 974 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier, |
| 975 | const char *BufPtr) { |
| 976 | assert(Identifier.getKind() == tok::identifier && "Not an identifier!"); |
| 977 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 978 | |
| 979 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 980 | IdentifierInfo *II; |
| 981 | if (BufPtr && !Identifier.needsCleaning()) { |
| 982 | // No cleaning needed, just use the characters from the lexed buffer. |
| 983 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 984 | } else { |
| 985 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | c35717a | 2007-07-13 17:10:38 +0000 | [diff] [blame] | 986 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 987 | IdentifierBuffer.resize(Identifier.getLength()); |
| 988 | const char *TmpBuf = &IdentifierBuffer[0]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 989 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 990 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 991 | } |
| 992 | Identifier.setIdentifierInfo(II); |
| 993 | return II; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 998 | /// identifier. This callback looks up the identifier in the map and/or |
| 999 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 1000 | void Preprocessor::HandleIdentifier(LexerToken &Identifier) { |
| 1001 | assert(Identifier.getIdentifierInfo() && |
| 1002 | "Can't handle identifiers without identifier info!"); |
| 1003 | |
| 1004 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 1005 | |
| 1006 | // If this identifier was poisoned, and if it was not produced from a macro |
| 1007 | // expansion, emit an error. |
| 1008 | if (II.isPoisoned() && CurLexer) { |
| 1009 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 1010 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 1011 | else |
| 1012 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 1013 | } |
| 1014 | |
| 1015 | // If this is a macro to be expanded, do it. |
| 1016 | if (MacroInfo *MI = II.getMacroInfo()) { |
| 1017 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 1018 | if (MI->isEnabled()) { |
| 1019 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 1020 | return; |
| 1021 | } else { |
| 1022 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 1023 | // expanded, even if it's in a context where it could be expanded in the |
| 1024 | // future. |
| 1025 | Identifier.setFlag(LexerToken::DisableExpand); |
| 1026 | } |
| 1027 | } |
| 1028 | } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) { |
| 1029 | // If this identifier is a macro on some other target, emit a diagnostic. |
| 1030 | // This diagnosic is only emitted when macro expansion is enabled, because |
| 1031 | // the macro would not have been expanded for the other target either. |
| 1032 | II.setIsOtherTargetMacro(false); // Don't warn on second use. |
| 1033 | getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(), |
| 1034 | diag::port_target_macro_use); |
| 1035 | |
| 1036 | } |
| 1037 | |
| 1038 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 1039 | // then we act as if it is the actual operator and not the textual |
| 1040 | // representation of it. |
| 1041 | if (II.isCPlusPlusOperatorKeyword()) |
| 1042 | Identifier.setIdentifierInfo(0); |
| 1043 | |
| 1044 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 1045 | // turning "for" into a keyword. |
| 1046 | Identifier.setKind(II.getTokenID()); |
| 1047 | |
| 1048 | // If this is an extension token, diagnose its use. |
| 1049 | // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99 |
| 1050 | // For now, I'm just commenting it out (while I work on attributes). |
| 1051 | if (II.isExtensionToken() && Features.C99) |
| 1052 | Diag(Identifier, diag::ext_token_used); |
| 1053 | } |
| 1054 | |
| 1055 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 1056 | /// the current file. This either returns the EOF token or pops a level off |
| 1057 | /// the include stack and keeps going. |
| 1058 | bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) { |
| 1059 | assert(!CurMacroExpander && |
| 1060 | "Ending a file when currently in a macro!"); |
| 1061 | |
| 1062 | // See if this file had a controlling macro. |
| 1063 | if (CurLexer) { // Not ending a macro, ignore it. |
| 1064 | if (const IdentifierInfo *ControllingMacro = |
| 1065 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
| 1066 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 1067 | if (const FileEntry *FE = |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 1068 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1069 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | // If this is a #include'd file, pop it off the include stack and continue |
| 1074 | // lexing the #includer file. |
| 1075 | if (!IncludeMacroStack.empty()) { |
| 1076 | // We're done with the #included file. |
| 1077 | RemoveTopOfLexerStack(); |
| 1078 | |
| 1079 | // Notify the client, if desired, that we are in a new source file. |
| 1080 | if (Callbacks && !isEndOfMacro && CurLexer) { |
| 1081 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 1082 | |
| 1083 | // Get the file entry for the current file. |
| 1084 | if (const FileEntry *FE = |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 1085 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1086 | FileType = HeaderInfo.getFileDirFlavor(FE); |
| 1087 | |
| 1088 | Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 1089 | PPCallbacks::ExitFile, FileType); |
| 1090 | } |
| 1091 | |
| 1092 | // Client should lex another token. |
| 1093 | return false; |
| 1094 | } |
| 1095 | |
| 1096 | Result.startToken(); |
| 1097 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 1098 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
| 1099 | Result.setKind(tok::eof); |
| 1100 | |
| 1101 | // We're done with the #included file. |
| 1102 | delete CurLexer; |
| 1103 | CurLexer = 0; |
| 1104 | |
| 1105 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 1106 | // diagnostic is enabled, walk all of the identifiers, looking for macros that |
| 1107 | // have not been used. |
| 1108 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
| 1109 | for (IdentifierTable::iterator I = Identifiers.begin(), |
| 1110 | E = Identifiers.end(); I != E; ++I) { |
| 1111 | const IdentifierInfo &II = I->getValue(); |
| 1112 | if (II.getMacroInfo() && !II.getMacroInfo()->isUsed()) |
| 1113 | Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | /// HandleEndOfMacro - This callback is invoked when the lexer hits the end of |
| 1121 | /// the current macro expansion or token stream expansion. |
| 1122 | bool Preprocessor::HandleEndOfMacro(LexerToken &Result) { |
| 1123 | assert(CurMacroExpander && !CurLexer && |
| 1124 | "Ending a macro when currently in a #include file!"); |
| 1125 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 1126 | // Delete or cache the now-dead macro expander. |
| 1127 | if (NumCachedMacroExpanders == MacroExpanderCacheSize) |
| 1128 | delete CurMacroExpander; |
| 1129 | else |
| 1130 | MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | |
| 1132 | // Handle this like a #include file being popped off the stack. |
| 1133 | CurMacroExpander = 0; |
| 1134 | return HandleEndOfFile(Result, true); |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | //===----------------------------------------------------------------------===// |
| 1139 | // Utility Methods for Preprocessor Directive Handling. |
| 1140 | //===----------------------------------------------------------------------===// |
| 1141 | |
| 1142 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 1143 | /// current line until the tok::eom token is found. |
| 1144 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 1145 | LexerToken Tmp; |
| 1146 | do { |
| 1147 | LexUnexpandedToken(Tmp); |
| 1148 | } while (Tmp.getKind() != tok::eom); |
| 1149 | } |
| 1150 | |
| 1151 | /// isCXXNamedOperator - Returns "true" if the token is a named operator in C++. |
| 1152 | static bool isCXXNamedOperator(const std::string &Spelling) { |
| 1153 | return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" || |
| 1154 | Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" || |
| 1155 | Spelling == "or" || Spelling == "xor"; |
| 1156 | } |
| 1157 | |
| 1158 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 1159 | /// #define or #undef. This sets the token kind to eom and discards the rest |
| 1160 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 1161 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
| 1162 | /// else (e.g. #ifdef). |
| 1163 | void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) { |
| 1164 | // Read the token, don't allow macro expansion on it. |
| 1165 | LexUnexpandedToken(MacroNameTok); |
| 1166 | |
| 1167 | // Missing macro name? |
| 1168 | if (MacroNameTok.getKind() == tok::eom) |
| 1169 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 1170 | |
| 1171 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 1172 | if (II == 0) { |
| 1173 | std::string Spelling = getSpelling(MacroNameTok); |
| 1174 | if (isCXXNamedOperator(Spelling)) |
| 1175 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 1176 | // except for their spellings. |
| 1177 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling); |
| 1178 | else |
| 1179 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 1180 | // Fall through on error. |
| 1181 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
| 1182 | // Error if defining "defined": C99 6.10.8.4. |
| 1183 | Diag(MacroNameTok, diag::err_defined_macro_name); |
| 1184 | } else if (isDefineUndef && II->getMacroInfo() && |
| 1185 | II->getMacroInfo()->isBuiltinMacro()) { |
| 1186 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
| 1187 | if (isDefineUndef == 1) |
| 1188 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 1189 | else |
| 1190 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
| 1191 | } else { |
| 1192 | // Okay, we got a good identifier node. Return it. |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 1197 | // token kind to tok::eom. |
| 1198 | MacroNameTok.setKind(tok::eom); |
| 1199 | return DiscardUntilEndOfDirective(); |
| 1200 | } |
| 1201 | |
| 1202 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
| 1203 | /// not, emit a diagnostic and consume up until the eom. |
| 1204 | void Preprocessor::CheckEndOfDirective(const char *DirType) { |
| 1205 | LexerToken Tmp; |
| 1206 | Lex(Tmp); |
| 1207 | // There should be no tokens after the directive, but we allow them as an |
| 1208 | // extension. |
| 1209 | while (Tmp.getKind() == tok::comment) // Skip comments in -C mode. |
| 1210 | Lex(Tmp); |
| 1211 | |
| 1212 | if (Tmp.getKind() != tok::eom) { |
| 1213 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType); |
| 1214 | DiscardUntilEndOfDirective(); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 1221 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 1222 | /// file. Lex the rest of the file, until we see an #endif. If |
| 1223 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 1224 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 1225 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 1226 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 1227 | /// the first valid token. |
| 1228 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 1229 | bool FoundNonSkipPortion, |
| 1230 | bool FoundElse) { |
| 1231 | ++NumSkipped; |
| 1232 | assert(CurMacroExpander == 0 && CurLexer && |
| 1233 | "Lexing a macro, not a file?"); |
| 1234 | |
| 1235 | CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
| 1236 | FoundNonSkipPortion, FoundElse); |
| 1237 | |
| 1238 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 1239 | // disabling warnings, etc. |
| 1240 | CurLexer->LexingRawMode = true; |
| 1241 | LexerToken Tok; |
| 1242 | while (1) { |
| 1243 | CurLexer->Lex(Tok); |
| 1244 | |
| 1245 | // If this is the end of the buffer, we have an error. |
| 1246 | if (Tok.getKind() == tok::eof) { |
| 1247 | // Emit errors for each unterminated conditional on the stack, including |
| 1248 | // the current one. |
| 1249 | while (!CurLexer->ConditionalStack.empty()) { |
| 1250 | Diag(CurLexer->ConditionalStack.back().IfLoc, |
| 1251 | diag::err_pp_unterminated_conditional); |
| 1252 | CurLexer->ConditionalStack.pop_back(); |
| 1253 | } |
| 1254 | |
| 1255 | // Just return and let the caller lex after this #include. |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | // If this token is not a preprocessor directive, just skip it. |
| 1260 | if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine()) |
| 1261 | continue; |
| 1262 | |
| 1263 | // We just parsed a # character at the start of a line, so we're in |
| 1264 | // directive mode. Tell the lexer this so any newlines we see will be |
| 1265 | // converted into an EOM token (this terminates the macro). |
| 1266 | CurLexer->ParsingPreprocessorDirective = true; |
| 1267 | CurLexer->KeepCommentMode = false; |
| 1268 | |
| 1269 | |
| 1270 | // Read the next token, the directive flavor. |
| 1271 | LexUnexpandedToken(Tok); |
| 1272 | |
| 1273 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 1274 | // something bogus), skip it. |
| 1275 | if (Tok.getKind() != tok::identifier) { |
| 1276 | CurLexer->ParsingPreprocessorDirective = false; |
| 1277 | // Restore comment saving mode. |
| 1278 | CurLexer->KeepCommentMode = KeepComments; |
| 1279 | continue; |
| 1280 | } |
| 1281 | |
| 1282 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 1283 | // this is safe in the face of spelling differences, because there is no way |
| 1284 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 1285 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 1286 | // other common directives. |
| 1287 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 1288 | char FirstChar = RawCharData[0]; |
| 1289 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
| 1290 | FirstChar != 'i' && FirstChar != 'e') { |
| 1291 | CurLexer->ParsingPreprocessorDirective = false; |
| 1292 | // Restore comment saving mode. |
| 1293 | CurLexer->KeepCommentMode = KeepComments; |
| 1294 | continue; |
| 1295 | } |
| 1296 | |
| 1297 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 1298 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 1299 | // when skipping. |
| 1300 | // TODO: could do this with zero copies in the no-clean case by using |
| 1301 | // strncmp below. |
| 1302 | char Directive[20]; |
| 1303 | unsigned IdLen; |
| 1304 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
| 1305 | IdLen = Tok.getLength(); |
| 1306 | memcpy(Directive, RawCharData, IdLen); |
| 1307 | Directive[IdLen] = 0; |
| 1308 | } else { |
| 1309 | std::string DirectiveStr = getSpelling(Tok); |
| 1310 | IdLen = DirectiveStr.size(); |
| 1311 | if (IdLen >= 20) { |
| 1312 | CurLexer->ParsingPreprocessorDirective = false; |
| 1313 | // Restore comment saving mode. |
| 1314 | CurLexer->KeepCommentMode = KeepComments; |
| 1315 | continue; |
| 1316 | } |
| 1317 | memcpy(Directive, &DirectiveStr[0], IdLen); |
| 1318 | Directive[IdLen] = 0; |
| 1319 | } |
| 1320 | |
| 1321 | if (FirstChar == 'i' && Directive[1] == 'f') { |
| 1322 | if ((IdLen == 2) || // "if" |
| 1323 | (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" |
| 1324 | (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" |
| 1325 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 1326 | // bother parsing the condition. |
| 1327 | DiscardUntilEndOfDirective(); |
| 1328 | CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
| 1329 | /*foundnonskip*/false, |
| 1330 | /*fnddelse*/false); |
| 1331 | } |
| 1332 | } else if (FirstChar == 'e') { |
| 1333 | if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" |
| 1334 | CheckEndOfDirective("#endif"); |
| 1335 | PPConditionalInfo CondInfo; |
| 1336 | CondInfo.WasSkipping = true; // Silence bogus warning. |
| 1337 | bool InCond = CurLexer->popConditionalLevel(CondInfo); |
| 1338 | InCond = InCond; // Silence warning in no-asserts mode. |
| 1339 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 1340 | |
| 1341 | // If we popped the outermost skipping block, we're done skipping! |
| 1342 | if (!CondInfo.WasSkipping) |
| 1343 | break; |
| 1344 | } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". |
| 1345 | // #else directive in a skipping conditional. If not in some other |
| 1346 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 1347 | // as a non-skipping conditional. |
| 1348 | CheckEndOfDirective("#else"); |
| 1349 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1350 | |
| 1351 | // If this is a #else with a #else before it, report the error. |
| 1352 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
| 1353 | |
| 1354 | // Note that we've seen a #else in this conditional. |
| 1355 | CondInfo.FoundElse = true; |
| 1356 | |
| 1357 | // If the conditional is at the top level, and the #if block wasn't |
| 1358 | // entered, enter the #else block now. |
| 1359 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 1360 | CondInfo.FoundNonSkip = true; |
| 1361 | break; |
| 1362 | } |
| 1363 | } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". |
| 1364 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1365 | |
| 1366 | bool ShouldEnter; |
| 1367 | // If this is in a skipping block or if we're already handled this #if |
| 1368 | // block, don't bother parsing the condition. |
| 1369 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 1370 | DiscardUntilEndOfDirective(); |
| 1371 | ShouldEnter = false; |
| 1372 | } else { |
| 1373 | // Restore the value of LexingRawMode so that identifiers are |
| 1374 | // looked up, etc, inside the #elif expression. |
| 1375 | assert(CurLexer->LexingRawMode && "We have to be skipping here!"); |
| 1376 | CurLexer->LexingRawMode = false; |
| 1377 | IdentifierInfo *IfNDefMacro = 0; |
| 1378 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 1379 | CurLexer->LexingRawMode = true; |
| 1380 | } |
| 1381 | |
| 1382 | // If this is a #elif with a #else before it, report the error. |
| 1383 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
| 1384 | |
| 1385 | // If this condition is true, enter it! |
| 1386 | if (ShouldEnter) { |
| 1387 | CondInfo.FoundNonSkip = true; |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | CurLexer->ParsingPreprocessorDirective = false; |
| 1394 | // Restore comment saving mode. |
| 1395 | CurLexer->KeepCommentMode = KeepComments; |
| 1396 | } |
| 1397 | |
| 1398 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 1399 | // of the file, just stop skipping and return to lexing whatever came after |
| 1400 | // the #if block. |
| 1401 | CurLexer->LexingRawMode = false; |
| 1402 | } |
| 1403 | |
| 1404 | //===----------------------------------------------------------------------===// |
| 1405 | // Preprocessor Directive Handling. |
| 1406 | //===----------------------------------------------------------------------===// |
| 1407 | |
| 1408 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
| 1409 | /// at the start of a line. This consumes the directive, modifies the |
| 1410 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 1411 | /// read is the correct one. |
| 1412 | void Preprocessor::HandleDirective(LexerToken &Result) { |
| 1413 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
| 1414 | |
| 1415 | // We just parsed a # character at the start of a line, so we're in directive |
| 1416 | // mode. Tell the lexer this so any newlines we see will be converted into an |
| 1417 | // EOM token (which terminates the directive). |
| 1418 | CurLexer->ParsingPreprocessorDirective = true; |
| 1419 | |
| 1420 | ++NumDirectives; |
| 1421 | |
| 1422 | // We are about to read a token. For the multiple-include optimization FA to |
| 1423 | // work, we have to remember if we had read any tokens *before* this |
| 1424 | // pp-directive. |
| 1425 | bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal(); |
| 1426 | |
| 1427 | // Read the next token, the directive flavor. This isn't expanded due to |
| 1428 | // C99 6.10.3p8. |
| 1429 | LexUnexpandedToken(Result); |
| 1430 | |
| 1431 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 1432 | // #define A(x) #x |
| 1433 | // A(abc |
| 1434 | // #warning blah |
| 1435 | // def) |
| 1436 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 1437 | if (InMacroArgs) |
| 1438 | Diag(Result, diag::ext_embedded_directive); |
| 1439 | |
| 1440 | TryAgain: |
| 1441 | switch (Result.getKind()) { |
| 1442 | case tok::eom: |
| 1443 | return; // null directive. |
| 1444 | case tok::comment: |
| 1445 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 1446 | LexUnexpandedToken(Result); |
| 1447 | goto TryAgain; |
| 1448 | |
| 1449 | case tok::numeric_constant: |
| 1450 | // FIXME: implement # 7 line numbers! |
| 1451 | DiscardUntilEndOfDirective(); |
| 1452 | return; |
| 1453 | default: |
| 1454 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 1455 | if (II == 0) break; // Not an identifier. |
| 1456 | |
| 1457 | // Ask what the preprocessor keyword ID is. |
| 1458 | switch (II->getPPKeywordID()) { |
| 1459 | default: break; |
| 1460 | // C99 6.10.1 - Conditional Inclusion. |
| 1461 | case tok::pp_if: |
| 1462 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 1463 | case tok::pp_ifdef: |
| 1464 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 1465 | case tok::pp_ifndef: |
| 1466 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 1467 | case tok::pp_elif: |
| 1468 | return HandleElifDirective(Result); |
| 1469 | case tok::pp_else: |
| 1470 | return HandleElseDirective(Result); |
| 1471 | case tok::pp_endif: |
| 1472 | return HandleEndifDirective(Result); |
| 1473 | |
| 1474 | // C99 6.10.2 - Source File Inclusion. |
| 1475 | case tok::pp_include: |
| 1476 | return HandleIncludeDirective(Result); // Handle #include. |
| 1477 | |
| 1478 | // C99 6.10.3 - Macro Replacement. |
| 1479 | case tok::pp_define: |
| 1480 | return HandleDefineDirective(Result, false); |
| 1481 | case tok::pp_undef: |
| 1482 | return HandleUndefDirective(Result); |
| 1483 | |
| 1484 | // C99 6.10.4 - Line Control. |
| 1485 | case tok::pp_line: |
| 1486 | // FIXME: implement #line |
| 1487 | DiscardUntilEndOfDirective(); |
| 1488 | return; |
| 1489 | |
| 1490 | // C99 6.10.5 - Error Directive. |
| 1491 | case tok::pp_error: |
| 1492 | return HandleUserDiagnosticDirective(Result, false); |
| 1493 | |
| 1494 | // C99 6.10.6 - Pragma Directive. |
| 1495 | case tok::pp_pragma: |
| 1496 | return HandlePragmaDirective(); |
| 1497 | |
| 1498 | // GNU Extensions. |
| 1499 | case tok::pp_import: |
| 1500 | return HandleImportDirective(Result); |
| 1501 | case tok::pp_include_next: |
| 1502 | return HandleIncludeNextDirective(Result); |
| 1503 | |
| 1504 | case tok::pp_warning: |
| 1505 | Diag(Result, diag::ext_pp_warning_directive); |
| 1506 | return HandleUserDiagnosticDirective(Result, true); |
| 1507 | case tok::pp_ident: |
| 1508 | return HandleIdentSCCSDirective(Result); |
| 1509 | case tok::pp_sccs: |
| 1510 | return HandleIdentSCCSDirective(Result); |
| 1511 | case tok::pp_assert: |
| 1512 | //isExtension = true; // FIXME: implement #assert |
| 1513 | break; |
| 1514 | case tok::pp_unassert: |
| 1515 | //isExtension = true; // FIXME: implement #unassert |
| 1516 | break; |
| 1517 | |
| 1518 | // clang extensions. |
| 1519 | case tok::pp_define_target: |
| 1520 | return HandleDefineDirective(Result, true); |
| 1521 | case tok::pp_define_other_target: |
| 1522 | return HandleDefineOtherTargetDirective(Result); |
| 1523 | } |
| 1524 | break; |
| 1525 | } |
| 1526 | |
| 1527 | // If we reached here, the preprocessing token is not valid! |
| 1528 | Diag(Result, diag::err_pp_invalid_directive); |
| 1529 | |
| 1530 | // Read the rest of the PP line. |
| 1531 | DiscardUntilEndOfDirective(); |
| 1532 | |
| 1533 | // Okay, we're done parsing the directive. |
| 1534 | } |
| 1535 | |
| 1536 | void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok, |
| 1537 | bool isWarning) { |
| 1538 | // Read the rest of the line raw. We do this because we don't want macros |
| 1539 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 1540 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 1541 | // collapse multiple consequtive white space between tokens, but this isn't |
| 1542 | // specified by the standard. |
| 1543 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 1544 | |
| 1545 | unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error; |
| 1546 | return Diag(Tok, DiagID, Message); |
| 1547 | } |
| 1548 | |
| 1549 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 1550 | /// |
| 1551 | void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) { |
| 1552 | // Yes, this directive is an extension. |
| 1553 | Diag(Tok, diag::ext_pp_ident_directive); |
| 1554 | |
| 1555 | // Read the string argument. |
| 1556 | LexerToken StrTok; |
| 1557 | Lex(StrTok); |
| 1558 | |
| 1559 | // If the token kind isn't a string, it's a malformed directive. |
| 1560 | if (StrTok.getKind() != tok::string_literal && |
| 1561 | StrTok.getKind() != tok::wide_string_literal) |
| 1562 | return Diag(StrTok, diag::err_pp_malformed_ident); |
| 1563 | |
| 1564 | // Verify that there is nothing after the string, other than EOM. |
| 1565 | CheckEndOfDirective("#ident"); |
| 1566 | |
| 1567 | if (Callbacks) |
| 1568 | Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok)); |
| 1569 | } |
| 1570 | |
| 1571 | //===----------------------------------------------------------------------===// |
| 1572 | // Preprocessor Include Directive Handling. |
| 1573 | //===----------------------------------------------------------------------===// |
| 1574 | |
| 1575 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 1576 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 1577 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 1578 | /// caller is expected to provide a buffer that is large enough to hold the |
| 1579 | /// spelling of the filename, but is also expected to handle the case when |
| 1580 | /// this method decides to use a different buffer. |
| 1581 | bool Preprocessor::GetIncludeFilenameSpelling(const LexerToken &FilenameTok, |
| 1582 | const char *&BufStart, |
| 1583 | const char *&BufEnd) { |
| 1584 | // Get the text form of the filename. |
| 1585 | unsigned Len = getSpelling(FilenameTok, BufStart); |
| 1586 | BufEnd = BufStart+Len; |
| 1587 | assert(BufStart != BufEnd && "Can't have tokens with empty spellings!"); |
| 1588 | |
| 1589 | // Make sure the filename is <x> or "x". |
| 1590 | bool isAngled; |
| 1591 | if (BufStart[0] == '<') { |
| 1592 | if (BufEnd[-1] != '>') { |
| 1593 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1594 | BufStart = 0; |
| 1595 | return true; |
| 1596 | } |
| 1597 | isAngled = true; |
| 1598 | } else if (BufStart[0] == '"') { |
| 1599 | if (BufEnd[-1] != '"') { |
| 1600 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1601 | BufStart = 0; |
| 1602 | return true; |
| 1603 | } |
| 1604 | isAngled = false; |
| 1605 | } else { |
| 1606 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1607 | BufStart = 0; |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | // Diagnose #include "" as invalid. |
| 1612 | if (BufEnd-BufStart <= 2) { |
| 1613 | Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename); |
| 1614 | BufStart = 0; |
| 1615 | return ""; |
| 1616 | } |
| 1617 | |
| 1618 | // Skip the brackets. |
| 1619 | ++BufStart; |
| 1620 | --BufEnd; |
| 1621 | return isAngled; |
| 1622 | } |
| 1623 | |
| 1624 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1625 | /// file to be included from the lexer, then include it! This is a common |
| 1626 | /// routine with functionality shared between #include, #include_next and |
| 1627 | /// #import. |
| 1628 | void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok, |
| 1629 | const DirectoryLookup *LookupFrom, |
| 1630 | bool isImport) { |
| 1631 | |
| 1632 | LexerToken FilenameTok; |
| 1633 | CurLexer->LexIncludeFilename(FilenameTok); |
| 1634 | |
| 1635 | // If the token kind is EOM, the error has already been diagnosed. |
| 1636 | if (FilenameTok.getKind() == tok::eom) |
| 1637 | return; |
| 1638 | |
| 1639 | // Reserve a buffer to get the spelling. |
| 1640 | llvm::SmallVector<char, 128> FilenameBuffer; |
| 1641 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 1642 | |
| 1643 | const char *FilenameStart = &FilenameBuffer[0], *FilenameEnd; |
| 1644 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok, |
| 1645 | FilenameStart, FilenameEnd); |
| 1646 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1647 | // error. |
| 1648 | if (FilenameStart == 0) |
| 1649 | return; |
| 1650 | |
| 1651 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 1652 | // preprocessor to lex this in case lexing the filename entered a macro. |
| 1653 | CheckEndOfDirective("#include"); |
| 1654 | |
| 1655 | // Check that we don't have infinite #include recursion. |
| 1656 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) |
| 1657 | return Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1658 | |
| 1659 | // Search include directories. |
| 1660 | const DirectoryLookup *CurDir; |
| 1661 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
| 1662 | isAngled, LookupFrom, CurDir); |
| 1663 | if (File == 0) |
| 1664 | return Diag(FilenameTok, diag::err_pp_file_not_found, |
| 1665 | std::string(FilenameStart, FilenameEnd)); |
| 1666 | |
| 1667 | // Ask HeaderInfo if we should enter this #include file. |
| 1668 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
| 1669 | // If it returns true, #including this file will have no effect. |
| 1670 | return; |
| 1671 | } |
| 1672 | |
| 1673 | // Look up the file, create a File ID for it. |
| 1674 | unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation()); |
| 1675 | if (FileID == 0) |
| 1676 | return Diag(FilenameTok, diag::err_pp_file_not_found, |
| 1677 | std::string(FilenameStart, FilenameEnd)); |
| 1678 | |
| 1679 | // Finally, if all is good, enter the new file! |
| 1680 | EnterSourceFile(FileID, CurDir); |
| 1681 | } |
| 1682 | |
| 1683 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1684 | /// |
| 1685 | void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) { |
| 1686 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
| 1687 | |
| 1688 | // #include_next is like #include, except that we start searching after |
| 1689 | // the current found directory. If we can't do this, issue a |
| 1690 | // diagnostic. |
| 1691 | const DirectoryLookup *Lookup = CurDirLookup; |
| 1692 | if (isInPrimaryFile()) { |
| 1693 | Lookup = 0; |
| 1694 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 1695 | } else if (Lookup == 0) { |
| 1696 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 1697 | } else { |
| 1698 | // Start looking up in the next directory. |
| 1699 | ++Lookup; |
| 1700 | } |
| 1701 | |
| 1702 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1703 | } |
| 1704 | |
| 1705 | /// HandleImportDirective - Implements #import. |
| 1706 | /// |
| 1707 | void Preprocessor::HandleImportDirective(LexerToken &ImportTok) { |
| 1708 | Diag(ImportTok, diag::ext_pp_import_directive); |
| 1709 | |
| 1710 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1711 | } |
| 1712 | |
| 1713 | //===----------------------------------------------------------------------===// |
| 1714 | // Preprocessor Macro Directive Handling. |
| 1715 | //===----------------------------------------------------------------------===// |
| 1716 | |
| 1717 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1718 | /// definition has just been read. Lex the rest of the arguments and the |
| 1719 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1720 | /// parsing the arg list. |
| 1721 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1722 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; |
| 1723 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1724 | LexerToken Tok; |
| 1725 | while (1) { |
| 1726 | LexUnexpandedToken(Tok); |
| 1727 | switch (Tok.getKind()) { |
| 1728 | case tok::r_paren: |
| 1729 | // Found the end of the argument list. |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1730 | if (Arguments.empty()) { // #define FOO() |
| 1731 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
| 1732 | return false; |
| 1733 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | // Otherwise we have #define FOO(A,) |
| 1735 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1736 | return true; |
| 1737 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1738 | // Warn if use of C99 feature in non-C99 mode. |
| 1739 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1740 | |
| 1741 | // Lex the token after the identifier. |
| 1742 | LexUnexpandedToken(Tok); |
| 1743 | if (Tok.getKind() != tok::r_paren) { |
| 1744 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1745 | return true; |
| 1746 | } |
| 1747 | // Add the __VA_ARGS__ identifier as an argument. |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1748 | Arguments.push_back(Ident__VA_ARGS__); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | MI->setIsC99Varargs(); |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1750 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | case tok::eom: // #define X( |
| 1753 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1754 | return true; |
| 1755 | default: |
| 1756 | // Handle keywords and identifiers here to accept things like |
| 1757 | // #define Foo(for) for. |
| 1758 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1759 | if (II == 0) { |
| 1760 | // #define X(1 |
| 1761 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
| 1765 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1766 | // #define X(A,A. |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1767 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
| 1768 | Arguments.end()) { // C99 6.10.3p6 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1769 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName()); |
| 1770 | return true; |
| 1771 | } |
| 1772 | |
| 1773 | // Add the argument to the macro info. |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1774 | Arguments.push_back(II); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1775 | |
| 1776 | // Lex the token after the identifier. |
| 1777 | LexUnexpandedToken(Tok); |
| 1778 | |
| 1779 | switch (Tok.getKind()) { |
| 1780 | default: // #define X(A B |
| 1781 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1782 | return true; |
| 1783 | case tok::r_paren: // #define X(A) |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1784 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1785 | return false; |
| 1786 | case tok::comma: // #define X(A, |
| 1787 | break; |
| 1788 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1789 | // Diagnose extension. |
| 1790 | Diag(Tok, diag::ext_named_variadic_macro); |
| 1791 | |
| 1792 | // Lex the token after the identifier. |
| 1793 | LexUnexpandedToken(Tok); |
| 1794 | if (Tok.getKind() != tok::r_paren) { |
| 1795 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1796 | return true; |
| 1797 | } |
| 1798 | |
| 1799 | MI->setIsGNUVarargs(); |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1800 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1801 | return false; |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1808 | /// line then lets the caller lex the next real token. If 'isTargetSpecific' is |
| 1809 | /// true, then this is a "#define_target", otherwise this is a "#define". |
| 1810 | /// |
| 1811 | void Preprocessor::HandleDefineDirective(LexerToken &DefineTok, |
| 1812 | bool isTargetSpecific) { |
| 1813 | ++NumDefined; |
| 1814 | |
| 1815 | LexerToken MacroNameTok; |
| 1816 | ReadMacroName(MacroNameTok, 1); |
| 1817 | |
| 1818 | // Error reading macro name? If so, diagnostic already issued. |
| 1819 | if (MacroNameTok.getKind() == tok::eom) |
| 1820 | return; |
Chris Lattner | c215bd6 | 2007-07-14 22:11:41 +0000 | [diff] [blame] | 1821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1822 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1823 | // mode. |
| 1824 | CurLexer->KeepCommentMode = KeepMacroComments; |
| 1825 | |
| 1826 | // Create the new macro. |
| 1827 | MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation()); |
| 1828 | if (isTargetSpecific) MI->setIsTargetSpecific(); |
| 1829 | |
| 1830 | // If the identifier is an 'other target' macro, clear this bit. |
| 1831 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false); |
| 1832 | |
| 1833 | |
| 1834 | LexerToken Tok; |
| 1835 | LexUnexpandedToken(Tok); |
| 1836 | |
| 1837 | // If this is a function-like macro definition, parse the argument list, |
| 1838 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1839 | // check other constraints on the first token of the macro body. |
| 1840 | if (Tok.getKind() == tok::eom) { |
| 1841 | // If there is no body to this macro, we have no special handling here. |
| 1842 | } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) { |
| 1843 | // This is a function-like macro definition. Read the argument list. |
| 1844 | MI->setIsFunctionLike(); |
| 1845 | if (ReadMacroDefinitionArgList(MI)) { |
| 1846 | // Forget about MI. |
| 1847 | delete MI; |
| 1848 | // Throw away the rest of the line. |
| 1849 | if (CurLexer->ParsingPreprocessorDirective) |
| 1850 | DiscardUntilEndOfDirective(); |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | // Read the first token after the arg list for down below. |
| 1855 | LexUnexpandedToken(Tok); |
| 1856 | } else if (!Tok.hasLeadingSpace()) { |
| 1857 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1858 | // a diagnostic for something like "#define X+". |
| 1859 | if (Features.C99) { |
| 1860 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
| 1861 | } else { |
| 1862 | // FIXME: C90/C++ do not get this diagnostic, but it does get a similar |
| 1863 | // one in some cases! |
| 1864 | } |
| 1865 | } else { |
| 1866 | // This is a normal token with leading space. Clear the leading space |
| 1867 | // marker on the first token to get proper expansion. |
| 1868 | Tok.clearFlag(LexerToken::LeadingSpace); |
| 1869 | } |
| 1870 | |
| 1871 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1872 | // the GNU named varargs extension, enabled __VA_ARGS__. |
| 1873 | |
| 1874 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1875 | // This gets unpoisoned where it is allowed. |
| 1876 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1877 | if (MI->isC99Varargs()) |
| 1878 | Ident__VA_ARGS__->setIsPoisoned(false); |
| 1879 | |
| 1880 | // Read the rest of the macro body. |
Chris Lattner | b5e240f | 2007-07-14 21:54:03 +0000 | [diff] [blame] | 1881 | if (MI->isObjectLike()) { |
| 1882 | // Object-like macros are very simple, just read their body. |
| 1883 | while (Tok.getKind() != tok::eom) { |
| 1884 | MI->AddTokenToBody(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1885 | // Get the next token of the macro. |
| 1886 | LexUnexpandedToken(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Chris Lattner | b5e240f | 2007-07-14 21:54:03 +0000 | [diff] [blame] | 1889 | } else { |
| 1890 | // Otherwise, read the body of a function-like macro. This has to validate |
| 1891 | // the # (stringize) operator. |
| 1892 | while (Tok.getKind() != tok::eom) { |
| 1893 | MI->AddTokenToBody(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | b5e240f | 2007-07-14 21:54:03 +0000 | [diff] [blame] | 1895 | // Check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1896 | // parameters in function-like macro expansions. |
| 1897 | if (Tok.getKind() != tok::hash) { |
| 1898 | // Get the next token of the macro. |
| 1899 | LexUnexpandedToken(Tok); |
| 1900 | continue; |
| 1901 | } |
| 1902 | |
| 1903 | // Get the next token of the macro. |
| 1904 | LexUnexpandedToken(Tok); |
| 1905 | |
| 1906 | // Not a macro arg identifier? |
| 1907 | if (!Tok.getIdentifierInfo() || |
| 1908 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1909 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1910 | delete MI; |
| 1911 | |
| 1912 | // Disable __VA_ARGS__ again. |
| 1913 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1914 | return; |
| 1915 | } |
| 1916 | |
| 1917 | // Things look ok, add the param name token to the macro. |
| 1918 | MI->AddTokenToBody(Tok); |
| 1919 | |
| 1920 | // Get the next token of the macro. |
| 1921 | LexUnexpandedToken(Tok); |
| 1922 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Chris Lattner | c215bd6 | 2007-07-14 22:11:41 +0000 | [diff] [blame] | 1925 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1926 | // Disable __VA_ARGS__ again. |
| 1927 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1928 | |
| 1929 | // Check that there is no paste (##) operator at the begining or end of the |
| 1930 | // replacement list. |
| 1931 | unsigned NumTokens = MI->getNumTokens(); |
| 1932 | if (NumTokens != 0) { |
| 1933 | if (MI->getReplacementToken(0).getKind() == tok::hashhash) { |
| 1934 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
| 1935 | delete MI; |
| 1936 | return; |
| 1937 | } |
| 1938 | if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) { |
| 1939 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
| 1940 | delete MI; |
| 1941 | return; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | // If this is the primary source file, remember that this macro hasn't been |
| 1946 | // used yet. |
| 1947 | if (isInPrimaryFile()) |
| 1948 | MI->setIsUsed(false); |
| 1949 | |
| 1950 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1951 | // the macro bodies are identical and free the old definition. |
| 1952 | if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
| 1953 | if (!OtherMI->isUsed()) |
| 1954 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1955 | |
| 1956 | // Macros must be identical. This means all tokes and whitespace separation |
| 1957 | // must be the same. C99 6.10.3.2. |
| 1958 | if (!MI->isIdenticalTo(*OtherMI, *this)) { |
| 1959 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef, |
| 1960 | MacroNameTok.getIdentifierInfo()->getName()); |
| 1961 | Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2); |
| 1962 | } |
| 1963 | delete OtherMI; |
| 1964 | } |
| 1965 | |
| 1966 | MacroNameTok.getIdentifierInfo()->setMacroInfo(MI); |
| 1967 | } |
| 1968 | |
| 1969 | /// HandleDefineOtherTargetDirective - Implements #define_other_target. |
| 1970 | void Preprocessor::HandleDefineOtherTargetDirective(LexerToken &Tok) { |
| 1971 | LexerToken MacroNameTok; |
| 1972 | ReadMacroName(MacroNameTok, 1); |
| 1973 | |
| 1974 | // Error reading macro name? If so, diagnostic already issued. |
| 1975 | if (MacroNameTok.getKind() == tok::eom) |
| 1976 | return; |
| 1977 | |
| 1978 | // Check to see if this is the last token on the #undef line. |
| 1979 | CheckEndOfDirective("#define_other_target"); |
| 1980 | |
| 1981 | // If there is already a macro defined by this name, turn it into a |
| 1982 | // target-specific define. |
| 1983 | if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
| 1984 | MI->setIsTargetSpecific(true); |
| 1985 | return; |
| 1986 | } |
| 1987 | |
| 1988 | // Mark the identifier as being a macro on some other target. |
| 1989 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(); |
| 1990 | } |
| 1991 | |
| 1992 | |
| 1993 | /// HandleUndefDirective - Implements #undef. |
| 1994 | /// |
| 1995 | void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) { |
| 1996 | ++NumUndefined; |
| 1997 | |
| 1998 | LexerToken MacroNameTok; |
| 1999 | ReadMacroName(MacroNameTok, 2); |
| 2000 | |
| 2001 | // Error reading macro name? If so, diagnostic already issued. |
| 2002 | if (MacroNameTok.getKind() == tok::eom) |
| 2003 | return; |
| 2004 | |
| 2005 | // Check to see if this is the last token on the #undef line. |
| 2006 | CheckEndOfDirective("#undef"); |
| 2007 | |
| 2008 | // Okay, we finally have a valid identifier to undef. |
| 2009 | MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo(); |
| 2010 | |
| 2011 | // #undef untaints an identifier if it were marked by define_other_target. |
| 2012 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false); |
| 2013 | |
| 2014 | // If the macro is not defined, this is a noop undef, just return. |
| 2015 | if (MI == 0) return; |
| 2016 | |
| 2017 | if (!MI->isUsed()) |
| 2018 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
| 2019 | |
| 2020 | // Free macro definition. |
| 2021 | delete MI; |
| 2022 | MacroNameTok.getIdentifierInfo()->setMacroInfo(0); |
| 2023 | } |
| 2024 | |
| 2025 | |
| 2026 | //===----------------------------------------------------------------------===// |
| 2027 | // Preprocessor Conditional Directive Handling. |
| 2028 | //===----------------------------------------------------------------------===// |
| 2029 | |
| 2030 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 2031 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 2032 | /// if any tokens have been returned or pp-directives activated before this |
| 2033 | /// #ifndef has been lexed. |
| 2034 | /// |
| 2035 | void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef, |
| 2036 | bool ReadAnyTokensBeforeDirective) { |
| 2037 | ++NumIf; |
| 2038 | LexerToken DirectiveTok = Result; |
| 2039 | |
| 2040 | LexerToken MacroNameTok; |
| 2041 | ReadMacroName(MacroNameTok); |
| 2042 | |
| 2043 | // Error reading macro name? If so, diagnostic already issued. |
| 2044 | if (MacroNameTok.getKind() == tok::eom) |
| 2045 | return; |
| 2046 | |
| 2047 | // Check to see if this is the last token on the #if[n]def line. |
| 2048 | CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef"); |
| 2049 | |
| 2050 | // If the start of a top-level #ifdef, inform MIOpt. |
| 2051 | if (!ReadAnyTokensBeforeDirective && |
| 2052 | CurLexer->getConditionalStackDepth() == 0) { |
| 2053 | assert(isIfndef && "#ifdef shouldn't reach here"); |
| 2054 | CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); |
| 2055 | } |
| 2056 | |
| 2057 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 2058 | MacroInfo *MI = MII->getMacroInfo(); |
| 2059 | |
| 2060 | // If there is a macro, process it. |
| 2061 | if (MI) { |
| 2062 | // Mark it used. |
| 2063 | MI->setIsUsed(true); |
| 2064 | |
| 2065 | // If this is the first use of a target-specific macro, warn about it. |
| 2066 | if (MI->isTargetSpecific()) { |
| 2067 | MI->setIsTargetSpecific(false); // Don't warn on second use. |
| 2068 | getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(), |
| 2069 | diag::port_target_macro_use); |
| 2070 | } |
| 2071 | } else { |
| 2072 | // Use of a target-specific macro for some other target? If so, warn. |
| 2073 | if (MII->isOtherTargetMacro()) { |
| 2074 | MII->setIsOtherTargetMacro(false); // Don't warn on second use. |
| 2075 | getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(), |
| 2076 | diag::port_target_macro_use); |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | // Should we include the stuff contained by this directive? |
| 2081 | if (!MI == isIfndef) { |
| 2082 | // Yes, remember that we are inside a conditional, then lex the next token. |
| 2083 | CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, |
| 2084 | /*foundnonskip*/true, /*foundelse*/false); |
| 2085 | } else { |
| 2086 | // No, skip the contents of this block and return the first token after it. |
| 2087 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 2088 | /*Foundnonskip*/false, |
| 2089 | /*FoundElse*/false); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | /// HandleIfDirective - Implements the #if directive. |
| 2094 | /// |
| 2095 | void Preprocessor::HandleIfDirective(LexerToken &IfToken, |
| 2096 | bool ReadAnyTokensBeforeDirective) { |
| 2097 | ++NumIf; |
| 2098 | |
| 2099 | // Parse and evaluation the conditional expression. |
| 2100 | IdentifierInfo *IfNDefMacro = 0; |
| 2101 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
| 2102 | |
| 2103 | // Should we include the stuff contained by this directive? |
| 2104 | if (ConditionalTrue) { |
| 2105 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 2106 | // directive seen, handle it for the multiple-include optimization. |
| 2107 | if (!ReadAnyTokensBeforeDirective && |
| 2108 | CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro) |
| 2109 | CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
| 2110 | |
| 2111 | // Yes, remember that we are inside a conditional, then lex the next token. |
| 2112 | CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
| 2113 | /*foundnonskip*/true, /*foundelse*/false); |
| 2114 | } else { |
| 2115 | // No, skip the contents of this block and return the first token after it. |
| 2116 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
| 2117 | /*FoundElse*/false); |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | /// HandleEndifDirective - Implements the #endif directive. |
| 2122 | /// |
| 2123 | void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) { |
| 2124 | ++NumEndif; |
| 2125 | |
| 2126 | // Check that this is the whole directive. |
| 2127 | CheckEndOfDirective("#endif"); |
| 2128 | |
| 2129 | PPConditionalInfo CondInfo; |
| 2130 | if (CurLexer->popConditionalLevel(CondInfo)) { |
| 2131 | // No conditionals on the stack: this is an #endif without an #if. |
| 2132 | return Diag(EndifToken, diag::err_pp_endif_without_if); |
| 2133 | } |
| 2134 | |
| 2135 | // If this the end of a top-level #endif, inform MIOpt. |
| 2136 | if (CurLexer->getConditionalStackDepth() == 0) |
| 2137 | CurLexer->MIOpt.ExitTopLevelConditional(); |
| 2138 | |
| 2139 | assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode && |
| 2140 | "This code should only be reachable in the non-skipping case!"); |
| 2141 | } |
| 2142 | |
| 2143 | |
| 2144 | void Preprocessor::HandleElseDirective(LexerToken &Result) { |
| 2145 | ++NumElse; |
| 2146 | |
| 2147 | // #else directive in a non-skipping conditional... start skipping. |
| 2148 | CheckEndOfDirective("#else"); |
| 2149 | |
| 2150 | PPConditionalInfo CI; |
| 2151 | if (CurLexer->popConditionalLevel(CI)) |
| 2152 | return Diag(Result, diag::pp_err_else_without_if); |
| 2153 | |
| 2154 | // If this is a top-level #else, inform the MIOpt. |
| 2155 | if (CurLexer->getConditionalStackDepth() == 0) |
| 2156 | CurLexer->MIOpt.FoundTopLevelElse(); |
| 2157 | |
| 2158 | // If this is a #else with a #else before it, report the error. |
| 2159 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
| 2160 | |
| 2161 | // Finally, skip the rest of the contents of this block and return the first |
| 2162 | // token after it. |
| 2163 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 2164 | /*FoundElse*/true); |
| 2165 | } |
| 2166 | |
| 2167 | void Preprocessor::HandleElifDirective(LexerToken &ElifToken) { |
| 2168 | ++NumElse; |
| 2169 | |
| 2170 | // #elif directive in a non-skipping conditional... start skipping. |
| 2171 | // We don't care what the condition is, because we will always skip it (since |
| 2172 | // the block immediately before it was included). |
| 2173 | DiscardUntilEndOfDirective(); |
| 2174 | |
| 2175 | PPConditionalInfo CI; |
| 2176 | if (CurLexer->popConditionalLevel(CI)) |
| 2177 | return Diag(ElifToken, diag::pp_err_elif_without_if); |
| 2178 | |
| 2179 | // If this is a top-level #elif, inform the MIOpt. |
| 2180 | if (CurLexer->getConditionalStackDepth() == 0) |
| 2181 | CurLexer->MIOpt.FoundTopLevelElse(); |
| 2182 | |
| 2183 | // If this is a #elif with a #else before it, report the error. |
| 2184 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
| 2185 | |
| 2186 | // Finally, skip the rest of the contents of this block and return the first |
| 2187 | // token after it. |
| 2188 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 2189 | /*FoundElse*/CI.FoundElse); |
| 2190 | } |
| 2191 | |