1 //===- MacroInfo.h - Information about #defined identifiers -----*- C++ -*-===//
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 /// \file
10 /// Defines the clang::MacroInfo and clang::MacroDirective classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_LEX_MACROINFO_H
15 #define LLVM_CLANG_LEX_MACROINFO_H
16
17 #include "clang/Lex/Token.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/Allocator.h"
25 #include <algorithm>
26 #include <cassert>
27
28 namespace clang {
29
30 class DefMacroDirective;
31 class IdentifierInfo;
32 class Module;
33 class Preprocessor;
34 class SourceManager;
35
36 /// Encapsulates the data about a macro definition (e.g. its tokens).
37 ///
38 /// There's an instance of this class for every #define.
39 class MacroInfo {
40 //===--------------------------------------------------------------------===//
41 // State set when the macro is defined.
42
43 /// The location the macro is defined.
44 SourceLocation Location;
45
46 /// The location of the last token in the macro.
47 SourceLocation EndLocation;
48
49 /// The list of arguments for a function-like macro.
50 ///
51 /// ParameterList points to the first of NumParameters pointers.
52 ///
53 /// This can be empty, for, e.g. "#define X()". In a C99-style variadic
54 /// macro, this includes the \c __VA_ARGS__ identifier on the list.
55 IdentifierInfo **ParameterList = nullptr;
56
57 /// This is the list of tokens that the macro is defined to.
58 const Token *ReplacementTokens = nullptr;
59
60 /// \see ParameterList
61 unsigned NumParameters = 0;
62
63 /// \see ReplacementTokens
64 unsigned NumReplacementTokens = 0;
65
66 /// Length in characters of the macro definition.
67 mutable unsigned DefinitionLength;
68 mutable bool IsDefinitionLengthCached : 1;
69
70 /// True if this macro is function-like, false if it is object-like.
71 bool IsFunctionLike : 1;
72
73 /// True if this macro is of the form "#define X(...)" or
74 /// "#define X(Y,Z,...)".
75 ///
76 /// The __VA_ARGS__ token should be replaced with the contents of "..." in an
77 /// invocation.
78 bool IsC99Varargs : 1;
79
80 /// True if this macro is of the form "#define X(a...)".
81 ///
82 /// The "a" identifier in the replacement list will be replaced with all
83 /// arguments of the macro starting with the specified one.
84 bool IsGNUVarargs : 1;
85
86 /// True if this macro requires processing before expansion.
87 ///
88 /// This is the case for builtin macros such as __LINE__, so long as they have
89 /// not been redefined, but not for regular predefined macros from the
90 /// "<built-in>" memory buffer (see Preprocessing::getPredefinesFileID).
91 bool IsBuiltinMacro : 1;
92
93 /// Whether this macro contains the sequence ", ## __VA_ARGS__"
94 bool HasCommaPasting : 1;
95
96 //===--------------------------------------------------------------------===//
97 // State that changes as the macro is used.
98
99 /// True if we have started an expansion of this macro already.
100 ///
101 /// This disables recursive expansion, which would be quite bad for things
102 /// like \#define A A.
103 bool IsDisabled : 1;
104
105 /// True if this macro is either defined in the main file and has
106 /// been used, or if it is not defined in the main file.
107 ///
108 /// This is used to emit -Wunused-macros diagnostics.
109 bool IsUsed : 1;
110
111 /// True if this macro can be redefined without emitting a warning.
112 bool IsAllowRedefinitionsWithoutWarning : 1;
113
114 /// Must warn if the macro is unused at the end of translation unit.
115 bool IsWarnIfUnused : 1;
116
117 /// Whether this macro was used as header guard.
118 bool UsedForHeaderGuard : 1;
119
120 // Only the Preprocessor gets to create these.
121 MacroInfo(SourceLocation DefLoc);
122
123 public:
124 /// Return the location that the macro was defined at.
getDefinitionLoc()125 SourceLocation getDefinitionLoc() const { return Location; }
126
127 /// Set the location of the last token in the macro.
setDefinitionEndLoc(SourceLocation EndLoc)128 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
129
130 /// Return the location of the last token in the macro.
getDefinitionEndLoc()131 SourceLocation getDefinitionEndLoc() const { return EndLocation; }
132
133 /// Get length in characters of the macro definition.
getDefinitionLength(const SourceManager & SM)134 unsigned getDefinitionLength(const SourceManager &SM) const {
135 if (IsDefinitionLengthCached)
136 return DefinitionLength;
137 return getDefinitionLengthSlow(SM);
138 }
139
140 /// Return true if the specified macro definition is equal to
141 /// this macro in spelling, arguments, and whitespace.
142 ///
143 /// \param Syntactically if true, the macro definitions can be identical even
144 /// if they use different identifiers for the function macro parameters.
145 /// Otherwise the comparison is lexical and this implements the rules in
146 /// C99 6.10.3.
147 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
148 bool Syntactically) const;
149
150 /// Set or clear the isBuiltinMacro flag.
151 void setIsBuiltinMacro(bool Val = true) { IsBuiltinMacro = Val; }
152
153 /// Set the value of the IsUsed flag.
setIsUsed(bool Val)154 void setIsUsed(bool Val) { IsUsed = Val; }
155
156 /// Set the value of the IsAllowRedefinitionsWithoutWarning flag.
setIsAllowRedefinitionsWithoutWarning(bool Val)157 void setIsAllowRedefinitionsWithoutWarning(bool Val) {
158 IsAllowRedefinitionsWithoutWarning = Val;
159 }
160
161 /// Set the value of the IsWarnIfUnused flag.
setIsWarnIfUnused(bool val)162 void setIsWarnIfUnused(bool val) { IsWarnIfUnused = val; }
163
164 /// Set the specified list of identifiers as the parameter list for
165 /// this macro.
setParameterList(ArrayRef<IdentifierInfo * > List,llvm::BumpPtrAllocator & PPAllocator)166 void setParameterList(ArrayRef<IdentifierInfo *> List,
167 llvm::BumpPtrAllocator &PPAllocator) {
168 assert(ParameterList == nullptr && NumParameters == 0 &&
169 "Parameter list already set!");
170 if (List.empty())
171 return;
172
173 NumParameters = List.size();
174 ParameterList = PPAllocator.Allocate<IdentifierInfo *>(List.size());
175 std::copy(List.begin(), List.end(), ParameterList);
176 }
177
178 /// Parameters - The list of parameters for a function-like macro. This can
179 /// be empty, for, e.g. "#define X()".
180 using param_iterator = IdentifierInfo *const *;
param_empty()181 bool param_empty() const { return NumParameters == 0; }
param_begin()182 param_iterator param_begin() const { return ParameterList; }
param_end()183 param_iterator param_end() const { return ParameterList + NumParameters; }
getNumParams()184 unsigned getNumParams() const { return NumParameters; }
params()185 ArrayRef<const IdentifierInfo *> params() const {
186 return ArrayRef<const IdentifierInfo *>(ParameterList, NumParameters);
187 }
188
189 /// Return the parameter number of the specified identifier,
190 /// or -1 if the identifier is not a formal parameter identifier.
getParameterNum(const IdentifierInfo * Arg)191 int getParameterNum(const IdentifierInfo *Arg) const {
192 for (param_iterator I = param_begin(), E = param_end(); I != E; ++I)
193 if (*I == Arg)
194 return I - param_begin();
195 return -1;
196 }
197
198 /// Function/Object-likeness. Keep track of whether this macro has formal
199 /// parameters.
setIsFunctionLike()200 void setIsFunctionLike() { IsFunctionLike = true; }
isFunctionLike()201 bool isFunctionLike() const { return IsFunctionLike; }
isObjectLike()202 bool isObjectLike() const { return !IsFunctionLike; }
203
204 /// Varargs querying methods. This can only be set for function-like macros.
setIsC99Varargs()205 void setIsC99Varargs() { IsC99Varargs = true; }
setIsGNUVarargs()206 void setIsGNUVarargs() { IsGNUVarargs = true; }
isC99Varargs()207 bool isC99Varargs() const { return IsC99Varargs; }
isGNUVarargs()208 bool isGNUVarargs() const { return IsGNUVarargs; }
isVariadic()209 bool isVariadic() const { return IsC99Varargs || IsGNUVarargs; }
210
211 /// Return true if this macro requires processing before expansion.
212 ///
213 /// This is true only for builtin macro, such as \__LINE__, whose values
214 /// are not given by fixed textual expansions. Regular predefined macros
215 /// from the "<built-in>" buffer are not reported as builtins by this
216 /// function.
isBuiltinMacro()217 bool isBuiltinMacro() const { return IsBuiltinMacro; }
218
hasCommaPasting()219 bool hasCommaPasting() const { return HasCommaPasting; }
setHasCommaPasting()220 void setHasCommaPasting() { HasCommaPasting = true; }
221
222 /// Return false if this macro is defined in the main file and has
223 /// not yet been used.
isUsed()224 bool isUsed() const { return IsUsed; }
225
226 /// Return true if this macro can be redefined without warning.
isAllowRedefinitionsWithoutWarning()227 bool isAllowRedefinitionsWithoutWarning() const {
228 return IsAllowRedefinitionsWithoutWarning;
229 }
230
231 /// Return true if we should emit a warning if the macro is unused.
isWarnIfUnused()232 bool isWarnIfUnused() const { return IsWarnIfUnused; }
233
234 /// Return the number of tokens that this macro expands to.
getNumTokens()235 unsigned getNumTokens() const { return NumReplacementTokens; }
236
getReplacementToken(unsigned Tok)237 const Token &getReplacementToken(unsigned Tok) const {
238 assert(Tok < NumReplacementTokens && "Invalid token #");
239 return ReplacementTokens[Tok];
240 }
241
242 using const_tokens_iterator = const Token *;
243
tokens_begin()244 const_tokens_iterator tokens_begin() const { return ReplacementTokens; }
tokens_end()245 const_tokens_iterator tokens_end() const {
246 return ReplacementTokens + NumReplacementTokens;
247 }
tokens_empty()248 bool tokens_empty() const { return NumReplacementTokens == 0; }
tokens()249 ArrayRef<Token> tokens() const {
250 return llvm::ArrayRef(ReplacementTokens, NumReplacementTokens);
251 }
252
253 llvm::MutableArrayRef<Token>
allocateTokens(unsigned NumTokens,llvm::BumpPtrAllocator & PPAllocator)254 allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator) {
255 assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 &&
256 "Token list already allocated!");
257 NumReplacementTokens = NumTokens;
258 Token *NewReplacementTokens = PPAllocator.Allocate<Token>(NumTokens);
259 ReplacementTokens = NewReplacementTokens;
260 return llvm::MutableArrayRef(NewReplacementTokens, NumTokens);
261 }
262
setTokens(ArrayRef<Token> Tokens,llvm::BumpPtrAllocator & PPAllocator)263 void setTokens(ArrayRef<Token> Tokens, llvm::BumpPtrAllocator &PPAllocator) {
264 assert(
265 !IsDefinitionLengthCached &&
266 "Changing replacement tokens after definition length got calculated");
267 assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 &&
268 "Token list already set!");
269 if (Tokens.empty())
270 return;
271
272 NumReplacementTokens = Tokens.size();
273 Token *NewReplacementTokens = PPAllocator.Allocate<Token>(Tokens.size());
274 std::copy(Tokens.begin(), Tokens.end(), NewReplacementTokens);
275 ReplacementTokens = NewReplacementTokens;
276 }
277
278 /// Return true if this macro is enabled.
279 ///
280 /// In other words, that we are not currently in an expansion of this macro.
isEnabled()281 bool isEnabled() const { return !IsDisabled; }
282
EnableMacro()283 void EnableMacro() {
284 assert(IsDisabled && "Cannot enable an already-enabled macro!");
285 IsDisabled = false;
286 }
287
DisableMacro()288 void DisableMacro() {
289 assert(!IsDisabled && "Cannot disable an already-disabled macro!");
290 IsDisabled = true;
291 }
292
293 /// Determine whether this macro was used for a header guard.
isUsedForHeaderGuard()294 bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
295
setUsedForHeaderGuard(bool Val)296 void setUsedForHeaderGuard(bool Val) { UsedForHeaderGuard = Val; }
297
298 void dump() const;
299
300 private:
301 friend class Preprocessor;
302
303 unsigned getDefinitionLengthSlow(const SourceManager &SM) const;
304 };
305
306 /// Encapsulates changes to the "macros namespace" (the location where
307 /// the macro name became active, the location where it was undefined, etc.).
308 ///
309 /// MacroDirectives, associated with an identifier, are used to model the macro
310 /// history. Usually a macro definition (MacroInfo) is where a macro name
311 /// becomes active (MacroDirective) but #pragma push_macro / pop_macro can
312 /// create additional DefMacroDirectives for the same MacroInfo.
313 class MacroDirective {
314 public:
315 enum Kind {
316 MD_Define,
317 MD_Undefine,
318 MD_Visibility
319 };
320
321 protected:
322 /// Previous macro directive for the same identifier, or nullptr.
323 MacroDirective *Previous = nullptr;
324
325 SourceLocation Loc;
326
327 /// MacroDirective kind.
328 LLVM_PREFERRED_TYPE(Kind)
329 unsigned MDKind : 2;
330
331 /// True if the macro directive was loaded from a PCH file.
332 LLVM_PREFERRED_TYPE(bool)
333 unsigned IsFromPCH : 1;
334
335 // Used by VisibilityMacroDirective ----------------------------------------//
336
337 /// Whether the macro has public visibility (when described in a
338 /// module).
339 LLVM_PREFERRED_TYPE(bool)
340 unsigned IsPublic : 1;
341
MacroDirective(Kind K,SourceLocation Loc)342 MacroDirective(Kind K, SourceLocation Loc)
343 : Loc(Loc), MDKind(K), IsFromPCH(false), IsPublic(true) {}
344
345 public:
getKind()346 Kind getKind() const { return Kind(MDKind); }
347
getLocation()348 SourceLocation getLocation() const { return Loc; }
349
350 /// Set previous definition of the macro with the same name.
setPrevious(MacroDirective * Prev)351 void setPrevious(MacroDirective *Prev) { Previous = Prev; }
352
353 /// Get previous definition of the macro with the same name.
getPrevious()354 const MacroDirective *getPrevious() const { return Previous; }
355
356 /// Get previous definition of the macro with the same name.
getPrevious()357 MacroDirective *getPrevious() { return Previous; }
358
359 /// Return true if the macro directive was loaded from a PCH file.
isFromPCH()360 bool isFromPCH() const { return IsFromPCH; }
361
setIsFromPCH()362 void setIsFromPCH() { IsFromPCH = true; }
363
364 class DefInfo {
365 DefMacroDirective *DefDirective = nullptr;
366 SourceLocation UndefLoc;
367 bool IsPublic = true;
368
369 public:
370 DefInfo() = default;
DefInfo(DefMacroDirective * DefDirective,SourceLocation UndefLoc,bool isPublic)371 DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc,
372 bool isPublic)
373 : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {}
374
getDirective()375 const DefMacroDirective *getDirective() const { return DefDirective; }
getDirective()376 DefMacroDirective *getDirective() { return DefDirective; }
377
378 inline SourceLocation getLocation() const;
379 inline MacroInfo *getMacroInfo();
380
getMacroInfo()381 const MacroInfo *getMacroInfo() const {
382 return const_cast<DefInfo *>(this)->getMacroInfo();
383 }
384
getUndefLocation()385 SourceLocation getUndefLocation() const { return UndefLoc; }
isUndefined()386 bool isUndefined() const { return UndefLoc.isValid(); }
387
isPublic()388 bool isPublic() const { return IsPublic; }
389
isValid()390 bool isValid() const { return DefDirective != nullptr; }
isInvalid()391 bool isInvalid() const { return !isValid(); }
392
393 explicit operator bool() const { return isValid(); }
394
395 inline DefInfo getPreviousDefinition();
396
getPreviousDefinition()397 const DefInfo getPreviousDefinition() const {
398 return const_cast<DefInfo *>(this)->getPreviousDefinition();
399 }
400 };
401
402 /// Traverses the macro directives history and returns the next
403 /// macro definition directive along with info about its undefined location
404 /// (if there is one) and if it is public or private.
405 DefInfo getDefinition();
getDefinition()406 const DefInfo getDefinition() const {
407 return const_cast<MacroDirective *>(this)->getDefinition();
408 }
409
isDefined()410 bool isDefined() const {
411 if (const DefInfo Def = getDefinition())
412 return !Def.isUndefined();
413 return false;
414 }
415
getMacroInfo()416 const MacroInfo *getMacroInfo() const {
417 return getDefinition().getMacroInfo();
418 }
getMacroInfo()419 MacroInfo *getMacroInfo() { return getDefinition().getMacroInfo(); }
420
421 /// Find macro definition active in the specified source location. If
422 /// this macro was not defined there, return NULL.
423 const DefInfo findDirectiveAtLoc(SourceLocation L,
424 const SourceManager &SM) const;
425
426 void dump() const;
427
classof(const MacroDirective *)428 static bool classof(const MacroDirective *) { return true; }
429 };
430
431 /// A directive for a defined macro or a macro imported from a module.
432 class DefMacroDirective : public MacroDirective {
433 MacroInfo *Info;
434
435 public:
DefMacroDirective(MacroInfo * MI,SourceLocation Loc)436 DefMacroDirective(MacroInfo *MI, SourceLocation Loc)
437 : MacroDirective(MD_Define, Loc), Info(MI) {
438 assert(MI && "MacroInfo is null");
439 }
DefMacroDirective(MacroInfo * MI)440 explicit DefMacroDirective(MacroInfo *MI)
441 : DefMacroDirective(MI, MI->getDefinitionLoc()) {}
442
443 /// The data for the macro definition.
getInfo()444 const MacroInfo *getInfo() const { return Info; }
getInfo()445 MacroInfo *getInfo() { return Info; }
446
classof(const MacroDirective * MD)447 static bool classof(const MacroDirective *MD) {
448 return MD->getKind() == MD_Define;
449 }
450
classof(const DefMacroDirective *)451 static bool classof(const DefMacroDirective *) { return true; }
452 };
453
454 /// A directive for an undefined macro.
455 class UndefMacroDirective : public MacroDirective {
456 public:
UndefMacroDirective(SourceLocation UndefLoc)457 explicit UndefMacroDirective(SourceLocation UndefLoc)
458 : MacroDirective(MD_Undefine, UndefLoc) {
459 assert(UndefLoc.isValid() && "Invalid UndefLoc!");
460 }
461
classof(const MacroDirective * MD)462 static bool classof(const MacroDirective *MD) {
463 return MD->getKind() == MD_Undefine;
464 }
465
classof(const UndefMacroDirective *)466 static bool classof(const UndefMacroDirective *) { return true; }
467 };
468
469 /// A directive for setting the module visibility of a macro.
470 class VisibilityMacroDirective : public MacroDirective {
471 public:
VisibilityMacroDirective(SourceLocation Loc,bool Public)472 explicit VisibilityMacroDirective(SourceLocation Loc, bool Public)
473 : MacroDirective(MD_Visibility, Loc) {
474 IsPublic = Public;
475 }
476
477 /// Determine whether this macro is part of the public API of its
478 /// module.
isPublic()479 bool isPublic() const { return IsPublic; }
480
classof(const MacroDirective * MD)481 static bool classof(const MacroDirective *MD) {
482 return MD->getKind() == MD_Visibility;
483 }
484
classof(const VisibilityMacroDirective *)485 static bool classof(const VisibilityMacroDirective *) { return true; }
486 };
487
getLocation()488 inline SourceLocation MacroDirective::DefInfo::getLocation() const {
489 if (isInvalid())
490 return {};
491 return DefDirective->getLocation();
492 }
493
getMacroInfo()494 inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() {
495 if (isInvalid())
496 return nullptr;
497 return DefDirective->getInfo();
498 }
499
500 inline MacroDirective::DefInfo
getPreviousDefinition()501 MacroDirective::DefInfo::getPreviousDefinition() {
502 if (isInvalid() || DefDirective->getPrevious() == nullptr)
503 return {};
504 return DefDirective->getPrevious()->getDefinition();
505 }
506
507 /// Represents a macro directive exported by a module.
508 ///
509 /// There's an instance of this class for every macro #define or #undef that is
510 /// the final directive for a macro name within a module. These entities also
511 /// represent the macro override graph.
512 ///
513 /// These are stored in a FoldingSet in the preprocessor.
514 class ModuleMacro : public llvm::FoldingSetNode {
515 friend class Preprocessor;
516
517 /// The name defined by the macro.
518 const IdentifierInfo *II;
519
520 /// The body of the #define, or nullptr if this is a #undef.
521 MacroInfo *Macro;
522
523 /// The module that exports this macro.
524 Module *OwningModule;
525
526 /// The number of module macros that override this one.
527 unsigned NumOverriddenBy = 0;
528
529 /// The number of modules whose macros are directly overridden by this one.
530 unsigned NumOverrides;
531
ModuleMacro(Module * OwningModule,const IdentifierInfo * II,MacroInfo * Macro,ArrayRef<ModuleMacro * > Overrides)532 ModuleMacro(Module *OwningModule, const IdentifierInfo *II, MacroInfo *Macro,
533 ArrayRef<ModuleMacro *> Overrides)
534 : II(II), Macro(Macro), OwningModule(OwningModule),
535 NumOverrides(Overrides.size()) {
536 std::copy(Overrides.begin(), Overrides.end(),
537 reinterpret_cast<ModuleMacro **>(this + 1));
538 }
539
540 public:
541 static ModuleMacro *create(Preprocessor &PP, Module *OwningModule,
542 const IdentifierInfo *II, MacroInfo *Macro,
543 ArrayRef<ModuleMacro *> Overrides);
544
Profile(llvm::FoldingSetNodeID & ID)545 void Profile(llvm::FoldingSetNodeID &ID) const {
546 return Profile(ID, OwningModule, II);
547 }
548
Profile(llvm::FoldingSetNodeID & ID,Module * OwningModule,const IdentifierInfo * II)549 static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule,
550 const IdentifierInfo *II) {
551 ID.AddPointer(OwningModule);
552 ID.AddPointer(II);
553 }
554
555 /// Get the name of the macro.
getName()556 const IdentifierInfo *getName() const { return II; }
557
558 /// Get the ID of the module that exports this macro.
getOwningModule()559 Module *getOwningModule() const { return OwningModule; }
560
561 /// Get definition for this exported #define, or nullptr if this
562 /// represents a #undef.
getMacroInfo()563 MacroInfo *getMacroInfo() const { return Macro; }
564
565 /// Iterators over the overridden module IDs.
566 /// \{
567 using overrides_iterator = ModuleMacro *const *;
568
overrides_begin()569 overrides_iterator overrides_begin() const {
570 return reinterpret_cast<overrides_iterator>(this + 1);
571 }
572
overrides_end()573 overrides_iterator overrides_end() const {
574 return overrides_begin() + NumOverrides;
575 }
576
overrides()577 ArrayRef<ModuleMacro *> overrides() const {
578 return llvm::ArrayRef(overrides_begin(), overrides_end());
579 }
580 /// \}
581
582 /// Get the number of macros that override this one.
getNumOverridingMacros()583 unsigned getNumOverridingMacros() const { return NumOverriddenBy; }
584 };
585
586 /// A description of the current definition of a macro.
587 ///
588 /// The definition of a macro comprises a set of (at least one) defining
589 /// entities, which are either local MacroDirectives or imported ModuleMacros.
590 class MacroDefinition {
591 llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous;
592 ArrayRef<ModuleMacro *> ModuleMacros;
593
594 public:
595 MacroDefinition() = default;
MacroDefinition(DefMacroDirective * MD,ArrayRef<ModuleMacro * > MMs,bool IsAmbiguous)596 MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs,
597 bool IsAmbiguous)
598 : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {}
599
600 /// Determine whether there is a definition of this macro.
601 explicit operator bool() const {
602 return getLocalDirective() || !ModuleMacros.empty();
603 }
604
605 /// Get the MacroInfo that should be used for this definition.
getMacroInfo()606 MacroInfo *getMacroInfo() const {
607 if (!ModuleMacros.empty())
608 return ModuleMacros.back()->getMacroInfo();
609 if (auto *MD = getLocalDirective())
610 return MD->getMacroInfo();
611 return nullptr;
612 }
613
614 /// \c true if the definition is ambiguous, \c false otherwise.
isAmbiguous()615 bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); }
616
617 /// Get the latest non-imported, non-\#undef'd macro definition
618 /// for this macro.
getLocalDirective()619 DefMacroDirective *getLocalDirective() const {
620 return LatestLocalAndAmbiguous.getPointer();
621 }
622
623 /// Get the active module macros for this macro.
getModuleMacros()624 ArrayRef<ModuleMacro *> getModuleMacros() const { return ModuleMacros; }
625
forAllDefinitions(Fn F)626 template <typename Fn> void forAllDefinitions(Fn F) const {
627 if (auto *MD = getLocalDirective())
628 F(MD->getMacroInfo());
629 for (auto *MM : getModuleMacros())
630 F(MM->getMacroInfo());
631 }
632 };
633
634 } // namespace clang
635
636 #endif // LLVM_CLANG_LEX_MACROINFO_H
637