1//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the top level handling of macro expansion for the 11// preprocessor. 12// 13//===----------------------------------------------------------------------===// 14 15#include "clang/Lex/Preprocessor.h" 16#include "clang/Basic/Attributes.h" 17#include "clang/Basic/FileManager.h" 18#include "clang/Basic/SourceManager.h" 19#include "clang/Basic/TargetInfo.h" 20#include "clang/Lex/CodeCompletionHandler.h" 21#include "clang/Lex/ExternalPreprocessorSource.h" 22#include "clang/Lex/LexDiagnostic.h" 23#include "clang/Lex/MacroArgs.h" 24#include "clang/Lex/MacroInfo.h" 25#include "llvm/ADT/STLExtras.h" 26#include "llvm/ADT/SmallString.h" 27#include "llvm/ADT/StringSwitch.h" 28#include "llvm/Config/llvm-config.h" 29#include "llvm/Support/ErrorHandling.h" 30#include "llvm/Support/Format.h" 31#include "llvm/Support/raw_ostream.h" 32#include <cstdio> 33#include <ctime> 34using namespace clang; 35 36MacroDirective * 37Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const { 38 if (!II->hadMacroDefinition()) 39 return nullptr; 40 auto Pos = CurSubmoduleState->Macros.find(II); 41 return Pos == CurSubmoduleState->Macros.end() ? nullptr 42 : Pos->second.getLatest(); 43} 44 45void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ 46 assert(MD && "MacroDirective should be non-zero!"); 47 assert(!MD->getPrevious() && "Already attached to a MacroDirective history."); 48 49 MacroState &StoredMD = CurSubmoduleState->Macros[II]; 50 auto *OldMD = StoredMD.getLatest(); 51 MD->setPrevious(OldMD); 52 StoredMD.setLatest(MD); 53 StoredMD.overrideActiveModuleMacros(*this, II); 54 55 // Set up the identifier as having associated macro history. 56 II->setHasMacroDefinition(true); 57 if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end()) 58 II->setHasMacroDefinition(false); 59 if (II->isFromAST()) 60 II->setChangedSinceDeserialization(); 61} 62 63void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II, 64 MacroDirective *MD) { 65 assert(II && MD); 66 MacroState &StoredMD = CurSubmoduleState->Macros[II]; 67 assert(!StoredMD.getLatest() && 68 "the macro history was modified before initializing it from a pch"); 69 StoredMD = MD; 70 // Setup the identifier as having associated macro history. 71 II->setHasMacroDefinition(true); 72 if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end()) 73 II->setHasMacroDefinition(false); 74} 75 76ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II, 77 MacroInfo *Macro, 78 ArrayRef<ModuleMacro *> Overrides, 79 bool &New) { 80 llvm::FoldingSetNodeID ID; 81 ModuleMacro::Profile(ID, Mod, II); 82 83 void *InsertPos; 84 if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) { 85 New = false; 86 return MM; 87 } 88 89 auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides); 90 ModuleMacros.InsertNode(MM, InsertPos); 91 92 // Each overridden macro is now overridden by one more macro. 93 bool HidAny = false; 94 for (auto *O : Overrides) { 95 HidAny |= (O->NumOverriddenBy == 0); 96 ++O->NumOverriddenBy; 97 } 98 99 // If we were the first overrider for any macro, it's no longer a leaf. 100 auto &LeafMacros = LeafModuleMacros[II]; 101 if (HidAny) { 102 LeafMacros.erase(std::remove_if(LeafMacros.begin(), LeafMacros.end(), 103 [](ModuleMacro *MM) { 104 return MM->NumOverriddenBy != 0; 105 }), 106 LeafMacros.end()); 107 } 108 109 // The new macro is always a leaf macro. 110 LeafMacros.push_back(MM); 111 // The identifier now has defined macros (that may or may not be visible). 112 II->setHasMacroDefinition(true); 113 114 New = true; 115 return MM; 116} 117 118ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, IdentifierInfo *II) { 119 llvm::FoldingSetNodeID ID; 120 ModuleMacro::Profile(ID, Mod, II); 121 122 void *InsertPos; 123 return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos); 124} 125 126void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II, 127 ModuleMacroInfo &Info) { 128 assert(Info.ActiveModuleMacrosGeneration != 129 CurSubmoduleState->VisibleModules.getGeneration() && 130 "don't need to update this macro name info"); 131 Info.ActiveModuleMacrosGeneration = 132 CurSubmoduleState->VisibleModules.getGeneration(); 133 134 auto Leaf = LeafModuleMacros.find(II); 135 if (Leaf == LeafModuleMacros.end()) { 136 // No imported macros at all: nothing to do. 137 return; 138 } 139 140 Info.ActiveModuleMacros.clear(); 141 142 // Every macro that's locally overridden is overridden by a visible macro. 143 llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides; 144 for (auto *O : Info.OverriddenMacros) 145 NumHiddenOverrides[O] = -1; 146 147 // Collect all macros that are not overridden by a visible macro. 148 llvm::SmallVector<ModuleMacro *, 16> Worklist; 149 for (auto *LeafMM : Leaf->second) { 150 assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden"); 151 if (NumHiddenOverrides.lookup(LeafMM) == 0) 152 Worklist.push_back(LeafMM); 153 } 154 while (!Worklist.empty()) { 155 auto *MM = Worklist.pop_back_val(); 156 if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) { 157 // We only care about collecting definitions; undefinitions only act 158 // to override other definitions. 159 if (MM->getMacroInfo()) 160 Info.ActiveModuleMacros.push_back(MM); 161 } else { 162 for (auto *O : MM->overrides()) 163 if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros()) 164 Worklist.push_back(O); 165 } 166 } 167 // Our reverse postorder walk found the macros in reverse order. 168 std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end()); 169 170 // Determine whether the macro name is ambiguous. 171 MacroInfo *MI = nullptr; 172 bool IsSystemMacro = true; 173 bool IsAmbiguous = false; 174 if (auto *MD = Info.MD) { 175 while (MD && isa<VisibilityMacroDirective>(MD)) 176 MD = MD->getPrevious(); 177 if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) { 178 MI = DMD->getInfo(); 179 IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation()); 180 } 181 } 182 for (auto *Active : Info.ActiveModuleMacros) { 183 auto *NewMI = Active->getMacroInfo(); 184 185 // Before marking the macro as ambiguous, check if this is a case where 186 // both macros are in system headers. If so, we trust that the system 187 // did not get it wrong. This also handles cases where Clang's own 188 // headers have a different spelling of certain system macros: 189 // #define LONG_MAX __LONG_MAX__ (clang's limits.h) 190 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h) 191 // 192 // FIXME: Remove the defined-in-system-headers check. clang's limits.h 193 // overrides the system limits.h's macros, so there's no conflict here. 194 if (MI && NewMI != MI && 195 !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)) 196 IsAmbiguous = true; 197 IsSystemMacro &= Active->getOwningModule()->IsSystem || 198 SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc()); 199 MI = NewMI; 200 } 201 Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro; 202} 203 204void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) { 205 ArrayRef<ModuleMacro*> Leaf; 206 auto LeafIt = LeafModuleMacros.find(II); 207 if (LeafIt != LeafModuleMacros.end()) 208 Leaf = LeafIt->second; 209 const MacroState *State = nullptr; 210 auto Pos = CurSubmoduleState->Macros.find(II); 211 if (Pos != CurSubmoduleState->Macros.end()) 212 State = &Pos->second; 213 214 llvm::errs() << "MacroState " << State << " " << II->getNameStart(); 215 if (State && State->isAmbiguous(*this, II)) 216 llvm::errs() << " ambiguous"; 217 if (State && !State->getOverriddenMacros().empty()) { 218 llvm::errs() << " overrides"; 219 for (auto *O : State->getOverriddenMacros()) 220 llvm::errs() << " " << O->getOwningModule()->getFullModuleName(); 221 } 222 llvm::errs() << "\n"; 223 224 // Dump local macro directives. 225 for (auto *MD = State ? State->getLatest() : nullptr; MD; 226 MD = MD->getPrevious()) { 227 llvm::errs() << " "; 228 MD->dump(); 229 } 230 231 // Dump module macros. 232 llvm::DenseSet<ModuleMacro*> Active; 233 for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None) 234 Active.insert(MM); 235 llvm::DenseSet<ModuleMacro*> Visited; 236 llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end()); 237 while (!Worklist.empty()) { 238 auto *MM = Worklist.pop_back_val(); 239 llvm::errs() << " ModuleMacro " << MM << " " 240 << MM->getOwningModule()->getFullModuleName(); 241 if (!MM->getMacroInfo()) 242 llvm::errs() << " undef"; 243 244 if (Active.count(MM)) 245 llvm::errs() << " active"; 246 else if (!CurSubmoduleState->VisibleModules.isVisible( 247 MM->getOwningModule())) 248 llvm::errs() << " hidden"; 249 else if (MM->getMacroInfo()) 250 llvm::errs() << " overridden"; 251 252 if (!MM->overrides().empty()) { 253 llvm::errs() << " overrides"; 254 for (auto *O : MM->overrides()) { 255 llvm::errs() << " " << O->getOwningModule()->getFullModuleName(); 256 if (Visited.insert(O).second) 257 Worklist.push_back(O); 258 } 259 } 260 llvm::errs() << "\n"; 261 if (auto *MI = MM->getMacroInfo()) { 262 llvm::errs() << " "; 263 MI->dump(); 264 llvm::errs() << "\n"; 265 } 266 } 267} 268 269/// RegisterBuiltinMacro - Register the specified identifier in the identifier 270/// table and mark it as a builtin macro to be expanded. 271static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ 272 // Get the identifier. 273 IdentifierInfo *Id = PP.getIdentifierInfo(Name); 274 275 // Mark it as being a macro that is builtin. 276 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); 277 MI->setIsBuiltinMacro(); 278 PP.appendDefMacroDirective(Id, MI); 279 return Id; 280} 281 282 283/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the 284/// identifier table. 285void Preprocessor::RegisterBuiltinMacros() { 286 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); 287 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); 288 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); 289 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); 290 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); 291 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); 292 293 // C++ Standing Document Extensions. 294 if (LangOpts.CPlusPlus) 295 Ident__has_cpp_attribute = 296 RegisterBuiltinMacro(*this, "__has_cpp_attribute"); 297 else 298 Ident__has_cpp_attribute = nullptr; 299 300 // GCC Extensions. 301 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); 302 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); 303 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); 304 305 // Microsoft Extensions. 306 if (LangOpts.MicrosoftExt) { 307 Ident__identifier = RegisterBuiltinMacro(*this, "__identifier"); 308 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); 309 } else { 310 Ident__identifier = nullptr; 311 Ident__pragma = nullptr; 312 } 313 314 // Clang Extensions. 315 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); 316 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); 317 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); 318 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); 319 Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute"); 320 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); 321 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); 322 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); 323 Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier"); 324 325 // Modules. 326 if (LangOpts.Modules) { 327 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module"); 328 329 // __MODULE__ 330 if (!LangOpts.CurrentModule.empty()) 331 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__"); 332 else 333 Ident__MODULE__ = nullptr; 334 } else { 335 Ident__building_module = nullptr; 336 Ident__MODULE__ = nullptr; 337 } 338} 339 340/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token 341/// in its expansion, currently expands to that token literally. 342static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, 343 const IdentifierInfo *MacroIdent, 344 Preprocessor &PP) { 345 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); 346 347 // If the token isn't an identifier, it's always literally expanded. 348 if (!II) return true; 349 350 // If the information about this identifier is out of date, update it from 351 // the external source. 352 if (II->isOutOfDate()) 353 PP.getExternalSource()->updateOutOfDateIdentifier(*II); 354 355 // If the identifier is a macro, and if that macro is enabled, it may be 356 // expanded so it's not a trivial expansion. 357 if (auto *ExpansionMI = PP.getMacroInfo(II)) 358 if (ExpansionMI->isEnabled() && 359 // Fast expanding "#define X X" is ok, because X would be disabled. 360 II != MacroIdent) 361 return false; 362 363 // If this is an object-like macro invocation, it is safe to trivially expand 364 // it. 365 if (MI->isObjectLike()) return true; 366 367 // If this is a function-like macro invocation, it's safe to trivially expand 368 // as long as the identifier is not a macro argument. 369 return std::find(MI->arg_begin(), MI->arg_end(), II) == MI->arg_end(); 370 371} 372 373 374/// isNextPPTokenLParen - Determine whether the next preprocessor token to be 375/// lexed is a '('. If so, consume the token and return true, if not, this 376/// method should have no observable side-effect on the lexed tokens. 377bool Preprocessor::isNextPPTokenLParen() { 378 // Do some quick tests for rejection cases. 379 unsigned Val; 380 if (CurLexer) 381 Val = CurLexer->isNextPPTokenLParen(); 382 else if (CurPTHLexer) 383 Val = CurPTHLexer->isNextPPTokenLParen(); 384 else 385 Val = CurTokenLexer->isNextTokenLParen(); 386 387 if (Val == 2) { 388 // We have run off the end. If it's a source file we don't 389 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the 390 // macro stack. 391 if (CurPPLexer) 392 return false; 393 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { 394 IncludeStackInfo &Entry = IncludeMacroStack[i-1]; 395 if (Entry.TheLexer) 396 Val = Entry.TheLexer->isNextPPTokenLParen(); 397 else if (Entry.ThePTHLexer) 398 Val = Entry.ThePTHLexer->isNextPPTokenLParen(); 399 else 400 Val = Entry.TheTokenLexer->isNextTokenLParen(); 401 402 if (Val != 2) 403 break; 404 405 // Ran off the end of a source file? 406 if (Entry.ThePPLexer) 407 return false; 408 } 409 } 410 411 // Okay, if we know that the token is a '(', lex it and return. Otherwise we 412 // have found something that isn't a '(' or we found the end of the 413 // translation unit. In either case, return false. 414 return Val == 1; 415} 416 417/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be 418/// expanded as a macro, handle it and return the next token as 'Identifier'. 419bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, 420 const MacroDefinition &M) { 421 MacroInfo *MI = M.getMacroInfo(); 422 423 // If this is a macro expansion in the "#if !defined(x)" line for the file, 424 // then the macro could expand to different things in other contexts, we need 425 // to disable the optimization in this case. 426 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); 427 428 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. 429 if (MI->isBuiltinMacro()) { 430 if (Callbacks) 431 Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(), 432 /*Args=*/nullptr); 433 ExpandBuiltinMacro(Identifier); 434 return true; 435 } 436 437 /// Args - If this is a function-like macro expansion, this contains, 438 /// for each macro argument, the list of tokens that were provided to the 439 /// invocation. 440 MacroArgs *Args = nullptr; 441 442 // Remember where the end of the expansion occurred. For an object-like 443 // macro, this is the identifier. For a function-like macro, this is the ')'. 444 SourceLocation ExpansionEnd = Identifier.getLocation(); 445 446 // If this is a function-like macro, read the arguments. 447 if (MI->isFunctionLike()) { 448 // Remember that we are now parsing the arguments to a macro invocation. 449 // Preprocessor directives used inside macro arguments are not portable, and 450 // this enables the warning. 451 InMacroArgs = true; 452 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); 453 454 // Finished parsing args. 455 InMacroArgs = false; 456 457 // If there was an error parsing the arguments, bail out. 458 if (!Args) return true; 459 460 ++NumFnMacroExpanded; 461 } else { 462 ++NumMacroExpanded; 463 } 464 465 // Notice that this macro has been used. 466 markMacroAsUsed(MI); 467 468 // Remember where the token is expanded. 469 SourceLocation ExpandLoc = Identifier.getLocation(); 470 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); 471 472 if (Callbacks) { 473 if (InMacroArgs) { 474 // We can have macro expansion inside a conditional directive while 475 // reading the function macro arguments. To ensure, in that case, that 476 // MacroExpands callbacks still happen in source order, queue this 477 // callback to have it happen after the function macro callback. 478 DelayedMacroExpandsCallbacks.push_back( 479 MacroExpandsInfo(Identifier, M, ExpansionRange)); 480 } else { 481 Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args); 482 if (!DelayedMacroExpandsCallbacks.empty()) { 483 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { 484 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; 485 // FIXME: We lose macro args info with delayed callback. 486 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, 487 /*Args=*/nullptr); 488 } 489 DelayedMacroExpandsCallbacks.clear(); 490 } 491 } 492 } 493 494 // If the macro definition is ambiguous, complain. 495 if (M.isAmbiguous()) { 496 Diag(Identifier, diag::warn_pp_ambiguous_macro) 497 << Identifier.getIdentifierInfo(); 498 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) 499 << Identifier.getIdentifierInfo(); 500 M.forAllDefinitions([&](const MacroInfo *OtherMI) { 501 if (OtherMI != MI) 502 Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other) 503 << Identifier.getIdentifierInfo(); 504 }); 505 } 506 507 // If we started lexing a macro, enter the macro expansion body. 508 509 // If this macro expands to no tokens, don't bother to push it onto the 510 // expansion stack, only to take it right back off. 511 if (MI->getNumTokens() == 0) { 512 // No need for arg info. 513 if (Args) Args->destroy(*this); 514 515 // Propagate whitespace info as if we had pushed, then popped, 516 // a macro context. 517 Identifier.setFlag(Token::LeadingEmptyMacro); 518 PropagateLineStartLeadingSpaceInfo(Identifier); 519 ++NumFastMacroExpanded; 520 return false; 521 } else if (MI->getNumTokens() == 1 && 522 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), 523 *this)) { 524 // Otherwise, if this macro expands into a single trivially-expanded 525 // token: expand it now. This handles common cases like 526 // "#define VAL 42". 527 528 // No need for arg info. 529 if (Args) Args->destroy(*this); 530 531 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro 532 // identifier to the expanded token. 533 bool isAtStartOfLine = Identifier.isAtStartOfLine(); 534 bool hasLeadingSpace = Identifier.hasLeadingSpace(); 535 536 // Replace the result token. 537 Identifier = MI->getReplacementToken(0); 538 539 // Restore the StartOfLine/LeadingSpace markers. 540 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); 541 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); 542 543 // Update the tokens location to include both its expansion and physical 544 // locations. 545 SourceLocation Loc = 546 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, 547 ExpansionEnd,Identifier.getLength()); 548 Identifier.setLocation(Loc); 549 550 // If this is a disabled macro or #define X X, we must mark the result as 551 // unexpandable. 552 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { 553 if (MacroInfo *NewMI = getMacroInfo(NewII)) 554 if (!NewMI->isEnabled() || NewMI == MI) { 555 Identifier.setFlag(Token::DisableExpand); 556 // Don't warn for "#define X X" like "#define bool bool" from 557 // stdbool.h. 558 if (NewMI != MI || MI->isFunctionLike()) 559 Diag(Identifier, diag::pp_disabled_macro_expansion); 560 } 561 } 562 563 // Since this is not an identifier token, it can't be macro expanded, so 564 // we're done. 565 ++NumFastMacroExpanded; 566 return true; 567 } 568 569 // Start expanding the macro. 570 EnterMacro(Identifier, ExpansionEnd, MI, Args); 571 return false; 572} 573 574enum Bracket { 575 Brace, 576 Paren 577}; 578 579/// CheckMatchedBrackets - Returns true if the braces and parentheses in the 580/// token vector are properly nested. 581static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) { 582 SmallVector<Bracket, 8> Brackets; 583 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(), 584 E = Tokens.end(); 585 I != E; ++I) { 586 if (I->is(tok::l_paren)) { 587 Brackets.push_back(Paren); 588 } else if (I->is(tok::r_paren)) { 589 if (Brackets.empty() || Brackets.back() == Brace) 590 return false; 591 Brackets.pop_back(); 592 } else if (I->is(tok::l_brace)) { 593 Brackets.push_back(Brace); 594 } else if (I->is(tok::r_brace)) { 595 if (Brackets.empty() || Brackets.back() == Paren) 596 return false; 597 Brackets.pop_back(); 598 } 599 } 600 return Brackets.empty(); 601} 602 603/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new 604/// vector of tokens in NewTokens. The new number of arguments will be placed 605/// in NumArgs and the ranges which need to surrounded in parentheses will be 606/// in ParenHints. 607/// Returns false if the token stream cannot be changed. If this is because 608/// of an initializer list starting a macro argument, the range of those 609/// initializer lists will be place in InitLists. 610static bool GenerateNewArgTokens(Preprocessor &PP, 611 SmallVectorImpl<Token> &OldTokens, 612 SmallVectorImpl<Token> &NewTokens, 613 unsigned &NumArgs, 614 SmallVectorImpl<SourceRange> &ParenHints, 615 SmallVectorImpl<SourceRange> &InitLists) { 616 if (!CheckMatchedBrackets(OldTokens)) 617 return false; 618 619 // Once it is known that the brackets are matched, only a simple count of the 620 // braces is needed. 621 unsigned Braces = 0; 622 623 // First token of a new macro argument. 624 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin(); 625 626 // First closing brace in a new macro argument. Used to generate 627 // SourceRanges for InitLists. 628 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end(); 629 NumArgs = 0; 630 Token TempToken; 631 // Set to true when a macro separator token is found inside a braced list. 632 // If true, the fixed argument spans multiple old arguments and ParenHints 633 // will be updated. 634 bool FoundSeparatorToken = false; 635 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(), 636 E = OldTokens.end(); 637 I != E; ++I) { 638 if (I->is(tok::l_brace)) { 639 ++Braces; 640 } else if (I->is(tok::r_brace)) { 641 --Braces; 642 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken) 643 ClosingBrace = I; 644 } else if (I->is(tok::eof)) { 645 // EOF token is used to separate macro arguments 646 if (Braces != 0) { 647 // Assume comma separator is actually braced list separator and change 648 // it back to a comma. 649 FoundSeparatorToken = true; 650 I->setKind(tok::comma); 651 I->setLength(1); 652 } else { // Braces == 0 653 // Separator token still separates arguments. 654 ++NumArgs; 655 656 // If the argument starts with a brace, it can't be fixed with 657 // parentheses. A different diagnostic will be given. 658 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) { 659 InitLists.push_back( 660 SourceRange(ArgStartIterator->getLocation(), 661 PP.getLocForEndOfToken(ClosingBrace->getLocation()))); 662 ClosingBrace = E; 663 } 664 665 // Add left paren 666 if (FoundSeparatorToken) { 667 TempToken.startToken(); 668 TempToken.setKind(tok::l_paren); 669 TempToken.setLocation(ArgStartIterator->getLocation()); 670 TempToken.setLength(0); 671 NewTokens.push_back(TempToken); 672 } 673 674 // Copy over argument tokens 675 NewTokens.insert(NewTokens.end(), ArgStartIterator, I); 676 677 // Add right paren and store the paren locations in ParenHints 678 if (FoundSeparatorToken) { 679 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation()); 680 TempToken.startToken(); 681 TempToken.setKind(tok::r_paren); 682 TempToken.setLocation(Loc); 683 TempToken.setLength(0); 684 NewTokens.push_back(TempToken); 685 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(), 686 Loc)); 687 } 688 689 // Copy separator token 690 NewTokens.push_back(*I); 691 692 // Reset values 693 ArgStartIterator = I + 1; 694 FoundSeparatorToken = false; 695 } 696 } 697 } 698 699 return !ParenHints.empty() && InitLists.empty(); 700} 701 702/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next 703/// token is the '(' of the macro, this method is invoked to read all of the 704/// actual arguments specified for the macro invocation. This returns null on 705/// error. 706MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, 707 MacroInfo *MI, 708 SourceLocation &MacroEnd) { 709 // The number of fixed arguments to parse. 710 unsigned NumFixedArgsLeft = MI->getNumArgs(); 711 bool isVariadic = MI->isVariadic(); 712 713 // Outer loop, while there are more arguments, keep reading them. 714 Token Tok; 715 716 // Read arguments as unexpanded tokens. This avoids issues, e.g., where 717 // an argument value in a macro could expand to ',' or '(' or ')'. 718 LexUnexpandedToken(Tok); 719 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); 720 721 // ArgTokens - Build up a list of tokens that make up each argument. Each 722 // argument is separated by an EOF token. Use a SmallVector so we can avoid 723 // heap allocations in the common case. 724 SmallVector<Token, 64> ArgTokens; 725 bool ContainsCodeCompletionTok = false; 726 727 SourceLocation TooManyArgsLoc; 728 729 unsigned NumActuals = 0; 730 while (Tok.isNot(tok::r_paren)) { 731 if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod)) 732 break; 733 734 assert(Tok.isOneOf(tok::l_paren, tok::comma) && 735 "only expect argument separators here"); 736 737 unsigned ArgTokenStart = ArgTokens.size(); 738 SourceLocation ArgStartLoc = Tok.getLocation(); 739 740 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note 741 // that we already consumed the first one. 742 unsigned NumParens = 0; 743 744 while (1) { 745 // Read arguments as unexpanded tokens. This avoids issues, e.g., where 746 // an argument value in a macro could expand to ',' or '(' or ')'. 747 LexUnexpandedToken(Tok); 748 749 if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n" 750 if (!ContainsCodeCompletionTok) { 751 Diag(MacroName, diag::err_unterm_macro_invoc); 752 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 753 << MacroName.getIdentifierInfo(); 754 // Do not lose the EOF/EOD. Return it to the client. 755 MacroName = Tok; 756 return nullptr; 757 } else { 758 // Do not lose the EOF/EOD. 759 Token *Toks = new Token[1]; 760 Toks[0] = Tok; 761 EnterTokenStream(Toks, 1, true, true); 762 break; 763 } 764 } else if (Tok.is(tok::r_paren)) { 765 // If we found the ) token, the macro arg list is done. 766 if (NumParens-- == 0) { 767 MacroEnd = Tok.getLocation(); 768 break; 769 } 770 } else if (Tok.is(tok::l_paren)) { 771 ++NumParens; 772 } else if (Tok.is(tok::comma) && NumParens == 0 && 773 !(Tok.getFlags() & Token::IgnoredComma)) { 774 // In Microsoft-compatibility mode, single commas from nested macro 775 // expansions should not be considered as argument separators. We test 776 // for this with the IgnoredComma token flag above. 777 778 // Comma ends this argument if there are more fixed arguments expected. 779 // However, if this is a variadic macro, and this is part of the 780 // variadic part, then the comma is just an argument token. 781 if (!isVariadic) break; 782 if (NumFixedArgsLeft > 1) 783 break; 784 } else if (Tok.is(tok::comment) && !KeepMacroComments) { 785 // If this is a comment token in the argument list and we're just in 786 // -C mode (not -CC mode), discard the comment. 787 continue; 788 } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) { 789 // Reading macro arguments can cause macros that we are currently 790 // expanding from to be popped off the expansion stack. Doing so causes 791 // them to be reenabled for expansion. Here we record whether any 792 // identifiers we lex as macro arguments correspond to disabled macros. 793 // If so, we mark the token as noexpand. This is a subtle aspect of 794 // C99 6.10.3.4p2. 795 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) 796 if (!MI->isEnabled()) 797 Tok.setFlag(Token::DisableExpand); 798 } else if (Tok.is(tok::code_completion)) { 799 ContainsCodeCompletionTok = true; 800 if (CodeComplete) 801 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), 802 MI, NumActuals); 803 // Don't mark that we reached the code-completion point because the 804 // parser is going to handle the token and there will be another 805 // code-completion callback. 806 } 807 808 ArgTokens.push_back(Tok); 809 } 810 811 // If this was an empty argument list foo(), don't add this as an empty 812 // argument. 813 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) 814 break; 815 816 // If this is not a variadic macro, and too many args were specified, emit 817 // an error. 818 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) { 819 if (ArgTokens.size() != ArgTokenStart) 820 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation(); 821 else 822 TooManyArgsLoc = ArgStartLoc; 823 } 824 825 // Empty arguments are standard in C99 and C++0x, and are supported as an 826 // extension in other modes. 827 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) 828 Diag(Tok, LangOpts.CPlusPlus11 ? 829 diag::warn_cxx98_compat_empty_fnmacro_arg : 830 diag::ext_empty_fnmacro_arg); 831 832 // Add a marker EOF token to the end of the token list for this argument. 833 Token EOFTok; 834 EOFTok.startToken(); 835 EOFTok.setKind(tok::eof); 836 EOFTok.setLocation(Tok.getLocation()); 837 EOFTok.setLength(0); 838 ArgTokens.push_back(EOFTok); 839 ++NumActuals; 840 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0) 841 --NumFixedArgsLeft; 842 } 843 844 // Okay, we either found the r_paren. Check to see if we parsed too few 845 // arguments. 846 unsigned MinArgsExpected = MI->getNumArgs(); 847 848 // If this is not a variadic macro, and too many args were specified, emit 849 // an error. 850 if (!isVariadic && NumActuals > MinArgsExpected && 851 !ContainsCodeCompletionTok) { 852 // Emit the diagnostic at the macro name in case there is a missing ). 853 // Emitting it at the , could be far away from the macro name. 854 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc); 855 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 856 << MacroName.getIdentifierInfo(); 857 858 // Commas from braced initializer lists will be treated as argument 859 // separators inside macros. Attempt to correct for this with parentheses. 860 // TODO: See if this can be generalized to angle brackets for templates 861 // inside macro arguments. 862 863 SmallVector<Token, 4> FixedArgTokens; 864 unsigned FixedNumArgs = 0; 865 SmallVector<SourceRange, 4> ParenHints, InitLists; 866 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs, 867 ParenHints, InitLists)) { 868 if (!InitLists.empty()) { 869 DiagnosticBuilder DB = 870 Diag(MacroName, 871 diag::note_init_list_at_beginning_of_macro_argument); 872 for (SourceRange Range : InitLists) 873 DB << Range; 874 } 875 return nullptr; 876 } 877 if (FixedNumArgs != MinArgsExpected) 878 return nullptr; 879 880 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro); 881 for (SourceRange ParenLocation : ParenHints) { 882 DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "("); 883 DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")"); 884 } 885 ArgTokens.swap(FixedArgTokens); 886 NumActuals = FixedNumArgs; 887 } 888 889 // See MacroArgs instance var for description of this. 890 bool isVarargsElided = false; 891 892 if (ContainsCodeCompletionTok) { 893 // Recover from not-fully-formed macro invocation during code-completion. 894 Token EOFTok; 895 EOFTok.startToken(); 896 EOFTok.setKind(tok::eof); 897 EOFTok.setLocation(Tok.getLocation()); 898 EOFTok.setLength(0); 899 for (; NumActuals < MinArgsExpected; ++NumActuals) 900 ArgTokens.push_back(EOFTok); 901 } 902 903 if (NumActuals < MinArgsExpected) { 904 // There are several cases where too few arguments is ok, handle them now. 905 if (NumActuals == 0 && MinArgsExpected == 1) { 906 // #define A(X) or #define A(...) ---> A() 907 908 // If there is exactly one argument, and that argument is missing, 909 // then we have an empty "()" argument empty list. This is fine, even if 910 // the macro expects one argument (the argument is just empty). 911 isVarargsElided = MI->isVariadic(); 912 } else if (MI->isVariadic() && 913 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) 914 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() 915 // Varargs where the named vararg parameter is missing: OK as extension. 916 // #define A(x, ...) 917 // A("blah") 918 // 919 // If the macro contains the comma pasting extension, the diagnostic 920 // is suppressed; we know we'll get another diagnostic later. 921 if (!MI->hasCommaPasting()) { 922 Diag(Tok, diag::ext_missing_varargs_arg); 923 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 924 << MacroName.getIdentifierInfo(); 925 } 926 927 // Remember this occurred, allowing us to elide the comma when used for 928 // cases like: 929 // #define A(x, foo...) blah(a, ## foo) 930 // #define B(x, ...) blah(a, ## __VA_ARGS__) 931 // #define C(...) blah(a, ## __VA_ARGS__) 932 // A(x) B(x) C() 933 isVarargsElided = true; 934 } else if (!ContainsCodeCompletionTok) { 935 // Otherwise, emit the error. 936 Diag(Tok, diag::err_too_few_args_in_macro_invoc); 937 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 938 << MacroName.getIdentifierInfo(); 939 return nullptr; 940 } 941 942 // Add a marker EOF token to the end of the token list for this argument. 943 SourceLocation EndLoc = Tok.getLocation(); 944 Tok.startToken(); 945 Tok.setKind(tok::eof); 946 Tok.setLocation(EndLoc); 947 Tok.setLength(0); 948 ArgTokens.push_back(Tok); 949 950 // If we expect two arguments, add both as empty. 951 if (NumActuals == 0 && MinArgsExpected == 2) 952 ArgTokens.push_back(Tok); 953 954 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() && 955 !ContainsCodeCompletionTok) { 956 // Emit the diagnostic at the macro name in case there is a missing ). 957 // Emitting it at the , could be far away from the macro name. 958 Diag(MacroName, diag::err_too_many_args_in_macro_invoc); 959 Diag(MI->getDefinitionLoc(), diag::note_macro_here) 960 << MacroName.getIdentifierInfo(); 961 return nullptr; 962 } 963 964 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); 965} 966 967/// \brief Keeps macro expanded tokens for TokenLexers. 968// 969/// Works like a stack; a TokenLexer adds the macro expanded tokens that is 970/// going to lex in the cache and when it finishes the tokens are removed 971/// from the end of the cache. 972Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, 973 ArrayRef<Token> tokens) { 974 assert(tokLexer); 975 if (tokens.empty()) 976 return nullptr; 977 978 size_t newIndex = MacroExpandedTokens.size(); 979 bool cacheNeedsToGrow = tokens.size() > 980 MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); 981 MacroExpandedTokens.append(tokens.begin(), tokens.end()); 982 983 if (cacheNeedsToGrow) { 984 // Go through all the TokenLexers whose 'Tokens' pointer points in the 985 // buffer and update the pointers to the (potential) new buffer array. 986 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { 987 TokenLexer *prevLexer; 988 size_t tokIndex; 989 std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; 990 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; 991 } 992 } 993 994 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); 995 return MacroExpandedTokens.data() + newIndex; 996} 997 998void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { 999 assert(!MacroExpandingLexersStack.empty()); 1000 size_t tokIndex = MacroExpandingLexersStack.back().second; 1001 assert(tokIndex < MacroExpandedTokens.size()); 1002 // Pop the cached macro expanded tokens from the end. 1003 MacroExpandedTokens.resize(tokIndex); 1004 MacroExpandingLexersStack.pop_back(); 1005} 1006 1007/// ComputeDATE_TIME - Compute the current time, enter it into the specified 1008/// scratch buffer, then return DATELoc/TIMELoc locations with the position of 1009/// the identifier tokens inserted. 1010static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, 1011 Preprocessor &PP) { 1012 time_t TT = time(nullptr); 1013 struct tm *TM = localtime(&TT); 1014 1015 static const char * const Months[] = { 1016 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" 1017 }; 1018 1019 { 1020 SmallString<32> TmpBuffer; 1021 llvm::raw_svector_ostream TmpStream(TmpBuffer); 1022 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon], 1023 TM->tm_mday, TM->tm_year + 1900); 1024 Token TmpTok; 1025 TmpTok.startToken(); 1026 PP.CreateString(TmpStream.str(), TmpTok); 1027 DATELoc = TmpTok.getLocation(); 1028 } 1029 1030 { 1031 SmallString<32> TmpBuffer; 1032 llvm::raw_svector_ostream TmpStream(TmpBuffer); 1033 TmpStream << llvm::format("\"%02d:%02d:%02d\"", 1034 TM->tm_hour, TM->tm_min, TM->tm_sec); 1035 Token TmpTok; 1036 TmpTok.startToken(); 1037 PP.CreateString(TmpStream.str(), TmpTok); 1038 TIMELoc = TmpTok.getLocation(); 1039 } 1040} 1041 1042 1043/// HasFeature - Return true if we recognize and implement the feature 1044/// specified by the identifier as a standard language feature. 1045static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { 1046 const LangOptions &LangOpts = PP.getLangOpts(); 1047 StringRef Feature = II->getName(); 1048 1049 // Normalize the feature name, __foo__ becomes foo. 1050 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) 1051 Feature = Feature.substr(2, Feature.size() - 4); 1052 1053 return llvm::StringSwitch<bool>(Feature) 1054 .Case("address_sanitizer", 1055 LangOpts.Sanitize.hasOneOf(SanitizerKind::Address | 1056 SanitizerKind::KernelAddress)) 1057 .Case("assume_nonnull", true) 1058 .Case("attribute_analyzer_noreturn", true) 1059 .Case("attribute_availability", true) 1060 .Case("attribute_availability_with_message", true) 1061 .Case("attribute_availability_app_extension", true) 1062 .Case("attribute_availability_with_version_underscores", true) 1063 .Case("attribute_availability_tvos", true) 1064 .Case("attribute_availability_watchos", true) 1065 .Case("attribute_cf_returns_not_retained", true) 1066 .Case("attribute_cf_returns_retained", true) 1067 .Case("attribute_cf_returns_on_parameters", true) 1068 .Case("attribute_deprecated_with_message", true) 1069 .Case("attribute_ext_vector_type", true) 1070 .Case("attribute_ns_returns_not_retained", true) 1071 .Case("attribute_ns_returns_retained", true) 1072 .Case("attribute_ns_consumes_self", true) 1073 .Case("attribute_ns_consumed", true) 1074 .Case("attribute_cf_consumed", true) 1075 .Case("attribute_objc_ivar_unused", true) 1076 .Case("attribute_objc_method_family", true) 1077 .Case("attribute_overloadable", true) 1078 .Case("attribute_unavailable_with_message", true) 1079 .Case("attribute_unused_on_fields", true) 1080 .Case("blocks", LangOpts.Blocks) 1081 .Case("c_thread_safety_attributes", true) 1082 .Case("cxx_exceptions", LangOpts.CXXExceptions) 1083 .Case("cxx_rtti", LangOpts.RTTI && LangOpts.RTTIData) 1084 .Case("enumerator_attributes", true) 1085 .Case("nullability", true) 1086 .Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory)) 1087 .Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread)) 1088 .Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow)) 1089 // Objective-C features 1090 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? 1091 .Case("objc_arc", LangOpts.ObjCAutoRefCount) 1092 .Case("objc_arc_weak", LangOpts.ObjCWeak) 1093 .Case("objc_default_synthesize_properties", LangOpts.ObjC2) 1094 .Case("objc_fixed_enum", LangOpts.ObjC2) 1095 .Case("objc_instancetype", LangOpts.ObjC2) 1096 .Case("objc_kindof", LangOpts.ObjC2) 1097 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) 1098 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) 1099 .Case("objc_property_explicit_atomic", 1100 true) // Does clang support explicit "atomic" keyword? 1101 .Case("objc_protocol_qualifier_mangling", true) 1102 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) 1103 .Case("ownership_holds", true) 1104 .Case("ownership_returns", true) 1105 .Case("ownership_takes", true) 1106 .Case("objc_bool", true) 1107 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) 1108 .Case("objc_array_literals", LangOpts.ObjC2) 1109 .Case("objc_dictionary_literals", LangOpts.ObjC2) 1110 .Case("objc_boxed_expressions", LangOpts.ObjC2) 1111 .Case("objc_boxed_nsvalue_expressions", LangOpts.ObjC2) 1112 .Case("arc_cf_code_audited", true) 1113 .Case("objc_bridge_id", true) 1114 .Case("objc_bridge_id_on_typedefs", true) 1115 .Case("objc_generics", LangOpts.ObjC2) 1116 .Case("objc_generics_variance", LangOpts.ObjC2) 1117 // C11 features 1118 .Case("c_alignas", LangOpts.C11) 1119 .Case("c_alignof", LangOpts.C11) 1120 .Case("c_atomic", LangOpts.C11) 1121 .Case("c_generic_selections", LangOpts.C11) 1122 .Case("c_static_assert", LangOpts.C11) 1123 .Case("c_thread_local", 1124 LangOpts.C11 && PP.getTargetInfo().isTLSSupported()) 1125 // C++11 features 1126 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11) 1127 .Case("cxx_alias_templates", LangOpts.CPlusPlus11) 1128 .Case("cxx_alignas", LangOpts.CPlusPlus11) 1129 .Case("cxx_alignof", LangOpts.CPlusPlus11) 1130 .Case("cxx_atomic", LangOpts.CPlusPlus11) 1131 .Case("cxx_attributes", LangOpts.CPlusPlus11) 1132 .Case("cxx_auto_type", LangOpts.CPlusPlus11) 1133 .Case("cxx_constexpr", LangOpts.CPlusPlus11) 1134 .Case("cxx_decltype", LangOpts.CPlusPlus11) 1135 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11) 1136 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11) 1137 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11) 1138 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11) 1139 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11) 1140 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11) 1141 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11) 1142 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11) 1143 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11) 1144 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11) 1145 .Case("cxx_lambdas", LangOpts.CPlusPlus11) 1146 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11) 1147 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11) 1148 .Case("cxx_noexcept", LangOpts.CPlusPlus11) 1149 .Case("cxx_nullptr", LangOpts.CPlusPlus11) 1150 .Case("cxx_override_control", LangOpts.CPlusPlus11) 1151 .Case("cxx_range_for", LangOpts.CPlusPlus11) 1152 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11) 1153 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11) 1154 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11) 1155 .Case("cxx_strong_enums", LangOpts.CPlusPlus11) 1156 .Case("cxx_static_assert", LangOpts.CPlusPlus11) 1157 .Case("cxx_thread_local", 1158 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported()) 1159 .Case("cxx_trailing_return", LangOpts.CPlusPlus11) 1160 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11) 1161 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11) 1162 .Case("cxx_user_literals", LangOpts.CPlusPlus11) 1163 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11) 1164 // C++1y features 1165 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus14) 1166 .Case("cxx_binary_literals", LangOpts.CPlusPlus14) 1167 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus14) 1168 .Case("cxx_decltype_auto", LangOpts.CPlusPlus14) 1169 .Case("cxx_generic_lambdas", LangOpts.CPlusPlus14) 1170 .Case("cxx_init_captures", LangOpts.CPlusPlus14) 1171 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus14) 1172 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus14) 1173 .Case("cxx_variable_templates", LangOpts.CPlusPlus14) 1174 // C++ TSes 1175 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays) 1176 //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts) 1177 // FIXME: Should this be __has_feature or __has_extension? 1178 //.Case("raw_invocation_type", LangOpts.CPlusPlus) 1179 // Type traits 1180 .Case("has_nothrow_assign", LangOpts.CPlusPlus) 1181 .Case("has_nothrow_copy", LangOpts.CPlusPlus) 1182 .Case("has_nothrow_constructor", LangOpts.CPlusPlus) 1183 .Case("has_trivial_assign", LangOpts.CPlusPlus) 1184 .Case("has_trivial_copy", LangOpts.CPlusPlus) 1185 .Case("has_trivial_constructor", LangOpts.CPlusPlus) 1186 .Case("has_trivial_destructor", LangOpts.CPlusPlus) 1187 .Case("has_virtual_destructor", LangOpts.CPlusPlus) 1188 .Case("is_abstract", LangOpts.CPlusPlus) 1189 .Case("is_base_of", LangOpts.CPlusPlus) 1190 .Case("is_class", LangOpts.CPlusPlus) 1191 .Case("is_constructible", LangOpts.CPlusPlus) 1192 .Case("is_convertible_to", LangOpts.CPlusPlus) 1193 .Case("is_empty", LangOpts.CPlusPlus) 1194 .Case("is_enum", LangOpts.CPlusPlus) 1195 .Case("is_final", LangOpts.CPlusPlus) 1196 .Case("is_literal", LangOpts.CPlusPlus) 1197 .Case("is_standard_layout", LangOpts.CPlusPlus) 1198 .Case("is_pod", LangOpts.CPlusPlus) 1199 .Case("is_polymorphic", LangOpts.CPlusPlus) 1200 .Case("is_sealed", LangOpts.MicrosoftExt) 1201 .Case("is_trivial", LangOpts.CPlusPlus) 1202 .Case("is_trivially_assignable", LangOpts.CPlusPlus) 1203 .Case("is_trivially_constructible", LangOpts.CPlusPlus) 1204 .Case("is_trivially_copyable", LangOpts.CPlusPlus) 1205 .Case("is_union", LangOpts.CPlusPlus) 1206 .Case("modules", LangOpts.Modules) 1207 .Case("safe_stack", LangOpts.Sanitize.has(SanitizerKind::SafeStack)) 1208 .Case("tls", PP.getTargetInfo().isTLSSupported()) 1209 .Case("underlying_type", LangOpts.CPlusPlus) 1210 .Default(false); 1211} 1212 1213/// HasExtension - Return true if we recognize and implement the feature 1214/// specified by the identifier, either as an extension or a standard language 1215/// feature. 1216static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { 1217 if (HasFeature(PP, II)) 1218 return true; 1219 1220 // If the use of an extension results in an error diagnostic, extensions are 1221 // effectively unavailable, so just return false here. 1222 if (PP.getDiagnostics().getExtensionHandlingBehavior() >= 1223 diag::Severity::Error) 1224 return false; 1225 1226 const LangOptions &LangOpts = PP.getLangOpts(); 1227 StringRef Extension = II->getName(); 1228 1229 // Normalize the extension name, __foo__ becomes foo. 1230 if (Extension.startswith("__") && Extension.endswith("__") && 1231 Extension.size() >= 4) 1232 Extension = Extension.substr(2, Extension.size() - 4); 1233 1234 // Because we inherit the feature list from HasFeature, this string switch 1235 // must be less restrictive than HasFeature's. 1236 return llvm::StringSwitch<bool>(Extension) 1237 // C11 features supported by other languages as extensions. 1238 .Case("c_alignas", true) 1239 .Case("c_alignof", true) 1240 .Case("c_atomic", true) 1241 .Case("c_generic_selections", true) 1242 .Case("c_static_assert", true) 1243 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported()) 1244 // C++11 features supported by other languages as extensions. 1245 .Case("cxx_atomic", LangOpts.CPlusPlus) 1246 .Case("cxx_deleted_functions", LangOpts.CPlusPlus) 1247 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) 1248 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) 1249 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) 1250 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) 1251 .Case("cxx_override_control", LangOpts.CPlusPlus) 1252 .Case("cxx_range_for", LangOpts.CPlusPlus) 1253 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) 1254 .Case("cxx_rvalue_references", LangOpts.CPlusPlus) 1255 .Case("cxx_variadic_templates", LangOpts.CPlusPlus) 1256 // C++1y features supported by other languages as extensions. 1257 .Case("cxx_binary_literals", true) 1258 .Case("cxx_init_captures", LangOpts.CPlusPlus11) 1259 .Case("cxx_variable_templates", LangOpts.CPlusPlus) 1260 .Default(false); 1261} 1262 1263/// EvaluateHasIncludeCommon - Process a '__has_include("path")' 1264/// or '__has_include_next("path")' expression. 1265/// Returns true if successful. 1266static bool EvaluateHasIncludeCommon(Token &Tok, 1267 IdentifierInfo *II, Preprocessor &PP, 1268 const DirectoryLookup *LookupFrom, 1269 const FileEntry *LookupFromFile) { 1270 // Save the location of the current token. If a '(' is later found, use 1271 // that location. If not, use the end of this location instead. 1272 SourceLocation LParenLoc = Tok.getLocation(); 1273 1274 // These expressions are only allowed within a preprocessor directive. 1275 if (!PP.isParsingIfOrElifDirective()) { 1276 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName(); 1277 // Return a valid identifier token. 1278 assert(Tok.is(tok::identifier)); 1279 Tok.setIdentifierInfo(II); 1280 return false; 1281 } 1282 1283 // Get '('. 1284 PP.LexNonComment(Tok); 1285 1286 // Ensure we have a '('. 1287 if (Tok.isNot(tok::l_paren)) { 1288 // No '(', use end of last token. 1289 LParenLoc = PP.getLocForEndOfToken(LParenLoc); 1290 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren; 1291 // If the next token looks like a filename or the start of one, 1292 // assume it is and process it as such. 1293 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) && 1294 !Tok.is(tok::less)) 1295 return false; 1296 } else { 1297 // Save '(' location for possible missing ')' message. 1298 LParenLoc = Tok.getLocation(); 1299 1300 if (PP.getCurrentLexer()) { 1301 // Get the file name. 1302 PP.getCurrentLexer()->LexIncludeFilename(Tok); 1303 } else { 1304 // We're in a macro, so we can't use LexIncludeFilename; just 1305 // grab the next token. 1306 PP.Lex(Tok); 1307 } 1308 } 1309 1310 // Reserve a buffer to get the spelling. 1311 SmallString<128> FilenameBuffer; 1312 StringRef Filename; 1313 SourceLocation EndLoc; 1314 1315 switch (Tok.getKind()) { 1316 case tok::eod: 1317 // If the token kind is EOD, the error has already been diagnosed. 1318 return false; 1319 1320 case tok::angle_string_literal: 1321 case tok::string_literal: { 1322 bool Invalid = false; 1323 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); 1324 if (Invalid) 1325 return false; 1326 break; 1327 } 1328 1329 case tok::less: 1330 // This could be a <foo/bar.h> file coming from a macro expansion. In this 1331 // case, glue the tokens together into FilenameBuffer and interpret those. 1332 FilenameBuffer.push_back('<'); 1333 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) { 1334 // Let the caller know a <eod> was found by changing the Token kind. 1335 Tok.setKind(tok::eod); 1336 return false; // Found <eod> but no ">"? Diagnostic already emitted. 1337 } 1338 Filename = FilenameBuffer; 1339 break; 1340 default: 1341 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); 1342 return false; 1343 } 1344 1345 SourceLocation FilenameLoc = Tok.getLocation(); 1346 1347 // Get ')'. 1348 PP.LexNonComment(Tok); 1349 1350 // Ensure we have a trailing ). 1351 if (Tok.isNot(tok::r_paren)) { 1352 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after) 1353 << II << tok::r_paren; 1354 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1355 return false; 1356 } 1357 1358 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); 1359 // If GetIncludeFilenameSpelling set the start ptr to null, there was an 1360 // error. 1361 if (Filename.empty()) 1362 return false; 1363 1364 // Search include directories. 1365 const DirectoryLookup *CurDir; 1366 const FileEntry *File = 1367 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile, 1368 CurDir, nullptr, nullptr, nullptr); 1369 1370 // Get the result value. A result of true means the file exists. 1371 return File != nullptr; 1372} 1373 1374/// EvaluateHasInclude - Process a '__has_include("path")' expression. 1375/// Returns true if successful. 1376static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, 1377 Preprocessor &PP) { 1378 return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr); 1379} 1380 1381/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. 1382/// Returns true if successful. 1383static bool EvaluateHasIncludeNext(Token &Tok, 1384 IdentifierInfo *II, Preprocessor &PP) { 1385 // __has_include_next is like __has_include, except that we start 1386 // searching after the current found directory. If we can't do this, 1387 // issue a diagnostic. 1388 // FIXME: Factor out duplication with 1389 // Preprocessor::HandleIncludeNextDirective. 1390 const DirectoryLookup *Lookup = PP.GetCurDirLookup(); 1391 const FileEntry *LookupFromFile = nullptr; 1392 if (PP.isInPrimaryFile()) { 1393 Lookup = nullptr; 1394 PP.Diag(Tok, diag::pp_include_next_in_primary); 1395 } else if (PP.getCurrentSubmodule()) { 1396 // Start looking up in the directory *after* the one in which the current 1397 // file would be found, if any. 1398 assert(PP.getCurrentLexer() && "#include_next directive in macro?"); 1399 LookupFromFile = PP.getCurrentLexer()->getFileEntry(); 1400 Lookup = nullptr; 1401 } else if (!Lookup) { 1402 PP.Diag(Tok, diag::pp_include_next_absolute_path); 1403 } else { 1404 // Start looking up in the next directory. 1405 ++Lookup; 1406 } 1407 1408 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile); 1409} 1410 1411/// \brief Process __building_module(identifier) expression. 1412/// \returns true if we are building the named module, false otherwise. 1413static bool EvaluateBuildingModule(Token &Tok, 1414 IdentifierInfo *II, Preprocessor &PP) { 1415 // Get '('. 1416 PP.LexNonComment(Tok); 1417 1418 // Ensure we have a '('. 1419 if (Tok.isNot(tok::l_paren)) { 1420 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1421 << tok::l_paren; 1422 return false; 1423 } 1424 1425 // Save '(' location for possible missing ')' message. 1426 SourceLocation LParenLoc = Tok.getLocation(); 1427 1428 // Get the module name. 1429 PP.LexNonComment(Tok); 1430 1431 // Ensure that we have an identifier. 1432 if (Tok.isNot(tok::identifier)) { 1433 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module); 1434 return false; 1435 } 1436 1437 bool Result 1438 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule; 1439 1440 // Get ')'. 1441 PP.LexNonComment(Tok); 1442 1443 // Ensure we have a trailing ). 1444 if (Tok.isNot(tok::r_paren)) { 1445 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1446 << tok::r_paren; 1447 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1448 return false; 1449 } 1450 1451 return Result; 1452} 1453 1454/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 1455/// as a builtin macro, handle it and return the next token as 'Tok'. 1456void Preprocessor::ExpandBuiltinMacro(Token &Tok) { 1457 // Figure out which token this is. 1458 IdentifierInfo *II = Tok.getIdentifierInfo(); 1459 assert(II && "Can't be a macro without id info!"); 1460 1461 // If this is an _Pragma or Microsoft __pragma directive, expand it, 1462 // invoke the pragma handler, then lex the token after it. 1463 if (II == Ident_Pragma) 1464 return Handle_Pragma(Tok); 1465 else if (II == Ident__pragma) // in non-MS mode this is null 1466 return HandleMicrosoft__pragma(Tok); 1467 1468 ++NumBuiltinMacroExpanded; 1469 1470 SmallString<128> TmpBuffer; 1471 llvm::raw_svector_ostream OS(TmpBuffer); 1472 1473 // Set up the return result. 1474 Tok.setIdentifierInfo(nullptr); 1475 Tok.clearFlag(Token::NeedsCleaning); 1476 1477 if (II == Ident__LINE__) { 1478 // C99 6.10.8: "__LINE__: The presumed line number (within the current 1479 // source file) of the current source line (an integer constant)". This can 1480 // be affected by #line. 1481 SourceLocation Loc = Tok.getLocation(); 1482 1483 // Advance to the location of the first _, this might not be the first byte 1484 // of the token if it starts with an escaped newline. 1485 Loc = AdvanceToTokenCharacter(Loc, 0); 1486 1487 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of 1488 // a macro expansion. This doesn't matter for object-like macros, but 1489 // can matter for a function-like macro that expands to contain __LINE__. 1490 // Skip down through expansion points until we find a file loc for the 1491 // end of the expansion history. 1492 Loc = SourceMgr.getExpansionRange(Loc).second; 1493 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); 1494 1495 // __LINE__ expands to a simple numeric value. 1496 OS << (PLoc.isValid()? PLoc.getLine() : 1); 1497 Tok.setKind(tok::numeric_constant); 1498 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { 1499 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a 1500 // character string literal)". This can be affected by #line. 1501 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1502 1503 // __BASE_FILE__ is a GNU extension that returns the top of the presumed 1504 // #include stack instead of the current file. 1505 if (II == Ident__BASE_FILE__ && PLoc.isValid()) { 1506 SourceLocation NextLoc = PLoc.getIncludeLoc(); 1507 while (NextLoc.isValid()) { 1508 PLoc = SourceMgr.getPresumedLoc(NextLoc); 1509 if (PLoc.isInvalid()) 1510 break; 1511 1512 NextLoc = PLoc.getIncludeLoc(); 1513 } 1514 } 1515 1516 // Escape this filename. Turn '\' -> '\\' '"' -> '\"' 1517 SmallString<128> FN; 1518 if (PLoc.isValid()) { 1519 FN += PLoc.getFilename(); 1520 Lexer::Stringify(FN); 1521 OS << '"' << FN << '"'; 1522 } 1523 Tok.setKind(tok::string_literal); 1524 } else if (II == Ident__DATE__) { 1525 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1526 if (!DATELoc.isValid()) 1527 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1528 Tok.setKind(tok::string_literal); 1529 Tok.setLength(strlen("\"Mmm dd yyyy\"")); 1530 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), 1531 Tok.getLocation(), 1532 Tok.getLength())); 1533 return; 1534 } else if (II == Ident__TIME__) { 1535 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1536 if (!TIMELoc.isValid()) 1537 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1538 Tok.setKind(tok::string_literal); 1539 Tok.setLength(strlen("\"hh:mm:ss\"")); 1540 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), 1541 Tok.getLocation(), 1542 Tok.getLength())); 1543 return; 1544 } else if (II == Ident__INCLUDE_LEVEL__) { 1545 // Compute the presumed include depth of this token. This can be affected 1546 // by GNU line markers. 1547 unsigned Depth = 0; 1548 1549 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1550 if (PLoc.isValid()) { 1551 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1552 for (; PLoc.isValid(); ++Depth) 1553 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1554 } 1555 1556 // __INCLUDE_LEVEL__ expands to a simple numeric value. 1557 OS << Depth; 1558 Tok.setKind(tok::numeric_constant); 1559 } else if (II == Ident__TIMESTAMP__) { 1560 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1561 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be 1562 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. 1563 1564 // Get the file that we are lexing out of. If we're currently lexing from 1565 // a macro, dig into the include stack. 1566 const FileEntry *CurFile = nullptr; 1567 PreprocessorLexer *TheLexer = getCurrentFileLexer(); 1568 1569 if (TheLexer) 1570 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); 1571 1572 const char *Result; 1573 if (CurFile) { 1574 time_t TT = CurFile->getModificationTime(); 1575 struct tm *TM = localtime(&TT); 1576 Result = asctime(TM); 1577 } else { 1578 Result = "??? ??? ?? ??:??:?? ????\n"; 1579 } 1580 // Surround the string with " and strip the trailing newline. 1581 OS << '"' << StringRef(Result).drop_back() << '"'; 1582 Tok.setKind(tok::string_literal); 1583 } else if (II == Ident__COUNTER__) { 1584 // __COUNTER__ expands to a simple numeric value. 1585 OS << CounterValue++; 1586 Tok.setKind(tok::numeric_constant); 1587 } else if (II == Ident__has_feature || 1588 II == Ident__has_extension || 1589 II == Ident__has_builtin || 1590 II == Ident__is_identifier || 1591 II == Ident__has_attribute || 1592 II == Ident__has_declspec || 1593 II == Ident__has_cpp_attribute) { 1594 // The argument to these builtins should be a parenthesized identifier. 1595 SourceLocation StartLoc = Tok.getLocation(); 1596 1597 bool IsValid = false; 1598 IdentifierInfo *FeatureII = nullptr; 1599 IdentifierInfo *ScopeII = nullptr; 1600 1601 // Read the '('. 1602 LexUnexpandedToken(Tok); 1603 if (Tok.is(tok::l_paren)) { 1604 // Read the identifier 1605 LexUnexpandedToken(Tok); 1606 if ((FeatureII = Tok.getIdentifierInfo())) { 1607 // If we're checking __has_cpp_attribute, it is possible to receive a 1608 // scope token. Read the "::", if it's available. 1609 LexUnexpandedToken(Tok); 1610 bool IsScopeValid = true; 1611 if (II == Ident__has_cpp_attribute && Tok.is(tok::coloncolon)) { 1612 LexUnexpandedToken(Tok); 1613 // The first thing we read was not the feature, it was the scope. 1614 ScopeII = FeatureII; 1615 if ((FeatureII = Tok.getIdentifierInfo())) 1616 LexUnexpandedToken(Tok); 1617 else 1618 IsScopeValid = false; 1619 } 1620 // Read the closing paren. 1621 if (IsScopeValid && Tok.is(tok::r_paren)) 1622 IsValid = true; 1623 } 1624 // Eat tokens until ')'. 1625 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && 1626 Tok.isNot(tok::eof)) 1627 LexUnexpandedToken(Tok); 1628 } 1629 1630 int Value = 0; 1631 if (!IsValid) 1632 Diag(StartLoc, diag::err_feature_check_malformed); 1633 else if (II == Ident__is_identifier) 1634 Value = FeatureII->getTokenID() == tok::identifier; 1635 else if (II == Ident__has_builtin) { 1636 // Check for a builtin is trivial. 1637 if (FeatureII->getBuiltinID() != 0) { 1638 Value = true; 1639 } else { 1640 StringRef Feature = FeatureII->getName(); 1641 Value = llvm::StringSwitch<bool>(Feature) 1642 .Case("__make_integer_seq", getLangOpts().CPlusPlus) 1643 .Default(false); 1644 } 1645 } else if (II == Ident__has_attribute) 1646 Value = hasAttribute(AttrSyntax::GNU, nullptr, FeatureII, 1647 getTargetInfo(), getLangOpts()); 1648 else if (II == Ident__has_cpp_attribute) 1649 Value = hasAttribute(AttrSyntax::CXX, ScopeII, FeatureII, 1650 getTargetInfo(), getLangOpts()); 1651 else if (II == Ident__has_declspec) 1652 Value = hasAttribute(AttrSyntax::Declspec, nullptr, FeatureII, 1653 getTargetInfo(), getLangOpts()); 1654 else if (II == Ident__has_extension) 1655 Value = HasExtension(*this, FeatureII); 1656 else { 1657 assert(II == Ident__has_feature && "Must be feature check"); 1658 Value = HasFeature(*this, FeatureII); 1659 } 1660 1661 if (!IsValid) 1662 return; 1663 OS << Value; 1664 Tok.setKind(tok::numeric_constant); 1665 } else if (II == Ident__has_include || 1666 II == Ident__has_include_next) { 1667 // The argument to these two builtins should be a parenthesized 1668 // file name string literal using angle brackets (<>) or 1669 // double-quotes (""). 1670 bool Value; 1671 if (II == Ident__has_include) 1672 Value = EvaluateHasInclude(Tok, II, *this); 1673 else 1674 Value = EvaluateHasIncludeNext(Tok, II, *this); 1675 1676 if (Tok.isNot(tok::r_paren)) 1677 return; 1678 OS << (int)Value; 1679 Tok.setKind(tok::numeric_constant); 1680 } else if (II == Ident__has_warning) { 1681 // The argument should be a parenthesized string literal. 1682 // The argument to these builtins should be a parenthesized identifier. 1683 SourceLocation StartLoc = Tok.getLocation(); 1684 bool IsValid = false; 1685 bool Value = false; 1686 // Read the '('. 1687 LexUnexpandedToken(Tok); 1688 do { 1689 if (Tok.isNot(tok::l_paren)) { 1690 Diag(StartLoc, diag::err_warning_check_malformed); 1691 break; 1692 } 1693 1694 LexUnexpandedToken(Tok); 1695 std::string WarningName; 1696 SourceLocation StrStartLoc = Tok.getLocation(); 1697 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", 1698 /*MacroExpansion=*/false)) { 1699 // Eat tokens until ')'. 1700 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && 1701 Tok.isNot(tok::eof)) 1702 LexUnexpandedToken(Tok); 1703 break; 1704 } 1705 1706 // Is the end a ')'? 1707 if (!(IsValid = Tok.is(tok::r_paren))) { 1708 Diag(StartLoc, diag::err_warning_check_malformed); 1709 break; 1710 } 1711 1712 // FIXME: Should we accept "-R..." flags here, or should that be handled 1713 // by a separate __has_remark? 1714 if (WarningName.size() < 3 || WarningName[0] != '-' || 1715 WarningName[1] != 'W') { 1716 Diag(StrStartLoc, diag::warn_has_warning_invalid_option); 1717 break; 1718 } 1719 1720 // Finally, check if the warning flags maps to a diagnostic group. 1721 // We construct a SmallVector here to talk to getDiagnosticIDs(). 1722 // Although we don't use the result, this isn't a hot path, and not 1723 // worth special casing. 1724 SmallVector<diag::kind, 10> Diags; 1725 Value = !getDiagnostics().getDiagnosticIDs()-> 1726 getDiagnosticsInGroup(diag::Flavor::WarningOrError, 1727 WarningName.substr(2), Diags); 1728 } while (false); 1729 1730 if (!IsValid) 1731 return; 1732 OS << (int)Value; 1733 Tok.setKind(tok::numeric_constant); 1734 } else if (II == Ident__building_module) { 1735 // The argument to this builtin should be an identifier. The 1736 // builtin evaluates to 1 when that identifier names the module we are 1737 // currently building. 1738 OS << (int)EvaluateBuildingModule(Tok, II, *this); 1739 Tok.setKind(tok::numeric_constant); 1740 } else if (II == Ident__MODULE__) { 1741 // The current module as an identifier. 1742 OS << getLangOpts().CurrentModule; 1743 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); 1744 Tok.setIdentifierInfo(ModuleII); 1745 Tok.setKind(ModuleII->getTokenID()); 1746 } else if (II == Ident__identifier) { 1747 SourceLocation Loc = Tok.getLocation(); 1748 1749 // We're expecting '__identifier' '(' identifier ')'. Try to recover 1750 // if the parens are missing. 1751 LexNonComment(Tok); 1752 if (Tok.isNot(tok::l_paren)) { 1753 // No '(', use end of last token. 1754 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after) 1755 << II << tok::l_paren; 1756 // If the next token isn't valid as our argument, we can't recover. 1757 if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1758 Tok.setKind(tok::identifier); 1759 return; 1760 } 1761 1762 SourceLocation LParenLoc = Tok.getLocation(); 1763 LexNonComment(Tok); 1764 1765 if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1766 Tok.setKind(tok::identifier); 1767 else { 1768 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier) 1769 << Tok.getKind(); 1770 // Don't walk past anything that's not a real token. 1771 if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation()) 1772 return; 1773 } 1774 1775 // Discard the ')', preserving 'Tok' as our result. 1776 Token RParen; 1777 LexNonComment(RParen); 1778 if (RParen.isNot(tok::r_paren)) { 1779 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after) 1780 << Tok.getKind() << tok::r_paren; 1781 Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1782 } 1783 return; 1784 } else { 1785 llvm_unreachable("Unknown identifier!"); 1786 } 1787 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); 1788} 1789 1790void Preprocessor::markMacroAsUsed(MacroInfo *MI) { 1791 // If the 'used' status changed, and the macro requires 'unused' warning, 1792 // remove its SourceLocation from the warn-for-unused-macro locations. 1793 if (MI->isWarnIfUnused() && !MI->isUsed()) 1794 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 1795 MI->setIsUsed(true); 1796} 1797