• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the MacroInfo interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Lex/MacroInfo.h"
15 #include "clang/Lex/Preprocessor.h"
16 using namespace clang;
17 
MacroInfo(SourceLocation DefLoc)18 MacroInfo::MacroInfo(SourceLocation DefLoc)
19   : Location(DefLoc),
20     ArgumentList(nullptr),
21     NumArguments(0),
22     IsDefinitionLengthCached(false),
23     IsFunctionLike(false),
24     IsC99Varargs(false),
25     IsGNUVarargs(false),
26     IsBuiltinMacro(false),
27     HasCommaPasting(false),
28     IsDisabled(false),
29     IsUsed(false),
30     IsAllowRedefinitionsWithoutWarning(false),
31     IsWarnIfUnused(false),
32     FromASTFile(false),
33     UsedForHeaderGuard(false) {
34 }
35 
getDefinitionLengthSlow(SourceManager & SM) const36 unsigned 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>
53       startInfo = SM.getDecomposedExpansionLoc(macroStart);
54   std::pair<FileID, unsigned>
55       endInfo = SM.getDecomposedExpansionLoc(macroEnd);
56   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 
65 /// \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.
isIdenticalTo(const MacroInfo & Other,Preprocessor & PP,bool Syntactically) const72 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
73                               bool Syntactically) const {
74   bool Lexically = !Syntactically;
75 
76   // Check # tokens in replacement, number of args, and various flags all match.
77   if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
78       getNumArgs() != Other.getNumArgs() ||
79       isFunctionLike() != Other.isFunctionLike() ||
80       isC99Varargs() != Other.isC99Varargs() ||
81       isGNUVarargs() != Other.isGNUVarargs())
82     return false;
83 
84   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   }
90 
91   // Check all the tokens.
92   for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
93     const Token &A = ReplacementTokens[i];
94     const Token &B = Other.ReplacementTokens[i];
95     if (A.getKind() != B.getKind())
96       return false;
97 
98     // 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()))
103       return false;
104 
105     // If this is an identifier, it is easy.
106     if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
107       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());
114       if (AArgNum == -1)
115         return false;
116       if (AArgNum != Other.getArgumentNum(B.getIdentifierInfo()))
117         return false;
118       continue;
119     }
120 
121     // Otherwise, check the spelling.
122     if (PP.getSpelling(A) != PP.getSpelling(B))
123       return false;
124   }
125 
126   return true;
127 }
128 
getDefinition()129 MacroDirective::DefInfo MacroDirective::getDefinition() {
130   MacroDirective *MD = this;
131   SourceLocation UndefLoc;
132   Optional<bool> isPublic;
133   for (; MD; MD = MD->getPrevious()) {
134     if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
135       return DefInfo(DefMD, UndefLoc,
136                      !isPublic.hasValue() || isPublic.getValue());
137 
138     if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
139       UndefLoc = UndefMD->getLocation();
140       continue;
141     }
142 
143     VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
144     if (!isPublic.hasValue())
145       isPublic = VisMD->isPublic();
146   }
147 
148   return DefInfo(nullptr, UndefLoc,
149                  !isPublic.hasValue() || isPublic.getValue());
150 }
151 
152 const MacroDirective::DefInfo
findDirectiveAtLoc(SourceLocation L,SourceManager & SM) const153 MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
154   assert(L.isValid() && "SourceLocation is invalid.");
155   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();
161   }
162   return DefInfo();
163 }
164