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, 1161 IdentifierInfo *II, Preprocessor &PP, 1162 const DirectoryLookup *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 const DirectoryLookup *CurDir; 1232 Optional<FileEntryRef> File = 1233 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile, 1234 CurDir, nullptr, nullptr, nullptr, nullptr, nullptr); 1235 1236 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) { 1237 SrcMgr::CharacteristicKind FileType = SrcMgr::C_User; 1238 if (File) 1239 FileType = 1240 PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry()); 1241 Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType); 1242 } 1243 1244 // Get the result value. A result of true means the file exists. 1245 return File.hasValue(); 1246 } 1247 1248 /// EvaluateHasInclude - Process a '__has_include("path")' expression. 1249 /// Returns true if successful. 1250 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, 1251 Preprocessor &PP) { 1252 return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr); 1253 } 1254 1255 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. 1256 /// Returns true if successful. 1257 static bool EvaluateHasIncludeNext(Token &Tok, 1258 IdentifierInfo *II, Preprocessor &PP) { 1259 // __has_include_next is like __has_include, except that we start 1260 // searching after the current found directory. If we can't do this, 1261 // issue a diagnostic. 1262 // FIXME: Factor out duplication with 1263 // Preprocessor::HandleIncludeNextDirective. 1264 const DirectoryLookup *Lookup = PP.GetCurDirLookup(); 1265 const FileEntry *LookupFromFile = nullptr; 1266 if (PP.isInPrimaryFile() && PP.getLangOpts().IsHeaderFile) { 1267 // If the main file is a header, then it's either for PCH/AST generation, 1268 // or libclang opened it. Either way, handle it as a normal include below 1269 // and do not complain about __has_include_next. 1270 } else if (PP.isInPrimaryFile()) { 1271 Lookup = nullptr; 1272 PP.Diag(Tok, diag::pp_include_next_in_primary); 1273 } else if (PP.getCurrentLexerSubmodule()) { 1274 // Start looking up in the directory *after* the one in which the current 1275 // file would be found, if any. 1276 assert(PP.getCurrentLexer() && "#include_next directive in macro?"); 1277 LookupFromFile = PP.getCurrentLexer()->getFileEntry(); 1278 Lookup = nullptr; 1279 } else if (!Lookup) { 1280 PP.Diag(Tok, diag::pp_include_next_absolute_path); 1281 } else { 1282 // Start looking up in the next directory. 1283 ++Lookup; 1284 } 1285 1286 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile); 1287 } 1288 1289 /// Process single-argument builtin feature-like macros that return 1290 /// integer values. 1291 static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS, 1292 Token &Tok, IdentifierInfo *II, 1293 Preprocessor &PP, bool ExpandArgs, 1294 llvm::function_ref< 1295 int(Token &Tok, 1296 bool &HasLexedNextTok)> Op) { 1297 // Parse the initial '('. 1298 PP.LexUnexpandedToken(Tok); 1299 if (Tok.isNot(tok::l_paren)) { 1300 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1301 << tok::l_paren; 1302 1303 // Provide a dummy '0' value on output stream to elide further errors. 1304 if (!Tok.isOneOf(tok::eof, tok::eod)) { 1305 OS << 0; 1306 Tok.setKind(tok::numeric_constant); 1307 } 1308 return; 1309 } 1310 1311 unsigned ParenDepth = 1; 1312 SourceLocation LParenLoc = Tok.getLocation(); 1313 llvm::Optional<int> Result; 1314 1315 Token ResultTok; 1316 bool SuppressDiagnostic = false; 1317 while (true) { 1318 // Parse next token. 1319 if (ExpandArgs) 1320 PP.Lex(Tok); 1321 else 1322 PP.LexUnexpandedToken(Tok); 1323 1324 already_lexed: 1325 switch (Tok.getKind()) { 1326 case tok::eof: 1327 case tok::eod: 1328 // Don't provide even a dummy value if the eod or eof marker is 1329 // reached. Simply provide a diagnostic. 1330 PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc); 1331 return; 1332 1333 case tok::comma: 1334 if (!SuppressDiagnostic) { 1335 PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc); 1336 SuppressDiagnostic = true; 1337 } 1338 continue; 1339 1340 case tok::l_paren: 1341 ++ParenDepth; 1342 if (Result.hasValue()) 1343 break; 1344 if (!SuppressDiagnostic) { 1345 PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II; 1346 SuppressDiagnostic = true; 1347 } 1348 continue; 1349 1350 case tok::r_paren: 1351 if (--ParenDepth > 0) 1352 continue; 1353 1354 // The last ')' has been reached; return the value if one found or 1355 // a diagnostic and a dummy value. 1356 if (Result.hasValue()) { 1357 OS << Result.getValue(); 1358 // For strict conformance to __has_cpp_attribute rules, use 'L' 1359 // suffix for dated literals. 1360 if (Result.getValue() > 1) 1361 OS << 'L'; 1362 } else { 1363 OS << 0; 1364 if (!SuppressDiagnostic) 1365 PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc); 1366 } 1367 Tok.setKind(tok::numeric_constant); 1368 return; 1369 1370 default: { 1371 // Parse the macro argument, if one not found so far. 1372 if (Result.hasValue()) 1373 break; 1374 1375 bool HasLexedNextToken = false; 1376 Result = Op(Tok, HasLexedNextToken); 1377 ResultTok = Tok; 1378 if (HasLexedNextToken) 1379 goto already_lexed; 1380 continue; 1381 } 1382 } 1383 1384 // Diagnose missing ')'. 1385 if (!SuppressDiagnostic) { 1386 if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) { 1387 if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo()) 1388 Diag << LastII; 1389 else 1390 Diag << ResultTok.getKind(); 1391 Diag << tok::r_paren << ResultTok.getLocation(); 1392 } 1393 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1394 SuppressDiagnostic = true; 1395 } 1396 } 1397 } 1398 1399 /// Helper function to return the IdentifierInfo structure of a Token 1400 /// or generate a diagnostic if none available. 1401 static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok, 1402 Preprocessor &PP, 1403 signed DiagID) { 1404 IdentifierInfo *II; 1405 if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo())) 1406 return II; 1407 1408 PP.Diag(Tok.getLocation(), DiagID); 1409 return nullptr; 1410 } 1411 1412 /// Implements the __is_target_arch builtin macro. 1413 static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) { 1414 std::string ArchName = II->getName().lower() + "--"; 1415 llvm::Triple Arch(ArchName); 1416 const llvm::Triple &TT = TI.getTriple(); 1417 if (TT.isThumb()) { 1418 // arm matches thumb or thumbv7. armv7 matches thumbv7. 1419 if ((Arch.getSubArch() == llvm::Triple::NoSubArch || 1420 Arch.getSubArch() == TT.getSubArch()) && 1421 ((TT.getArch() == llvm::Triple::thumb && 1422 Arch.getArch() == llvm::Triple::arm) || 1423 (TT.getArch() == llvm::Triple::thumbeb && 1424 Arch.getArch() == llvm::Triple::armeb))) 1425 return true; 1426 } 1427 // Check the parsed arch when it has no sub arch to allow Clang to 1428 // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7. 1429 return (Arch.getSubArch() == llvm::Triple::NoSubArch || 1430 Arch.getSubArch() == TT.getSubArch()) && 1431 Arch.getArch() == TT.getArch(); 1432 } 1433 1434 /// Implements the __is_target_vendor builtin macro. 1435 static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) { 1436 StringRef VendorName = TI.getTriple().getVendorName(); 1437 if (VendorName.empty()) 1438 VendorName = "unknown"; 1439 return VendorName.equals_insensitive(II->getName()); 1440 } 1441 1442 /// Implements the __is_target_os builtin macro. 1443 static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) { 1444 std::string OSName = 1445 (llvm::Twine("unknown-unknown-") + II->getName().lower()).str(); 1446 llvm::Triple OS(OSName); 1447 if (OS.getOS() == llvm::Triple::Darwin) { 1448 // Darwin matches macos, ios, etc. 1449 return TI.getTriple().isOSDarwin(); 1450 } 1451 return TI.getTriple().getOS() == OS.getOS(); 1452 } 1453 1454 /// Implements the __is_target_environment builtin macro. 1455 static bool isTargetEnvironment(const TargetInfo &TI, 1456 const IdentifierInfo *II) { 1457 std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str(); 1458 llvm::Triple Env(EnvName); 1459 return TI.getTriple().getEnvironment() == Env.getEnvironment(); 1460 } 1461 1462 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 1463 /// as a builtin macro, handle it and return the next token as 'Tok'. 1464 void Preprocessor::ExpandBuiltinMacro(Token &Tok) { 1465 // Figure out which token this is. 1466 IdentifierInfo *II = Tok.getIdentifierInfo(); 1467 assert(II && "Can't be a macro without id info!"); 1468 1469 // If this is an _Pragma or Microsoft __pragma directive, expand it, 1470 // invoke the pragma handler, then lex the token after it. 1471 if (II == Ident_Pragma) 1472 return Handle_Pragma(Tok); 1473 else if (II == Ident__pragma) // in non-MS mode this is null 1474 return HandleMicrosoft__pragma(Tok); 1475 1476 ++NumBuiltinMacroExpanded; 1477 1478 SmallString<128> TmpBuffer; 1479 llvm::raw_svector_ostream OS(TmpBuffer); 1480 1481 // Set up the return result. 1482 Tok.setIdentifierInfo(nullptr); 1483 Tok.clearFlag(Token::NeedsCleaning); 1484 bool IsAtStartOfLine = Tok.isAtStartOfLine(); 1485 bool HasLeadingSpace = Tok.hasLeadingSpace(); 1486 1487 if (II == Ident__LINE__) { 1488 // C99 6.10.8: "__LINE__: The presumed line number (within the current 1489 // source file) of the current source line (an integer constant)". This can 1490 // be affected by #line. 1491 SourceLocation Loc = Tok.getLocation(); 1492 1493 // Advance to the location of the first _, this might not be the first byte 1494 // of the token if it starts with an escaped newline. 1495 Loc = AdvanceToTokenCharacter(Loc, 0); 1496 1497 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of 1498 // a macro expansion. This doesn't matter for object-like macros, but 1499 // can matter for a function-like macro that expands to contain __LINE__. 1500 // Skip down through expansion points until we find a file loc for the 1501 // end of the expansion history. 1502 Loc = SourceMgr.getExpansionRange(Loc).getEnd(); 1503 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); 1504 1505 // __LINE__ expands to a simple numeric value. 1506 OS << (PLoc.isValid()? PLoc.getLine() : 1); 1507 Tok.setKind(tok::numeric_constant); 1508 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ || 1509 II == Ident__FILE_NAME__) { 1510 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a 1511 // character string literal)". This can be affected by #line. 1512 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1513 1514 // __BASE_FILE__ is a GNU extension that returns the top of the presumed 1515 // #include stack instead of the current file. 1516 if (II == Ident__BASE_FILE__ && PLoc.isValid()) { 1517 SourceLocation NextLoc = PLoc.getIncludeLoc(); 1518 while (NextLoc.isValid()) { 1519 PLoc = SourceMgr.getPresumedLoc(NextLoc); 1520 if (PLoc.isInvalid()) 1521 break; 1522 1523 NextLoc = PLoc.getIncludeLoc(); 1524 } 1525 } 1526 1527 // Escape this filename. Turn '\' -> '\\' '"' -> '\"' 1528 SmallString<256> FN; 1529 if (PLoc.isValid()) { 1530 // __FILE_NAME__ is a Clang-specific extension that expands to the 1531 // the last part of __FILE__. 1532 if (II == Ident__FILE_NAME__) { 1533 // Try to get the last path component, failing that return the original 1534 // presumed location. 1535 StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename()); 1536 if (PLFileName != "") 1537 FN += PLFileName; 1538 else 1539 FN += PLoc.getFilename(); 1540 } else { 1541 FN += PLoc.getFilename(); 1542 } 1543 getLangOpts().remapPathPrefix(FN); 1544 Lexer::Stringify(FN); 1545 OS << '"' << FN << '"'; 1546 } 1547 Tok.setKind(tok::string_literal); 1548 } else if (II == Ident__DATE__) { 1549 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1550 if (!DATELoc.isValid()) 1551 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1552 Tok.setKind(tok::string_literal); 1553 Tok.setLength(strlen("\"Mmm dd yyyy\"")); 1554 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), 1555 Tok.getLocation(), 1556 Tok.getLength())); 1557 return; 1558 } else if (II == Ident__TIME__) { 1559 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1560 if (!TIMELoc.isValid()) 1561 ComputeDATE_TIME(DATELoc, TIMELoc, *this); 1562 Tok.setKind(tok::string_literal); 1563 Tok.setLength(strlen("\"hh:mm:ss\"")); 1564 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), 1565 Tok.getLocation(), 1566 Tok.getLength())); 1567 return; 1568 } else if (II == Ident__INCLUDE_LEVEL__) { 1569 // Compute the presumed include depth of this token. This can be affected 1570 // by GNU line markers. 1571 unsigned Depth = 0; 1572 1573 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 1574 if (PLoc.isValid()) { 1575 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1576 for (; PLoc.isValid(); ++Depth) 1577 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 1578 } 1579 1580 // __INCLUDE_LEVEL__ expands to a simple numeric value. 1581 OS << Depth; 1582 Tok.setKind(tok::numeric_constant); 1583 } else if (II == Ident__TIMESTAMP__) { 1584 Diag(Tok.getLocation(), diag::warn_pp_date_time); 1585 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be 1586 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. 1587 1588 // Get the file that we are lexing out of. If we're currently lexing from 1589 // a macro, dig into the include stack. 1590 const FileEntry *CurFile = nullptr; 1591 PreprocessorLexer *TheLexer = getCurrentFileLexer(); 1592 1593 if (TheLexer) 1594 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); 1595 1596 const char *Result; 1597 if (CurFile) { 1598 time_t TT = CurFile->getModificationTime(); 1599 struct tm *TM = localtime(&TT); 1600 Result = asctime(TM); 1601 } else { 1602 Result = "??? ??? ?? ??:??:?? ????\n"; 1603 } 1604 // Surround the string with " and strip the trailing newline. 1605 OS << '"' << StringRef(Result).drop_back() << '"'; 1606 Tok.setKind(tok::string_literal); 1607 } else if (II == Ident__COUNTER__) { 1608 // __COUNTER__ expands to a simple numeric value. 1609 OS << CounterValue++; 1610 Tok.setKind(tok::numeric_constant); 1611 } else if (II == Ident__has_feature) { 1612 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, 1613 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1614 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, 1615 diag::err_feature_check_malformed); 1616 return II && HasFeature(*this, II->getName()); 1617 }); 1618 } else if (II == Ident__has_extension) { 1619 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, 1620 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1621 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, 1622 diag::err_feature_check_malformed); 1623 return II && HasExtension(*this, II->getName()); 1624 }); 1625 } else if (II == Ident__has_builtin) { 1626 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, 1627 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1628 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, 1629 diag::err_feature_check_malformed); 1630 if (!II) 1631 return false; 1632 else if (II->getBuiltinID() != 0) { 1633 switch (II->getBuiltinID()) { 1634 case Builtin::BI__builtin_operator_new: 1635 case Builtin::BI__builtin_operator_delete: 1636 // denotes date of behavior change to support calling arbitrary 1637 // usual allocation and deallocation functions. Required by libc++ 1638 return 201802; 1639 default: 1640 return true; 1641 } 1642 return true; 1643 } else if (II->getTokenID() != tok::identifier || 1644 II->hasRevertedTokenIDToIdentifier()) { 1645 // Treat all keywords that introduce a custom syntax of the form 1646 // 1647 // '__some_keyword' '(' [...] ')' 1648 // 1649 // as being "builtin functions", even if the syntax isn't a valid 1650 // function call (for example, because the builtin takes a type 1651 // argument). 1652 if (II->getName().startswith("__builtin_") || 1653 II->getName().startswith("__is_") || 1654 II->getName().startswith("__has_")) 1655 return true; 1656 return llvm::StringSwitch<bool>(II->getName()) 1657 .Case("__array_rank", true) 1658 .Case("__array_extent", true) 1659 .Case("__reference_binds_to_temporary", true) 1660 .Case("__underlying_type", true) 1661 .Default(false); 1662 } else { 1663 return llvm::StringSwitch<bool>(II->getName()) 1664 // Report builtin templates as being builtins. 1665 .Case("__make_integer_seq", getLangOpts().CPlusPlus) 1666 .Case("__type_pack_element", getLangOpts().CPlusPlus) 1667 // Likewise for some builtin preprocessor macros. 1668 // FIXME: This is inconsistent; we usually suggest detecting 1669 // builtin macros via #ifdef. Don't add more cases here. 1670 .Case("__is_target_arch", true) 1671 .Case("__is_target_vendor", true) 1672 .Case("__is_target_os", true) 1673 .Case("__is_target_environment", true) 1674 .Default(false); 1675 } 1676 }); 1677 } else if (II == Ident__is_identifier) { 1678 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, 1679 [](Token &Tok, bool &HasLexedNextToken) -> int { 1680 return Tok.is(tok::identifier); 1681 }); 1682 } else if (II == Ident__has_attribute) { 1683 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true, 1684 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1685 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, 1686 diag::err_feature_check_malformed); 1687 return II ? hasAttribute(AttrSyntax::GNU, nullptr, II, 1688 getTargetInfo(), getLangOpts()) : 0; 1689 }); 1690 } else if (II == Ident__has_declspec) { 1691 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true, 1692 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1693 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, 1694 diag::err_feature_check_malformed); 1695 if (II) { 1696 const LangOptions &LangOpts = getLangOpts(); 1697 return LangOpts.DeclSpecKeyword && 1698 hasAttribute(AttrSyntax::Declspec, nullptr, II, 1699 getTargetInfo(), LangOpts); 1700 } 1701 1702 return false; 1703 }); 1704 } else if (II == Ident__has_cpp_attribute || 1705 II == Ident__has_c_attribute) { 1706 bool IsCXX = II == Ident__has_cpp_attribute; 1707 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true, 1708 [&](Token &Tok, bool &HasLexedNextToken) -> int { 1709 IdentifierInfo *ScopeII = nullptr; 1710 IdentifierInfo *II = ExpectFeatureIdentifierInfo( 1711 Tok, *this, diag::err_feature_check_malformed); 1712 if (!II) 1713 return false; 1714 1715 // It is possible to receive a scope token. Read the "::", if it is 1716 // available, and the subsequent identifier. 1717 LexUnexpandedToken(Tok); 1718 if (Tok.isNot(tok::coloncolon)) 1719 HasLexedNextToken = true; 1720 else { 1721 ScopeII = II; 1722 // Lex an expanded token for the attribute name. 1723 Lex(Tok); 1724 II = ExpectFeatureIdentifierInfo(Tok, *this, 1725 diag::err_feature_check_malformed); 1726 } 1727 1728 AttrSyntax Syntax = IsCXX ? AttrSyntax::CXX : AttrSyntax::C; 1729 return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(), 1730 getLangOpts()) 1731 : 0; 1732 }); 1733 } else if (II == Ident__has_include || 1734 II == Ident__has_include_next) { 1735 // The argument to these two builtins should be a parenthesized 1736 // file name string literal using angle brackets (<>) or 1737 // double-quotes (""). 1738 bool Value; 1739 if (II == Ident__has_include) 1740 Value = EvaluateHasInclude(Tok, II, *this); 1741 else 1742 Value = EvaluateHasIncludeNext(Tok, II, *this); 1743 1744 if (Tok.isNot(tok::r_paren)) 1745 return; 1746 OS << (int)Value; 1747 Tok.setKind(tok::numeric_constant); 1748 } else if (II == Ident__has_warning) { 1749 // The argument should be a parenthesized string literal. 1750 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, 1751 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1752 std::string WarningName; 1753 SourceLocation StrStartLoc = Tok.getLocation(); 1754 1755 HasLexedNextToken = Tok.is(tok::string_literal); 1756 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", 1757 /*AllowMacroExpansion=*/false)) 1758 return false; 1759 1760 // FIXME: Should we accept "-R..." flags here, or should that be 1761 // handled by a separate __has_remark? 1762 if (WarningName.size() < 3 || WarningName[0] != '-' || 1763 WarningName[1] != 'W') { 1764 Diag(StrStartLoc, diag::warn_has_warning_invalid_option); 1765 return false; 1766 } 1767 1768 // Finally, check if the warning flags maps to a diagnostic group. 1769 // We construct a SmallVector here to talk to getDiagnosticIDs(). 1770 // Although we don't use the result, this isn't a hot path, and not 1771 // worth special casing. 1772 SmallVector<diag::kind, 10> Diags; 1773 return !getDiagnostics().getDiagnosticIDs()-> 1774 getDiagnosticsInGroup(diag::Flavor::WarningOrError, 1775 WarningName.substr(2), Diags); 1776 }); 1777 } else if (II == Ident__building_module) { 1778 // The argument to this builtin should be an identifier. The 1779 // builtin evaluates to 1 when that identifier names the module we are 1780 // currently building. 1781 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, 1782 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1783 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, 1784 diag::err_expected_id_building_module); 1785 return getLangOpts().isCompilingModule() && II && 1786 (II->getName() == getLangOpts().CurrentModule); 1787 }); 1788 } else if (II == Ident__MODULE__) { 1789 // The current module as an identifier. 1790 OS << getLangOpts().CurrentModule; 1791 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); 1792 Tok.setIdentifierInfo(ModuleII); 1793 Tok.setKind(ModuleII->getTokenID()); 1794 } else if (II == Ident__identifier) { 1795 SourceLocation Loc = Tok.getLocation(); 1796 1797 // We're expecting '__identifier' '(' identifier ')'. Try to recover 1798 // if the parens are missing. 1799 LexNonComment(Tok); 1800 if (Tok.isNot(tok::l_paren)) { 1801 // No '(', use end of last token. 1802 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after) 1803 << II << tok::l_paren; 1804 // If the next token isn't valid as our argument, we can't recover. 1805 if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1806 Tok.setKind(tok::identifier); 1807 return; 1808 } 1809 1810 SourceLocation LParenLoc = Tok.getLocation(); 1811 LexNonComment(Tok); 1812 1813 if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1814 Tok.setKind(tok::identifier); 1815 else if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) { 1816 StringLiteralParser Literal(Tok, *this); 1817 if (Literal.hadError) 1818 return; 1819 1820 Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString())); 1821 Tok.setKind(tok::identifier); 1822 } else { 1823 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier) 1824 << Tok.getKind(); 1825 // Don't walk past anything that's not a real token. 1826 if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation()) 1827 return; 1828 } 1829 1830 // Discard the ')', preserving 'Tok' as our result. 1831 Token RParen; 1832 LexNonComment(RParen); 1833 if (RParen.isNot(tok::r_paren)) { 1834 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after) 1835 << Tok.getKind() << tok::r_paren; 1836 Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1837 } 1838 return; 1839 } else if (II == Ident__is_target_arch) { 1840 EvaluateFeatureLikeBuiltinMacro( 1841 OS, Tok, II, *this, false, 1842 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1843 IdentifierInfo *II = ExpectFeatureIdentifierInfo( 1844 Tok, *this, diag::err_feature_check_malformed); 1845 return II && isTargetArch(getTargetInfo(), II); 1846 }); 1847 } else if (II == Ident__is_target_vendor) { 1848 EvaluateFeatureLikeBuiltinMacro( 1849 OS, Tok, II, *this, false, 1850 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1851 IdentifierInfo *II = ExpectFeatureIdentifierInfo( 1852 Tok, *this, diag::err_feature_check_malformed); 1853 return II && isTargetVendor(getTargetInfo(), II); 1854 }); 1855 } else if (II == Ident__is_target_os) { 1856 EvaluateFeatureLikeBuiltinMacro( 1857 OS, Tok, II, *this, false, 1858 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1859 IdentifierInfo *II = ExpectFeatureIdentifierInfo( 1860 Tok, *this, diag::err_feature_check_malformed); 1861 return II && isTargetOS(getTargetInfo(), II); 1862 }); 1863 } else if (II == Ident__is_target_environment) { 1864 EvaluateFeatureLikeBuiltinMacro( 1865 OS, Tok, II, *this, false, 1866 [this](Token &Tok, bool &HasLexedNextToken) -> int { 1867 IdentifierInfo *II = ExpectFeatureIdentifierInfo( 1868 Tok, *this, diag::err_feature_check_malformed); 1869 return II && isTargetEnvironment(getTargetInfo(), II); 1870 }); 1871 } else { 1872 llvm_unreachable("Unknown identifier!"); 1873 } 1874 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); 1875 Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine); 1876 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); 1877 } 1878 1879 void Preprocessor::markMacroAsUsed(MacroInfo *MI) { 1880 // If the 'used' status changed, and the macro requires 'unused' warning, 1881 // remove its SourceLocation from the warn-for-unused-macro locations. 1882 if (MI->isWarnIfUnused() && !MI->isUsed()) 1883 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 1884 MI->setIsUsed(true); 1885 } 1886