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 /// \file 11 /// \brief Implements # directive processing for the Preprocessor. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Lex/Preprocessor.h" 16 #include "clang/Basic/FileManager.h" 17 #include "clang/Basic/SourceManager.h" 18 #include "clang/Lex/CodeCompletionHandler.h" 19 #include "clang/Lex/HeaderSearch.h" 20 #include "clang/Lex/LexDiagnostic.h" 21 #include "clang/Lex/LiteralSupport.h" 22 #include "clang/Lex/MacroInfo.h" 23 #include "clang/Lex/ModuleLoader.h" 24 #include "clang/Lex/Pragma.h" 25 #include "llvm/ADT/APInt.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/SaveAndRestore.h" 28 using namespace clang; 29 30 //===----------------------------------------------------------------------===// 31 // Utility Methods for Preprocessor Directive Handling. 32 //===----------------------------------------------------------------------===// 33 34 MacroInfo *Preprocessor::AllocateMacroInfo() { 35 MacroInfoChain *MIChain; 36 37 if (MICache) { 38 MIChain = MICache; 39 MICache = MICache->Next; 40 } 41 else { 42 MIChain = BP.Allocate<MacroInfoChain>(); 43 } 44 45 MIChain->Next = MIChainHead; 46 MIChain->Prev = 0; 47 if (MIChainHead) 48 MIChainHead->Prev = MIChain; 49 MIChainHead = MIChain; 50 51 return &(MIChain->MI); 52 } 53 54 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { 55 MacroInfo *MI = AllocateMacroInfo(); 56 new (MI) MacroInfo(L); 57 return MI; 58 } 59 60 MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L, 61 unsigned SubModuleID) { 62 LLVM_STATIC_ASSERT(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID), 63 "alignment for MacroInfo is less than the ID"); 64 DeserializedMacroInfoChain *MIChain = 65 BP.Allocate<DeserializedMacroInfoChain>(); 66 MIChain->Next = DeserialMIChainHead; 67 DeserialMIChainHead = MIChain; 68 69 MacroInfo *MI = &MIChain->MI; 70 new (MI) MacroInfo(L); 71 MI->FromASTFile = true; 72 MI->setOwningModuleID(SubModuleID); 73 return MI; 74 } 75 76 DefMacroDirective * 77 Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc, 78 bool isImported) { 79 DefMacroDirective *MD = BP.Allocate<DefMacroDirective>(); 80 new (MD) DefMacroDirective(MI, Loc, isImported); 81 return MD; 82 } 83 84 UndefMacroDirective * 85 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) { 86 UndefMacroDirective *MD = BP.Allocate<UndefMacroDirective>(); 87 new (MD) UndefMacroDirective(UndefLoc); 88 return MD; 89 } 90 91 VisibilityMacroDirective * 92 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc, 93 bool isPublic) { 94 VisibilityMacroDirective *MD = BP.Allocate<VisibilityMacroDirective>(); 95 new (MD) VisibilityMacroDirective(Loc, isPublic); 96 return MD; 97 } 98 99 /// \brief Release the specified MacroInfo to be reused for allocating 100 /// new MacroInfo objects. 101 void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) { 102 MacroInfoChain *MIChain = (MacroInfoChain*) MI; 103 if (MacroInfoChain *Prev = MIChain->Prev) { 104 MacroInfoChain *Next = MIChain->Next; 105 Prev->Next = Next; 106 if (Next) 107 Next->Prev = Prev; 108 } 109 else { 110 assert(MIChainHead == MIChain); 111 MIChainHead = MIChain->Next; 112 MIChainHead->Prev = 0; 113 } 114 MIChain->Next = MICache; 115 MICache = MIChain; 116 117 MI->Destroy(); 118 } 119 120 /// \brief Read and discard all tokens remaining on the current line until 121 /// the tok::eod token is found. 122 void Preprocessor::DiscardUntilEndOfDirective() { 123 Token Tmp; 124 do { 125 LexUnexpandedToken(Tmp); 126 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); 127 } while (Tmp.isNot(tok::eod)); 128 } 129 130 /// \brief Lex and validate a macro name, which occurs after a 131 /// \#define or \#undef. 132 /// 133 /// This sets the token kind to eod and discards the rest 134 /// of the macro line if the macro name is invalid. \p isDefineUndef is 1 if 135 /// this is due to a a \#define, 2 if \#undef directive, 0 if it is something 136 /// else (e.g. \#ifdef). 137 void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { 138 // Read the token, don't allow macro expansion on it. 139 LexUnexpandedToken(MacroNameTok); 140 141 if (MacroNameTok.is(tok::code_completion)) { 142 if (CodeComplete) 143 CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); 144 setCodeCompletionReached(); 145 LexUnexpandedToken(MacroNameTok); 146 } 147 148 // Missing macro name? 149 if (MacroNameTok.is(tok::eod)) { 150 Diag(MacroNameTok, diag::err_pp_missing_macro_name); 151 return; 152 } 153 154 IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 155 if (II == 0) { 156 bool Invalid = false; 157 std::string Spelling = getSpelling(MacroNameTok, &Invalid); 158 if (Invalid) 159 return; 160 161 const IdentifierInfo &Info = Identifiers.get(Spelling); 162 163 // Allow #defining |and| and friends in microsoft mode. 164 if (Info.isCPlusPlusOperatorKeyword() && getLangOpts().MicrosoftMode) { 165 MacroNameTok.setIdentifierInfo(getIdentifierInfo(Spelling)); 166 return; 167 } 168 169 if (Info.isCPlusPlusOperatorKeyword()) 170 // C++ 2.5p2: Alternative tokens behave the same as its primary token 171 // except for their spellings. 172 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; 173 else 174 Diag(MacroNameTok, diag::err_pp_macro_not_identifier); 175 // Fall through on error. 176 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { 177 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4. 178 Diag(MacroNameTok, diag::err_defined_macro_name); 179 } else if (isDefineUndef == 2 && II->hasMacroDefinition() && 180 getMacroInfo(II)->isBuiltinMacro()) { 181 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 182 // and C++ [cpp.predefined]p4], but allow it as an extension. 183 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); 184 return; 185 } else { 186 // Okay, we got a good identifier node. Return it. 187 return; 188 } 189 190 // Invalid macro name, read and discard the rest of the line. Then set the 191 // token kind to tok::eod. 192 MacroNameTok.setKind(tok::eod); 193 return DiscardUntilEndOfDirective(); 194 } 195 196 /// \brief Ensure that the next token is a tok::eod token. 197 /// 198 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is 199 /// true, then we consider macros that expand to zero tokens as being ok. 200 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { 201 Token Tmp; 202 // Lex unexpanded tokens for most directives: macros might expand to zero 203 // tokens, causing us to miss diagnosing invalid lines. Some directives (like 204 // #line) allow empty macros. 205 if (EnableMacros) 206 Lex(Tmp); 207 else 208 LexUnexpandedToken(Tmp); 209 210 // There should be no tokens after the directive, but we allow them as an 211 // extension. 212 while (Tmp.is(tok::comment)) // Skip comments in -C mode. 213 LexUnexpandedToken(Tmp); 214 215 if (Tmp.isNot(tok::eod)) { 216 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, 217 // or if this is a macro-style preprocessing directive, because it is more 218 // trouble than it is worth to insert /**/ and check that there is no /**/ 219 // in the range also. 220 FixItHint Hint; 221 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) && 222 !CurTokenLexer) 223 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); 224 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; 225 DiscardUntilEndOfDirective(); 226 } 227 } 228 229 230 231 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and 232 /// decided that the subsequent tokens are in the \#if'd out portion of the 233 /// file. Lex the rest of the file, until we see an \#endif. If 234 /// FoundNonSkipPortion is true, then we have already emitted code for part of 235 /// this \#if directive, so \#else/\#elif blocks should never be entered. 236 /// If ElseOk is true, then \#else directives are ok, if not, then we have 237 /// already seen one so a \#else directive is a duplicate. When this returns, 238 /// the caller can lex the first valid token. 239 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, 240 bool FoundNonSkipPortion, 241 bool FoundElse, 242 SourceLocation ElseLoc) { 243 ++NumSkipped; 244 assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); 245 246 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, 247 FoundNonSkipPortion, FoundElse); 248 249 if (CurPTHLexer) { 250 PTHSkipExcludedConditionalBlock(); 251 return; 252 } 253 254 // Enter raw mode to disable identifier lookup (and thus macro expansion), 255 // disabling warnings, etc. 256 CurPPLexer->LexingRawMode = true; 257 Token Tok; 258 while (1) { 259 CurLexer->Lex(Tok); 260 261 if (Tok.is(tok::code_completion)) { 262 if (CodeComplete) 263 CodeComplete->CodeCompleteInConditionalExclusion(); 264 setCodeCompletionReached(); 265 continue; 266 } 267 268 // If this is the end of the buffer, we have an error. 269 if (Tok.is(tok::eof)) { 270 // Emit errors for each unterminated conditional on the stack, including 271 // the current one. 272 while (!CurPPLexer->ConditionalStack.empty()) { 273 if (CurLexer->getFileLoc() != CodeCompletionFileLoc) 274 Diag(CurPPLexer->ConditionalStack.back().IfLoc, 275 diag::err_pp_unterminated_conditional); 276 CurPPLexer->ConditionalStack.pop_back(); 277 } 278 279 // Just return and let the caller lex after this #include. 280 break; 281 } 282 283 // If this token is not a preprocessor directive, just skip it. 284 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) 285 continue; 286 287 // We just parsed a # character at the start of a line, so we're in 288 // directive mode. Tell the lexer this so any newlines we see will be 289 // converted into an EOD token (this terminates the macro). 290 CurPPLexer->ParsingPreprocessorDirective = true; 291 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); 292 293 294 // Read the next token, the directive flavor. 295 LexUnexpandedToken(Tok); 296 297 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or 298 // something bogus), skip it. 299 if (Tok.isNot(tok::raw_identifier)) { 300 CurPPLexer->ParsingPreprocessorDirective = false; 301 // Restore comment saving mode. 302 if (CurLexer) CurLexer->resetExtendedTokenMode(); 303 continue; 304 } 305 306 // If the first letter isn't i or e, it isn't intesting to us. We know that 307 // this is safe in the face of spelling differences, because there is no way 308 // to spell an i/e in a strange way that is another letter. Skipping this 309 // allows us to avoid looking up the identifier info for #define/#undef and 310 // other common directives. 311 const char *RawCharData = Tok.getRawIdentifierData(); 312 313 char FirstChar = RawCharData[0]; 314 if (FirstChar >= 'a' && FirstChar <= 'z' && 315 FirstChar != 'i' && FirstChar != 'e') { 316 CurPPLexer->ParsingPreprocessorDirective = false; 317 // Restore comment saving mode. 318 if (CurLexer) CurLexer->resetExtendedTokenMode(); 319 continue; 320 } 321 322 // Get the identifier name without trigraphs or embedded newlines. Note 323 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled 324 // when skipping. 325 char DirectiveBuf[20]; 326 StringRef Directive; 327 if (!Tok.needsCleaning() && Tok.getLength() < 20) { 328 Directive = StringRef(RawCharData, Tok.getLength()); 329 } else { 330 std::string DirectiveStr = getSpelling(Tok); 331 unsigned IdLen = DirectiveStr.size(); 332 if (IdLen >= 20) { 333 CurPPLexer->ParsingPreprocessorDirective = false; 334 // Restore comment saving mode. 335 if (CurLexer) CurLexer->resetExtendedTokenMode(); 336 continue; 337 } 338 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); 339 Directive = StringRef(DirectiveBuf, IdLen); 340 } 341 342 if (Directive.startswith("if")) { 343 StringRef Sub = Directive.substr(2); 344 if (Sub.empty() || // "if" 345 Sub == "def" || // "ifdef" 346 Sub == "ndef") { // "ifndef" 347 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't 348 // bother parsing the condition. 349 DiscardUntilEndOfDirective(); 350 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, 351 /*foundnonskip*/false, 352 /*foundelse*/false); 353 } 354 } else if (Directive[0] == 'e') { 355 StringRef Sub = Directive.substr(1); 356 if (Sub == "ndif") { // "endif" 357 PPConditionalInfo CondInfo; 358 CondInfo.WasSkipping = true; // Silence bogus warning. 359 bool InCond = CurPPLexer->popConditionalLevel(CondInfo); 360 (void)InCond; // Silence warning in no-asserts mode. 361 assert(!InCond && "Can't be skipping if not in a conditional!"); 362 363 // If we popped the outermost skipping block, we're done skipping! 364 if (!CondInfo.WasSkipping) { 365 // Restore the value of LexingRawMode so that trailing comments 366 // are handled correctly, if we've reached the outermost block. 367 CurPPLexer->LexingRawMode = false; 368 CheckEndOfDirective("endif"); 369 CurPPLexer->LexingRawMode = true; 370 if (Callbacks) 371 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); 372 break; 373 } else { 374 DiscardUntilEndOfDirective(); 375 } 376 } else if (Sub == "lse") { // "else". 377 // #else directive in a skipping conditional. If not in some other 378 // skipping conditional, and if #else hasn't already been seen, enter it 379 // as a non-skipping conditional. 380 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 381 382 // If this is a #else with a #else before it, report the error. 383 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); 384 385 // Note that we've seen a #else in this conditional. 386 CondInfo.FoundElse = true; 387 388 // If the conditional is at the top level, and the #if block wasn't 389 // entered, enter the #else block now. 390 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { 391 CondInfo.FoundNonSkip = true; 392 // Restore the value of LexingRawMode so that trailing comments 393 // are handled correctly. 394 CurPPLexer->LexingRawMode = false; 395 CheckEndOfDirective("else"); 396 CurPPLexer->LexingRawMode = true; 397 if (Callbacks) 398 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc); 399 break; 400 } else { 401 DiscardUntilEndOfDirective(); // C99 6.10p4. 402 } 403 } else if (Sub == "lif") { // "elif". 404 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); 405 406 bool ShouldEnter; 407 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); 408 // If this is in a skipping block or if we're already handled this #if 409 // block, don't bother parsing the condition. 410 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { 411 DiscardUntilEndOfDirective(); 412 ShouldEnter = false; 413 } else { 414 // Restore the value of LexingRawMode so that identifiers are 415 // looked up, etc, inside the #elif expression. 416 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); 417 CurPPLexer->LexingRawMode = false; 418 IdentifierInfo *IfNDefMacro = 0; 419 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 420 CurPPLexer->LexingRawMode = true; 421 } 422 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); 423 424 // If this is a #elif with a #else before it, report the error. 425 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); 426 427 // If this condition is true, enter it! 428 if (ShouldEnter) { 429 CondInfo.FoundNonSkip = true; 430 if (Callbacks) 431 Callbacks->Elif(Tok.getLocation(), 432 SourceRange(ConditionalBegin, ConditionalEnd), 433 CondInfo.IfLoc); 434 break; 435 } 436 } 437 } 438 439 CurPPLexer->ParsingPreprocessorDirective = false; 440 // Restore comment saving mode. 441 if (CurLexer) CurLexer->resetExtendedTokenMode(); 442 } 443 444 // Finally, if we are out of the conditional (saw an #endif or ran off the end 445 // of the file, just stop skipping and return to lexing whatever came after 446 // the #if block. 447 CurPPLexer->LexingRawMode = false; 448 449 if (Callbacks) { 450 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc; 451 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation())); 452 } 453 } 454 455 void Preprocessor::PTHSkipExcludedConditionalBlock() { 456 457 while (1) { 458 assert(CurPTHLexer); 459 assert(CurPTHLexer->LexingRawMode == false); 460 461 // Skip to the next '#else', '#elif', or #endif. 462 if (CurPTHLexer->SkipBlock()) { 463 // We have reached an #endif. Both the '#' and 'endif' tokens 464 // have been consumed by the PTHLexer. Just pop off the condition level. 465 PPConditionalInfo CondInfo; 466 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); 467 (void)InCond; // Silence warning in no-asserts mode. 468 assert(!InCond && "Can't be skipping if not in a conditional!"); 469 break; 470 } 471 472 // We have reached a '#else' or '#elif'. Lex the next token to get 473 // the directive flavor. 474 Token Tok; 475 LexUnexpandedToken(Tok); 476 477 // We can actually look up the IdentifierInfo here since we aren't in 478 // raw mode. 479 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); 480 481 if (K == tok::pp_else) { 482 // #else: Enter the else condition. We aren't in a nested condition 483 // since we skip those. We're always in the one matching the last 484 // blocked we skipped. 485 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 486 // Note that we've seen a #else in this conditional. 487 CondInfo.FoundElse = true; 488 489 // If the #if block wasn't entered then enter the #else block now. 490 if (!CondInfo.FoundNonSkip) { 491 CondInfo.FoundNonSkip = true; 492 493 // Scan until the eod token. 494 CurPTHLexer->ParsingPreprocessorDirective = true; 495 DiscardUntilEndOfDirective(); 496 CurPTHLexer->ParsingPreprocessorDirective = false; 497 498 break; 499 } 500 501 // Otherwise skip this block. 502 continue; 503 } 504 505 assert(K == tok::pp_elif); 506 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); 507 508 // If this is a #elif with a #else before it, report the error. 509 if (CondInfo.FoundElse) 510 Diag(Tok, diag::pp_err_elif_after_else); 511 512 // If this is in a skipping block or if we're already handled this #if 513 // block, don't bother parsing the condition. We just skip this block. 514 if (CondInfo.FoundNonSkip) 515 continue; 516 517 // Evaluate the condition of the #elif. 518 IdentifierInfo *IfNDefMacro = 0; 519 CurPTHLexer->ParsingPreprocessorDirective = true; 520 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); 521 CurPTHLexer->ParsingPreprocessorDirective = false; 522 523 // If this condition is true, enter it! 524 if (ShouldEnter) { 525 CondInfo.FoundNonSkip = true; 526 break; 527 } 528 529 // Otherwise, skip this block and go to the next one. 530 continue; 531 } 532 } 533 534 const FileEntry *Preprocessor::LookupFile( 535 StringRef Filename, 536 bool isAngled, 537 const DirectoryLookup *FromDir, 538 const DirectoryLookup *&CurDir, 539 SmallVectorImpl<char> *SearchPath, 540 SmallVectorImpl<char> *RelativePath, 541 Module **SuggestedModule, 542 bool SkipCache) { 543 // If the header lookup mechanism may be relative to the current file, pass in 544 // info about where the current file is. 545 const FileEntry *CurFileEnt = 0; 546 if (!FromDir) { 547 FileID FID = getCurrentFileLexer()->getFileID(); 548 CurFileEnt = SourceMgr.getFileEntryForID(FID); 549 550 // If there is no file entry associated with this file, it must be the 551 // predefines buffer. Any other file is not lexed with a normal lexer, so 552 // it won't be scanned for preprocessor directives. If we have the 553 // predefines buffer, resolve #include references (which come from the 554 // -include command line argument) as if they came from the main file, this 555 // affects file lookup etc. 556 if (CurFileEnt == 0) { 557 FID = SourceMgr.getMainFileID(); 558 CurFileEnt = SourceMgr.getFileEntryForID(FID); 559 } 560 } 561 562 // Do a standard file entry lookup. 563 CurDir = CurDirLookup; 564 const FileEntry *FE = HeaderInfo.LookupFile( 565 Filename, isAngled, FromDir, CurDir, CurFileEnt, 566 SearchPath, RelativePath, SuggestedModule, SkipCache); 567 if (FE) return FE; 568 569 // Otherwise, see if this is a subframework header. If so, this is relative 570 // to one of the headers on the #include stack. Walk the list of the current 571 // headers on the #include stack and pass them to HeaderInfo. 572 if (IsFileLexer()) { 573 if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) 574 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt, 575 SearchPath, RelativePath, 576 SuggestedModule))) 577 return FE; 578 } 579 580 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { 581 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; 582 if (IsFileLexer(ISEntry)) { 583 if ((CurFileEnt = 584 SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) 585 if ((FE = HeaderInfo.LookupSubframeworkHeader( 586 Filename, CurFileEnt, SearchPath, RelativePath, 587 SuggestedModule))) 588 return FE; 589 } 590 } 591 592 // Otherwise, we really couldn't find the file. 593 return 0; 594 } 595 596 597 //===----------------------------------------------------------------------===// 598 // Preprocessor Directive Handling. 599 //===----------------------------------------------------------------------===// 600 601 class Preprocessor::ResetMacroExpansionHelper { 602 public: 603 ResetMacroExpansionHelper(Preprocessor *pp) 604 : PP(pp), save(pp->DisableMacroExpansion) { 605 if (pp->MacroExpansionInDirectivesOverride) 606 pp->DisableMacroExpansion = false; 607 } 608 ~ResetMacroExpansionHelper() { 609 PP->DisableMacroExpansion = save; 610 } 611 private: 612 Preprocessor *PP; 613 bool save; 614 }; 615 616 /// HandleDirective - This callback is invoked when the lexer sees a # token 617 /// at the start of a line. This consumes the directive, modifies the 618 /// lexer/preprocessor state, and advances the lexer(s) so that the next token 619 /// read is the correct one. 620 void Preprocessor::HandleDirective(Token &Result) { 621 // FIXME: Traditional: # with whitespace before it not recognized by K&R? 622 623 // We just parsed a # character at the start of a line, so we're in directive 624 // mode. Tell the lexer this so any newlines we see will be converted into an 625 // EOD token (which terminates the directive). 626 CurPPLexer->ParsingPreprocessorDirective = true; 627 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); 628 629 ++NumDirectives; 630 631 // We are about to read a token. For the multiple-include optimization FA to 632 // work, we have to remember if we had read any tokens *before* this 633 // pp-directive. 634 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); 635 636 // Save the '#' token in case we need to return it later. 637 Token SavedHash = Result; 638 639 // Read the next token, the directive flavor. This isn't expanded due to 640 // C99 6.10.3p8. 641 LexUnexpandedToken(Result); 642 643 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: 644 // #define A(x) #x 645 // A(abc 646 // #warning blah 647 // def) 648 // If so, the user is relying on undefined behavior, emit a diagnostic. Do 649 // not support this for #include-like directives, since that can result in 650 // terrible diagnostics, and does not work in GCC. 651 if (InMacroArgs) { 652 if (IdentifierInfo *II = Result.getIdentifierInfo()) { 653 switch (II->getPPKeywordID()) { 654 case tok::pp_include: 655 case tok::pp_import: 656 case tok::pp_include_next: 657 case tok::pp___include_macros: 658 Diag(Result, diag::err_embedded_include) << II->getName(); 659 DiscardUntilEndOfDirective(); 660 return; 661 default: 662 break; 663 } 664 } 665 Diag(Result, diag::ext_embedded_directive); 666 } 667 668 // Temporarily enable macro expansion if set so 669 // and reset to previous state when returning from this function. 670 ResetMacroExpansionHelper helper(this); 671 672 switch (Result.getKind()) { 673 case tok::eod: 674 return; // null directive. 675 case tok::code_completion: 676 if (CodeComplete) 677 CodeComplete->CodeCompleteDirective( 678 CurPPLexer->getConditionalStackDepth() > 0); 679 setCodeCompletionReached(); 680 return; 681 case tok::numeric_constant: // # 7 GNU line marker directive. 682 if (getLangOpts().AsmPreprocessor) 683 break; // # 4 is not a preprocessor directive in .S files. 684 return HandleDigitDirective(Result); 685 default: 686 IdentifierInfo *II = Result.getIdentifierInfo(); 687 if (II == 0) break; // Not an identifier. 688 689 // Ask what the preprocessor keyword ID is. 690 switch (II->getPPKeywordID()) { 691 default: break; 692 // C99 6.10.1 - Conditional Inclusion. 693 case tok::pp_if: 694 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); 695 case tok::pp_ifdef: 696 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); 697 case tok::pp_ifndef: 698 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); 699 case tok::pp_elif: 700 return HandleElifDirective(Result); 701 case tok::pp_else: 702 return HandleElseDirective(Result); 703 case tok::pp_endif: 704 return HandleEndifDirective(Result); 705 706 // C99 6.10.2 - Source File Inclusion. 707 case tok::pp_include: 708 // Handle #include. 709 return HandleIncludeDirective(SavedHash.getLocation(), Result); 710 case tok::pp___include_macros: 711 // Handle -imacros. 712 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); 713 714 // C99 6.10.3 - Macro Replacement. 715 case tok::pp_define: 716 return HandleDefineDirective(Result); 717 case tok::pp_undef: 718 return HandleUndefDirective(Result); 719 720 // C99 6.10.4 - Line Control. 721 case tok::pp_line: 722 return HandleLineDirective(Result); 723 724 // C99 6.10.5 - Error Directive. 725 case tok::pp_error: 726 return HandleUserDiagnosticDirective(Result, false); 727 728 // C99 6.10.6 - Pragma Directive. 729 case tok::pp_pragma: 730 return HandlePragmaDirective(PIK_HashPragma); 731 732 // GNU Extensions. 733 case tok::pp_import: 734 return HandleImportDirective(SavedHash.getLocation(), Result); 735 case tok::pp_include_next: 736 return HandleIncludeNextDirective(SavedHash.getLocation(), Result); 737 738 case tok::pp_warning: 739 Diag(Result, diag::ext_pp_warning_directive); 740 return HandleUserDiagnosticDirective(Result, true); 741 case tok::pp_ident: 742 return HandleIdentSCCSDirective(Result); 743 case tok::pp_sccs: 744 return HandleIdentSCCSDirective(Result); 745 case tok::pp_assert: 746 //isExtension = true; // FIXME: implement #assert 747 break; 748 case tok::pp_unassert: 749 //isExtension = true; // FIXME: implement #unassert 750 break; 751 752 case tok::pp___public_macro: 753 if (getLangOpts().Modules) 754 return HandleMacroPublicDirective(Result); 755 break; 756 757 case tok::pp___private_macro: 758 if (getLangOpts().Modules) 759 return HandleMacroPrivateDirective(Result); 760 break; 761 } 762 break; 763 } 764 765 // If this is a .S file, treat unknown # directives as non-preprocessor 766 // directives. This is important because # may be a comment or introduce 767 // various pseudo-ops. Just return the # token and push back the following 768 // token to be lexed next time. 769 if (getLangOpts().AsmPreprocessor) { 770 Token *Toks = new Token[2]; 771 // Return the # and the token after it. 772 Toks[0] = SavedHash; 773 Toks[1] = Result; 774 775 // If the second token is a hashhash token, then we need to translate it to 776 // unknown so the token lexer doesn't try to perform token pasting. 777 if (Result.is(tok::hashhash)) 778 Toks[1].setKind(tok::unknown); 779 780 // Enter this token stream so that we re-lex the tokens. Make sure to 781 // enable macro expansion, in case the token after the # is an identifier 782 // that is expanded. 783 EnterTokenStream(Toks, 2, false, true); 784 return; 785 } 786 787 // If we reached here, the preprocessing token is not valid! 788 Diag(Result, diag::err_pp_invalid_directive); 789 790 // Read the rest of the PP line. 791 DiscardUntilEndOfDirective(); 792 793 // Okay, we're done parsing the directive. 794 } 795 796 /// GetLineValue - Convert a numeric token into an unsigned value, emitting 797 /// Diagnostic DiagID if it is invalid, and returning the value in Val. 798 static bool GetLineValue(Token &DigitTok, unsigned &Val, 799 unsigned DiagID, Preprocessor &PP, 800 bool IsGNULineDirective=false) { 801 if (DigitTok.isNot(tok::numeric_constant)) { 802 PP.Diag(DigitTok, DiagID); 803 804 if (DigitTok.isNot(tok::eod)) 805 PP.DiscardUntilEndOfDirective(); 806 return true; 807 } 808 809 SmallString<64> IntegerBuffer; 810 IntegerBuffer.resize(DigitTok.getLength()); 811 const char *DigitTokBegin = &IntegerBuffer[0]; 812 bool Invalid = false; 813 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); 814 if (Invalid) 815 return true; 816 817 // Verify that we have a simple digit-sequence, and compute the value. This 818 // is always a simple digit string computed in decimal, so we do this manually 819 // here. 820 Val = 0; 821 for (unsigned i = 0; i != ActualLength; ++i) { 822 if (!isDigit(DigitTokBegin[i])) { 823 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), 824 diag::err_pp_line_digit_sequence) << IsGNULineDirective; 825 PP.DiscardUntilEndOfDirective(); 826 return true; 827 } 828 829 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); 830 if (NextVal < Val) { // overflow. 831 PP.Diag(DigitTok, DiagID); 832 PP.DiscardUntilEndOfDirective(); 833 return true; 834 } 835 Val = NextVal; 836 } 837 838 if (DigitTokBegin[0] == '0' && Val) 839 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal) 840 << IsGNULineDirective; 841 842 return false; 843 } 844 845 /// \brief Handle a \#line directive: C99 6.10.4. 846 /// 847 /// The two acceptable forms are: 848 /// \verbatim 849 /// # line digit-sequence 850 /// # line digit-sequence "s-char-sequence" 851 /// \endverbatim 852 void Preprocessor::HandleLineDirective(Token &Tok) { 853 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are 854 // expanded. 855 Token DigitTok; 856 Lex(DigitTok); 857 858 // Validate the number and convert it to an unsigned. 859 unsigned LineNo; 860 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) 861 return; 862 863 if (LineNo == 0) 864 Diag(DigitTok, diag::ext_pp_line_zero); 865 866 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a 867 // number greater than 2147483647". C90 requires that the line # be <= 32767. 868 unsigned LineLimit = 32768U; 869 if (LangOpts.C99 || LangOpts.CPlusPlus11) 870 LineLimit = 2147483648U; 871 if (LineNo >= LineLimit) 872 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; 873 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U) 874 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big); 875 876 int FilenameID = -1; 877 Token StrTok; 878 Lex(StrTok); 879 880 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a 881 // string followed by eod. 882 if (StrTok.is(tok::eod)) 883 ; // ok 884 else if (StrTok.isNot(tok::string_literal)) { 885 Diag(StrTok, diag::err_pp_line_invalid_filename); 886 return DiscardUntilEndOfDirective(); 887 } else if (StrTok.hasUDSuffix()) { 888 Diag(StrTok, diag::err_invalid_string_udl); 889 return DiscardUntilEndOfDirective(); 890 } else { 891 // Parse and validate the string, converting it into a unique ID. 892 StringLiteralParser Literal(&StrTok, 1, *this); 893 assert(Literal.isAscii() && "Didn't allow wide strings in"); 894 if (Literal.hadError) 895 return DiscardUntilEndOfDirective(); 896 if (Literal.Pascal) { 897 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 898 return DiscardUntilEndOfDirective(); 899 } 900 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); 901 902 // Verify that there is nothing after the string, other than EOD. Because 903 // of C99 6.10.4p5, macros that expand to empty tokens are ok. 904 CheckEndOfDirective("line", true); 905 } 906 907 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); 908 909 if (Callbacks) 910 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), 911 PPCallbacks::RenameFile, 912 SrcMgr::C_User); 913 } 914 915 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line 916 /// marker directive. 917 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, 918 bool &IsSystemHeader, bool &IsExternCHeader, 919 Preprocessor &PP) { 920 unsigned FlagVal; 921 Token FlagTok; 922 PP.Lex(FlagTok); 923 if (FlagTok.is(tok::eod)) return false; 924 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 925 return true; 926 927 if (FlagVal == 1) { 928 IsFileEntry = true; 929 930 PP.Lex(FlagTok); 931 if (FlagTok.is(tok::eod)) return false; 932 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 933 return true; 934 } else if (FlagVal == 2) { 935 IsFileExit = true; 936 937 SourceManager &SM = PP.getSourceManager(); 938 // If we are leaving the current presumed file, check to make sure the 939 // presumed include stack isn't empty! 940 FileID CurFileID = 941 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first; 942 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); 943 if (PLoc.isInvalid()) 944 return true; 945 946 // If there is no include loc (main file) or if the include loc is in a 947 // different physical file, then we aren't in a "1" line marker flag region. 948 SourceLocation IncLoc = PLoc.getIncludeLoc(); 949 if (IncLoc.isInvalid() || 950 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) { 951 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); 952 PP.DiscardUntilEndOfDirective(); 953 return true; 954 } 955 956 PP.Lex(FlagTok); 957 if (FlagTok.is(tok::eod)) return false; 958 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) 959 return true; 960 } 961 962 // We must have 3 if there are still flags. 963 if (FlagVal != 3) { 964 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 965 PP.DiscardUntilEndOfDirective(); 966 return true; 967 } 968 969 IsSystemHeader = true; 970 971 PP.Lex(FlagTok); 972 if (FlagTok.is(tok::eod)) return false; 973 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) 974 return true; 975 976 // We must have 4 if there is yet another flag. 977 if (FlagVal != 4) { 978 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 979 PP.DiscardUntilEndOfDirective(); 980 return true; 981 } 982 983 IsExternCHeader = true; 984 985 PP.Lex(FlagTok); 986 if (FlagTok.is(tok::eod)) return false; 987 988 // There are no more valid flags here. 989 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); 990 PP.DiscardUntilEndOfDirective(); 991 return true; 992 } 993 994 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is 995 /// one of the following forms: 996 /// 997 /// # 42 998 /// # 42 "file" ('1' | '2')? 999 /// # 42 "file" ('1' | '2')? '3' '4'? 1000 /// 1001 void Preprocessor::HandleDigitDirective(Token &DigitTok) { 1002 // Validate the number and convert it to an unsigned. GNU does not have a 1003 // line # limit other than it fit in 32-bits. 1004 unsigned LineNo; 1005 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, 1006 *this, true)) 1007 return; 1008 1009 Token StrTok; 1010 Lex(StrTok); 1011 1012 bool IsFileEntry = false, IsFileExit = false; 1013 bool IsSystemHeader = false, IsExternCHeader = false; 1014 int FilenameID = -1; 1015 1016 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a 1017 // string followed by eod. 1018 if (StrTok.is(tok::eod)) 1019 ; // ok 1020 else if (StrTok.isNot(tok::string_literal)) { 1021 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 1022 return DiscardUntilEndOfDirective(); 1023 } else if (StrTok.hasUDSuffix()) { 1024 Diag(StrTok, diag::err_invalid_string_udl); 1025 return DiscardUntilEndOfDirective(); 1026 } else { 1027 // Parse and validate the string, converting it into a unique ID. 1028 StringLiteralParser Literal(&StrTok, 1, *this); 1029 assert(Literal.isAscii() && "Didn't allow wide strings in"); 1030 if (Literal.hadError) 1031 return DiscardUntilEndOfDirective(); 1032 if (Literal.Pascal) { 1033 Diag(StrTok, diag::err_pp_linemarker_invalid_filename); 1034 return DiscardUntilEndOfDirective(); 1035 } 1036 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); 1037 1038 // If a filename was present, read any flags that are present. 1039 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, 1040 IsSystemHeader, IsExternCHeader, *this)) 1041 return; 1042 } 1043 1044 // Create a line note with this information. 1045 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, 1046 IsFileEntry, IsFileExit, 1047 IsSystemHeader, IsExternCHeader); 1048 1049 // If the preprocessor has callbacks installed, notify them of the #line 1050 // change. This is used so that the line marker comes out in -E mode for 1051 // example. 1052 if (Callbacks) { 1053 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; 1054 if (IsFileEntry) 1055 Reason = PPCallbacks::EnterFile; 1056 else if (IsFileExit) 1057 Reason = PPCallbacks::ExitFile; 1058 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; 1059 if (IsExternCHeader) 1060 FileKind = SrcMgr::C_ExternCSystem; 1061 else if (IsSystemHeader) 1062 FileKind = SrcMgr::C_System; 1063 1064 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); 1065 } 1066 } 1067 1068 1069 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. 1070 /// 1071 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, 1072 bool isWarning) { 1073 // PTH doesn't emit #warning or #error directives. 1074 if (CurPTHLexer) 1075 return CurPTHLexer->DiscardToEndOfLine(); 1076 1077 // Read the rest of the line raw. We do this because we don't want macros 1078 // to be expanded and we don't require that the tokens be valid preprocessing 1079 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does 1080 // collapse multiple consequtive white space between tokens, but this isn't 1081 // specified by the standard. 1082 SmallString<128> Message; 1083 CurLexer->ReadToEndOfLine(&Message); 1084 1085 // Find the first non-whitespace character, so that we can make the 1086 // diagnostic more succinct. 1087 StringRef Msg = Message.str().ltrim(" "); 1088 1089 if (isWarning) 1090 Diag(Tok, diag::pp_hash_warning) << Msg; 1091 else 1092 Diag(Tok, diag::err_pp_hash_error) << Msg; 1093 } 1094 1095 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. 1096 /// 1097 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { 1098 // Yes, this directive is an extension. 1099 Diag(Tok, diag::ext_pp_ident_directive); 1100 1101 // Read the string argument. 1102 Token StrTok; 1103 Lex(StrTok); 1104 1105 // If the token kind isn't a string, it's a malformed directive. 1106 if (StrTok.isNot(tok::string_literal) && 1107 StrTok.isNot(tok::wide_string_literal)) { 1108 Diag(StrTok, diag::err_pp_malformed_ident); 1109 if (StrTok.isNot(tok::eod)) 1110 DiscardUntilEndOfDirective(); 1111 return; 1112 } 1113 1114 if (StrTok.hasUDSuffix()) { 1115 Diag(StrTok, diag::err_invalid_string_udl); 1116 return DiscardUntilEndOfDirective(); 1117 } 1118 1119 // Verify that there is nothing after the string, other than EOD. 1120 CheckEndOfDirective("ident"); 1121 1122 if (Callbacks) { 1123 bool Invalid = false; 1124 std::string Str = getSpelling(StrTok, &Invalid); 1125 if (!Invalid) 1126 Callbacks->Ident(Tok.getLocation(), Str); 1127 } 1128 } 1129 1130 /// \brief Handle a #public directive. 1131 void Preprocessor::HandleMacroPublicDirective(Token &Tok) { 1132 Token MacroNameTok; 1133 ReadMacroName(MacroNameTok, 2); 1134 1135 // Error reading macro name? If so, diagnostic already issued. 1136 if (MacroNameTok.is(tok::eod)) 1137 return; 1138 1139 // Check to see if this is the last token on the #__public_macro line. 1140 CheckEndOfDirective("__public_macro"); 1141 1142 IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 1143 // Okay, we finally have a valid identifier to undef. 1144 MacroDirective *MD = getMacroDirective(II); 1145 1146 // If the macro is not defined, this is an error. 1147 if (MD == 0) { 1148 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; 1149 return; 1150 } 1151 1152 // Note that this macro has now been exported. 1153 appendMacroDirective(II, AllocateVisibilityMacroDirective( 1154 MacroNameTok.getLocation(), /*IsPublic=*/true)); 1155 } 1156 1157 /// \brief Handle a #private directive. 1158 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { 1159 Token MacroNameTok; 1160 ReadMacroName(MacroNameTok, 2); 1161 1162 // Error reading macro name? If so, diagnostic already issued. 1163 if (MacroNameTok.is(tok::eod)) 1164 return; 1165 1166 // Check to see if this is the last token on the #__private_macro line. 1167 CheckEndOfDirective("__private_macro"); 1168 1169 IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); 1170 // Okay, we finally have a valid identifier to undef. 1171 MacroDirective *MD = getMacroDirective(II); 1172 1173 // If the macro is not defined, this is an error. 1174 if (MD == 0) { 1175 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; 1176 return; 1177 } 1178 1179 // Note that this macro has now been marked private. 1180 appendMacroDirective(II, AllocateVisibilityMacroDirective( 1181 MacroNameTok.getLocation(), /*IsPublic=*/false)); 1182 } 1183 1184 //===----------------------------------------------------------------------===// 1185 // Preprocessor Include Directive Handling. 1186 //===----------------------------------------------------------------------===// 1187 1188 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully 1189 /// checked and spelled filename, e.g. as an operand of \#include. This returns 1190 /// true if the input filename was in <>'s or false if it were in ""'s. The 1191 /// caller is expected to provide a buffer that is large enough to hold the 1192 /// spelling of the filename, but is also expected to handle the case when 1193 /// this method decides to use a different buffer. 1194 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, 1195 StringRef &Buffer) { 1196 // Get the text form of the filename. 1197 assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); 1198 1199 // Make sure the filename is <x> or "x". 1200 bool isAngled; 1201 if (Buffer[0] == '<') { 1202 if (Buffer.back() != '>') { 1203 Diag(Loc, diag::err_pp_expects_filename); 1204 Buffer = StringRef(); 1205 return true; 1206 } 1207 isAngled = true; 1208 } else if (Buffer[0] == '"') { 1209 if (Buffer.back() != '"') { 1210 Diag(Loc, diag::err_pp_expects_filename); 1211 Buffer = StringRef(); 1212 return true; 1213 } 1214 isAngled = false; 1215 } else { 1216 Diag(Loc, diag::err_pp_expects_filename); 1217 Buffer = StringRef(); 1218 return true; 1219 } 1220 1221 // Diagnose #include "" as invalid. 1222 if (Buffer.size() <= 2) { 1223 Diag(Loc, diag::err_pp_empty_filename); 1224 Buffer = StringRef(); 1225 return true; 1226 } 1227 1228 // Skip the brackets. 1229 Buffer = Buffer.substr(1, Buffer.size()-2); 1230 return isAngled; 1231 } 1232 1233 /// \brief Handle cases where the \#include name is expanded from a macro 1234 /// as multiple tokens, which need to be glued together. 1235 /// 1236 /// This occurs for code like: 1237 /// \code 1238 /// \#define FOO <a/b.h> 1239 /// \#include FOO 1240 /// \endcode 1241 /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. 1242 /// 1243 /// This code concatenates and consumes tokens up to the '>' token. It returns 1244 /// false if the > was found, otherwise it returns true if it finds and consumes 1245 /// the EOD marker. 1246 bool Preprocessor::ConcatenateIncludeName( 1247 SmallString<128> &FilenameBuffer, 1248 SourceLocation &End) { 1249 Token CurTok; 1250 1251 Lex(CurTok); 1252 while (CurTok.isNot(tok::eod)) { 1253 End = CurTok.getLocation(); 1254 1255 // FIXME: Provide code completion for #includes. 1256 if (CurTok.is(tok::code_completion)) { 1257 setCodeCompletionReached(); 1258 Lex(CurTok); 1259 continue; 1260 } 1261 1262 // Append the spelling of this token to the buffer. If there was a space 1263 // before it, add it now. 1264 if (CurTok.hasLeadingSpace()) 1265 FilenameBuffer.push_back(' '); 1266 1267 // Get the spelling of the token, directly into FilenameBuffer if possible. 1268 unsigned PreAppendSize = FilenameBuffer.size(); 1269 FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); 1270 1271 const char *BufPtr = &FilenameBuffer[PreAppendSize]; 1272 unsigned ActualLen = getSpelling(CurTok, BufPtr); 1273 1274 // If the token was spelled somewhere else, copy it into FilenameBuffer. 1275 if (BufPtr != &FilenameBuffer[PreAppendSize]) 1276 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); 1277 1278 // Resize FilenameBuffer to the correct size. 1279 if (CurTok.getLength() != ActualLen) 1280 FilenameBuffer.resize(PreAppendSize+ActualLen); 1281 1282 // If we found the '>' marker, return success. 1283 if (CurTok.is(tok::greater)) 1284 return false; 1285 1286 Lex(CurTok); 1287 } 1288 1289 // If we hit the eod marker, emit an error and return true so that the caller 1290 // knows the EOD has been read. 1291 Diag(CurTok.getLocation(), diag::err_pp_expects_filename); 1292 return true; 1293 } 1294 1295 /// HandleIncludeDirective - The "\#include" tokens have just been read, read 1296 /// the file to be included from the lexer, then include it! This is a common 1297 /// routine with functionality shared between \#include, \#include_next and 1298 /// \#import. LookupFrom is set when this is a \#include_next directive, it 1299 /// specifies the file to start searching from. 1300 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, 1301 Token &IncludeTok, 1302 const DirectoryLookup *LookupFrom, 1303 bool isImport) { 1304 1305 Token FilenameTok; 1306 CurPPLexer->LexIncludeFilename(FilenameTok); 1307 1308 // Reserve a buffer to get the spelling. 1309 SmallString<128> FilenameBuffer; 1310 StringRef Filename; 1311 SourceLocation End; 1312 SourceLocation CharEnd; // the end of this directive, in characters 1313 1314 switch (FilenameTok.getKind()) { 1315 case tok::eod: 1316 // If the token kind is EOD, the error has already been diagnosed. 1317 return; 1318 1319 case tok::angle_string_literal: 1320 case tok::string_literal: 1321 Filename = getSpelling(FilenameTok, FilenameBuffer); 1322 End = FilenameTok.getLocation(); 1323 CharEnd = End.getLocWithOffset(FilenameTok.getLength()); 1324 break; 1325 1326 case tok::less: 1327 // This could be a <foo/bar.h> file coming from a macro expansion. In this 1328 // case, glue the tokens together into FilenameBuffer and interpret those. 1329 FilenameBuffer.push_back('<'); 1330 if (ConcatenateIncludeName(FilenameBuffer, End)) 1331 return; // Found <eod> but no ">"? Diagnostic already emitted. 1332 Filename = FilenameBuffer.str(); 1333 CharEnd = End.getLocWithOffset(1); 1334 break; 1335 default: 1336 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); 1337 DiscardUntilEndOfDirective(); 1338 return; 1339 } 1340 1341 CharSourceRange FilenameRange 1342 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd); 1343 StringRef OriginalFilename = Filename; 1344 bool isAngled = 1345 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); 1346 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1347 // error. 1348 if (Filename.empty()) { 1349 DiscardUntilEndOfDirective(); 1350 return; 1351 } 1352 1353 // Verify that there is nothing after the filename, other than EOD. Note that 1354 // we allow macros that expand to nothing after the filename, because this 1355 // falls into the category of "#include pp-tokens new-line" specified in 1356 // C99 6.10.2p4. 1357 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); 1358 1359 // Check that we don't have infinite #include recursion. 1360 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { 1361 Diag(FilenameTok, diag::err_pp_include_too_deep); 1362 return; 1363 } 1364 1365 // Complain about attempts to #include files in an audit pragma. 1366 if (PragmaARCCFCodeAuditedLoc.isValid()) { 1367 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited); 1368 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here); 1369 1370 // Immediately leave the pragma. 1371 PragmaARCCFCodeAuditedLoc = SourceLocation(); 1372 } 1373 1374 if (HeaderInfo.HasIncludeAliasMap()) { 1375 // Map the filename with the brackets still attached. If the name doesn't 1376 // map to anything, fall back on the filename we've already gotten the 1377 // spelling for. 1378 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename); 1379 if (!NewName.empty()) 1380 Filename = NewName; 1381 } 1382 1383 // Search include directories. 1384 const DirectoryLookup *CurDir; 1385 SmallString<1024> SearchPath; 1386 SmallString<1024> RelativePath; 1387 // We get the raw path only if we have 'Callbacks' to which we later pass 1388 // the path. 1389 Module *SuggestedModule = 0; 1390 const FileEntry *File = LookupFile( 1391 Filename, isAngled, LookupFrom, CurDir, 1392 Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL, 1393 getLangOpts().Modules? &SuggestedModule : 0); 1394 1395 if (Callbacks) { 1396 if (!File) { 1397 // Give the clients a chance to recover. 1398 SmallString<128> RecoveryPath; 1399 if (Callbacks->FileNotFound(Filename, RecoveryPath)) { 1400 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) { 1401 // Add the recovery path to the list of search paths. 1402 DirectoryLookup DL(DE, SrcMgr::C_User, false); 1403 HeaderInfo.AddSearchPath(DL, isAngled); 1404 1405 // Try the lookup again, skipping the cache. 1406 File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0, 1407 getLangOpts().Modules? &SuggestedModule : 0, 1408 /*SkipCache*/true); 1409 } 1410 } 1411 } 1412 1413 if (!SuggestedModule) { 1414 // Notify the callback object that we've seen an inclusion directive. 1415 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, 1416 FilenameRange, File, 1417 SearchPath, RelativePath, 1418 /*ImportedModule=*/0); 1419 } 1420 } 1421 1422 if (File == 0) { 1423 if (!SuppressIncludeNotFoundError) { 1424 // If the file could not be located and it was included via angle 1425 // brackets, we can attempt a lookup as though it were a quoted path to 1426 // provide the user with a possible fixit. 1427 if (isAngled) { 1428 File = LookupFile(Filename, false, LookupFrom, CurDir, 1429 Callbacks ? &SearchPath : 0, 1430 Callbacks ? &RelativePath : 0, 1431 getLangOpts().Modules ? &SuggestedModule : 0); 1432 if (File) { 1433 SourceRange Range(FilenameTok.getLocation(), CharEnd); 1434 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) << 1435 Filename << 1436 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\""); 1437 } 1438 } 1439 // If the file is still not found, just go with the vanilla diagnostic 1440 if (!File) 1441 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; 1442 } 1443 if (!File) 1444 return; 1445 } 1446 1447 // If we are supposed to import a module rather than including the header, 1448 // do so now. 1449 if (SuggestedModule) { 1450 // Compute the module access path corresponding to this module. 1451 // FIXME: Should we have a second loadModule() overload to avoid this 1452 // extra lookup step? 1453 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; 1454 for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent) 1455 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name), 1456 FilenameTok.getLocation())); 1457 std::reverse(Path.begin(), Path.end()); 1458 1459 // Warn that we're replacing the include/import with a module import. 1460 SmallString<128> PathString; 1461 for (unsigned I = 0, N = Path.size(); I != N; ++I) { 1462 if (I) 1463 PathString += '.'; 1464 PathString += Path[I].first->getName(); 1465 } 1466 int IncludeKind = 0; 1467 1468 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { 1469 case tok::pp_include: 1470 IncludeKind = 0; 1471 break; 1472 1473 case tok::pp_import: 1474 IncludeKind = 1; 1475 break; 1476 1477 case tok::pp_include_next: 1478 IncludeKind = 2; 1479 break; 1480 1481 case tok::pp___include_macros: 1482 IncludeKind = 3; 1483 break; 1484 1485 default: 1486 llvm_unreachable("unknown include directive kind"); 1487 } 1488 1489 // Determine whether we are actually building the module that this 1490 // include directive maps to. 1491 bool BuildingImportedModule 1492 = Path[0].first->getName() == getLangOpts().CurrentModule; 1493 1494 if (!BuildingImportedModule && getLangOpts().ObjC2) { 1495 // If we're not building the imported module, warn that we're going 1496 // to automatically turn this inclusion directive into a module import. 1497 // We only do this in Objective-C, where we have a module-import syntax. 1498 CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd), 1499 /*IsTokenRange=*/false); 1500 Diag(HashLoc, diag::warn_auto_module_import) 1501 << IncludeKind << PathString 1502 << FixItHint::CreateReplacement(ReplaceRange, 1503 "@import " + PathString.str().str() + ";"); 1504 } 1505 1506 // Load the module. 1507 // If this was an #__include_macros directive, only make macros visible. 1508 Module::NameVisibilityKind Visibility 1509 = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible; 1510 ModuleLoadResult Imported 1511 = TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility, 1512 /*IsIncludeDirective=*/true); 1513 assert((Imported == 0 || Imported == SuggestedModule) && 1514 "the imported module is different than the suggested one"); 1515 1516 // If this header isn't part of the module we're building, we're done. 1517 if (!BuildingImportedModule && Imported) { 1518 if (Callbacks) { 1519 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, 1520 FilenameRange, File, 1521 SearchPath, RelativePath, Imported); 1522 } 1523 return; 1524 } 1525 1526 // If we failed to find a submodule that we expected to find, we can 1527 // continue. Otherwise, there's an error in the included file, so we 1528 // don't want to include it. 1529 if (!BuildingImportedModule && !Imported.isMissingExpected()) { 1530 return; 1531 } 1532 } 1533 1534 if (Callbacks && SuggestedModule) { 1535 // We didn't notify the callback object that we've seen an inclusion 1536 // directive before. Now that we are parsing the include normally and not 1537 // turning it to a module import, notify the callback object. 1538 Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, 1539 FilenameRange, File, 1540 SearchPath, RelativePath, 1541 /*ImportedModule=*/0); 1542 } 1543 1544 // The #included file will be considered to be a system header if either it is 1545 // in a system include directory, or if the #includer is a system include 1546 // header. 1547 SrcMgr::CharacteristicKind FileCharacter = 1548 std::max(HeaderInfo.getFileDirFlavor(File), 1549 SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); 1550 1551 // Ask HeaderInfo if we should enter this #include file. If not, #including 1552 // this file will have no effect. 1553 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { 1554 if (Callbacks) 1555 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); 1556 return; 1557 } 1558 1559 // Look up the file, create a File ID for it. 1560 SourceLocation IncludePos = End; 1561 // If the filename string was the result of macro expansions, set the include 1562 // position on the file where it will be included and after the expansions. 1563 if (IncludePos.isMacroID()) 1564 IncludePos = SourceMgr.getExpansionRange(IncludePos).second; 1565 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter); 1566 assert(!FID.isInvalid() && "Expected valid file ID"); 1567 1568 // Finally, if all is good, enter the new file! 1569 EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); 1570 } 1571 1572 /// HandleIncludeNextDirective - Implements \#include_next. 1573 /// 1574 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, 1575 Token &IncludeNextTok) { 1576 Diag(IncludeNextTok, diag::ext_pp_include_next_directive); 1577 1578 // #include_next is like #include, except that we start searching after 1579 // the current found directory. If we can't do this, issue a 1580 // diagnostic. 1581 const DirectoryLookup *Lookup = CurDirLookup; 1582 if (isInPrimaryFile()) { 1583 Lookup = 0; 1584 Diag(IncludeNextTok, diag::pp_include_next_in_primary); 1585 } else if (Lookup == 0) { 1586 Diag(IncludeNextTok, diag::pp_include_next_absolute_path); 1587 } else { 1588 // Start looking up in the next directory. 1589 ++Lookup; 1590 } 1591 1592 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup); 1593 } 1594 1595 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode 1596 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) { 1597 // The Microsoft #import directive takes a type library and generates header 1598 // files from it, and includes those. This is beyond the scope of what clang 1599 // does, so we ignore it and error out. However, #import can optionally have 1600 // trailing attributes that span multiple lines. We're going to eat those 1601 // so we can continue processing from there. 1602 Diag(Tok, diag::err_pp_import_directive_ms ); 1603 1604 // Read tokens until we get to the end of the directive. Note that the 1605 // directive can be split over multiple lines using the backslash character. 1606 DiscardUntilEndOfDirective(); 1607 } 1608 1609 /// HandleImportDirective - Implements \#import. 1610 /// 1611 void Preprocessor::HandleImportDirective(SourceLocation HashLoc, 1612 Token &ImportTok) { 1613 if (!LangOpts.ObjC1) { // #import is standard for ObjC. 1614 if (LangOpts.MicrosoftMode) 1615 return HandleMicrosoftImportDirective(ImportTok); 1616 Diag(ImportTok, diag::ext_pp_import_directive); 1617 } 1618 return HandleIncludeDirective(HashLoc, ImportTok, 0, true); 1619 } 1620 1621 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a 1622 /// pseudo directive in the predefines buffer. This handles it by sucking all 1623 /// tokens through the preprocessor and discarding them (only keeping the side 1624 /// effects on the preprocessor). 1625 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, 1626 Token &IncludeMacrosTok) { 1627 // This directive should only occur in the predefines buffer. If not, emit an 1628 // error and reject it. 1629 SourceLocation Loc = IncludeMacrosTok.getLocation(); 1630 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { 1631 Diag(IncludeMacrosTok.getLocation(), 1632 diag::pp_include_macros_out_of_predefines); 1633 DiscardUntilEndOfDirective(); 1634 return; 1635 } 1636 1637 // Treat this as a normal #include for checking purposes. If this is 1638 // successful, it will push a new lexer onto the include stack. 1639 HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false); 1640 1641 Token TmpTok; 1642 do { 1643 Lex(TmpTok); 1644 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); 1645 } while (TmpTok.isNot(tok::hashhash)); 1646 } 1647 1648 //===----------------------------------------------------------------------===// 1649 // Preprocessor Macro Directive Handling. 1650 //===----------------------------------------------------------------------===// 1651 1652 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro 1653 /// definition has just been read. Lex the rest of the arguments and the 1654 /// closing ), updating MI with what we learn. Return true if an error occurs 1655 /// parsing the arg list. 1656 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) { 1657 SmallVector<IdentifierInfo*, 32> Arguments; 1658 1659 while (1) { 1660 LexUnexpandedToken(Tok); 1661 switch (Tok.getKind()) { 1662 case tok::r_paren: 1663 // Found the end of the argument list. 1664 if (Arguments.empty()) // #define FOO() 1665 return false; 1666 // Otherwise we have #define FOO(A,) 1667 Diag(Tok, diag::err_pp_expected_ident_in_arg_list); 1668 return true; 1669 case tok::ellipsis: // #define X(... -> C99 varargs 1670 if (!LangOpts.C99) 1671 Diag(Tok, LangOpts.CPlusPlus11 ? 1672 diag::warn_cxx98_compat_variadic_macro : 1673 diag::ext_variadic_macro); 1674 1675 // OpenCL v1.2 s6.9.e: variadic macros are not supported. 1676 if (LangOpts.OpenCL) { 1677 Diag(Tok, diag::err_pp_opencl_variadic_macros); 1678 return true; 1679 } 1680 1681 // Lex the token after the identifier. 1682 LexUnexpandedToken(Tok); 1683 if (Tok.isNot(tok::r_paren)) { 1684 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1685 return true; 1686 } 1687 // Add the __VA_ARGS__ identifier as an argument. 1688 Arguments.push_back(Ident__VA_ARGS__); 1689 MI->setIsC99Varargs(); 1690 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1691 return false; 1692 case tok::eod: // #define X( 1693 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1694 return true; 1695 default: 1696 // Handle keywords and identifiers here to accept things like 1697 // #define Foo(for) for. 1698 IdentifierInfo *II = Tok.getIdentifierInfo(); 1699 if (II == 0) { 1700 // #define X(1 1701 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); 1702 return true; 1703 } 1704 1705 // If this is already used as an argument, it is used multiple times (e.g. 1706 // #define X(A,A. 1707 if (std::find(Arguments.begin(), Arguments.end(), II) != 1708 Arguments.end()) { // C99 6.10.3p6 1709 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; 1710 return true; 1711 } 1712 1713 // Add the argument to the macro info. 1714 Arguments.push_back(II); 1715 1716 // Lex the token after the identifier. 1717 LexUnexpandedToken(Tok); 1718 1719 switch (Tok.getKind()) { 1720 default: // #define X(A B 1721 Diag(Tok, diag::err_pp_expected_comma_in_arg_list); 1722 return true; 1723 case tok::r_paren: // #define X(A) 1724 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1725 return false; 1726 case tok::comma: // #define X(A, 1727 break; 1728 case tok::ellipsis: // #define X(A... -> GCC extension 1729 // Diagnose extension. 1730 Diag(Tok, diag::ext_named_variadic_macro); 1731 1732 // Lex the token after the identifier. 1733 LexUnexpandedToken(Tok); 1734 if (Tok.isNot(tok::r_paren)) { 1735 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); 1736 return true; 1737 } 1738 1739 MI->setIsGNUVarargs(); 1740 MI->setArgumentList(&Arguments[0], Arguments.size(), BP); 1741 return false; 1742 } 1743 } 1744 } 1745 } 1746 1747 /// HandleDefineDirective - Implements \#define. This consumes the entire macro 1748 /// line then lets the caller lex the next real token. 1749 void Preprocessor::HandleDefineDirective(Token &DefineTok) { 1750 ++NumDefined; 1751 1752 Token MacroNameTok; 1753 ReadMacroName(MacroNameTok, 1); 1754 1755 // Error reading macro name? If so, diagnostic already issued. 1756 if (MacroNameTok.is(tok::eod)) 1757 return; 1758 1759 Token LastTok = MacroNameTok; 1760 1761 // If we are supposed to keep comments in #defines, reenable comment saving 1762 // mode. 1763 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); 1764 1765 // Create the new macro. 1766 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); 1767 1768 Token Tok; 1769 LexUnexpandedToken(Tok); 1770 1771 // If this is a function-like macro definition, parse the argument list, 1772 // marking each of the identifiers as being used as macro arguments. Also, 1773 // check other constraints on the first token of the macro body. 1774 if (Tok.is(tok::eod)) { 1775 // If there is no body to this macro, we have no special handling here. 1776 } else if (Tok.hasLeadingSpace()) { 1777 // This is a normal token with leading space. Clear the leading space 1778 // marker on the first token to get proper expansion. 1779 Tok.clearFlag(Token::LeadingSpace); 1780 } else if (Tok.is(tok::l_paren)) { 1781 // This is a function-like macro definition. Read the argument list. 1782 MI->setIsFunctionLike(); 1783 if (ReadMacroDefinitionArgList(MI, LastTok)) { 1784 // Forget about MI. 1785 ReleaseMacroInfo(MI); 1786 // Throw away the rest of the line. 1787 if (CurPPLexer->ParsingPreprocessorDirective) 1788 DiscardUntilEndOfDirective(); 1789 return; 1790 } 1791 1792 // If this is a definition of a variadic C99 function-like macro, not using 1793 // the GNU named varargs extension, enabled __VA_ARGS__. 1794 1795 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. 1796 // This gets unpoisoned where it is allowed. 1797 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); 1798 if (MI->isC99Varargs()) 1799 Ident__VA_ARGS__->setIsPoisoned(false); 1800 1801 // Read the first token after the arg list for down below. 1802 LexUnexpandedToken(Tok); 1803 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) { 1804 // C99 requires whitespace between the macro definition and the body. Emit 1805 // a diagnostic for something like "#define X+". 1806 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); 1807 } else { 1808 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the 1809 // first character of a replacement list is not a character required by 1810 // subclause 5.2.1, then there shall be white-space separation between the 1811 // identifier and the replacement list.". 5.2.1 lists this set: 1812 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which 1813 // is irrelevant here. 1814 bool isInvalid = false; 1815 if (Tok.is(tok::at)) // @ is not in the list above. 1816 isInvalid = true; 1817 else if (Tok.is(tok::unknown)) { 1818 // If we have an unknown token, it is something strange like "`". Since 1819 // all of valid characters would have lexed into a single character 1820 // token of some sort, we know this is not a valid case. 1821 isInvalid = true; 1822 } 1823 if (isInvalid) 1824 Diag(Tok, diag::ext_missing_whitespace_after_macro_name); 1825 else 1826 Diag(Tok, diag::warn_missing_whitespace_after_macro_name); 1827 } 1828 1829 if (!Tok.is(tok::eod)) 1830 LastTok = Tok; 1831 1832 // Read the rest of the macro body. 1833 if (MI->isObjectLike()) { 1834 // Object-like macros are very simple, just read their body. 1835 while (Tok.isNot(tok::eod)) { 1836 LastTok = Tok; 1837 MI->AddTokenToBody(Tok); 1838 // Get the next token of the macro. 1839 LexUnexpandedToken(Tok); 1840 } 1841 1842 } else { 1843 // Otherwise, read the body of a function-like macro. While we are at it, 1844 // check C99 6.10.3.2p1: ensure that # operators are followed by macro 1845 // parameters in function-like macro expansions. 1846 while (Tok.isNot(tok::eod)) { 1847 LastTok = Tok; 1848 1849 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) { 1850 MI->AddTokenToBody(Tok); 1851 1852 // Get the next token of the macro. 1853 LexUnexpandedToken(Tok); 1854 continue; 1855 } 1856 1857 if (Tok.is(tok::hashhash)) { 1858 1859 // If we see token pasting, check if it looks like the gcc comma 1860 // pasting extension. We'll use this information to suppress 1861 // diagnostics later on. 1862 1863 // Get the next token of the macro. 1864 LexUnexpandedToken(Tok); 1865 1866 if (Tok.is(tok::eod)) { 1867 MI->AddTokenToBody(LastTok); 1868 break; 1869 } 1870 1871 unsigned NumTokens = MI->getNumTokens(); 1872 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ && 1873 MI->getReplacementToken(NumTokens-1).is(tok::comma)) 1874 MI->setHasCommaPasting(); 1875 1876 // Things look ok, add the '##' and param name tokens to the macro. 1877 MI->AddTokenToBody(LastTok); 1878 MI->AddTokenToBody(Tok); 1879 LastTok = Tok; 1880 1881 // Get the next token of the macro. 1882 LexUnexpandedToken(Tok); 1883 continue; 1884 } 1885 1886 // Get the next token of the macro. 1887 LexUnexpandedToken(Tok); 1888 1889 // Check for a valid macro arg identifier. 1890 if (Tok.getIdentifierInfo() == 0 || 1891 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { 1892 1893 // If this is assembler-with-cpp mode, we accept random gibberish after 1894 // the '#' because '#' is often a comment character. However, change 1895 // the kind of the token to tok::unknown so that the preprocessor isn't 1896 // confused. 1897 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) { 1898 LastTok.setKind(tok::unknown); 1899 } else { 1900 Diag(Tok, diag::err_pp_stringize_not_parameter); 1901 ReleaseMacroInfo(MI); 1902 1903 // Disable __VA_ARGS__ again. 1904 Ident__VA_ARGS__->setIsPoisoned(true); 1905 return; 1906 } 1907 } 1908 1909 // Things look ok, add the '#' and param name tokens to the macro. 1910 MI->AddTokenToBody(LastTok); 1911 MI->AddTokenToBody(Tok); 1912 LastTok = Tok; 1913 1914 // Get the next token of the macro. 1915 LexUnexpandedToken(Tok); 1916 } 1917 } 1918 1919 1920 // Disable __VA_ARGS__ again. 1921 Ident__VA_ARGS__->setIsPoisoned(true); 1922 1923 // Check that there is no paste (##) operator at the beginning or end of the 1924 // replacement list. 1925 unsigned NumTokens = MI->getNumTokens(); 1926 if (NumTokens != 0) { 1927 if (MI->getReplacementToken(0).is(tok::hashhash)) { 1928 Diag(MI->getReplacementToken(0), diag::err_paste_at_start); 1929 ReleaseMacroInfo(MI); 1930 return; 1931 } 1932 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { 1933 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); 1934 ReleaseMacroInfo(MI); 1935 return; 1936 } 1937 } 1938 1939 MI->setDefinitionEndLoc(LastTok.getLocation()); 1940 1941 // Finally, if this identifier already had a macro defined for it, verify that 1942 // the macro bodies are identical, and issue diagnostics if they are not. 1943 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { 1944 // It is very common for system headers to have tons of macro redefinitions 1945 // and for warnings to be disabled in system headers. If this is the case, 1946 // then don't bother calling MacroInfo::isIdenticalTo. 1947 if (!getDiagnostics().getSuppressSystemWarnings() || 1948 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { 1949 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) 1950 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); 1951 1952 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and 1953 // C++ [cpp.predefined]p4, but allow it as an extension. 1954 if (OtherMI->isBuiltinMacro()) 1955 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro); 1956 // Macros must be identical. This means all tokens and whitespace 1957 // separation must be the same. C99 6.10.3p2. 1958 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() && 1959 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) { 1960 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) 1961 << MacroNameTok.getIdentifierInfo(); 1962 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); 1963 } 1964 } 1965 if (OtherMI->isWarnIfUnused()) 1966 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); 1967 } 1968 1969 DefMacroDirective *MD = 1970 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI); 1971 1972 assert(!MI->isUsed()); 1973 // If we need warning for not using the macro, add its location in the 1974 // warn-because-unused-macro set. If it gets used it will be removed from set. 1975 if (isInPrimaryFile() && // don't warn for include'd macros. 1976 Diags->getDiagnosticLevel(diag::pp_macro_not_used, 1977 MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) { 1978 MI->setIsWarnIfUnused(true); 1979 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); 1980 } 1981 1982 // If the callbacks want to know, tell them about the macro definition. 1983 if (Callbacks) 1984 Callbacks->MacroDefined(MacroNameTok, MD); 1985 } 1986 1987 /// HandleUndefDirective - Implements \#undef. 1988 /// 1989 void Preprocessor::HandleUndefDirective(Token &UndefTok) { 1990 ++NumUndefined; 1991 1992 Token MacroNameTok; 1993 ReadMacroName(MacroNameTok, 2); 1994 1995 // Error reading macro name? If so, diagnostic already issued. 1996 if (MacroNameTok.is(tok::eod)) 1997 return; 1998 1999 // Check to see if this is the last token on the #undef line. 2000 CheckEndOfDirective("undef"); 2001 2002 // Okay, we finally have a valid identifier to undef. 2003 MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); 2004 const MacroInfo *MI = MD ? MD->getMacroInfo() : 0; 2005 2006 // If the callbacks want to know, tell them about the macro #undef. 2007 // Note: no matter if the macro was defined or not. 2008 if (Callbacks) 2009 Callbacks->MacroUndefined(MacroNameTok, MD); 2010 2011 // If the macro is not defined, this is a noop undef, just return. 2012 if (MI == 0) return; 2013 2014 if (!MI->isUsed() && MI->isWarnIfUnused()) 2015 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); 2016 2017 if (MI->isWarnIfUnused()) 2018 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 2019 2020 appendMacroDirective(MacroNameTok.getIdentifierInfo(), 2021 AllocateUndefMacroDirective(MacroNameTok.getLocation())); 2022 } 2023 2024 2025 //===----------------------------------------------------------------------===// 2026 // Preprocessor Conditional Directive Handling. 2027 //===----------------------------------------------------------------------===// 2028 2029 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef 2030 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is 2031 /// true if any tokens have been returned or pp-directives activated before this 2032 /// \#ifndef has been lexed. 2033 /// 2034 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, 2035 bool ReadAnyTokensBeforeDirective) { 2036 ++NumIf; 2037 Token DirectiveTok = Result; 2038 2039 Token MacroNameTok; 2040 ReadMacroName(MacroNameTok); 2041 2042 // Error reading macro name? If so, diagnostic already issued. 2043 if (MacroNameTok.is(tok::eod)) { 2044 // Skip code until we get to #endif. This helps with recovery by not 2045 // emitting an error when the #endif is reached. 2046 SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 2047 /*Foundnonskip*/false, /*FoundElse*/false); 2048 return; 2049 } 2050 2051 // Check to see if this is the last token on the #if[n]def line. 2052 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); 2053 2054 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); 2055 MacroDirective *MD = getMacroDirective(MII); 2056 MacroInfo *MI = MD ? MD->getMacroInfo() : 0; 2057 2058 if (CurPPLexer->getConditionalStackDepth() == 0) { 2059 // If the start of a top-level #ifdef and if the macro is not defined, 2060 // inform MIOpt that this might be the start of a proper include guard. 2061 // Otherwise it is some other form of unknown conditional which we can't 2062 // handle. 2063 if (!ReadAnyTokensBeforeDirective && MI == 0) { 2064 assert(isIfndef && "#ifdef shouldn't reach here"); 2065 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); 2066 } else 2067 CurPPLexer->MIOpt.EnterTopLevelConditional(); 2068 } 2069 2070 // If there is a macro, process it. 2071 if (MI) // Mark it used. 2072 markMacroAsUsed(MI); 2073 2074 if (Callbacks) { 2075 if (isIfndef) 2076 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); 2077 else 2078 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD); 2079 } 2080 2081 // Should we include the stuff contained by this directive? 2082 if (!MI == isIfndef) { 2083 // Yes, remember that we are inside a conditional, then lex the next token. 2084 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), 2085 /*wasskip*/false, /*foundnonskip*/true, 2086 /*foundelse*/false); 2087 } else { 2088 // No, skip the contents of this block. 2089 SkipExcludedConditionalBlock(DirectiveTok.getLocation(), 2090 /*Foundnonskip*/false, 2091 /*FoundElse*/false); 2092 } 2093 } 2094 2095 /// HandleIfDirective - Implements the \#if directive. 2096 /// 2097 void Preprocessor::HandleIfDirective(Token &IfToken, 2098 bool ReadAnyTokensBeforeDirective) { 2099 ++NumIf; 2100 2101 // Parse and evaluate the conditional expression. 2102 IdentifierInfo *IfNDefMacro = 0; 2103 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); 2104 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); 2105 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); 2106 2107 // If this condition is equivalent to #ifndef X, and if this is the first 2108 // directive seen, handle it for the multiple-include optimization. 2109 if (CurPPLexer->getConditionalStackDepth() == 0) { 2110 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) 2111 CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); 2112 else 2113 CurPPLexer->MIOpt.EnterTopLevelConditional(); 2114 } 2115 2116 if (Callbacks) 2117 Callbacks->If(IfToken.getLocation(), 2118 SourceRange(ConditionalBegin, ConditionalEnd)); 2119 2120 // Should we include the stuff contained by this directive? 2121 if (ConditionalTrue) { 2122 // Yes, remember that we are inside a conditional, then lex the next token. 2123 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, 2124 /*foundnonskip*/true, /*foundelse*/false); 2125 } else { 2126 // No, skip the contents of this block. 2127 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, 2128 /*FoundElse*/false); 2129 } 2130 } 2131 2132 /// HandleEndifDirective - Implements the \#endif directive. 2133 /// 2134 void Preprocessor::HandleEndifDirective(Token &EndifToken) { 2135 ++NumEndif; 2136 2137 // Check that this is the whole directive. 2138 CheckEndOfDirective("endif"); 2139 2140 PPConditionalInfo CondInfo; 2141 if (CurPPLexer->popConditionalLevel(CondInfo)) { 2142 // No conditionals on the stack: this is an #endif without an #if. 2143 Diag(EndifToken, diag::err_pp_endif_without_if); 2144 return; 2145 } 2146 2147 // If this the end of a top-level #endif, inform MIOpt. 2148 if (CurPPLexer->getConditionalStackDepth() == 0) 2149 CurPPLexer->MIOpt.ExitTopLevelConditional(); 2150 2151 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && 2152 "This code should only be reachable in the non-skipping case!"); 2153 2154 if (Callbacks) 2155 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc); 2156 } 2157 2158 /// HandleElseDirective - Implements the \#else directive. 2159 /// 2160 void Preprocessor::HandleElseDirective(Token &Result) { 2161 ++NumElse; 2162 2163 // #else directive in a non-skipping conditional... start skipping. 2164 CheckEndOfDirective("else"); 2165 2166 PPConditionalInfo CI; 2167 if (CurPPLexer->popConditionalLevel(CI)) { 2168 Diag(Result, diag::pp_err_else_without_if); 2169 return; 2170 } 2171 2172 // If this is a top-level #else, inform the MIOpt. 2173 if (CurPPLexer->getConditionalStackDepth() == 0) 2174 CurPPLexer->MIOpt.EnterTopLevelConditional(); 2175 2176 // If this is a #else with a #else before it, report the error. 2177 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); 2178 2179 if (Callbacks) 2180 Callbacks->Else(Result.getLocation(), CI.IfLoc); 2181 2182 // Finally, skip the rest of the contents of this block. 2183 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 2184 /*FoundElse*/true, Result.getLocation()); 2185 } 2186 2187 /// HandleElifDirective - Implements the \#elif directive. 2188 /// 2189 void Preprocessor::HandleElifDirective(Token &ElifToken) { 2190 ++NumElse; 2191 2192 // #elif directive in a non-skipping conditional... start skipping. 2193 // We don't care what the condition is, because we will always skip it (since 2194 // the block immediately before it was included). 2195 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); 2196 DiscardUntilEndOfDirective(); 2197 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); 2198 2199 PPConditionalInfo CI; 2200 if (CurPPLexer->popConditionalLevel(CI)) { 2201 Diag(ElifToken, diag::pp_err_elif_without_if); 2202 return; 2203 } 2204 2205 // If this is a top-level #elif, inform the MIOpt. 2206 if (CurPPLexer->getConditionalStackDepth() == 0) 2207 CurPPLexer->MIOpt.EnterTopLevelConditional(); 2208 2209 // If this is a #elif with a #else before it, report the error. 2210 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); 2211 2212 if (Callbacks) 2213 Callbacks->Elif(ElifToken.getLocation(), 2214 SourceRange(ConditionalBegin, ConditionalEnd), CI.IfLoc); 2215 2216 // Finally, skip the rest of the contents of this block. 2217 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, 2218 /*FoundElse*/CI.FoundElse, 2219 ElifToken.getLocation()); 2220 } 2221