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 ArgumentList(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 getNumArgs() != Other.getNumArgs() ||
78 isFunctionLike() != Other.isFunctionLike() ||
79 isC99Varargs() != Other.isC99Varargs() ||
80 isGNUVarargs() != Other.isGNUVarargs())
85 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
87 if (*I != *OI) return false;
90 // Check all the tokens.
91 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
92 const Token &A = ReplacementTokens[i];
93 const Token &B = Other.ReplacementTokens[i];
94 if (A.getKind() != B.getKind())
97 // If this isn't the first first token, check that the whitespace and
98 // start-of-line characteristics match.
100 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
101 A.hasLeadingSpace() != B.hasLeadingSpace()))
104 // If this is an identifier, it is easy.
105 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
106 if (A.getIdentifierInfo() == B.getIdentifierInfo())
110 // With syntactic equivalence the parameter names can be different as long
111 // as they are used in the same place.
112 int AArgNum = getArgumentNum(A.getIdentifierInfo());
115 if (AArgNum != Other.getArgumentNum(B.getIdentifierInfo()))
120 // Otherwise, check the spelling.
121 if (PP.getSpelling(A) != PP.getSpelling(B))
128 LLVM_DUMP_METHOD void MacroInfo::dump() const {
129 llvm::raw_ostream &Out = llvm::errs();
131 // FIXME: Dump locations.
132 Out << "MacroInfo " << this;
133 if (IsBuiltinMacro) Out << " builtin";
134 if (IsDisabled) Out << " disabled";
135 if (IsUsed) Out << " used";
136 if (IsAllowRedefinitionsWithoutWarning)
137 Out << " allow_redefinitions_without_warning";
138 if (IsWarnIfUnused) Out << " warn_if_unused";
139 if (UsedForHeaderGuard) Out << " header_guard";
141 Out << "\n #define <macro>";
142 if (IsFunctionLike) {
144 for (unsigned I = 0; I != NumArguments; ++I) {
146 Out << ArgumentList[I]->getName();
148 if (IsC99Varargs || IsGNUVarargs) {
149 if (NumArguments && IsC99Varargs) Out << ", ";
156 for (const Token &Tok : ReplacementTokens) {
157 // Leading space is semantically meaningful in a macro definition,
158 // so preserve it in the dump output.
159 if (First || Tok.hasLeadingSpace())
163 if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
165 else if (Tok.isLiteral() && Tok.getLiteralData())
166 Out << StringRef(Tok.getLiteralData(), Tok.getLength());
167 else if (auto *II = Tok.getIdentifierInfo())
168 Out << II->getName();
170 Out << Tok.getName();
174 MacroDirective::DefInfo MacroDirective::getDefinition() {
175 MacroDirective *MD = this;
176 SourceLocation UndefLoc;
177 Optional<bool> isPublic;
178 for (; MD; MD = MD->getPrevious()) {
179 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
180 return DefInfo(DefMD, UndefLoc,
181 !isPublic.hasValue() || isPublic.getValue());
183 if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
184 UndefLoc = UndefMD->getLocation();
188 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
189 if (!isPublic.hasValue())
190 isPublic = VisMD->isPublic();
193 return DefInfo(nullptr, UndefLoc,
194 !isPublic.hasValue() || isPublic.getValue());
197 const MacroDirective::DefInfo
198 MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
199 assert(L.isValid() && "SourceLocation is invalid.");
200 for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
201 if (Def.getLocation().isInvalid() || // For macros defined on the command line.
202 SM.isBeforeInTranslationUnit(Def.getLocation(), L))
203 return (!Def.isUndefined() ||
204 SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
210 LLVM_DUMP_METHOD void MacroDirective::dump() const {
211 llvm::raw_ostream &Out = llvm::errs();
214 case MD_Define: Out << "DefMacroDirective"; break;
215 case MD_Undefine: Out << "UndefMacroDirective"; break;
216 case MD_Visibility: Out << "VisibilityMacroDirective"; break;
219 // FIXME: Dump SourceLocation.
220 if (auto *Prev = getPrevious())
221 Out << " prev " << Prev;
222 if (IsFromPCH) Out << " from_pch";
224 if (isa<VisibilityMacroDirective>(this))
225 Out << (IsPublic ? " public" : " private");
227 if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
228 if (auto *Info = DMD->getInfo()) {
236 ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
237 IdentifierInfo *II, MacroInfo *Macro,
238 ArrayRef<ModuleMacro *> Overrides) {
239 void *Mem = PP.getPreprocessorAllocator().Allocate(
240 sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
241 alignof(ModuleMacro));
242 return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);