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