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