1 //===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the clang::MacroInfo and clang::MacroDirective classes.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LEX_MACROINFO_H
16 #define LLVM_CLANG_LEX_MACROINFO_H
18 #include "clang/Lex/Token.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/Allocator.h"
31 /// \brief Encapsulates the data about a macro definition (e.g. its tokens).
33 /// There's an instance of this class for every #define.
35 //===--------------------------------------------------------------------===//
36 // State set when the macro is defined.
38 /// \brief The location the macro is defined.
39 SourceLocation Location;
40 /// \brief The location of the last token in the macro.
41 SourceLocation EndLocation;
43 /// \brief The list of arguments for a function-like macro.
45 /// ArgumentList points to the first of NumArguments pointers.
47 /// This can be empty, for, e.g. "#define X()". In a C99-style variadic
48 /// macro, this includes the \c __VA_ARGS__ identifier on the list.
49 IdentifierInfo **ArgumentList;
52 unsigned NumArguments;
54 /// \brief This is the list of tokens that the macro is defined to.
55 SmallVector<Token, 8> ReplacementTokens;
57 /// \brief Length in characters of the macro definition.
58 mutable unsigned DefinitionLength;
59 mutable bool IsDefinitionLengthCached : 1;
61 /// \brief True if this macro is function-like, false if it is object-like.
62 bool IsFunctionLike : 1;
64 /// \brief True if this macro is of the form "#define X(...)" or
65 /// "#define X(Y,Z,...)".
67 /// The __VA_ARGS__ token should be replaced with the contents of "..." in an
69 bool IsC99Varargs : 1;
71 /// \brief True if this macro is of the form "#define X(a...)".
73 /// The "a" identifier in the replacement list will be replaced with all
74 /// arguments of the macro starting with the specified one.
75 bool IsGNUVarargs : 1;
77 /// \brief True if this macro requires processing before expansion.
79 /// This is the case for builtin macros such as __LINE__, so long as they have
80 /// not been redefined, but not for regular predefined macros from the
81 /// "<built-in>" memory buffer (see Preprocessing::getPredefinesFileID).
82 bool IsBuiltinMacro : 1;
84 /// \brief Whether this macro contains the sequence ", ## __VA_ARGS__"
85 bool HasCommaPasting : 1;
87 //===--------------------------------------------------------------------===//
88 // State that changes as the macro is used.
90 /// \brief True if we have started an expansion of this macro already.
92 /// This disables recursive expansion, which would be quite bad for things
93 /// like \#define A A.
96 /// \brief True if this macro is either defined in the main file and has
97 /// been used, or if it is not defined in the main file.
99 /// This is used to emit -Wunused-macros diagnostics.
102 /// \brief True if this macro can be redefined without emitting a warning.
103 bool IsAllowRedefinitionsWithoutWarning : 1;
105 /// \brief Must warn if the macro is unused at the end of translation unit.
106 bool IsWarnIfUnused : 1;
108 /// \brief Whether this macro info was loaded from an AST file.
109 bool FromASTFile : 1;
111 /// \brief Whether this macro was used as header guard.
112 bool UsedForHeaderGuard : 1;
114 // Only the Preprocessor gets to create and destroy these.
115 MacroInfo(SourceLocation DefLoc);
116 ~MacroInfo() = default;
119 /// \brief Return the location that the macro was defined at.
120 SourceLocation getDefinitionLoc() const { return Location; }
122 /// \brief Set the location of the last token in the macro.
123 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
125 /// \brief Return the location of the last token in the macro.
126 SourceLocation getDefinitionEndLoc() const { return EndLocation; }
128 /// \brief Get length in characters of the macro definition.
129 unsigned getDefinitionLength(SourceManager &SM) const {
130 if (IsDefinitionLengthCached)
131 return DefinitionLength;
132 return getDefinitionLengthSlow(SM);
135 /// \brief Return true if the specified macro definition is equal to
136 /// this macro in spelling, arguments, and whitespace.
138 /// \param Syntactically if true, the macro definitions can be identical even
139 /// if they use different identifiers for the function macro parameters.
140 /// Otherwise the comparison is lexical and this implements the rules in
142 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
143 bool Syntactically) const;
145 /// \brief Set or clear the isBuiltinMacro flag.
146 void setIsBuiltinMacro(bool Val = true) { IsBuiltinMacro = Val; }
148 /// \brief Set the value of the IsUsed flag.
149 void setIsUsed(bool Val) { IsUsed = Val; }
151 /// \brief Set the value of the IsAllowRedefinitionsWithoutWarning flag.
152 void setIsAllowRedefinitionsWithoutWarning(bool Val) {
153 IsAllowRedefinitionsWithoutWarning = Val;
156 /// \brief Set the value of the IsWarnIfUnused flag.
157 void setIsWarnIfUnused(bool val) { IsWarnIfUnused = val; }
159 /// \brief Set the specified list of identifiers as the argument list for
161 void setArgumentList(ArrayRef<IdentifierInfo *> List,
162 llvm::BumpPtrAllocator &PPAllocator) {
163 assert(ArgumentList == nullptr && NumArguments == 0 &&
164 "Argument list already set!");
168 NumArguments = List.size();
169 ArgumentList = PPAllocator.Allocate<IdentifierInfo *>(List.size());
170 std::copy(List.begin(), List.end(), ArgumentList);
173 /// Arguments - The list of arguments for a function-like macro. This can be
174 /// empty, for, e.g. "#define X()".
175 typedef IdentifierInfo *const *arg_iterator;
176 bool arg_empty() const { return NumArguments == 0; }
177 arg_iterator arg_begin() const { return ArgumentList; }
178 arg_iterator arg_end() const { return ArgumentList + NumArguments; }
179 unsigned getNumArgs() const { return NumArguments; }
180 ArrayRef<const IdentifierInfo *> args() const {
181 return ArrayRef<const IdentifierInfo *>(ArgumentList, NumArguments);
184 /// \brief Return the argument number of the specified identifier,
185 /// or -1 if the identifier is not a formal argument identifier.
186 int getArgumentNum(const IdentifierInfo *Arg) const {
187 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
189 return I - arg_begin();
193 /// Function/Object-likeness. Keep track of whether this macro has formal
195 void setIsFunctionLike() { IsFunctionLike = true; }
196 bool isFunctionLike() const { return IsFunctionLike; }
197 bool isObjectLike() const { return !IsFunctionLike; }
199 /// Varargs querying methods. This can only be set for function-like macros.
200 void setIsC99Varargs() { IsC99Varargs = true; }
201 void setIsGNUVarargs() { IsGNUVarargs = true; }
202 bool isC99Varargs() const { return IsC99Varargs; }
203 bool isGNUVarargs() const { return IsGNUVarargs; }
204 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
206 /// \brief Return true if this macro requires processing before expansion.
208 /// This is true only for builtin macro, such as \__LINE__, whose values
209 /// are not given by fixed textual expansions. Regular predefined macros
210 /// from the "<built-in>" buffer are not reported as builtins by this
212 bool isBuiltinMacro() const { return IsBuiltinMacro; }
214 bool hasCommaPasting() const { return HasCommaPasting; }
215 void setHasCommaPasting() { HasCommaPasting = true; }
217 /// \brief Return false if this macro is defined in the main file and has
218 /// not yet been used.
219 bool isUsed() const { return IsUsed; }
221 /// \brief Return true if this macro can be redefined without warning.
222 bool isAllowRedefinitionsWithoutWarning() const {
223 return IsAllowRedefinitionsWithoutWarning;
226 /// \brief Return true if we should emit a warning if the macro is unused.
227 bool isWarnIfUnused() const { return IsWarnIfUnused; }
229 /// \brief Return the number of tokens that this macro expands to.
231 unsigned getNumTokens() const { return ReplacementTokens.size(); }
233 const Token &getReplacementToken(unsigned Tok) const {
234 assert(Tok < ReplacementTokens.size() && "Invalid token #");
235 return ReplacementTokens[Tok];
238 typedef SmallVectorImpl<Token>::const_iterator tokens_iterator;
239 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
240 tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
241 bool tokens_empty() const { return ReplacementTokens.empty(); }
242 ArrayRef<Token> tokens() const { return ReplacementTokens; }
244 /// \brief Add the specified token to the replacement text for the macro.
245 void AddTokenToBody(const Token &Tok) {
247 !IsDefinitionLengthCached &&
248 "Changing replacement tokens after definition length got calculated");
249 ReplacementTokens.push_back(Tok);
252 /// \brief Return true if this macro is enabled.
254 /// In other words, that we are not currently in an expansion of this macro.
255 bool isEnabled() const { return !IsDisabled; }
258 assert(IsDisabled && "Cannot enable an already-enabled macro!");
262 void DisableMacro() {
263 assert(!IsDisabled && "Cannot disable an already-disabled macro!");
267 /// \brief Determine whether this macro info came from an AST file (such as
268 /// a precompiled header or module) rather than having been parsed.
269 bool isFromASTFile() const { return FromASTFile; }
271 /// \brief Determine whether this macro was used for a header guard.
272 bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
274 void setUsedForHeaderGuard(bool Val) { UsedForHeaderGuard = Val; }
276 /// \brief Retrieve the global ID of the module that owns this particular
278 unsigned getOwningModuleID() const {
280 return *(const unsigned *)(this + 1);
288 unsigned getDefinitionLengthSlow(SourceManager &SM) const;
290 void setOwningModuleID(unsigned ID) {
291 assert(isFromASTFile());
292 *(unsigned *)(this + 1) = ID;
295 friend class Preprocessor;
298 class DefMacroDirective;
300 /// \brief Encapsulates changes to the "macros namespace" (the location where
301 /// the macro name became active, the location where it was undefined, etc.).
303 /// MacroDirectives, associated with an identifier, are used to model the macro
304 /// history. Usually a macro definition (MacroInfo) is where a macro name
305 /// becomes active (MacroDirective) but #pragma push_macro / pop_macro can
306 /// create additional DefMacroDirectives for the same MacroInfo.
307 class MacroDirective {
309 enum Kind { MD_Define, MD_Undefine, MD_Visibility };
312 /// \brief Previous macro directive for the same identifier, or NULL.
313 MacroDirective *Previous;
317 /// \brief MacroDirective kind.
320 /// \brief True if the macro directive was loaded from a PCH file.
321 unsigned IsFromPCH : 1;
323 // Used by VisibilityMacroDirective ----------------------------------------//
325 /// \brief Whether the macro has public visibility (when described in a
327 unsigned IsPublic : 1;
329 MacroDirective(Kind K, SourceLocation Loc)
330 : Previous(nullptr), Loc(Loc), MDKind(K), IsFromPCH(false),
334 Kind getKind() const { return Kind(MDKind); }
336 SourceLocation getLocation() const { return Loc; }
338 /// \brief Set previous definition of the macro with the same name.
339 void setPrevious(MacroDirective *Prev) { Previous = Prev; }
341 /// \brief Get previous definition of the macro with the same name.
342 const MacroDirective *getPrevious() const { return Previous; }
344 /// \brief Get previous definition of the macro with the same name.
345 MacroDirective *getPrevious() { return Previous; }
347 /// \brief Return true if the macro directive was loaded from a PCH file.
348 bool isFromPCH() const { return IsFromPCH; }
350 void setIsFromPCH() { IsFromPCH = true; }
353 DefMacroDirective *DefDirective;
354 SourceLocation UndefLoc;
358 DefInfo() : DefDirective(nullptr), IsPublic(true) {}
360 DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc,
362 : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {}
364 const DefMacroDirective *getDirective() const { return DefDirective; }
365 DefMacroDirective *getDirective() { return DefDirective; }
367 inline SourceLocation getLocation() const;
368 inline MacroInfo *getMacroInfo();
369 const MacroInfo *getMacroInfo() const {
370 return const_cast<DefInfo *>(this)->getMacroInfo();
373 SourceLocation getUndefLocation() const { return UndefLoc; }
374 bool isUndefined() const { return UndefLoc.isValid(); }
376 bool isPublic() const { return IsPublic; }
378 bool isValid() const { return DefDirective != nullptr; }
379 bool isInvalid() const { return !isValid(); }
381 explicit operator bool() const { return isValid(); }
383 inline DefInfo getPreviousDefinition();
384 const DefInfo getPreviousDefinition() const {
385 return const_cast<DefInfo *>(this)->getPreviousDefinition();
389 /// \brief Traverses the macro directives history and returns the next
390 /// macro definition directive along with info about its undefined location
391 /// (if there is one) and if it is public or private.
392 DefInfo getDefinition();
393 const DefInfo getDefinition() const {
394 return const_cast<MacroDirective *>(this)->getDefinition();
397 bool isDefined() const {
398 if (const DefInfo Def = getDefinition())
399 return !Def.isUndefined();
403 const MacroInfo *getMacroInfo() const {
404 return getDefinition().getMacroInfo();
406 MacroInfo *getMacroInfo() { return getDefinition().getMacroInfo(); }
408 /// \brief Find macro definition active in the specified source location. If
409 /// this macro was not defined there, return NULL.
410 const DefInfo findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const;
414 static bool classof(const MacroDirective *) { return true; }
417 /// \brief A directive for a defined macro or a macro imported from a module.
418 class DefMacroDirective : public MacroDirective {
422 DefMacroDirective(MacroInfo *MI, SourceLocation Loc)
423 : MacroDirective(MD_Define, Loc), Info(MI) {
424 assert(MI && "MacroInfo is null");
426 explicit DefMacroDirective(MacroInfo *MI)
427 : DefMacroDirective(MI, MI->getDefinitionLoc()) {}
429 /// \brief The data for the macro definition.
430 const MacroInfo *getInfo() const { return Info; }
431 MacroInfo *getInfo() { return Info; }
433 static bool classof(const MacroDirective *MD) {
434 return MD->getKind() == MD_Define;
436 static bool classof(const DefMacroDirective *) { return true; }
439 /// \brief A directive for an undefined macro.
440 class UndefMacroDirective : public MacroDirective {
442 explicit UndefMacroDirective(SourceLocation UndefLoc)
443 : MacroDirective(MD_Undefine, UndefLoc) {
444 assert(UndefLoc.isValid() && "Invalid UndefLoc!");
447 static bool classof(const MacroDirective *MD) {
448 return MD->getKind() == MD_Undefine;
450 static bool classof(const UndefMacroDirective *) { return true; }
453 /// \brief A directive for setting the module visibility of a macro.
454 class VisibilityMacroDirective : public MacroDirective {
456 explicit VisibilityMacroDirective(SourceLocation Loc, bool Public)
457 : MacroDirective(MD_Visibility, Loc) {
461 /// \brief Determine whether this macro is part of the public API of its
463 bool isPublic() const { return IsPublic; }
465 static bool classof(const MacroDirective *MD) {
466 return MD->getKind() == MD_Visibility;
468 static bool classof(const VisibilityMacroDirective *) { return true; }
471 inline SourceLocation MacroDirective::DefInfo::getLocation() const {
473 return SourceLocation();
474 return DefDirective->getLocation();
477 inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() {
480 return DefDirective->getInfo();
483 inline MacroDirective::DefInfo
484 MacroDirective::DefInfo::getPreviousDefinition() {
485 if (isInvalid() || DefDirective->getPrevious() == nullptr)
487 return DefDirective->getPrevious()->getDefinition();
490 /// \brief Represents a macro directive exported by a module.
492 /// There's an instance of this class for every macro #define or #undef that is
493 /// the final directive for a macro name within a module. These entities also
494 /// represent the macro override graph.
496 /// These are stored in a FoldingSet in the preprocessor.
497 class ModuleMacro : public llvm::FoldingSetNode {
498 /// The name defined by the macro.
500 /// The body of the #define, or nullptr if this is a #undef.
502 /// The module that exports this macro.
503 Module *OwningModule;
504 /// The number of module macros that override this one.
505 unsigned NumOverriddenBy;
506 /// The number of modules whose macros are directly overridden by this one.
507 unsigned NumOverrides;
508 // ModuleMacro *OverriddenMacros[NumOverrides];
510 friend class Preprocessor;
512 ModuleMacro(Module *OwningModule, IdentifierInfo *II, MacroInfo *Macro,
513 ArrayRef<ModuleMacro *> Overrides)
514 : II(II), Macro(Macro), OwningModule(OwningModule), NumOverriddenBy(0),
515 NumOverrides(Overrides.size()) {
516 std::copy(Overrides.begin(), Overrides.end(),
517 reinterpret_cast<ModuleMacro **>(this + 1));
521 static ModuleMacro *create(Preprocessor &PP, Module *OwningModule,
522 IdentifierInfo *II, MacroInfo *Macro,
523 ArrayRef<ModuleMacro *> Overrides);
525 void Profile(llvm::FoldingSetNodeID &ID) const {
526 return Profile(ID, OwningModule, II);
528 static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule,
529 IdentifierInfo *II) {
530 ID.AddPointer(OwningModule);
534 /// Get the ID of the module that exports this macro.
535 Module *getOwningModule() const { return OwningModule; }
537 /// Get definition for this exported #define, or nullptr if this
538 /// represents a #undef.
539 MacroInfo *getMacroInfo() const { return Macro; }
541 /// Iterators over the overridden module IDs.
543 typedef ModuleMacro *const *overrides_iterator;
544 overrides_iterator overrides_begin() const {
545 return reinterpret_cast<overrides_iterator>(this + 1);
547 overrides_iterator overrides_end() const {
548 return overrides_begin() + NumOverrides;
550 ArrayRef<ModuleMacro *> overrides() const {
551 return llvm::makeArrayRef(overrides_begin(), overrides_end());
555 /// Get the number of macros that override this one.
556 unsigned getNumOverridingMacros() const { return NumOverriddenBy; }
559 /// \brief A description of the current definition of a macro.
561 /// The definition of a macro comprises a set of (at least one) defining
562 /// entities, which are either local MacroDirectives or imported ModuleMacros.
563 class MacroDefinition {
564 llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous;
565 ArrayRef<ModuleMacro *> ModuleMacros;
568 MacroDefinition() : LatestLocalAndAmbiguous(), ModuleMacros() {}
569 MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs,
571 : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {}
573 /// \brief Determine whether there is a definition of this macro.
574 explicit operator bool() const {
575 return getLocalDirective() || !ModuleMacros.empty();
578 /// \brief Get the MacroInfo that should be used for this definition.
579 MacroInfo *getMacroInfo() const {
580 if (!ModuleMacros.empty())
581 return ModuleMacros.back()->getMacroInfo();
582 if (auto *MD = getLocalDirective())
583 return MD->getMacroInfo();
587 /// \brief \c true if the definition is ambiguous, \c false otherwise.
588 bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); }
590 /// \brief Get the latest non-imported, non-\#undef'd macro definition
592 DefMacroDirective *getLocalDirective() const {
593 return LatestLocalAndAmbiguous.getPointer();
596 /// \brief Get the active module macros for this macro.
597 ArrayRef<ModuleMacro *> getModuleMacros() const { return ModuleMacros; }
599 template <typename Fn> void forAllDefinitions(Fn F) const {
600 if (auto *MD = getLocalDirective())
601 F(MD->getMacroInfo());
602 for (auto *MM : getModuleMacros())
603 F(MM->getMacroInfo());
607 } // end namespace clang