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