1 //===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
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 //===----------------------------------------------------------------------===//
10 // This file implements the MacroInfo interface.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/MacroInfo.h"
15 #include "clang/Lex/Preprocessor.h"
16 using namespace clang;
18 MacroInfo::MacroInfo(SourceLocation DefLoc)
20 ParameterList(nullptr),
22 IsDefinitionLengthCached(false),
23 IsFunctionLike(false),
26 IsBuiltinMacro(false),
27 HasCommaPasting(false),
30 IsAllowRedefinitionsWithoutWarning(false),
31 IsWarnIfUnused(false),
32 UsedForHeaderGuard(false) {
35 unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const {
36 assert(!IsDefinitionLengthCached);
37 IsDefinitionLengthCached = true;
39 if (ReplacementTokens.empty())
40 return (DefinitionLength = 0);
42 const Token &firstToken = ReplacementTokens.front();
43 const Token &lastToken = ReplacementTokens.back();
44 SourceLocation macroStart = firstToken.getLocation();
45 SourceLocation macroEnd = lastToken.getLocation();
46 assert(macroStart.isValid() && macroEnd.isValid());
47 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
48 "Macro defined in macro?");
49 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
50 "Macro defined in macro?");
51 std::pair<FileID, unsigned>
52 startInfo = SM.getDecomposedExpansionLoc(macroStart);
53 std::pair<FileID, unsigned>
54 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
55 assert(startInfo.first == endInfo.first &&
56 "Macro definition spanning multiple FileIDs ?");
57 assert(startInfo.second <= endInfo.second);
58 DefinitionLength = endInfo.second - startInfo.second;
59 DefinitionLength += lastToken.getLength();
61 return DefinitionLength;
64 /// \brief Return true if the specified macro definition is equal to
65 /// this macro in spelling, arguments, and whitespace.
67 /// \param Syntactically if true, the macro definitions can be identical even
68 /// if they use different identifiers for the function macro parameters.
69 /// Otherwise the comparison is lexical and this implements the rules in
71 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
72 bool Syntactically) const {
73 bool Lexically = !Syntactically;
75 // Check # tokens in replacement, number of args, and various flags all match.
76 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
77 getNumParams() != Other.getNumParams() ||
78 isFunctionLike() != Other.isFunctionLike() ||
79 isC99Varargs() != Other.isC99Varargs() ||
80 isGNUVarargs() != Other.isGNUVarargs())
85 for (param_iterator I = param_begin(), OI = Other.param_begin(),
88 if (*I != *OI) return false;
91 // Check all the tokens.
92 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
93 const Token &A = ReplacementTokens[i];
94 const Token &B = Other.ReplacementTokens[i];
95 if (A.getKind() != B.getKind())
98 // If this isn't the first first token, check that the whitespace and
99 // start-of-line characteristics match.
101 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
102 A.hasLeadingSpace() != B.hasLeadingSpace()))
105 // If this is an identifier, it is easy.
106 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
107 if (A.getIdentifierInfo() == B.getIdentifierInfo())
111 // With syntactic equivalence the parameter names can be different as long
112 // as they are used in the same place.
113 int AArgNum = getParameterNum(A.getIdentifierInfo());
116 if (AArgNum != Other.getParameterNum(B.getIdentifierInfo()))
121 // Otherwise, check the spelling.
122 if (PP.getSpelling(A) != PP.getSpelling(B))
129 LLVM_DUMP_METHOD void MacroInfo::dump() const {
130 llvm::raw_ostream &Out = llvm::errs();
132 // FIXME: Dump locations.
133 Out << "MacroInfo " << this;
134 if (IsBuiltinMacro) Out << " builtin";
135 if (IsDisabled) Out << " disabled";
136 if (IsUsed) Out << " used";
137 if (IsAllowRedefinitionsWithoutWarning)
138 Out << " allow_redefinitions_without_warning";
139 if (IsWarnIfUnused) Out << " warn_if_unused";
140 if (UsedForHeaderGuard) Out << " header_guard";
142 Out << "\n #define <macro>";
143 if (IsFunctionLike) {
145 for (unsigned I = 0; I != NumParameters; ++I) {
147 Out << ParameterList[I]->getName();
149 if (IsC99Varargs || IsGNUVarargs) {
150 if (NumParameters && IsC99Varargs) Out << ", ";
157 for (const Token &Tok : ReplacementTokens) {
158 // Leading space is semantically meaningful in a macro definition,
159 // so preserve it in the dump output.
160 if (First || Tok.hasLeadingSpace())
164 if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
166 else if (Tok.isLiteral() && Tok.getLiteralData())
167 Out << StringRef(Tok.getLiteralData(), Tok.getLength());
168 else if (auto *II = Tok.getIdentifierInfo())
169 Out << II->getName();
171 Out << Tok.getName();
175 MacroDirective::DefInfo MacroDirective::getDefinition() {
176 MacroDirective *MD = this;
177 SourceLocation UndefLoc;
178 Optional<bool> isPublic;
179 for (; MD; MD = MD->getPrevious()) {
180 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
181 return DefInfo(DefMD, UndefLoc,
182 !isPublic.hasValue() || isPublic.getValue());
184 if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
185 UndefLoc = UndefMD->getLocation();
189 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
190 if (!isPublic.hasValue())
191 isPublic = VisMD->isPublic();
194 return DefInfo(nullptr, UndefLoc,
195 !isPublic.hasValue() || isPublic.getValue());
198 const MacroDirective::DefInfo
199 MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
200 assert(L.isValid() && "SourceLocation is invalid.");
201 for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
202 if (Def.getLocation().isInvalid() || // For macros defined on the command line.
203 SM.isBeforeInTranslationUnit(Def.getLocation(), L))
204 return (!Def.isUndefined() ||
205 SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
211 LLVM_DUMP_METHOD void MacroDirective::dump() const {
212 llvm::raw_ostream &Out = llvm::errs();
215 case MD_Define: Out << "DefMacroDirective"; break;
216 case MD_Undefine: Out << "UndefMacroDirective"; break;
217 case MD_Visibility: Out << "VisibilityMacroDirective"; break;
220 // FIXME: Dump SourceLocation.
221 if (auto *Prev = getPrevious())
222 Out << " prev " << Prev;
223 if (IsFromPCH) Out << " from_pch";
225 if (isa<VisibilityMacroDirective>(this))
226 Out << (IsPublic ? " public" : " private");
228 if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
229 if (auto *Info = DMD->getInfo()) {
237 ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
238 IdentifierInfo *II, MacroInfo *Macro,
239 ArrayRef<ModuleMacro *> Overrides) {
240 void *Mem = PP.getPreprocessorAllocator().Allocate(
241 sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
242 alignof(ModuleMacro));
243 return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);