• 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(0),
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 }
33 
getDefinitionLengthSlow(SourceManager & SM) const34 unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
35   assert(!IsDefinitionLengthCached);
36   IsDefinitionLengthCached = true;
37 
38   if (ReplacementTokens.empty())
39     return (DefinitionLength = 0);
40 
41   const Token &firstToken = ReplacementTokens.front();
42   const Token &lastToken = ReplacementTokens.back();
43   SourceLocation macroStart = firstToken.getLocation();
44   SourceLocation macroEnd = lastToken.getLocation();
45   assert(macroStart.isValid() && macroEnd.isValid());
46   assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
47          "Macro defined in macro?");
48   assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
49          "Macro defined in macro?");
50   std::pair<FileID, unsigned>
51       startInfo = SM.getDecomposedExpansionLoc(macroStart);
52   std::pair<FileID, unsigned>
53       endInfo = SM.getDecomposedExpansionLoc(macroEnd);
54   assert(startInfo.first == endInfo.first &&
55          "Macro definition spanning multiple FileIDs ?");
56   assert(startInfo.second <= endInfo.second);
57   DefinitionLength = endInfo.second - startInfo.second;
58   DefinitionLength += lastToken.getLength();
59 
60   return DefinitionLength;
61 }
62 
63 /// isIdenticalTo - Return true if the specified macro definition is equal to
64 /// this macro in spelling, arguments, and whitespace.  This is used to emit
65 /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
66 ///
isIdenticalTo(const MacroInfo & Other,Preprocessor & PP) const67 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
68   // Check # tokens in replacement, number of args, and various flags all match.
69   if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
70       getNumArgs() != Other.getNumArgs() ||
71       isFunctionLike() != Other.isFunctionLike() ||
72       isC99Varargs() != Other.isC99Varargs() ||
73       isGNUVarargs() != Other.isGNUVarargs())
74     return false;
75 
76   // Check arguments.
77   for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
78        I != E; ++I, ++OI)
79     if (*I != *OI) return false;
80 
81   // Check all the tokens.
82   for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
83     const Token &A = ReplacementTokens[i];
84     const Token &B = Other.ReplacementTokens[i];
85     if (A.getKind() != B.getKind())
86       return false;
87 
88     // If this isn't the first first token, check that the whitespace and
89     // start-of-line characteristics match.
90     if (i != 0 &&
91         (A.isAtStartOfLine() != B.isAtStartOfLine() ||
92          A.hasLeadingSpace() != B.hasLeadingSpace()))
93       return false;
94 
95     // If this is an identifier, it is easy.
96     if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
97       if (A.getIdentifierInfo() != B.getIdentifierInfo())
98         return false;
99       continue;
100     }
101 
102     // Otherwise, check the spelling.
103     if (PP.getSpelling(A) != PP.getSpelling(B))
104       return false;
105   }
106 
107   return true;
108 }
109 
110 const MacroDirective *
findDirectiveAtLoc(SourceLocation L,SourceManager & SM) const111 MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
112   assert(L.isValid() && "SourceLocation is invalid.");
113   for (const MacroDirective *MD = this; MD; MD = MD->Previous) {
114     if (MD->getLocation().isInvalid() ||  // For macros defined on the command line.
115         SM.isBeforeInTranslationUnit(MD->getLocation(), L))
116       return (MD->UndefLocation.isInvalid() ||
117               SM.isBeforeInTranslationUnit(L, MD->UndefLocation)) ? MD : NULL;
118   }
119   return NULL;
120 }
121