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;
26 IsAllowRedefinitionsWithoutWarning = false;
32 MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) {
33 Location = MI.Location;
34 EndLocation = MI.EndLocation;
35 ReplacementTokens = MI.ReplacementTokens;
36 IsFunctionLike = MI.IsFunctionLike;
37 IsC99Varargs = MI.IsC99Varargs;
38 IsGNUVarargs = MI.IsGNUVarargs;
39 IsBuiltinMacro = MI.IsBuiltinMacro;
40 IsFromAST = MI.IsFromAST;
41 IsDisabled = MI.IsDisabled;
43 IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
46 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
49 /// isIdenticalTo - Return true if the specified macro definition is equal to
50 /// this macro in spelling, arguments, and whitespace. This is used to emit
51 /// duplicate definition warnings. This implements the rules in C99 6.10.3.
53 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
54 // Check # tokens in replacement, number of args, and various flags all match.
55 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
56 getNumArgs() != Other.getNumArgs() ||
57 isFunctionLike() != Other.isFunctionLike() ||
58 isC99Varargs() != Other.isC99Varargs() ||
59 isGNUVarargs() != Other.isGNUVarargs())
63 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
65 if (*I != *OI) return false;
67 // Check all the tokens.
68 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
69 const Token &A = ReplacementTokens[i];
70 const Token &B = Other.ReplacementTokens[i];
71 if (A.getKind() != B.getKind())
74 // If this isn't the first first token, check that the whitespace and
75 // start-of-line characteristics match.
77 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
78 A.hasLeadingSpace() != B.hasLeadingSpace()))
81 // If this is an identifier, it is easy.
82 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
83 if (A.getIdentifierInfo() != B.getIdentifierInfo())
88 // Otherwise, check the spelling.
89 if (PP.getSpelling(A) != PP.getSpelling(B))