blob: 1e5deeb1919b5383fd1c4eeae992dcd4c80fda3d [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 UsedForHeaderGuard(false) {
Alexander Kornienkoe61e5622012-09-28 22:24:03 +000033}
34
Yaron Keren7762b832017-04-27 09:56:39 +000035unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const {
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000036 assert(!IsDefinitionLengthCached);
37 IsDefinitionLengthCached = true;
38
39 if (ReplacementTokens.empty())
40 return (DefinitionLength = 0);
41
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>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000052 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000053 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000054 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000055 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();
60
61 return DefinitionLength;
62}
63
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000064/// \brief Return true if the specified macro definition is equal to
65/// this macro in spelling, arguments, and whitespace.
66///
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
70/// C99 6.10.3.
71bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
72 bool Syntactically) const {
73 bool Lexically = !Syntactically;
74
Chris Lattner6e0d42c2006-07-08 20:32:52 +000075 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +000076 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner564f4782007-07-14 22:46:43 +000077 getNumArgs() != Other.getNumArgs() ||
Chris Lattnercefc7682006-07-08 08:28:12 +000078 isFunctionLike() != Other.isFunctionLike() ||
79 isC99Varargs() != Other.isC99Varargs() ||
80 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +000081 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +000082
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000083 if (Lexically) {
84 // Check arguments.
85 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
86 I != E; ++I, ++OI)
87 if (*I != *OI) return false;
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner21284df2006-07-08 07:16:08 +000090 // Check all the tokens.
91 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattner146762e2007-07-20 16:59:19 +000092 const Token &A = ReplacementTokens[i];
93 const Token &B = Other.ReplacementTokens[i];
Chris Lattner794c0012009-03-09 20:33:32 +000094 if (A.getKind() != B.getKind())
95 return false;
Mike Stump11289f42009-09-09 15:08:12 +000096
Chris Lattner794c0012009-03-09 20:33:32 +000097 // If this isn't the first first token, check that the whitespace and
98 // start-of-line characteristics match.
99 if (i != 0 &&
100 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
101 A.hasLeadingSpace() != B.hasLeadingSpace()))
Chris Lattner21284df2006-07-08 07:16:08 +0000102 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner21284df2006-07-08 07:16:08 +0000104 // If this is an identifier, it is easy.
105 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +0000106 if (A.getIdentifierInfo() == B.getIdentifierInfo())
107 continue;
108 if (Lexically)
109 return false;
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());
Argyrios Kyrtzidisf0eaa642013-04-03 21:29:07 +0000113 if (AArgNum == -1)
114 return false;
115 if (AArgNum != Other.getArgumentNum(B.getIdentifierInfo()))
Chris Lattner21284df2006-07-08 07:16:08 +0000116 return false;
117 continue;
118 }
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner21284df2006-07-08 07:16:08 +0000120 // Otherwise, check the spelling.
121 if (PP.getSpelling(A) != PP.getSpelling(B))
122 return false;
123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattnere8eef322006-07-08 07:01:00 +0000125 return true;
126}
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000127
Yaron Kerencdae9412016-01-29 19:38:18 +0000128LLVM_DUMP_METHOD void MacroInfo::dump() const {
Richard Smitha5d2a492014-07-18 04:54:02 +0000129 llvm::raw_ostream &Out = llvm::errs();
130
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";
Richard Smitha5d2a492014-07-18 04:54:02 +0000139 if (UsedForHeaderGuard) Out << " header_guard";
140
141 Out << "\n #define <macro>";
142 if (IsFunctionLike) {
143 Out << "(";
144 for (unsigned I = 0; I != NumArguments; ++I) {
145 if (I) Out << ", ";
146 Out << ArgumentList[I]->getName();
147 }
148 if (IsC99Varargs || IsGNUVarargs) {
149 if (NumArguments && IsC99Varargs) Out << ", ";
150 Out << "...";
151 }
152 Out << ")";
153 }
154
Richard Smith85f93f32015-11-05 20:55:14 +0000155 bool First = true;
Richard Smitha5d2a492014-07-18 04:54:02 +0000156 for (const Token &Tok : ReplacementTokens) {
Richard Smith85f93f32015-11-05 20:55:14 +0000157 // Leading space is semantically meaningful in a macro definition,
158 // so preserve it in the dump output.
159 if (First || Tok.hasLeadingSpace())
160 Out << " ";
161 First = false;
162
Richard Smitha5d2a492014-07-18 04:54:02 +0000163 if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
164 Out << Punc;
Richard Smitha5d2a492014-07-18 04:54:02 +0000165 else if (Tok.isLiteral() && Tok.getLiteralData())
166 Out << StringRef(Tok.getLiteralData(), Tok.getLength());
Richard Smith85f93f32015-11-05 20:55:14 +0000167 else if (auto *II = Tok.getIdentifierInfo())
168 Out << II->getName();
Richard Smitha5d2a492014-07-18 04:54:02 +0000169 else
170 Out << Tok.getName();
171 }
172}
173
Richard Smith2e87e142014-01-27 23:54:39 +0000174MacroDirective::DefInfo MacroDirective::getDefinition() {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000175 MacroDirective *MD = this;
176 SourceLocation UndefLoc;
177 Optional<bool> isPublic;
178 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000179 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
180 return DefInfo(DefMD, UndefLoc,
181 !isPublic.hasValue() || isPublic.getValue());
182
183 if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
184 UndefLoc = UndefMD->getLocation();
185 continue;
186 }
187
188 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
189 if (!isPublic.hasValue())
190 isPublic = VisMD->isPublic();
191 }
192
Craig Topperd2d442c2014-05-17 23:10:59 +0000193 return DefInfo(nullptr, UndefLoc,
194 !isPublic.hasValue() || isPublic.getValue());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000195}
196
197const MacroDirective::DefInfo
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000198MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
199 assert(L.isValid() && "SourceLocation is invalid.");
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000200 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()))
205 ? Def : DefInfo();
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000206 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000207 return DefInfo();
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000208}
Richard Smitha5d2a492014-07-18 04:54:02 +0000209
Yaron Kerencdae9412016-01-29 19:38:18 +0000210LLVM_DUMP_METHOD void MacroDirective::dump() const {
Richard Smitha5d2a492014-07-18 04:54:02 +0000211 llvm::raw_ostream &Out = llvm::errs();
212
213 switch (getKind()) {
214 case MD_Define: Out << "DefMacroDirective"; break;
215 case MD_Undefine: Out << "UndefMacroDirective"; break;
216 case MD_Visibility: Out << "VisibilityMacroDirective"; break;
217 }
218 Out << " " << this;
219 // FIXME: Dump SourceLocation.
220 if (auto *Prev = getPrevious())
221 Out << " prev " << Prev;
222 if (IsFromPCH) Out << " from_pch";
Richard Smitha5d2a492014-07-18 04:54:02 +0000223
Richard Smith3ffa61d2015-04-30 23:10:40 +0000224 if (isa<VisibilityMacroDirective>(this))
225 Out << (IsPublic ? " public" : " private");
Richard Smitha5d2a492014-07-18 04:54:02 +0000226
227 if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
228 if (auto *Info = DMD->getInfo()) {
229 Out << "\n ";
230 Info->dump();
231 }
232 }
Richard Smithe657bbd2014-07-18 22:13:40 +0000233 Out << "\n";
Richard Smitha5d2a492014-07-18 04:54:02 +0000234}
Richard Smithe56c8bc2015-04-22 00:26:11 +0000235
Richard Smithb8b2ed62015-04-23 18:18:26 +0000236ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
Richard Smithe56c8bc2015-04-22 00:26:11 +0000237 IdentifierInfo *II, MacroInfo *Macro,
Richard Smith447ed432015-04-23 04:13:52 +0000238 ArrayRef<ModuleMacro *> Overrides) {
239 void *Mem = PP.getPreprocessorAllocator().Allocate(
240 sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000241 alignof(ModuleMacro));
Richard Smithb8b2ed62015-04-23 18:18:26 +0000242 return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);
Richard Smithe56c8bc2015-04-22 00:26:11 +0000243}