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) : Location(DefLoc) {
19 IsFunctionLike = false;
22 IsBuiltinMacro = false;
24 ChangedAfterLoad = false;
27 IsAllowRedefinitionsWithoutWarning = false;
28 IsWarnIfUnused = false;
29 IsDefinitionLengthCached = false;
36 MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) {
37 Location = MI.Location;
38 EndLocation = MI.EndLocation;
39 ReplacementTokens = MI.ReplacementTokens;
40 IsFunctionLike = MI.IsFunctionLike;
41 IsC99Varargs = MI.IsC99Varargs;
42 IsGNUVarargs = MI.IsGNUVarargs;
43 IsBuiltinMacro = MI.IsBuiltinMacro;
44 IsFromAST = MI.IsFromAST;
45 ChangedAfterLoad = MI.ChangedAfterLoad;
46 IsDisabled = MI.IsDisabled;
48 IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
49 IsWarnIfUnused = MI.IsWarnIfUnused;
50 IsDefinitionLengthCached = MI.IsDefinitionLengthCached;
51 DefinitionLength = MI.DefinitionLength;
52 IsPublic = MI.IsPublic;
56 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
59 unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
60 assert(!IsDefinitionLengthCached);
61 IsDefinitionLengthCached = true;
63 if (ReplacementTokens.empty())
64 return (DefinitionLength = 0);
66 const Token &firstToken = ReplacementTokens.front();
67 const Token &lastToken = ReplacementTokens.back();
68 SourceLocation macroStart = firstToken.getLocation();
69 SourceLocation macroEnd = lastToken.getLocation();
70 assert(macroStart.isValid() && macroEnd.isValid());
71 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
72 "Macro defined in macro?");
73 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
74 "Macro defined in macro?");
75 std::pair<FileID, unsigned>
76 startInfo = SM.getDecomposedExpansionLoc(macroStart);
77 std::pair<FileID, unsigned>
78 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
79 assert(startInfo.first == endInfo.first &&
80 "Macro definition spanning multiple FileIDs ?");
81 assert(startInfo.second <= endInfo.second);
82 DefinitionLength = endInfo.second - startInfo.second;
83 DefinitionLength += lastToken.getLength();
85 return DefinitionLength;
88 /// isIdenticalTo - Return true if the specified macro definition is equal to
89 /// this macro in spelling, arguments, and whitespace. This is used to emit
90 /// duplicate definition warnings. This implements the rules in C99 6.10.3.
92 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
93 // Check # tokens in replacement, number of args, and various flags all match.
94 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
95 getNumArgs() != Other.getNumArgs() ||
96 isFunctionLike() != Other.isFunctionLike() ||
97 isC99Varargs() != Other.isC99Varargs() ||
98 isGNUVarargs() != Other.isGNUVarargs())
102 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
104 if (*I != *OI) return false;
106 // Check all the tokens.
107 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
108 const Token &A = ReplacementTokens[i];
109 const Token &B = Other.ReplacementTokens[i];
110 if (A.getKind() != B.getKind())
113 // If this isn't the first first token, check that the whitespace and
114 // start-of-line characteristics match.
116 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
117 A.hasLeadingSpace() != B.hasLeadingSpace()))
120 // If this is an identifier, it is easy.
121 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
122 if (A.getIdentifierInfo() != B.getIdentifierInfo())
127 // Otherwise, check the spelling.
128 if (PP.getSpelling(A) != PP.getSpelling(B))