PPDirectives.cpp revision 2d474ba9e8ae43a1a5a9f72718c0d79092b9453f
1//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements # directive processing for the Preprocessor. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Lex/Preprocessor.h" 15#include "clang/Lex/LiteralSupport.h" 16#include "clang/Lex/HeaderSearch.h" 17#include "clang/Lex/MacroInfo.h" 18#include "clang/Lex/LexDiagnostic.h" 19#include "clang/Basic/FileManager.h" 20#include "clang/Basic/SourceManager.h" 21#include "llvm/ADT/APInt.h" 22using namespace clang; 23 24//===----------------------------------------------------------------------===// 25// Utility Methods for Preprocessor Directive Handling. 26//===----------------------------------------------------------------------===// 27 28MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { 29 MacroInfo *MI; 30 31 if (!MICache.empty()) { 32 MI = MICache.back(); 33 MICache.pop_back(); 34 } else 35 MI = (MacroInfo*) BP.Allocate<MacroInfo>(); 36 new (MI) MacroInfo(L); 37 return MI; 38} 39 40/// ReleaseMacroInfo - Release the specified MacroInfo. This memory will 41/// be reused for allocating new MacroInfo objects. 42void Preprocessor::ReleaseMacroInfo(MacroInfo* MI) { 43 MICache.push_back(MI); 44 MI->FreeArgumentList(BP); 45} 46 47 48/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the 49/// current line until the tok::eom token is found. 50void Preprocessor::DiscardUntilEndOfDirective() { 51 Token Tmp; 52 do { 53 LexUnexpandedToken(Tmp); 54 } while (Tmp.isNot(tok::eom)); 55} 56 57/// ReadMacroName - Lex and validate a macro name, which occurs after a 58/// #define or #undef. This sets the token kind to eom and discards the rest 59/// of the macro line if the macro name is invalid. isDefineUndef is 1 if 60/// this is due to a a #define, 2 if #undef directive, 0 if it is something 61/// else (e.g. #ifdef). 62void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { 63 // Read the token, don't allow macro expansion on it. 64 LexUnexpandedToken(MacroNameTok); 65 66 // Missing macro name? 67 if (MacroNameTok.is(tok::eom)) { 68 Diag(MacroNameTok, diag::err_pp_missing_macro_name); 69 return; 70 } 71 72 IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 73 if (II == 0) { 74 bool Invalid = false; 75 std::string Spelling = getSpelling(MacroNameTok, &Invalid); 76 if (Invalid) 77 return; 78 79 const IdentifierInfo &Info = Identifiers.get(Spelling); 80 if (Info.isCPlusPlusOperatorKeyword()) 81 // C++ 2.5p2: Alternative tokens behave the same as its primary token 82 // except for their spellings. 83 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; 84 else 85 Diag(MacroNameTok, diag::err_pp_macro_not_identifier); 86 // Fall through on error. 87 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { 88 // Error if defining "defined": C99 6.10.8.4. 89 Diag(MacroNameTok, diag::err_defined_macro_name); 90 } else if (isDefineUndef && II->hasMacroDefinition() && 91 getMacroInfo(II)->isBuiltinMacro()) { 92 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. 93 if (isDefineUndef == 1) 94 Diag(MacroNameTok, diag::pp_redef_builtin_macro); 95 else 96 Diag(MacroNameTok, diag::pp_undef_builtin_macro); 97 } else { 98 // Okay, we got a good identifier node. Return it. 99 return; 100 } 101 102 // Invalid macro name, read and discard the rest of the line. Then set the 103 // token kind to tok::eom. 104 MacroNameTok.setKind(tok::eom); 105 return DiscardUntilEndOfDirective(); 106} 107 108/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If 109/// not, emit a diagnostic and consume up until the eom. If EnableMacros is 110/// true, then we consider macros that expand to zero tokens as being ok. 111void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { 112 Token Tmp; 113 // Lex unexpanded tokens for most directives: macros might expand to zero 114 // tokens, causing us to miss diagnosing invalid lines. Some directives (like 115 // #line) allow empty macros. 116 if (EnableMacros) 117 Lex(Tmp); 118 else 119 LexUnexpandedToken(Tmp); 120 121 // There should be no tokens after the directive, but we allow them as an 122 // extension. 123 while (Tmp.is(tok::comment)) // Skip comments in -C mode. 124 LexUnexpandedToken(Tmp); 125 126 if (Tmp.isNot(tok::eom)) { 127 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, 128 // because it is more trouble than it is worth to insert /**/ and check that 129 // there is no /**/ in the range also. 130 FixItHint Hint; 131 if (Features.GNUMode || Features.C99 || Features.CPlusPlus) 132 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); 133 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; 134 DiscardUntilEndOfDirective(); 135 } 136} 137 138 139 140/// SkipExcludedConditionalBlock - We just read a #if or related directive and 141/// decided that the subsequent tokens are in the #if'd out portion of the 142/// file. Lex the rest of the file, until we see an #endif. If 143/// FoundNonSkipPortion is true, then we have already emitted code for part of 144/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk 145/// is true, then #else directives are ok, if not, then we have already seen one 146/// so a #else directive is a duplicate. When this returns, the caller can lex 147/// the first valid token. 148void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, 149 bool FoundNonSkipPortion, 150 bool FoundElse) { 151 ++NumSkipped; 152 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); 153 154 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, 155 FoundNonSkipPortion, FoundElse); 156 157 if (CurPTHLexer) { 158 PTHSkipExcludedConditionalBlock(); 159 return; 160 } 161 162 // Enter raw mode to disable identifier lookup (and thus macro expansion), 163 // disabling warnings, etc. 164 CurPPLexer->LexingRawMode = true; 165 Token Tok; 166 while (1) { 167 CurLexer->Lex(Tok); 168 169 // If this is the end of the buffer, we have an error. 170 if (Tok.is(tok::eof)) { 171 // Emit errors for each unterminated conditional on the stack, including 172 // the current one. 173 while (!CurPPLexer->ConditionalStack.empty()) { 174 if (!isCodeCompletionFile(Tok.getLocation())) 175 Diag(CurPPLexer->ConditionalStack.back().IfLoc, 176 diag::err_pp_unterminated_conditional); 177 CurPPLexer->ConditionalStack.pop_back(); 178 } 179 180 // Just return and let the caller lex after this #include. 181 break; 182 } 183 184 // If this token is not a preprocessor directive, just skip it. 185 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) 186 continue; 187 188 // We just parsed a # character at the start of a line, so we're in 189 // directive mode. Tell the lexer this so any newlines we see will be 190 // converted into an EOM token (this terminates the macro). 191 CurPPLexer->ParsingPreprocessorDirective = true; 192 if (CurLexer) CurLexer->SetCommentRetentionState(false); 193 194 195 // Read the next token, the directive flavor. 196 LexUnexpandedToken(Tok); 197 198 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or 199 // something bogus), skip it. 200 if (Tok.isNot(tok::identifier)) { 201 CurPPLexer->ParsingPreprocessorDirective = false; 202 // Restore comment saving mode. 203 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 204 continue; 205 } 206 207 // If the first letter isn't i or e, it isn't intesting to us. We know that 208 // this is safe in the face of spelling differences, because there is no way 209 // to spell an i/e in a strange way that is another letter. Skipping this 210 // allows us to avoid looking up the identifier info for #define/#undef and 211 // other common directives. 212 bool Invalid = false; 213 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation(), 214 &Invalid); 215 if (Invalid) 216 return; 217 218 char FirstChar = RawCharData[0]; 219 if (FirstChar >= 'a' && FirstChar <= 'z' && 220 FirstChar != 'i' && FirstChar != 'e') { 221 CurPPLexer->ParsingPreprocessorDirective = false; 222 // Restore comment saving mode. 223 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 224 continue; 225 } 226 227 // Get the identifier name without trigraphs or embedded newlines. Note 228 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled 229 // when skipping. 230 char DirectiveBuf[20]; 231 llvm::StringRef Directive; 232 if (!Tok.needsCleaning() && Tok.getLength() < 20) { 233 Directive = llvm::StringRef(RawCharData, Tok.getLength()); 234 } else { 235 std::string DirectiveStr = getSpelling(Tok); 236 unsigned IdLen = DirectiveStr.size(); 237 if (IdLen >= 20) { 238 CurPPLexer->ParsingPreprocessorDirective = false; 239 // Restore comment saving mode. 240 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 241 continue; 242 } 243 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); 244 Directive = llvm::StringRef(DirectiveBuf, IdLen); 245 } 246 247 if (Directive.startswith("if")) { 248 llvm::StringRef Sub = Directive.substr(2); 249 if (Sub.empty() || // "if" 250 Sub == "def" || // "ifdef" 251 Sub == "ndef") { // "ifndef" 252 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't 253 // bother parsing the condition. 254 DiscardUntilEndOfDirective(); 255 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, 256 /*foundnonskip*/false, 257 /*fnddelse*/false); 258 } 259 } else if (Directive[0] == 'e') { 260 llvm::StringRef Sub = Directive.substr(1); 261 if (Sub == "ndif") { // "endif" 262 CheckEndOfDirective("endif"); 263 PPConditionalInfo CondInfo; 264 CondInfo.WasSkipping = true; // Silence bogus warning. 265 bool InCond = CurPPLexer->popConditionalLevel(CondInfo); 266 InCond = InCond; // Silence warning in no-asserts mode. 267 assert(!InCond && "Can't be skipping if not in a conditional!"); 268 269 // If we popped the outermost skipping block, we're done skipping! 270 if (!CondInfo.WasSkipping) 271 break; 272 } else if (Sub == "lse") { // "else". 273 // #else directive in a skipping conditional. If not in some other 274 // skipping conditional, and if #else hasn't already been seen, enter it 275 // as a non-skipping conditional. 276 DiscardUntilEndOfDirective(); // C99 6.10p4. 277 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 278 279 // If this is a #else with a #else before it, report the error. 280 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); 281 282 // Note that we've seen a #else in this conditional. 283 CondInfo.FoundElse = true; 284 285 // If the conditional is at the top level, and the #if block wasn't 286 // entered, enter the #else block now. 287 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { 288 CondInfo.FoundNonSkip = true; 289 break; 290 } 291 } else if (Sub == "lif") { // "elif". 292 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 293 294 bool ShouldEnter; 295 // If this is in a skipping block or if we're already handled this #if 296 // block, don't bother parsing the condition. 297 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { 298 DiscardUntilEndOfDirective(); 299 ShouldEnter = false; 300 } else { 301 // Restore the value of LexingRawMode so that identifiers are 302 // looked up, etc, inside the #elif expression. 303 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); 304 CurPPLexer->LexingRawMode = false; 305 IdentifierInfo *IfNDefMacro = 0; 306 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 307 CurPPLexer->LexingRawMode = true; 308 } 309 310 // If this is a #elif with a #else before it, report the error. 311 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); 312 313 // If this condition is true, enter it! 314 if (ShouldEnter) { 315 CondInfo.FoundNonSkip = true; 316 break; 317 } 318 } 319 } 320 321 CurPPLexer->ParsingPreprocessorDirective = false; 322 // Restore comment saving mode. 323 if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); 324 } 325 326 // Finally, if we are out of the conditional (saw an #endif or ran off the end 327 // of the file, just stop skipping and return to lexing whatever came after 328 // the #if block. 329 CurPPLexer->LexingRawMode = false; 330} 331 332void Preprocessor::PTHSkipExcludedConditionalBlock() { 333 334 while (1) { 335 assert(CurPTHLexer); 336 assert(CurPTHLexer->LexingRawMode == false); 337 338 // Skip to the next '#else', '#elif', or #endif. 339 if (CurPTHLexer->SkipBlock()) { 340 // We have reached an #endif. Both the '#' and 'endif' tokens 341 // have been consumed by the PTHLexer. Just pop off the condition level. 342 PPConditionalInfo CondInfo; 343 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); 344 InCond = InCond; // Silence warning in no-asserts mode. 345 assert(!InCond && "Can't be skipping if not in a conditional!"); 346 break; 347 } 348 349 // We have reached a '#else' or '#elif'. Lex the next token to get 350 // the directive flavor. 351 Token Tok; 352 LexUnexpandedToken(Tok); 353 354 // We can actually look up the IdentifierInfo here since we aren't in 355 // raw mode. 356 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); 357 358 if (K == tok::pp_else) { 359 // #else: Enter the else condition. We aren't in a nested condition 360 // since we skip those. We're always in the one matching the last 361 // blocked we skipped. 362 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 363 // Note that we've seen a #else in this conditional. 364 CondInfo.FoundElse = true; 365 366 // If the #if block wasn't entered then enter the #else block now. 367 if (!CondInfo.FoundNonSkip) { 368 CondInfo.FoundNonSkip = true; 369 370 // Scan until the eom token. 371 CurPTHLexer->ParsingPreprocessorDirective = true; 372 DiscardUntilEndOfDirective(); 373 CurPTHLexer->ParsingPreprocessorDirective = false; 374 375 break; 376 } 377 378 // Otherwise skip this block. 379 continue; 380 } 381 382 assert(K == tok::pp_elif); 383 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 384 385 // If this is a #elif with a #else before it, report the error. 386 if (CondInfo.FoundElse) 387 Diag(Tok, diag::pp_err_elif_after_else); 388 389 // If this is in a skipping block or if we're already handled this #if 390 // block, don't bother parsing the condition. We just skip this block. 391 if (CondInfo.FoundNonSkip) 392 continue; 393 394 // Evaluate the condition of the #elif. 395 IdentifierInfo *IfNDefMacro = 0; 396 CurPTHLexer->ParsingPreprocessorDirective = true; 397 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 398 CurPTHLexer->ParsingPreprocessorDirective = false; 399 400 // If this condition is true, enter it! 401 if (ShouldEnter) { 402 CondInfo.FoundNonSkip = true; 403 break; 404 } 405 406 // Otherwise, skip this block and go to the next one. 407 continue; 408 } 409} 410 411/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 412/// return null on failure. isAngled indicates whether the file reference is 413/// for system #include's or not (i.e. using <> instead of ""). 414const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename, 415 bool isAngled, 416 const DirectoryLookup *FromDir, 417 const DirectoryLookup *&CurDir) { 418 // If the header lookup mechanism may be relative to the current file, pass in 419 // info about where the current file is. 420 const FileEntry *CurFileEnt = 0; 421 if (!FromDir) { 422 FileID FID = getCurrentFileLexer()->getFileID(); 423 CurFileEnt = SourceMgr.getFileEntryForID(FID); 424 425 // If there is no file entry associated with this file, it must be the 426 // predefines buffer. Any other file is not lexed with a normal lexer, so 427 // it won't be scanned for preprocessor directives. If we have the 428 // predefines buffer, resolve #include references (which come from the 429 // -include command line argument) as if they came from the main file, this 430 // affects file lookup etc. 431 if (CurFileEnt == 0) { 432 FID = SourceMgr.getMainFileID(); 433 CurFileEnt = SourceMgr.getFileEntryForID(FID); 434 } 435 } 436 437 // Do a standard file entry lookup. 438 CurDir = CurDirLookup; 439 const FileEntry *FE = 440 HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt); 441 if (FE) return FE; 442 443 // Otherwise, see if this is a subframework header. If so, this is relative 444 // to one of the headers on the #include stack. Walk the list of the current 445 // headers on the #include stack and pass them to HeaderInfo. 446 if (IsFileLexer()) { 447 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) 448 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt))) 449 return FE; 450 } 451 452 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { 453 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; 454 if (IsFileLexer(ISEntry)) { 455 if ((CurFileEnt = 456 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) 457 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt))) 458 return FE; 459 } 460 } 461 462 // Otherwise, we really couldn't find the file. 463 return 0; 464} 465 466 467//===----------------------------------------------------------------------===// 468// Preprocessor Directive Handling. 469//===----------------------------------------------------------------------===// 470 471/// HandleDirective - This callback is invoked when the lexer sees a # token 472/// at the start of a line. This consumes the directive, modifies the 473/// lexer/preprocessor state, and advances the lexer(s) so that the next token 474/// read is the correct one. 475void Preprocessor::HandleDirective(Token &Result) { 476 // FIXME: Traditional: # with whitespace before it not recognized by K&R? 477 478 // We just parsed a # character at the start of a line, so we're in directive 479 // mode. Tell the lexer this so any newlines we see will be converted into an 480 // EOM token (which terminates the directive). 481 CurPPLexer->ParsingPreprocessorDirective = true; 482 483 ++NumDirectives; 484 485 // We are about to read a token. For the multiple-include optimization FA to 486 // work, we have to remember if we had read any tokens *before* this 487 // pp-directive. 488 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); 489 490 // Save the '#' token in case we need to return it later. 491 Token SavedHash = Result; 492 493 // Read the next token, the directive flavor. This isn't expanded due to 494 // C99 6.10.3p8. 495 LexUnexpandedToken(Result); 496 497 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: 498 // #define A(x) #x 499 // A(abc 500 // #warning blah 501 // def) 502 // If so, the user is relying on non-portable behavior, emit a diagnostic. 503 if (InMacroArgs) 504 Diag(Result, diag::ext_embedded_directive); 505 506TryAgain: 507 switch (Result.getKind()) { 508 case tok::eom: 509 return; // null directive. 510 case tok::comment: 511 // Handle stuff like "# /*foo*/ define X" in -E -C mode. 512 LexUnexpandedToken(Result); 513 goto TryAgain; 514 515 case tok::numeric_constant: // # 7 GNU line marker directive. 516 if (getLangOptions().AsmPreprocessor) 517 break; // # 4 is not a preprocessor directive in .S files. 518 return HandleDigitDirective(Result); 519 default: 520 IdentifierInfo *II = Result.getIdentifierInfo(); 521 if (II == 0) break; // Not an identifier. 522 523 // Ask what the preprocessor keyword ID is. 524 switch (II->getPPKeywordID()) { 525 default: break; 526 // C99 6.10.1 - Conditional Inclusion. 527 case tok::pp_if: 528 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); 529 case tok::pp_ifdef: 530 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); 531 case tok::pp_ifndef: 532 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); 533 case tok::pp_elif: 534 return HandleElifDirective(Result); 535 case tok::pp_else: 536 return HandleElseDirective(Result); 537 case tok::pp_endif: 538 return HandleEndifDirective(Result); 539 540 // C99 6.10.2 - Source File Inclusion. 541 case tok::pp_include: 542 return HandleIncludeDirective(Result); // Handle #include. 543 case tok::pp___include_macros: 544 return HandleIncludeMacrosDirective(Result); // Handle -imacros. 545 546 // C99 6.10.3 - Macro Replacement. 547 case tok::pp_define: 548 return HandleDefineDirective(Result); 549 case tok::pp_undef: 550 return HandleUndefDirective(Result); 551 552 // C99 6.10.4 - Line Control. 553 case tok::pp_line: 554 return HandleLineDirective(Result); 555 556 // C99 6.10.5 - Error Directive. 557 case tok::pp_error: 558 return HandleUserDiagnosticDirective(Result, false); 559 560 // C99 6.10.6 - Pragma Directive. 561 case tok::pp_pragma: 562 return HandlePragmaDirective(); 563 564 // GNU Extensions. 565 case tok::pp_import: 566 return HandleImportDirective(Result); 567 case tok::pp_include_next: 568 return HandleIncludeNextDirective(Result); 569 570 case tok::pp_warning: 571 Diag(Result, diag::ext_pp_warning_directive); 572 return HandleUserDiagnosticDirective(Result, true); 573 case tok::pp_ident: 574 return HandleIdentSCCSDirective(Result); 575 case tok::pp_sccs: 576 return HandleIdentSCCSDirective(Result); 577 case tok::pp_assert: 578 //isExtension = true; // FIXME: implement #assert 579 break; 580 case tok::pp_unassert: 581 //isExtension = true; // FIXME: implement #unassert 582 break; 583 } 584 break; 585 } 586 587 // If this is a .S file, treat unknown # directives as non-preprocessor 588 // directives. This is important because # may be a comment or introduce 589 // various pseudo-ops. Just return the # token and push back the following 590 // token to be lexed next time. 591 if (getLangOptions().AsmPreprocessor) { 592 Token *Toks = new Token[2]; 593 // Return the # and the token after it. 594 Toks[0] = SavedHash; 595 Toks[1] = Result; 596 // Enter this token stream so that we re-lex the tokens. Make sure to 597 // enable macro expansion, in case the token after the # is an identifier 598 // that is expanded. 599 EnterTokenStream(Toks, 2, false, true); 600 return; 601 } 602 603 // If we reached here, the preprocessing token is not valid! 604 Diag(Result, diag::err_pp_invalid_directive); 605 606 // Read the rest of the PP line. 607 DiscardUntilEndOfDirective(); 608 609 // Okay, we're done parsing the directive. 610} 611 612/// GetLineValue - Convert a numeric token into an unsigned value, emitting 613/// Diagnostic DiagID if it is invalid, and returning the value in Val. 614static bool GetLineValue(Token &DigitTok, unsigned &Val, 615 unsigned DiagID, Preprocessor &PP) { 616 if (DigitTok.isNot(tok::numeric_constant)) { 617 PP.Diag(DigitTok, DiagID); 618 619 if (DigitTok.isNot(tok::eom)) 620 PP.DiscardUntilEndOfDirective(); 621 return true; 622 } 623 624 llvm::SmallString<64> IntegerBuffer; 625 IntegerBuffer.resize(DigitTok.getLength()); 626 const char *DigitTokBegin = &IntegerBuffer[0]; 627 bool Invalid = false; 628 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); 629 if (Invalid) 630 return true; 631 632 // Verify that we have a simple digit-sequence, and compute the value. This 633 // is always a simple digit string computed in decimal, so we do this manually 634 // here. 635 Val = 0; 636 for (unsigned i = 0; i != ActualLength; ++i) { 637 if (!isdigit(DigitTokBegin[i])) { 638 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), 639 diag::err_pp_line_digit_sequence); 640 PP.DiscardUntilEndOfDirective(); 641 return true; 642 } 643 644 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); 645 if (NextVal < Val) { // overflow. 646 PP.Diag(DigitTok, DiagID); 647 PP.DiscardUntilEndOfDirective(); 648 return true; 649 } 650 Val = NextVal; 651 } 652 653 // Reject 0, this is needed both by #line numbers and flags. 654 if (Val == 0) { 655 PP.Diag(DigitTok, DiagID); 656 PP.DiscardUntilEndOfDirective(); 657 return true; 658 } 659 660 if (DigitTokBegin[0] == '0') 661 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); 662 663 return false; 664} 665 666/// HandleLineDirective - Handle #line directive: C99 6.10.4. The two 667/// acceptable forms are: 668/// # line digit-sequence 669/// # line digit-sequence "s-char-sequence" 670void Preprocessor::HandleLineDirective(Token &Tok) { 671 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are 672 // expanded. 673 Token DigitTok; 674 Lex(DigitTok); 675 676 // Validate the number and convert it to an unsigned. 677 unsigned LineNo; 678 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) 679 return; 680 681 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a 682 // number greater than 2147483647". C90 requires that the line # be <= 32767. 683 unsigned LineLimit = Features.C99 ? 2147483648U : 32768U; 684 if (LineNo >= LineLimit) 685 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; 686 687 int FilenameID = -1; 688 Token StrTok; 689 Lex(StrTok); 690 691 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a 692 // string followed by eom. 693 if (StrTok.is(tok::eom)) 694 ; // ok 695 else if (StrTok.isNot(tok::string_literal)) { 696 Diag(StrTok, diag::err_pp_line_invalid_filename); 697 DiscardUntilEndOfDirective(); 698 return; 699 } else { 700 // Parse and validate the string, converting it into a unique ID. 701 StringLiteralParser Literal(&StrTok, 1, *this); 702 assert(!Literal.AnyWide && "Didn't allow wide strings in"); 703 if (Literal.hadError) 704 return DiscardUntilEndOfDirective(); 705 if (Literal.Pascal) { 706 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 707 return DiscardUntilEndOfDirective(); 708 } 709 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), 710 Literal.GetStringLength()); 711 712 // Verify that there is nothing after the string, other than EOM. Because 713 // of C99 6.10.4p5, macros that expand to empty tokens are ok. 714 CheckEndOfDirective("line", true); 715 } 716 717 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); 718 719 if (Callbacks) 720 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), 721 PPCallbacks::RenameFile, 722 SrcMgr::C_User); 723} 724 725/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line 726/// marker directive. 727static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, 728 bool &IsSystemHeader, bool &IsExternCHeader, 729 Preprocessor &PP) { 730 unsigned FlagVal; 731 Token FlagTok; 732 PP.Lex(FlagTok); 733 if (FlagTok.is(tok::eom)) return false; 734 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 735 return true; 736 737 if (FlagVal == 1) { 738 IsFileEntry = true; 739 740 PP.Lex(FlagTok); 741 if (FlagTok.is(tok::eom)) return false; 742 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 743 return true; 744 } else if (FlagVal == 2) { 745 IsFileExit = true; 746 747 SourceManager &SM = PP.getSourceManager(); 748 // If we are leaving the current presumed file, check to make sure the 749 // presumed include stack isn't empty! 750 FileID CurFileID = 751 SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first; 752 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); 753 754 // If there is no include loc (main file) or if the include loc is in a 755 // different physical file, then we aren't in a "1" line marker flag region. 756 SourceLocation IncLoc = PLoc.getIncludeLoc(); 757 if (IncLoc.isInvalid() || 758 SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) { 759 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); 760 PP.DiscardUntilEndOfDirective(); 761 return true; 762 } 763 764 PP.Lex(FlagTok); 765 if (FlagTok.is(tok::eom)) return false; 766 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 767 return true; 768 } 769 770 // We must have 3 if there are still flags. 771 if (FlagVal != 3) { 772 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 773 PP.DiscardUntilEndOfDirective(); 774 return true; 775 } 776 777 IsSystemHeader = true; 778 779 PP.Lex(FlagTok); 780 if (FlagTok.is(tok::eom)) return false; 781 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 782 return true; 783 784 // We must have 4 if there is yet another flag. 785 if (FlagVal != 4) { 786 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 787 PP.DiscardUntilEndOfDirective(); 788 return true; 789 } 790 791 IsExternCHeader = true; 792 793 PP.Lex(FlagTok); 794 if (FlagTok.is(tok::eom)) return false; 795 796 // There are no more valid flags here. 797 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 798 PP.DiscardUntilEndOfDirective(); 799 return true; 800} 801 802/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is 803/// one of the following forms: 804/// 805/// # 42 806/// # 42 "file" ('1' | '2')? 807/// # 42 "file" ('1' | '2')? '3' '4'? 808/// 809void Preprocessor::HandleDigitDirective(Token &DigitTok) { 810 // Validate the number and convert it to an unsigned. GNU does not have a 811 // line # limit other than it fit in 32-bits. 812 unsigned LineNo; 813 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, 814 *this)) 815 return; 816 817 Token StrTok; 818 Lex(StrTok); 819 820 bool IsFileEntry = false, IsFileExit = false; 821 bool IsSystemHeader = false, IsExternCHeader = false; 822 int FilenameID = -1; 823 824 // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a 825 // string followed by eom. 826 if (StrTok.is(tok::eom)) 827 ; // ok 828 else if (StrTok.isNot(tok::string_literal)) { 829 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 830 return DiscardUntilEndOfDirective(); 831 } else { 832 // Parse and validate the string, converting it into a unique ID. 833 StringLiteralParser Literal(&StrTok, 1, *this); 834 assert(!Literal.AnyWide && "Didn't allow wide strings in"); 835 if (Literal.hadError) 836 return DiscardUntilEndOfDirective(); 837 if (Literal.Pascal) { 838 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 839 return DiscardUntilEndOfDirective(); 840 } 841 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), 842 Literal.GetStringLength()); 843 844 // If a filename was present, read any flags that are present. 845 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, 846 IsSystemHeader, IsExternCHeader, *this)) 847 return; 848 } 849 850 // Create a line note with this information. 851 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, 852 IsFileEntry, IsFileExit, 853 IsSystemHeader, IsExternCHeader); 854 855 // If the preprocessor has callbacks installed, notify them of the #line 856 // change. This is used so that the line marker comes out in -E mode for 857 // example. 858 if (Callbacks) { 859 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; 860 if (IsFileEntry) 861 Reason = PPCallbacks::EnterFile; 862 else if (IsFileExit) 863 Reason = PPCallbacks::ExitFile; 864 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; 865 if (IsExternCHeader) 866 FileKind = SrcMgr::C_ExternCSystem; 867 else if (IsSystemHeader) 868 FileKind = SrcMgr::C_System; 869 870 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); 871 } 872} 873 874 875/// HandleUserDiagnosticDirective - Handle a #warning or #error directive. 876/// 877void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, 878 bool isWarning) { 879 // PTH doesn't emit #warning or #error directives. 880 if (CurPTHLexer) 881 return CurPTHLexer->DiscardToEndOfLine(); 882 883 // Read the rest of the line raw. We do this because we don't want macros 884 // to be expanded and we don't require that the tokens be valid preprocessing 885 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does 886 // collapse multiple consequtive white space between tokens, but this isn't 887 // specified by the standard. 888 std::string Message = CurLexer->ReadToEndOfLine(); 889 if (isWarning) 890 Diag(Tok, diag::pp_hash_warning) << Message; 891 else 892 Diag(Tok, diag::err_pp_hash_error) << Message; 893} 894 895/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. 896/// 897void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { 898 // Yes, this directive is an extension. 899 Diag(Tok, diag::ext_pp_ident_directive); 900 901 // Read the string argument. 902 Token StrTok; 903 Lex(StrTok); 904 905 // If the token kind isn't a string, it's a malformed directive. 906 if (StrTok.isNot(tok::string_literal) && 907 StrTok.isNot(tok::wide_string_literal)) { 908 Diag(StrTok, diag::err_pp_malformed_ident); 909 if (StrTok.isNot(tok::eom)) 910 DiscardUntilEndOfDirective(); 911 return; 912 } 913 914 // Verify that there is nothing after the string, other than EOM. 915 CheckEndOfDirective("ident"); 916 917 if (Callbacks) { 918 bool Invalid = false; 919 std::string Str = getSpelling(StrTok, &Invalid); 920 if (!Invalid) 921 Callbacks->Ident(Tok.getLocation(), Str); 922 } 923} 924 925//===----------------------------------------------------------------------===// 926// Preprocessor Include Directive Handling. 927//===----------------------------------------------------------------------===// 928 929/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully 930/// checked and spelled filename, e.g. as an operand of #include. This returns 931/// true if the input filename was in <>'s or false if it were in ""'s. The 932/// caller is expected to provide a buffer that is large enough to hold the 933/// spelling of the filename, but is also expected to handle the case when 934/// this method decides to use a different buffer. 935bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, 936 llvm::StringRef &Buffer) { 937 // Get the text form of the filename. 938 assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); 939 940 // Make sure the filename is <x> or "x". 941 bool isAngled; 942 if (Buffer[0] == '<') { 943 if (Buffer.back() != '>') { 944 Diag(Loc, diag::err_pp_expects_filename); 945 Buffer = llvm::StringRef(); 946 return true; 947 } 948 isAngled = true; 949 } else if (Buffer[0] == '"') { 950 if (Buffer.back() != '"') { 951 Diag(Loc, diag::err_pp_expects_filename); 952 Buffer = llvm::StringRef(); 953 return true; 954 } 955 isAngled = false; 956 } else { 957 Diag(Loc, diag::err_pp_expects_filename); 958 Buffer = llvm::StringRef(); 959 return true; 960 } 961 962 // Diagnose #include "" as invalid. 963 if (Buffer.size() <= 2) { 964 Diag(Loc, diag::err_pp_empty_filename); 965 Buffer = llvm::StringRef(); 966 return true; 967 } 968 969 // Skip the brackets. 970 Buffer = Buffer.substr(1, Buffer.size()-2); 971 return isAngled; 972} 973 974/// ConcatenateIncludeName - Handle cases where the #include name is expanded 975/// from a macro as multiple tokens, which need to be glued together. This 976/// occurs for code like: 977/// #define FOO <a/b.h> 978/// #include FOO 979/// because in this case, "<a/b.h>" is returned as 7 tokens, not one. 980/// 981/// This code concatenates and consumes tokens up to the '>' token. It returns 982/// false if the > was found, otherwise it returns true if it finds and consumes 983/// the EOM marker. 984bool Preprocessor::ConcatenateIncludeName( 985 llvm::SmallString<128> &FilenameBuffer) { 986 Token CurTok; 987 988 Lex(CurTok); 989 while (CurTok.isNot(tok::eom)) { 990 // Append the spelling of this token to the buffer. If there was a space 991 // before it, add it now. 992 if (CurTok.hasLeadingSpace()) 993 FilenameBuffer.push_back(' '); 994 995 // Get the spelling of the token, directly into FilenameBuffer if possible. 996 unsigned PreAppendSize = FilenameBuffer.size(); 997 FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); 998 999 const char *BufPtr = &FilenameBuffer[PreAppendSize]; 1000 unsigned ActualLen = getSpelling(CurTok, BufPtr); 1001 1002 // If the token was spelled somewhere else, copy it into FilenameBuffer. 1003 if (BufPtr != &FilenameBuffer[PreAppendSize]) 1004 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); 1005 1006 // Resize FilenameBuffer to the correct size. 1007 if (CurTok.getLength() != ActualLen) 1008 FilenameBuffer.resize(PreAppendSize+ActualLen); 1009 1010 // If we found the '>' marker, return success. 1011 if (CurTok.is(tok::greater)) 1012 return false; 1013 1014 Lex(CurTok); 1015 } 1016 1017 // If we hit the eom marker, emit an error and return true so that the caller 1018 // knows the EOM has been read. 1019 Diag(CurTok.getLocation(), diag::err_pp_expects_filename); 1020 return true; 1021} 1022 1023/// HandleIncludeDirective - The "#include" tokens have just been read, read the 1024/// file to be included from the lexer, then include it! This is a common 1025/// routine with functionality shared between #include, #include_next and 1026/// #import. LookupFrom is set when this is a #include_next directive, it 1027/// specifies the file to start searching from. 1028void Preprocessor::HandleIncludeDirective(Token &IncludeTok, 1029 const DirectoryLookup *LookupFrom, 1030 bool isImport) { 1031 1032 Token FilenameTok; 1033 CurPPLexer->LexIncludeFilename(FilenameTok); 1034 1035 // Reserve a buffer to get the spelling. 1036 llvm::SmallString<128> FilenameBuffer; 1037 llvm::StringRef Filename; 1038 1039 switch (FilenameTok.getKind()) { 1040 case tok::eom: 1041 // If the token kind is EOM, the error has already been diagnosed. 1042 return; 1043 1044 case tok::angle_string_literal: 1045 case tok::string_literal: 1046 Filename = getSpelling(FilenameTok, FilenameBuffer); 1047 break; 1048 1049 case tok::less: 1050 // This could be a <foo/bar.h> file coming from a macro expansion. In this 1051 // case, glue the tokens together into FilenameBuffer and interpret those. 1052 FilenameBuffer.push_back('<'); 1053 if (ConcatenateIncludeName(FilenameBuffer)) 1054 return; // Found <eom> but no ">"? Diagnostic already emitted. 1055 Filename = FilenameBuffer.str(); 1056 break; 1057 default: 1058 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); 1059 DiscardUntilEndOfDirective(); 1060 return; 1061 } 1062 1063 bool isAngled = 1064 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); 1065 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1066 // error. 1067 if (Filename.empty()) { 1068 DiscardUntilEndOfDirective(); 1069 return; 1070 } 1071 1072 // Verify that there is nothing after the filename, other than EOM. Note that 1073 // we allow macros that expand to nothing after the filename, because this 1074 // falls into the category of "#include pp-tokens new-line" specified in 1075 // C99 6.10.2p4. 1076 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); 1077 1078 // Check that we don't have infinite #include recursion. 1079 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { 1080 Diag(FilenameTok, diag::err_pp_include_too_deep); 1081 return; 1082 } 1083 1084 // Search include directories. 1085 const DirectoryLookup *CurDir; 1086 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir); 1087 if (File == 0) { 1088 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; 1089 return; 1090 } 1091 1092 // The #included file will be considered to be a system header if either it is 1093 // in a system include directory, or if the #includer is a system include 1094 // header. 1095 SrcMgr::CharacteristicKind FileCharacter = 1096 std::max(HeaderInfo.getFileDirFlavor(File), 1097 SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); 1098 1099 // Ask HeaderInfo if we should enter this #include file. If not, #including 1100 // this file will have no effect. 1101 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { 1102 if (Callbacks) 1103 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); 1104 return; 1105 } 1106 1107 // Look up the file, create a File ID for it. 1108 FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), 1109 FileCharacter); 1110 if (FID.isInvalid()) { 1111 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; 1112 return; 1113 } 1114 1115 // Finally, if all is good, enter the new file! 1116 EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); 1117} 1118 1119/// HandleIncludeNextDirective - Implements #include_next. 1120/// 1121void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { 1122 Diag(IncludeNextTok, diag::ext_pp_include_next_directive); 1123 1124 // #include_next is like #include, except that we start searching after 1125 // the current found directory. If we can't do this, issue a 1126 // diagnostic. 1127 const DirectoryLookup *Lookup = CurDirLookup; 1128 if (isInPrimaryFile()) { 1129 Lookup = 0; 1130 Diag(IncludeNextTok, diag::pp_include_next_in_primary); 1131 } else if (Lookup == 0) { 1132 Diag(IncludeNextTok, diag::pp_include_next_absolute_path); 1133 } else { 1134 // Start looking up in the next directory. 1135 ++Lookup; 1136 } 1137 1138 return HandleIncludeDirective(IncludeNextTok, Lookup); 1139} 1140 1141/// HandleImportDirective - Implements #import. 1142/// 1143void Preprocessor::HandleImportDirective(Token &ImportTok) { 1144 if (!Features.ObjC1) // #import is standard for ObjC. 1145 Diag(ImportTok, diag::ext_pp_import_directive); 1146 1147 return HandleIncludeDirective(ImportTok, 0, true); 1148} 1149 1150/// HandleIncludeMacrosDirective - The -imacros command line option turns into a 1151/// pseudo directive in the predefines buffer. This handles it by sucking all 1152/// tokens through the preprocessor and discarding them (only keeping the side 1153/// effects on the preprocessor). 1154void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { 1155 // This directive should only occur in the predefines buffer. If not, emit an 1156 // error and reject it. 1157 SourceLocation Loc = IncludeMacrosTok.getLocation(); 1158 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { 1159 Diag(IncludeMacrosTok.getLocation(), 1160 diag::pp_include_macros_out_of_predefines); 1161 DiscardUntilEndOfDirective(); 1162 return; 1163 } 1164 1165 // Treat this as a normal #include for checking purposes. If this is 1166 // successful, it will push a new lexer onto the include stack. 1167 HandleIncludeDirective(IncludeMacrosTok, 0, false); 1168 1169 Token TmpTok; 1170 do { 1171 Lex(TmpTok); 1172 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); 1173 } while (TmpTok.isNot(tok::hashhash)); 1174} 1175 1176//===----------------------------------------------------------------------===// 1177// Preprocessor Macro Directive Handling. 1178//===----------------------------------------------------------------------===// 1179 1180/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro 1181/// definition has just been read. Lex the rest of the arguments and the 1182/// closing ), updating MI with what we learn. Return true if an error occurs 1183/// parsing the arg list. 1184bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { 1185 llvm::SmallVector<IdentifierInfo*, 32> Arguments; 1186 1187 Token Tok; 1188 while (1) { 1189 LexUnexpandedToken(Tok); 1190 switch (Tok.getKind()) { 1191 case tok::r_paren: 1192 // Found the end of the argument list. 1193 if (Arguments.empty()) // #define FOO() 1194 return false; 1195 // Otherwise we have #define FOO(A,) 1196 Diag(Tok, diag::err_pp_expected_ident_in_arg_list); 1197 return true; 1198 case tok::ellipsis: // #define X(... -> C99 varargs 1199 // Warn if use of C99 feature in non-C99 mode. 1200 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); 1201 1202 // Lex the token after the identifier. 1203 LexUnexpandedToken(Tok); 1204 if (Tok.isNot(tok::r_paren)) { 1205 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1206 return true; 1207 } 1208 // Add the __VA_ARGS__ identifier as an argument. 1209 Arguments.push_back(Ident__VA_ARGS__); 1210 MI->setIsC99Varargs(); 1211 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1212 return false; 1213 case tok::eom: // #define X( 1214 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1215 return true; 1216 default: 1217 // Handle keywords and identifiers here to accept things like 1218 // #define Foo(for) for. 1219 IdentifierInfo *II = Tok.getIdentifierInfo(); 1220 if (II == 0) { 1221 // #define X(1 1222 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); 1223 return true; 1224 } 1225 1226 // If this is already used as an argument, it is used multiple times (e.g. 1227 // #define X(A,A. 1228 if (std::find(Arguments.begin(), Arguments.end(), II) != 1229 Arguments.end()) { // C99 6.10.3p6 1230 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; 1231 return true; 1232 } 1233 1234 // Add the argument to the macro info. 1235 Arguments.push_back(II); 1236 1237 // Lex the token after the identifier. 1238 LexUnexpandedToken(Tok); 1239 1240 switch (Tok.getKind()) { 1241 default: // #define X(A B 1242 Diag(Tok, diag::err_pp_expected_comma_in_arg_list); 1243 return true; 1244 case tok::r_paren: // #define X(A) 1245 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1246 return false; 1247 case tok::comma: // #define X(A, 1248 break; 1249 case tok::ellipsis: // #define X(A... -> GCC extension 1250 // Diagnose extension. 1251 Diag(Tok, diag::ext_named_variadic_macro); 1252 1253 // Lex the token after the identifier. 1254 LexUnexpandedToken(Tok); 1255 if (Tok.isNot(tok::r_paren)) { 1256 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1257 return true; 1258 } 1259 1260 MI->setIsGNUVarargs(); 1261 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1262 return false; 1263 } 1264 } 1265 } 1266} 1267 1268/// HandleDefineDirective - Implements #define. This consumes the entire macro 1269/// line then lets the caller lex the next real token. 1270void Preprocessor::HandleDefineDirective(Token &DefineTok) { 1271 ++NumDefined; 1272 1273 Token MacroNameTok; 1274 ReadMacroName(MacroNameTok, 1); 1275 1276 // Error reading macro name? If so, diagnostic already issued. 1277 if (MacroNameTok.is(tok::eom)) 1278 return; 1279 1280 Token LastTok = MacroNameTok; 1281 1282 // If we are supposed to keep comments in #defines, reenable comment saving 1283 // mode. 1284 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); 1285 1286 // Create the new macro. 1287 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); 1288 1289 Token Tok; 1290 LexUnexpandedToken(Tok); 1291 1292 // If this is a function-like macro definition, parse the argument list, 1293 // marking each of the identifiers as being used as macro arguments. Also, 1294 // check other constraints on the first token of the macro body. 1295 if (Tok.is(tok::eom)) { 1296 // If there is no body to this macro, we have no special handling here. 1297 } else if (Tok.hasLeadingSpace()) { 1298 // This is a normal token with leading space. Clear the leading space 1299 // marker on the first token to get proper expansion. 1300 Tok.clearFlag(Token::LeadingSpace); 1301 } else if (Tok.is(tok::l_paren)) { 1302 // This is a function-like macro definition. Read the argument list. 1303 MI->setIsFunctionLike(); 1304 if (ReadMacroDefinitionArgList(MI)) { 1305 // Forget about MI. 1306 ReleaseMacroInfo(MI); 1307 // Throw away the rest of the line. 1308 if (CurPPLexer->ParsingPreprocessorDirective) 1309 DiscardUntilEndOfDirective(); 1310 return; 1311 } 1312 1313 // If this is a definition of a variadic C99 function-like macro, not using 1314 // the GNU named varargs extension, enabled __VA_ARGS__. 1315 1316 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. 1317 // This gets unpoisoned where it is allowed. 1318 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); 1319 if (MI->isC99Varargs()) 1320 Ident__VA_ARGS__->setIsPoisoned(false); 1321 1322 // Read the first token after the arg list for down below. 1323 LexUnexpandedToken(Tok); 1324 } else if (Features.C99) { 1325 // C99 requires whitespace between the macro definition and the body. Emit 1326 // a diagnostic for something like "#define X+". 1327 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); 1328 } else { 1329 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the 1330 // first character of a replacement list is not a character required by 1331 // subclause 5.2.1, then there shall be white-space separation between the 1332 // identifier and the replacement list.". 5.2.1 lists this set: 1333 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which 1334 // is irrelevant here. 1335 bool isInvalid = false; 1336 if (Tok.is(tok::at)) // @ is not in the list above. 1337 isInvalid = true; 1338 else if (Tok.is(tok::unknown)) { 1339 // If we have an unknown token, it is something strange like "`". Since 1340 // all of valid characters would have lexed into a single character 1341 // token of some sort, we know this is not a valid case. 1342 isInvalid = true; 1343 } 1344 if (isInvalid) 1345 Diag(Tok, diag::ext_missing_whitespace_after_macro_name); 1346 else 1347 Diag(Tok, diag::warn_missing_whitespace_after_macro_name); 1348 } 1349 1350 if (!Tok.is(tok::eom)) 1351 LastTok = Tok; 1352 1353 // Read the rest of the macro body. 1354 if (MI->isObjectLike()) { 1355 // Object-like macros are very simple, just read their body. 1356 while (Tok.isNot(tok::eom)) { 1357 LastTok = Tok; 1358 MI->AddTokenToBody(Tok); 1359 // Get the next token of the macro. 1360 LexUnexpandedToken(Tok); 1361 } 1362 1363 } else { 1364 // Otherwise, read the body of a function-like macro. While we are at it, 1365 // check C99 6.10.3.2p1: ensure that # operators are followed by macro 1366 // parameters in function-like macro expansions. 1367 while (Tok.isNot(tok::eom)) { 1368 LastTok = Tok; 1369 1370 if (Tok.isNot(tok::hash)) { 1371 MI->AddTokenToBody(Tok); 1372 1373 // Get the next token of the macro. 1374 LexUnexpandedToken(Tok); 1375 continue; 1376 } 1377 1378 // Get the next token of the macro. 1379 LexUnexpandedToken(Tok); 1380 1381 // Check for a valid macro arg identifier. 1382 if (Tok.getIdentifierInfo() == 0 || 1383 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { 1384 1385 // If this is assembler-with-cpp mode, we accept random gibberish after 1386 // the '#' because '#' is often a comment character. However, change 1387 // the kind of the token to tok::unknown so that the preprocessor isn't 1388 // confused. 1389 if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) { 1390 LastTok.setKind(tok::unknown); 1391 } else { 1392 Diag(Tok, diag::err_pp_stringize_not_parameter); 1393 ReleaseMacroInfo(MI); 1394 1395 // Disable __VA_ARGS__ again. 1396 Ident__VA_ARGS__->setIsPoisoned(true); 1397 return; 1398 } 1399 } 1400 1401 // Things look ok, add the '#' and param name tokens to the macro. 1402 MI->AddTokenToBody(LastTok); 1403 MI->AddTokenToBody(Tok); 1404 LastTok = Tok; 1405 1406 // Get the next token of the macro. 1407 LexUnexpandedToken(Tok); 1408 } 1409 } 1410 1411 1412 // Disable __VA_ARGS__ again. 1413 Ident__VA_ARGS__->setIsPoisoned(true); 1414 1415 // Check that there is no paste (##) operator at the begining or end of the 1416 // replacement list. 1417 unsigned NumTokens = MI->getNumTokens(); 1418 if (NumTokens != 0) { 1419 if (MI->getReplacementToken(0).is(tok::hashhash)) { 1420 Diag(MI->getReplacementToken(0), diag::err_paste_at_start); 1421 ReleaseMacroInfo(MI); 1422 return; 1423 } 1424 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { 1425 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); 1426 ReleaseMacroInfo(MI); 1427 return; 1428 } 1429 } 1430 1431 // If this is the primary source file, remember that this macro hasn't been 1432 // used yet. 1433 if (isInPrimaryFile()) 1434 MI->setIsUsed(false); 1435 1436 MI->setDefinitionEndLoc(LastTok.getLocation()); 1437 1438 // Finally, if this identifier already had a macro defined for it, verify that 1439 // the macro bodies are identical and free the old definition. 1440 if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { 1441 // It is very common for system headers to have tons of macro redefinitions 1442 // and for warnings to be disabled in system headers. If this is the case, 1443 // then don't bother calling MacroInfo::isIdenticalTo. 1444 if (!getDiagnostics().getSuppressSystemWarnings() || 1445 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { 1446 if (!OtherMI->isUsed()) 1447 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); 1448 1449 // Macros must be identical. This means all tokes and whitespace 1450 // separation must be the same. C99 6.10.3.2. 1451 if (!MI->isIdenticalTo(*OtherMI, *this)) { 1452 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) 1453 << MacroNameTok.getIdentifierInfo(); 1454 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); 1455 } 1456 } 1457 1458 ReleaseMacroInfo(OtherMI); 1459 } 1460 1461 setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); 1462 1463 // If the callbacks want to know, tell them about the macro definition. 1464 if (Callbacks) 1465 Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI); 1466} 1467 1468/// HandleUndefDirective - Implements #undef. 1469/// 1470void Preprocessor::HandleUndefDirective(Token &UndefTok) { 1471 ++NumUndefined; 1472 1473 Token MacroNameTok; 1474 ReadMacroName(MacroNameTok, 2); 1475 1476 // Error reading macro name? If so, diagnostic already issued. 1477 if (MacroNameTok.is(tok::eom)) 1478 return; 1479 1480 // Check to see if this is the last token on the #undef line. 1481 CheckEndOfDirective("undef"); 1482 1483 // Okay, we finally have a valid identifier to undef. 1484 MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); 1485 1486 // If the macro is not defined, this is a noop undef, just return. 1487 if (MI == 0) return; 1488 1489 if (!MI->isUsed()) 1490 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); 1491 1492 // If the callbacks want to know, tell them about the macro #undef. 1493 if (Callbacks) 1494 Callbacks->MacroUndefined(MacroNameTok.getLocation(), 1495 MacroNameTok.getIdentifierInfo(), MI); 1496 1497 // Free macro definition. 1498 ReleaseMacroInfo(MI); 1499 setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); 1500} 1501 1502 1503//===----------------------------------------------------------------------===// 1504// Preprocessor Conditional Directive Handling. 1505//===----------------------------------------------------------------------===// 1506 1507/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is 1508/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true 1509/// if any tokens have been returned or pp-directives activated before this 1510/// #ifndef has been lexed. 1511/// 1512void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, 1513 bool ReadAnyTokensBeforeDirective) { 1514 ++NumIf; 1515 Token DirectiveTok = Result; 1516 1517 Token MacroNameTok; 1518 ReadMacroName(MacroNameTok); 1519 1520 // Error reading macro name? If so, diagnostic already issued. 1521 if (MacroNameTok.is(tok::eom)) { 1522 // Skip code until we get to #endif. This helps with recovery by not 1523 // emitting an error when the #endif is reached. 1524 SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 1525 /*Foundnonskip*/false, /*FoundElse*/false); 1526 return; 1527 } 1528 1529 // Check to see if this is the last token on the #if[n]def line. 1530 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); 1531 1532 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); 1533 MacroInfo *MI = getMacroInfo(MII); 1534 1535 if (CurPPLexer->getConditionalStackDepth() == 0) { 1536 // If the start of a top-level #ifdef and if the macro is not defined, 1537 // inform MIOpt that this might be the start of a proper include guard. 1538 // Otherwise it is some other form of unknown conditional which we can't 1539 // handle. 1540 if (!ReadAnyTokensBeforeDirective && MI == 0) { 1541 assert(isIfndef && "#ifdef shouldn't reach here"); 1542 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); 1543 } else 1544 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1545 } 1546 1547 // If there is a macro, process it. 1548 if (MI) // Mark it used. 1549 MI->setIsUsed(true); 1550 1551 // Should we include the stuff contained by this directive? 1552 if (!MI == isIfndef) { 1553 // Yes, remember that we are inside a conditional, then lex the next token. 1554 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), 1555 /*wasskip*/false, /*foundnonskip*/true, 1556 /*foundelse*/false); 1557 } else { 1558 // No, skip the contents of this block and return the first token after it. 1559 SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 1560 /*Foundnonskip*/false, 1561 /*FoundElse*/false); 1562 } 1563} 1564 1565/// HandleIfDirective - Implements the #if directive. 1566/// 1567void Preprocessor::HandleIfDirective(Token &IfToken, 1568 bool ReadAnyTokensBeforeDirective) { 1569 ++NumIf; 1570 1571 // Parse and evaluation the conditional expression. 1572 IdentifierInfo *IfNDefMacro = 0; 1573 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); 1574 1575 1576 // If this condition is equivalent to #ifndef X, and if this is the first 1577 // directive seen, handle it for the multiple-include optimization. 1578 if (CurPPLexer->getConditionalStackDepth() == 0) { 1579 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) 1580 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); 1581 else 1582 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1583 } 1584 1585 // Should we include the stuff contained by this directive? 1586 if (ConditionalTrue) { 1587 // Yes, remember that we are inside a conditional, then lex the next token. 1588 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, 1589 /*foundnonskip*/true, /*foundelse*/false); 1590 } else { 1591 // No, skip the contents of this block and return the first token after it. 1592 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, 1593 /*FoundElse*/false); 1594 } 1595} 1596 1597/// HandleEndifDirective - Implements the #endif directive. 1598/// 1599void Preprocessor::HandleEndifDirective(Token &EndifToken) { 1600 ++NumEndif; 1601 1602 // Check that this is the whole directive. 1603 CheckEndOfDirective("endif"); 1604 1605 PPConditionalInfo CondInfo; 1606 if (CurPPLexer->popConditionalLevel(CondInfo)) { 1607 // No conditionals on the stack: this is an #endif without an #if. 1608 Diag(EndifToken, diag::err_pp_endif_without_if); 1609 return; 1610 } 1611 1612 // If this the end of a top-level #endif, inform MIOpt. 1613 if (CurPPLexer->getConditionalStackDepth() == 0) 1614 CurPPLexer->MIOpt.ExitTopLevelConditional(); 1615 1616 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && 1617 "This code should only be reachable in the non-skipping case!"); 1618} 1619 1620 1621void Preprocessor::HandleElseDirective(Token &Result) { 1622 ++NumElse; 1623 1624 // #else directive in a non-skipping conditional... start skipping. 1625 CheckEndOfDirective("else"); 1626 1627 PPConditionalInfo CI; 1628 if (CurPPLexer->popConditionalLevel(CI)) { 1629 Diag(Result, diag::pp_err_else_without_if); 1630 return; 1631 } 1632 1633 // If this is a top-level #else, inform the MIOpt. 1634 if (CurPPLexer->getConditionalStackDepth() == 0) 1635 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1636 1637 // If this is a #else with a #else before it, report the error. 1638 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); 1639 1640 // Finally, skip the rest of the contents of this block and return the first 1641 // token after it. 1642 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 1643 /*FoundElse*/true); 1644} 1645 1646void Preprocessor::HandleElifDirective(Token &ElifToken) { 1647 ++NumElse; 1648 1649 // #elif directive in a non-skipping conditional... start skipping. 1650 // We don't care what the condition is, because we will always skip it (since 1651 // the block immediately before it was included). 1652 DiscardUntilEndOfDirective(); 1653 1654 PPConditionalInfo CI; 1655 if (CurPPLexer->popConditionalLevel(CI)) { 1656 Diag(ElifToken, diag::pp_err_elif_without_if); 1657 return; 1658 } 1659 1660 // If this is a top-level #elif, inform the MIOpt. 1661 if (CurPPLexer->getConditionalStackDepth() == 0) 1662 CurPPLexer->MIOpt.EnterTopLevelConditional(); 1663 1664 // If this is a #elif with a #else before it, report the error. 1665 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); 1666 1667 // Finally, skip the rest of the contents of this block and return the first 1668 // token after it. 1669 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 1670 /*FoundElse*/CI.FoundElse); 1671} 1672