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