1 //===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MacroInfo interface.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/MacroInfo.h"
15 #include "clang/Lex/Preprocessor.h"
16 using namespace clang;
18 MacroInfo::MacroInfo(SourceLocation DefLoc)
20 PreviousDefinition(0),
23 IsDefinitionLengthCached(false),
24 IsFunctionLike(false),
27 IsBuiltinMacro(false),
29 ChangedAfterLoad(false),
32 IsAllowRedefinitionsWithoutWarning(false),
33 IsWarnIfUnused(false),
39 MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator)
40 : Location(MI.Location),
41 EndLocation(MI.EndLocation),
42 UndefLocation(MI.UndefLocation),
43 PreviousDefinition(0),
46 ReplacementTokens(MI.ReplacementTokens),
47 DefinitionLength(MI.DefinitionLength),
48 IsDefinitionLengthCached(MI.IsDefinitionLengthCached),
49 IsFunctionLike(MI.IsFunctionLike),
50 IsC99Varargs(MI.IsC99Varargs),
51 IsGNUVarargs(MI.IsGNUVarargs),
52 IsBuiltinMacro(MI.IsBuiltinMacro),
53 IsFromAST(MI.IsFromAST),
54 ChangedAfterLoad(MI.ChangedAfterLoad),
55 IsDisabled(MI.IsDisabled),
57 IsAllowRedefinitionsWithoutWarning(MI.IsAllowRedefinitionsWithoutWarning),
58 IsWarnIfUnused(MI.IsWarnIfUnused),
59 IsPublic(MI.IsPublic),
60 IsHidden(MI.IsHidden),
61 IsAmbiguous(MI.IsAmbiguous) {
62 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
65 const MacroInfo *MacroInfo::findDefinitionAtLoc(SourceLocation L,
66 SourceManager &SM) const {
67 assert(L.isValid() && "SourceLocation is invalid.");
68 for (const MacroInfo *MI = this; MI; MI = MI->PreviousDefinition) {
69 if (MI->Location.isInvalid() || // For macros defined on the command line.
70 SM.isBeforeInTranslationUnit(MI->Location, L))
71 return (MI->UndefLocation.isInvalid() ||
72 SM.isBeforeInTranslationUnit(L, MI->UndefLocation)) ? MI : NULL;
77 unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
78 assert(!IsDefinitionLengthCached);
79 IsDefinitionLengthCached = true;
81 if (ReplacementTokens.empty())
82 return (DefinitionLength = 0);
84 const Token &firstToken = ReplacementTokens.front();
85 const Token &lastToken = ReplacementTokens.back();
86 SourceLocation macroStart = firstToken.getLocation();
87 SourceLocation macroEnd = lastToken.getLocation();
88 assert(macroStart.isValid() && macroEnd.isValid());
89 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
90 "Macro defined in macro?");
91 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
92 "Macro defined in macro?");
93 std::pair<FileID, unsigned>
94 startInfo = SM.getDecomposedExpansionLoc(macroStart);
95 std::pair<FileID, unsigned>
96 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
97 assert(startInfo.first == endInfo.first &&
98 "Macro definition spanning multiple FileIDs ?");
99 assert(startInfo.second <= endInfo.second);
100 DefinitionLength = endInfo.second - startInfo.second;
101 DefinitionLength += lastToken.getLength();
103 return DefinitionLength;
106 /// isIdenticalTo - Return true if the specified macro definition is equal to
107 /// this macro in spelling, arguments, and whitespace. This is used to emit
108 /// duplicate definition warnings. This implements the rules in C99 6.10.3.
110 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
111 // Check # tokens in replacement, number of args, and various flags all match.
112 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
113 getNumArgs() != Other.getNumArgs() ||
114 isFunctionLike() != Other.isFunctionLike() ||
115 isC99Varargs() != Other.isC99Varargs() ||
116 isGNUVarargs() != Other.isGNUVarargs())
120 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
122 if (*I != *OI) return false;
124 // Check all the tokens.
125 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
126 const Token &A = ReplacementTokens[i];
127 const Token &B = Other.ReplacementTokens[i];
128 if (A.getKind() != B.getKind())
131 // If this isn't the first first token, check that the whitespace and
132 // start-of-line characteristics match.
134 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
135 A.hasLeadingSpace() != B.hasLeadingSpace()))
138 // If this is an identifier, it is easy.
139 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
140 if (A.getIdentifierInfo() != B.getIdentifierInfo())
145 // Otherwise, check the spelling.
146 if (PP.getSpelling(A) != PP.getSpelling(B))