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