blob: 5abafe1cb76d22ab935d2bf01c92b3e9f403052e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MacroInfo interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/MacroInfo.h"
15#include "clang/Lex/Preprocessor.h"
16using namespace clang;
17
Alexander Kornienko8a64bb52012-08-29 00:20:03 +000018MacroInfo::MacroInfo(SourceLocation DefLoc)
19 : Location(DefLoc),
Alexander Kornienko8a64bb52012-08-29 00:20:03 +000020 ArgumentList(0),
21 NumArguments(0),
22 IsDefinitionLengthCached(false),
23 IsFunctionLike(false),
24 IsC99Varargs(false),
25 IsGNUVarargs(false),
26 IsBuiltinMacro(false),
Eli Friedman4fa4b482012-11-14 02:18:46 +000027 HasCommaPasting(false),
Alexander Kornienko8a64bb52012-08-29 00:20:03 +000028 IsDisabled(false),
29 IsUsed(false),
30 IsAllowRedefinitionsWithoutWarning(false),
Argyrios Kyrtzidisbaa74bd2013-03-22 21:12:51 +000031 IsWarnIfUnused(false),
32 FromASTFile(false) {
Alexander Kornienko66da0ab2012-09-28 22:24:03 +000033}
34
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000035unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
36 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 Carruthe7b2b6e2011-07-25 20:52:32 +000052 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000053 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000054 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidisb73377e2011-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 Kyrtzidisbd25ff82013-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
Reid Spencer5f016e22007-07-11 17:01:13 +000075 // Check # tokens in replacement, number of args, and various flags all match.
76 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000077 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000078 isFunctionLike() != Other.isFunctionLike() ||
79 isC99Varargs() != Other.isC99Varargs() ||
80 isGNUVarargs() != Other.isGNUVarargs())
81 return false;
82
Argyrios Kyrtzidisbd25ff82013-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 Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Check all the tokens.
91 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +000092 const Token &A = ReplacementTokens[i];
93 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +000094 if (A.getKind() != B.getKind())
95 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner688a2482009-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()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 // If this is an identifier, it is easy.
105 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
Argyrios Kyrtzidisbd25ff82013-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());
113 int BArgNum = Other.getArgumentNum(B.getIdentifierInfo());
114 if (AArgNum == -1 || AArgNum != BArgNum)
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 return false;
116 continue;
117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // Otherwise, check the spelling.
120 if (PP.getSpelling(A) != PP.getSpelling(B))
121 return false;
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 return true;
125}
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000126
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000127MacroDirective::DefInfo MacroDirective::getDefinition(bool AllowHidden) {
128 MacroDirective *MD = this;
129 SourceLocation UndefLoc;
130 Optional<bool> isPublic;
131 for (; MD; MD = MD->getPrevious()) {
132 if (!AllowHidden && MD->isHidden())
133 continue;
134
135 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
136 return DefInfo(DefMD, UndefLoc,
137 !isPublic.hasValue() || isPublic.getValue());
138
139 if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
140 UndefLoc = UndefMD->getLocation();
141 continue;
142 }
143
144 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
145 if (!isPublic.hasValue())
146 isPublic = VisMD->isPublic();
147 }
148
149 return DefInfo();
150}
151
152const MacroDirective::DefInfo
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000153MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
154 assert(L.isValid() && "SourceLocation is invalid.");
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000155 for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
156 if (Def.getLocation().isInvalid() || // For macros defined on the command line.
157 SM.isBeforeInTranslationUnit(Def.getLocation(), L))
158 return (!Def.isUndefined() ||
159 SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
160 ? Def : DefInfo();
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000161 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000162 return DefInfo();
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000163}