21 #include "llvm/ADT/Optional.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/Support/Casting.h" 24 #include "llvm/Support/Compiler.h" 25 #include "llvm/Support/raw_ostream.h" 29 using namespace clang;
32 : Location(DefLoc), IsDefinitionLengthCached(
false), IsFunctionLike(
false),
35 IsAllowRedefinitionsWithoutWarning(
false), IsWarnIfUnused(
false),
36 UsedForHeaderGuard(
false) {}
38 unsigned MacroInfo::getDefinitionLengthSlow(
const SourceManager &
SM)
const {
39 assert(!IsDefinitionLengthCached);
40 IsDefinitionLengthCached =
true;
42 if (ReplacementTokens.empty())
43 return (DefinitionLength = 0);
45 const Token &firstToken = ReplacementTokens.front();
46 const Token &lastToken = ReplacementTokens.back();
50 assert((macroStart.
isFileID() || firstToken.
is(tok::comment)) &&
51 "Macro defined in macro?");
52 assert((macroEnd.
isFileID() || lastToken.
is(tok::comment)) &&
53 "Macro defined in macro?");
54 std::pair<FileID, unsigned>
56 std::pair<FileID, unsigned>
58 assert(startInfo.first == endInfo.first &&
59 "Macro definition spanning multiple FileIDs ?");
60 assert(startInfo.second <= endInfo.second);
61 DefinitionLength = endInfo.second - startInfo.second;
62 DefinitionLength += lastToken.
getLength();
64 return DefinitionLength;
75 bool Syntactically)
const {
76 bool Lexically = !Syntactically;
79 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
91 if (*I != *OI)
return false;
95 for (
unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
96 const Token &A = ReplacementTokens[i];
97 const Token &B = Other.ReplacementTokens[i];
133 llvm::raw_ostream &Out = llvm::errs();
136 Out <<
"MacroInfo " <<
this;
137 if (IsBuiltinMacro) Out <<
" builtin";
138 if (IsDisabled) Out <<
" disabled";
139 if (IsUsed) Out <<
" used";
140 if (IsAllowRedefinitionsWithoutWarning)
141 Out <<
" allow_redefinitions_without_warning";
142 if (IsWarnIfUnused) Out <<
" warn_if_unused";
143 if (UsedForHeaderGuard) Out <<
" header_guard";
145 Out <<
"\n #define <macro>";
146 if (IsFunctionLike) {
148 for (
unsigned I = 0; I != NumParameters; ++I) {
150 Out << ParameterList[I]->getName();
152 if (IsC99Varargs || IsGNUVarargs) {
153 if (NumParameters && IsC99Varargs) Out <<
", ";
160 for (
const Token &
Tok : ReplacementTokens) {
163 if (First ||
Tok.hasLeadingSpace())
169 else if (
Tok.isLiteral() &&
Tok.getLiteralData())
170 Out << StringRef(
Tok.getLiteralData(),
Tok.getLength());
171 else if (
auto *II =
Tok.getIdentifierInfo())
172 Out << II->getName();
174 Out <<
Tok.getName();
184 return DefInfo(DefMD, UndefLoc,
185 !isPublic.hasValue() || isPublic.getValue());
188 UndefLoc = UndefMD->getLocation();
193 if (!isPublic.hasValue())
197 return DefInfo(
nullptr, UndefLoc,
198 !isPublic.hasValue() || isPublic.getValue());
204 assert(L.
isValid() &&
"SourceLocation is invalid.");
206 if (Def.getLocation().isInvalid() ||
208 return (!Def.isUndefined() ||
216 llvm::raw_ostream &Out = llvm::errs();
219 case MD_Define: Out <<
"DefMacroDirective";
break;
220 case MD_Undefine: Out <<
"UndefMacroDirective";
break;
221 case MD_Visibility: Out <<
"VisibilityMacroDirective";
break;
225 if (
auto *Prev = getPrevious())
226 Out <<
" prev " << Prev;
227 if (IsFromPCH) Out <<
" from_pch";
229 if (isa<VisibilityMacroDirective>(
this))
230 Out << (IsPublic ?
" public" :
" private");
232 if (
auto *DMD = dyn_cast<DefMacroDirective>(
this)) {
233 if (
auto *Info = DMD->getInfo()) {
247 return new (Mem)
ModuleMacro(OwningModule, II, Macro, Overrides);
llvm::BumpPtrAllocator & getPreprocessorAllocator()
param_iterator param_begin() const
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Defines the SourceManager interface.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
Defines the clang::MacroInfo and clang::MacroDirective classes.
A directive for an undefined macro.
static const NamedDecl * getDefinition(const Decl *D)
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
tok::TokenKind getKind() const
One of these records is kept for each identifier that is lexed.
Represents a macro directive exported by a module.
A directive for a defined macro or a macro imported from a module.
Token - This structure provides full information about a lexed token.
Describes a module or submodule.
A directive for setting the module visibility of a macro.
std::pair< FileID, unsigned > getDecomposedExpansionLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
IdentifierInfo *const * param_iterator
Parameters - The list of parameters for a function-like macro.
bool isPublic() const
Determine whether this macro is part of the public API of its module.
unsigned getNumParams() const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Defines the clang::Preprocessor interface.
DefInfo getDefinition()
Traverses the macro directives history and returns the next macro definition directive along with inf...
const char * getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple punctuation tokens like '!' or '', and returns NULL for literal and...
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
const DefInfo findDirectiveAtLoc(SourceLocation L, const SourceManager &SM) const
Find macro definition active in the specified source location.
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Encodes a location in the source.
IdentifierInfo * getIdentifierInfo() const
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
bool isC99Varargs() const
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
bool isFunctionLike() const
unsigned getLength() const
Encapsulates the data about a macro definition (e.g.
static ModuleMacro * create(Preprocessor &PP, Module *OwningModule, IdentifierInfo *II, MacroInfo *Macro, ArrayRef< ModuleMacro *> Overrides)
int getParameterNum(const IdentifierInfo *Arg) const
Return the parameter number of the specified identifier, or -1 if the identifier is not a formal para...
Defines the clang::TokenKind enum and support functions.
Defines the clang::SourceLocation class and associated facilities.
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 isGNUVarargs() const
static Decl::Kind getKind(const Decl *D)
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.