14#ifndef LLVM_CLANG_LEX_MACROINFO_H
15#define LLVM_CLANG_LEX_MACROINFO_H
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/PointerIntPair.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/Allocator.h"
59 const Token *ReplacementTokens =
nullptr;
62 unsigned NumParameters = 0;
65 unsigned NumReplacementTokens = 0;
68 mutable unsigned DefinitionLength;
69 mutable bool IsDefinitionLengthCached : 1;
72 bool IsFunctionLike : 1;
79 bool IsC99Varargs : 1;
85 bool IsGNUVarargs : 1;
92 bool IsBuiltinMacro : 1;
95 bool HasCommaPasting : 1;
113 bool IsAllowRedefinitionsWithoutWarning : 1;
116 bool IsWarnIfUnused : 1;
119 bool UsedForHeaderGuard : 1;
136 if (IsDefinitionLengthCached)
137 return DefinitionLength;
138 return getDefinitionLengthSlow(
SM);
149 bool Syntactically)
const;
159 IsAllowRedefinitionsWithoutWarning = Val;
168 llvm::BumpPtrAllocator &PPAllocator) {
169 assert(ParameterList ==
nullptr && NumParameters == 0 &&
170 "Parameter list already set!");
174 NumParameters = List.size();
175 ParameterList = PPAllocator.Allocate<
IdentifierInfo *>(List.size());
176 std::copy(List.begin(), List.end(), ParameterList);
210 bool isVariadic()
const {
return IsC99Varargs || IsGNUVarargs; }
229 return IsAllowRedefinitionsWithoutWarning;
239 assert(
Tok < NumReplacementTokens &&
"Invalid token #");
240 return ReplacementTokens[
Tok];
247 return ReplacementTokens + NumReplacementTokens;
256 assert(ReplacementTokens ==
nullptr && NumReplacementTokens == 0 &&
257 "Token list already allocated!");
258 NumReplacementTokens = NumTokens;
259 Token *NewReplacementTokens = PPAllocator.Allocate<
Token>(NumTokens);
260 ReplacementTokens = NewReplacementTokens;
266 !IsDefinitionLengthCached &&
267 "Changing replacement tokens after definition length got calculated");
268 assert(ReplacementTokens ==
nullptr && NumReplacementTokens == 0 &&
269 "Token list already set!");
273 NumReplacementTokens = Tokens.size();
274 Token *NewReplacementTokens = PPAllocator.Allocate<
Token>(Tokens.size());
275 std::copy(Tokens.begin(), Tokens.end(), NewReplacementTokens);
276 ReplacementTokens = NewReplacementTokens;
285 assert(IsDisabled &&
"Cannot enable an already-enabled macro!");
290 assert(!IsDisabled &&
"Cannot disable an already-disabled macro!");
329 LLVM_PREFERRED_TYPE(
Kind)
333 LLVM_PREFERRED_TYPE(
bool)
340 LLVM_PREFERRED_TYPE(
bool)
368 bool IsPublic =
true;
374 : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(
isPublic) {}
391 bool isValid()
const {
return DefDirective !=
nullptr; }
413 return !Def.isUndefined();
439 assert(MI &&
"MacroInfo is null");
460 assert(UndefLoc.
isValid() &&
"Invalid UndefLoc!");
492 return DefDirective->getLocation();
498 return DefDirective->getInfo();
503 if (
isInvalid() || DefDirective->getPrevious() ==
nullptr)
505 return DefDirective->getPrevious()->getDefinition();
515class ModuleMacro :
public llvm::FoldingSetNode {
528 unsigned NumOverriddenBy = 0;
531 unsigned NumOverrides;
535 : II(II), Macro(Macro), OwningModule(OwningModule),
536 NumOverrides(Overrides.size()) {
537 std::copy(Overrides.begin(), Overrides.end(),
538 reinterpret_cast<ModuleMacro **
>(
this + 1));
546 void Profile(llvm::FoldingSetNodeID &ID)
const {
547 return Profile(ID, OwningModule, II);
552 ID.AddPointer(OwningModule);
597 llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous;
603 : LatestLocalAndAmbiguous(MD, Info.IsAmbiguous),
604 ModuleMacros(Info.ActiveModuleMacros) {}
607 explicit operator bool()
const {
613 if (!ModuleMacros.empty())
614 return ModuleMacros.back()->getMacroInfo();
616 return MD->getMacroInfo();
621 bool isAmbiguous()
const {
return LatestLocalAndAmbiguous.getInt(); }
626 return LatestLocalAndAmbiguous.getPointer();
634 F(MD->getMacroInfo());
636 F(MM->getMacroInfo());
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Forward-declares types that need PointerLikeTypeTraits.
Defines the clang::SourceLocation class and associated facilities.
static bool isInvalid(LocType Loc, bool *Invalid)
A directive for a defined macro or a macro imported from a module.
static bool classof(const MacroDirective *MD)
static bool classof(const DefMacroDirective *)
const MacroInfo * getInfo() const
The data for the macro definition.
DefMacroDirective(MacroInfo *MI)
DefMacroDirective(MacroInfo *MI, SourceLocation Loc)
One of these records is kept for each identifier that is lexed.
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
DefMacroDirective * getLocalDirective() const
Get the latest non-imported, non-#undef'd macro definition for this macro.
ArrayRef< ModuleMacro * > getModuleMacros() const
Get the active module macros for this macro.
bool isAmbiguous() const
true if the definition is ambiguous, false otherwise.
MacroDefinition()=default
void forAllDefinitions(Fn F) const
MacroDefinition(DefMacroDirective *MD, ModuleMacroInfo Info)
const DefInfo getPreviousDefinition() const
DefMacroDirective * getDirective()
DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc, bool isPublic)
DefInfo getPreviousDefinition()
MacroInfo * getMacroInfo()
const MacroInfo * getMacroInfo() const
SourceLocation getUndefLocation() const
const DefMacroDirective * getDirective() const
SourceLocation getLocation() const
MacroDirective * Previous
Previous macro directive for the same identifier, or nullptr.
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
const MacroInfo * getMacroInfo() const
const DefInfo findDirectiveAtLoc(SourceLocation L, const SourceManager &SM) const
Find macro definition active in the specified source location.
unsigned IsPublic
Whether the macro has public visibility (when described in a module).
void setPrevious(MacroDirective *Prev)
Set previous definition of the macro with the same name.
unsigned IsFromPCH
True if the macro directive was loaded from a PCH file.
SourceLocation getLocation() const
static bool classof(const MacroDirective *)
unsigned MDKind
MacroDirective kind.
MacroInfo * getMacroInfo()
const DefInfo getDefinition() const
MacroDirective(Kind K, SourceLocation Loc)
bool isFromPCH() const
Return true if the macro directive was loaded from a PCH file.
MacroDirective * getPrevious()
Get previous definition of the macro with the same name.
DefInfo getDefinition()
Traverses the macro directives history and returns the next macro definition directive along with inf...
Encapsulates the data about a macro definition (e.g.
friend class Preprocessor
void setIsAllowRedefinitionsWithoutWarning(bool Val)
Set the value of the IsAllowRedefinitionsWithoutWarning flag.
bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, bool Syntactically) const
Return true if the specified macro definition is equal to this macro in spelling, arguments,...
bool isUsed() const
Return false if this macro is defined in the main file and has not yet been used.
bool isC99Varargs() const
bool isFunctionLike() const
const_tokens_iterator tokens_begin() const
bool isAllowRedefinitionsWithoutWarning() const
Return true if this macro can be redefined without warning.
SourceLocation getDefinitionEndLoc() const
Return the location of the last token in the macro.
void setUsedForHeaderGuard(bool Val)
void setHasCommaPasting()
param_iterator param_begin() const
const_tokens_iterator tokens_end() const
ArrayRef< const IdentifierInfo * > params() const
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
unsigned getNumParams() const
const Token & getReplacementToken(unsigned Tok) const
void setDefinitionEndLoc(SourceLocation EndLoc)
Set the location of the last token in the macro.
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
void setTokens(ArrayRef< Token > Tokens, llvm::BumpPtrAllocator &PPAllocator)
IdentifierInfo *const * param_iterator
Parameters - The list of parameters for a function-like macro.
const Token * const_tokens_iterator
void setParameterList(ArrayRef< IdentifierInfo * > List, llvm::BumpPtrAllocator &PPAllocator)
Set the specified list of identifiers as the parameter list for this macro.
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
bool tokens_empty() const
unsigned getDefinitionLength(const SourceManager &SM) const
Get length in characters of the macro definition.
llvm::MutableArrayRef< Token > allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator)
bool hasCommaPasting() const
void setIsFunctionLike()
Function/Object-likeness.
bool isObjectLike() const
param_iterator param_end() const
bool isUsedForHeaderGuard() const
Determine whether this macro was used for a header guard.
ArrayRef< Token > tokens() const
void setIsWarnIfUnused(bool val)
Set the value of the IsWarnIfUnused flag.
int getParameterNum(const IdentifierInfo *Arg) const
Return the parameter number of the specified identifier, or -1 if the identifier is not a formal para...
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
bool isGNUVarargs() const
void setIsC99Varargs()
Varargs querying methods. This can only be set for function-like macros.
bool isEnabled() const
Return true if this macro is enabled.
void setIsUsed(bool Val)
Set the value of the IsUsed flag.
void setIsBuiltinMacro(bool Val=true)
Set or clear the isBuiltinMacro flag.
Represents a macro directive exported by a module.
const IdentifierInfo * getName() const
Get the name of the macro.
friend class Preprocessor
MacroInfo * getMacroInfo() const
Get definition for this exported define, or nullptr if this represents a undef.
overrides_iterator overrides_begin() const
ArrayRef< ModuleMacro * > overrides() const
static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule, const IdentifierInfo *II)
unsigned getNumOverridingMacros() const
Get the number of macros that override this one.
void Profile(llvm::FoldingSetNodeID &ID) const
Module * getOwningModule() const
Get the ID of the module that exports this macro.
ModuleMacro *const * overrides_iterator
Iterators over the overridden module IDs.
overrides_iterator overrides_end() const
Describes a module or submodule.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
static bool classof(const UndefMacroDirective *)
static bool classof(const MacroDirective *MD)
UndefMacroDirective(SourceLocation UndefLoc)
VisibilityMacroDirective(SourceLocation Loc, bool Public)
static bool classof(const MacroDirective *MD)
static bool classof(const VisibilityMacroDirective *)
bool isPublic() const
Determine whether this macro is part of the public API of its module.
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
ArrayRef< ModuleMacro * > ActiveModuleMacros