blob: 5416886cc9f79b30529fbe42e940871b0aba8b71 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MacroInfo interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/MacroInfo.h"
Chris Lattner21284df2006-07-08 07:16:08 +000015#include "clang/Lex/Preprocessor.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000016using namespace clang;
Chris Lattnere8eef322006-07-08 07:01:00 +000017
Alexander Kornienko8b3f6232012-08-29 00:20:03 +000018MacroInfo::MacroInfo(SourceLocation DefLoc)
19 : Location(DefLoc),
Craig Topperd2d442c2014-05-17 23:10:59 +000020 ArgumentList(nullptr),
Alexander Kornienko8b3f6232012-08-29 00:20:03 +000021 NumArguments(0),
22 IsDefinitionLengthCached(false),
23 IsFunctionLike(false),
24 IsC99Varargs(false),
25 IsGNUVarargs(false),
26 IsBuiltinMacro(false),
Eli Friedman14d3c792012-11-14 02:18:46 +000027 HasCommaPasting(false),
Alexander Kornienko8b3f6232012-08-29 00:20:03 +000028 IsDisabled(false),
29 IsUsed(false),
30 IsAllowRedefinitionsWithoutWarning(false),
Argyrios Kyrtzidis4f32da12013-03-22 21:12:51 +000031 IsWarnIfUnused(false),
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +000032 FromASTFile(false),
33 UsedForHeaderGuard(false) {
Alexander Kornienkoe61e5622012-09-28 22:24:03 +000034}
35
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000036unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
37 assert(!IsDefinitionLengthCached);
38 IsDefinitionLengthCached = true;
39
40 if (ReplacementTokens.empty())
41 return (DefinitionLength = 0);
42
43 const Token &firstToken = ReplacementTokens.front();
44 const Token &lastToken = ReplacementTokens.back();
45 SourceLocation macroStart = firstToken.getLocation();
46 SourceLocation macroEnd = lastToken.getLocation();
47 assert(macroStart.isValid() && macroEnd.isValid());
48 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
49 "Macro defined in macro?");
50 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
51 "Macro defined in macro?");
52 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000053 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000054 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000055 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000056 assert(startInfo.first == endInfo.first &&
57 "Macro definition spanning multiple FileIDs ?");
58 assert(startInfo.second <= endInfo.second);
59 DefinitionLength = endInfo.second - startInfo.second;
60 DefinitionLength += lastToken.getLength();
61
62 return DefinitionLength;
63}
64
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000065/// \brief Return true if the specified macro definition is equal to
66/// this macro in spelling, arguments, and whitespace.
67///
68/// \param Syntactically if true, the macro definitions can be identical even
69/// if they use different identifiers for the function macro parameters.
70/// Otherwise the comparison is lexical and this implements the rules in
71/// C99 6.10.3.
72bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
73 bool Syntactically) const {
74 bool Lexically = !Syntactically;
75
Chris Lattner6e0d42c2006-07-08 20:32:52 +000076 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +000077 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner564f4782007-07-14 22:46:43 +000078 getNumArgs() != Other.getNumArgs() ||
Chris Lattnercefc7682006-07-08 08:28:12 +000079 isFunctionLike() != Other.isFunctionLike() ||
80 isC99Varargs() != Other.isC99Varargs() ||
81 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +000082 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +000083
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000084 if (Lexically) {
85 // Check arguments.
86 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
87 I != E; ++I, ++OI)
88 if (*I != *OI) return false;
89 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner21284df2006-07-08 07:16:08 +000091 // Check all the tokens.
92 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattner146762e2007-07-20 16:59:19 +000093 const Token &A = ReplacementTokens[i];
94 const Token &B = Other.ReplacementTokens[i];
Chris Lattner794c0012009-03-09 20:33:32 +000095 if (A.getKind() != B.getKind())
96 return false;
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattner794c0012009-03-09 20:33:32 +000098 // If this isn't the first first token, check that the whitespace and
99 // start-of-line characteristics match.
100 if (i != 0 &&
101 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
102 A.hasLeadingSpace() != B.hasLeadingSpace()))
Chris Lattner21284df2006-07-08 07:16:08 +0000103 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner21284df2006-07-08 07:16:08 +0000105 // If this is an identifier, it is easy.
106 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +0000107 if (A.getIdentifierInfo() == B.getIdentifierInfo())
108 continue;
109 if (Lexically)
110 return false;
111 // With syntactic equivalence the parameter names can be different as long
112 // as they are used in the same place.
113 int AArgNum = getArgumentNum(A.getIdentifierInfo());
Argyrios Kyrtzidisf0eaa642013-04-03 21:29:07 +0000114 if (AArgNum == -1)
115 return false;
116 if (AArgNum != Other.getArgumentNum(B.getIdentifierInfo()))
Chris Lattner21284df2006-07-08 07:16:08 +0000117 return false;
118 continue;
119 }
Mike Stump11289f42009-09-09 15:08:12 +0000120
Chris Lattner21284df2006-07-08 07:16:08 +0000121 // Otherwise, check the spelling.
122 if (PP.getSpelling(A) != PP.getSpelling(B))
123 return false;
124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattnere8eef322006-07-08 07:01:00 +0000126 return true;
127}
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000128
Richard Smitha5d2a492014-07-18 04:54:02 +0000129void MacroInfo::dump() const {
130 llvm::raw_ostream &Out = llvm::errs();
131
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 (FromASTFile) Out << " imported";
141 if (UsedForHeaderGuard) Out << " header_guard";
142
143 Out << "\n #define <macro>";
144 if (IsFunctionLike) {
145 Out << "(";
146 for (unsigned I = 0; I != NumArguments; ++I) {
147 if (I) Out << ", ";
148 Out << ArgumentList[I]->getName();
149 }
150 if (IsC99Varargs || IsGNUVarargs) {
151 if (NumArguments && IsC99Varargs) Out << ", ";
152 Out << "...";
153 }
154 Out << ")";
155 }
156
157 for (const Token &Tok : ReplacementTokens) {
158 Out << " ";
159 if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
160 Out << Punc;
161 else if (const char *Kwd = tok::getKeywordSpelling(Tok.getKind()))
162 Out << Kwd;
163 else if (Tok.is(tok::identifier))
164 Out << Tok.getIdentifierInfo()->getName();
165 else if (Tok.isLiteral() && Tok.getLiteralData())
166 Out << StringRef(Tok.getLiteralData(), Tok.getLength());
167 else
168 Out << Tok.getName();
169 }
170}
171
Richard Smith2e87e142014-01-27 23:54:39 +0000172MacroDirective::DefInfo MacroDirective::getDefinition() {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000173 MacroDirective *MD = this;
174 SourceLocation UndefLoc;
175 Optional<bool> isPublic;
176 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000177 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
178 return DefInfo(DefMD, UndefLoc,
179 !isPublic.hasValue() || isPublic.getValue());
180
181 if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
182 UndefLoc = UndefMD->getLocation();
183 continue;
184 }
185
186 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
187 if (!isPublic.hasValue())
188 isPublic = VisMD->isPublic();
189 }
190
Craig Topperd2d442c2014-05-17 23:10:59 +0000191 return DefInfo(nullptr, UndefLoc,
192 !isPublic.hasValue() || isPublic.getValue());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000193}
194
195const MacroDirective::DefInfo
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000196MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
197 assert(L.isValid() && "SourceLocation is invalid.");
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000198 for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
199 if (Def.getLocation().isInvalid() || // For macros defined on the command line.
200 SM.isBeforeInTranslationUnit(Def.getLocation(), L))
201 return (!Def.isUndefined() ||
202 SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
203 ? Def : DefInfo();
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000204 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000205 return DefInfo();
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000206}
Richard Smitha5d2a492014-07-18 04:54:02 +0000207
208void MacroDirective::dump() const {
209 llvm::raw_ostream &Out = llvm::errs();
210
211 switch (getKind()) {
212 case MD_Define: Out << "DefMacroDirective"; break;
213 case MD_Undefine: Out << "UndefMacroDirective"; break;
214 case MD_Visibility: Out << "VisibilityMacroDirective"; break;
215 }
216 Out << " " << this;
217 // FIXME: Dump SourceLocation.
218 if (auto *Prev = getPrevious())
219 Out << " prev " << Prev;
220 if (IsFromPCH) Out << " from_pch";
221 if (IsImported) Out << " imported";
222 if (IsAmbiguous) Out << " ambiguous";
223
224 if (IsPublic)
225 Out << " public";
226 else if (isa<VisibilityMacroDirective>(this))
227 Out << " private";
228
229 if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
230 if (auto *Info = DMD->getInfo()) {
231 Out << "\n ";
232 Info->dump();
233 }
234 }
Richard Smithe657bbd2014-07-18 22:13:40 +0000235 Out << "\n";
Richard Smitha5d2a492014-07-18 04:54:02 +0000236}