blob: 434c1200759647802bee3d2be4a1fa43b68b11e3 [file] [log] [blame]
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +00001//===- MacroInfo.cpp - Information about #defined identifiers -------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
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"
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +000015#include "clang/Basic/IdentifierTable.h"
16#include "clang/Basic/LLVM.h"
17#include "clang/Basic/SourceLocation.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/TokenKinds.h"
Chris Lattner21284df2006-07-08 07:16:08 +000020#include "clang/Lex/Preprocessor.h"
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +000021#include "clang/Lex/Token.h"
22#include "llvm/ADT/Optional.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/Casting.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/raw_ostream.h"
27#include <cassert>
28#include <utility>
29
Chris Lattner22eb9722006-06-18 05:43:12 +000030using namespace clang;
Chris Lattnere8eef322006-07-08 07:01:00 +000031
Alexander Kornienko8b3f6232012-08-29 00:20:03 +000032MacroInfo::MacroInfo(SourceLocation DefLoc)
Eugene Zelenko5dc60fe2017-12-04 23:16:21 +000033 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
34 IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
35 HasCommaPasting(false), IsDisabled(false), IsUsed(false),
36 IsAllowRedefinitionsWithoutWarning(false), IsWarnIfUnused(false),
37 UsedForHeaderGuard(false) {}
Alexander Kornienkoe61e5622012-09-28 22:24:03 +000038
Yaron Keren7762b832017-04-27 09:56:39 +000039unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const {
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000040 assert(!IsDefinitionLengthCached);
41 IsDefinitionLengthCached = true;
42
43 if (ReplacementTokens.empty())
44 return (DefinitionLength = 0);
45
46 const Token &firstToken = ReplacementTokens.front();
47 const Token &lastToken = ReplacementTokens.back();
48 SourceLocation macroStart = firstToken.getLocation();
49 SourceLocation macroEnd = lastToken.getLocation();
50 assert(macroStart.isValid() && macroEnd.isValid());
51 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
52 "Macro defined in macro?");
53 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
54 "Macro defined in macro?");
55 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000056 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000057 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000058 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000059 assert(startInfo.first == endInfo.first &&
60 "Macro definition spanning multiple FileIDs ?");
61 assert(startInfo.second <= endInfo.second);
62 DefinitionLength = endInfo.second - startInfo.second;
63 DefinitionLength += lastToken.getLength();
64
65 return DefinitionLength;
66}
67
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000068/// Return true if the specified macro definition is equal to
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000069/// this macro in spelling, arguments, and whitespace.
70///
71/// \param Syntactically if true, the macro definitions can be identical even
72/// if they use different identifiers for the function macro parameters.
73/// Otherwise the comparison is lexical and this implements the rules in
74/// C99 6.10.3.
75bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
76 bool Syntactically) const {
77 bool Lexically = !Syntactically;
78
Chris Lattner6e0d42c2006-07-08 20:32:52 +000079 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +000080 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Faisal Valiac506d72017-07-17 17:18:43 +000081 getNumParams() != Other.getNumParams() ||
Chris Lattnercefc7682006-07-08 08:28:12 +000082 isFunctionLike() != Other.isFunctionLike() ||
83 isC99Varargs() != Other.isC99Varargs() ||
84 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +000085 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +000086
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000087 if (Lexically) {
88 // Check arguments.
Faisal Valiac506d72017-07-17 17:18:43 +000089 for (param_iterator I = param_begin(), OI = Other.param_begin(),
90 E = param_end();
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +000091 I != E; ++I, ++OI)
92 if (*I != *OI) return false;
93 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Chris Lattner21284df2006-07-08 07:16:08 +000095 // Check all the tokens.
96 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattner146762e2007-07-20 16:59:19 +000097 const Token &A = ReplacementTokens[i];
98 const Token &B = Other.ReplacementTokens[i];
Chris Lattner794c0012009-03-09 20:33:32 +000099 if (A.getKind() != B.getKind())
100 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Chris Lattner794c0012009-03-09 20:33:32 +0000102 // If this isn't the first first token, check that the whitespace and
103 // start-of-line characteristics match.
104 if (i != 0 &&
105 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
106 A.hasLeadingSpace() != B.hasLeadingSpace()))
Chris Lattner21284df2006-07-08 07:16:08 +0000107 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner21284df2006-07-08 07:16:08 +0000109 // If this is an identifier, it is easy.
110 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +0000111 if (A.getIdentifierInfo() == B.getIdentifierInfo())
112 continue;
113 if (Lexically)
114 return false;
115 // With syntactic equivalence the parameter names can be different as long
116 // as they are used in the same place.
Faisal Valiac506d72017-07-17 17:18:43 +0000117 int AArgNum = getParameterNum(A.getIdentifierInfo());
Argyrios Kyrtzidisf0eaa642013-04-03 21:29:07 +0000118 if (AArgNum == -1)
119 return false;
Faisal Valiac506d72017-07-17 17:18:43 +0000120 if (AArgNum != Other.getParameterNum(B.getIdentifierInfo()))
Chris Lattner21284df2006-07-08 07:16:08 +0000121 return false;
122 continue;
123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattner21284df2006-07-08 07:16:08 +0000125 // Otherwise, check the spelling.
126 if (PP.getSpelling(A) != PP.getSpelling(B))
127 return false;
128 }
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattnere8eef322006-07-08 07:01:00 +0000130 return true;
131}
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000132
Yaron Kerencdae9412016-01-29 19:38:18 +0000133LLVM_DUMP_METHOD void MacroInfo::dump() const {
Richard Smitha5d2a492014-07-18 04:54:02 +0000134 llvm::raw_ostream &Out = llvm::errs();
135
136 // FIXME: Dump locations.
137 Out << "MacroInfo " << this;
138 if (IsBuiltinMacro) Out << " builtin";
139 if (IsDisabled) Out << " disabled";
140 if (IsUsed) Out << " used";
141 if (IsAllowRedefinitionsWithoutWarning)
142 Out << " allow_redefinitions_without_warning";
143 if (IsWarnIfUnused) Out << " warn_if_unused";
Richard Smitha5d2a492014-07-18 04:54:02 +0000144 if (UsedForHeaderGuard) Out << " header_guard";
145
146 Out << "\n #define <macro>";
147 if (IsFunctionLike) {
148 Out << "(";
Faisal Valiac506d72017-07-17 17:18:43 +0000149 for (unsigned I = 0; I != NumParameters; ++I) {
Richard Smitha5d2a492014-07-18 04:54:02 +0000150 if (I) Out << ", ";
Faisal Valiac506d72017-07-17 17:18:43 +0000151 Out << ParameterList[I]->getName();
Richard Smitha5d2a492014-07-18 04:54:02 +0000152 }
153 if (IsC99Varargs || IsGNUVarargs) {
Faisal Valiac506d72017-07-17 17:18:43 +0000154 if (NumParameters && IsC99Varargs) Out << ", ";
Richard Smitha5d2a492014-07-18 04:54:02 +0000155 Out << "...";
156 }
157 Out << ")";
158 }
159
Richard Smith85f93f32015-11-05 20:55:14 +0000160 bool First = true;
Richard Smitha5d2a492014-07-18 04:54:02 +0000161 for (const Token &Tok : ReplacementTokens) {
Richard Smith85f93f32015-11-05 20:55:14 +0000162 // Leading space is semantically meaningful in a macro definition,
163 // so preserve it in the dump output.
164 if (First || Tok.hasLeadingSpace())
165 Out << " ";
166 First = false;
167
Richard Smitha5d2a492014-07-18 04:54:02 +0000168 if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
169 Out << Punc;
Richard Smitha5d2a492014-07-18 04:54:02 +0000170 else if (Tok.isLiteral() && Tok.getLiteralData())
171 Out << StringRef(Tok.getLiteralData(), Tok.getLength());
Richard Smith85f93f32015-11-05 20:55:14 +0000172 else if (auto *II = Tok.getIdentifierInfo())
173 Out << II->getName();
Richard Smitha5d2a492014-07-18 04:54:02 +0000174 else
175 Out << Tok.getName();
176 }
177}
178
Richard Smith2e87e142014-01-27 23:54:39 +0000179MacroDirective::DefInfo MacroDirective::getDefinition() {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000180 MacroDirective *MD = this;
181 SourceLocation UndefLoc;
182 Optional<bool> isPublic;
183 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000184 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
185 return DefInfo(DefMD, UndefLoc,
186 !isPublic.hasValue() || isPublic.getValue());
187
188 if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
189 UndefLoc = UndefMD->getLocation();
190 continue;
191 }
192
193 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
194 if (!isPublic.hasValue())
195 isPublic = VisMD->isPublic();
196 }
197
Craig Topperd2d442c2014-05-17 23:10:59 +0000198 return DefInfo(nullptr, UndefLoc,
199 !isPublic.hasValue() || isPublic.getValue());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000200}
201
202const MacroDirective::DefInfo
Kristof Umann714a03a2018-10-31 17:19:20 +0000203MacroDirective::findDirectiveAtLoc(SourceLocation L,
204 const SourceManager &SM) const {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000205 assert(L.isValid() && "SourceLocation is invalid.");
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000206 for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
207 if (Def.getLocation().isInvalid() || // For macros defined on the command line.
208 SM.isBeforeInTranslationUnit(Def.getLocation(), L))
209 return (!Def.isUndefined() ||
210 SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
211 ? Def : DefInfo();
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000212 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000213 return DefInfo();
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +0000214}
Richard Smitha5d2a492014-07-18 04:54:02 +0000215
Yaron Kerencdae9412016-01-29 19:38:18 +0000216LLVM_DUMP_METHOD void MacroDirective::dump() const {
Richard Smitha5d2a492014-07-18 04:54:02 +0000217 llvm::raw_ostream &Out = llvm::errs();
218
219 switch (getKind()) {
220 case MD_Define: Out << "DefMacroDirective"; break;
221 case MD_Undefine: Out << "UndefMacroDirective"; break;
222 case MD_Visibility: Out << "VisibilityMacroDirective"; break;
223 }
224 Out << " " << this;
225 // FIXME: Dump SourceLocation.
226 if (auto *Prev = getPrevious())
227 Out << " prev " << Prev;
228 if (IsFromPCH) Out << " from_pch";
Richard Smitha5d2a492014-07-18 04:54:02 +0000229
Richard Smith3ffa61d2015-04-30 23:10:40 +0000230 if (isa<VisibilityMacroDirective>(this))
231 Out << (IsPublic ? " public" : " private");
Richard Smitha5d2a492014-07-18 04:54:02 +0000232
233 if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
234 if (auto *Info = DMD->getInfo()) {
235 Out << "\n ";
236 Info->dump();
237 }
238 }
Richard Smithe657bbd2014-07-18 22:13:40 +0000239 Out << "\n";
Richard Smitha5d2a492014-07-18 04:54:02 +0000240}
Richard Smithe56c8bc2015-04-22 00:26:11 +0000241
Richard Smithb8b2ed62015-04-23 18:18:26 +0000242ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
Richard Smithe56c8bc2015-04-22 00:26:11 +0000243 IdentifierInfo *II, MacroInfo *Macro,
Richard Smith447ed432015-04-23 04:13:52 +0000244 ArrayRef<ModuleMacro *> Overrides) {
245 void *Mem = PP.getPreprocessorAllocator().Allocate(
246 sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000247 alignof(ModuleMacro));
Richard Smithb8b2ed62015-04-23 18:18:26 +0000248 return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);
Richard Smithe56c8bc2015-04-22 00:26:11 +0000249}