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