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