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