blob: a9a8ad7fd6d313549320a5e9f2c26c72f4b5f5ca [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattner21284df2006-07-08 07:16:08 +000015#include "clang/Lex/Preprocessor.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000016using namespace clang;
Chris Lattnere8eef322006-07-08 07:01:00 +000017
Chris Lattnercefc7682006-07-08 08:28:12 +000018MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc) {
19 IsFunctionLike = false;
20 IsC99Varargs = false;
21 IsGNUVarargs = false;
22 IsBuiltinMacro = false;
Chris Lattner81278c62006-10-14 19:03:49 +000023 IsTargetSpecific = false;
Chris Lattnercefc7682006-07-08 08:28:12 +000024 IsDisabled = false;
25 IsUsed = true;
Chris Lattner564f4782007-07-14 22:46:43 +000026
27 ArgumentList = 0;
28 NumArguments = 0;
Chris Lattnercefc7682006-07-08 08:28:12 +000029}
30
Chris Lattner21284df2006-07-08 07:16:08 +000031/// isIdenticalTo - Return true if the specified macro definition is equal to
32/// this macro in spelling, arguments, and whitespace. This is used to emit
33/// duplicate definition warnings. This implements the rules in C99 6.10.3.
Chris Lattner81278c62006-10-14 19:03:49 +000034///
35/// Note that this intentionally does not check isTargetSpecific for matching.
36///
Chris Lattner21284df2006-07-08 07:16:08 +000037bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
Chris Lattner6e0d42c2006-07-08 20:32:52 +000038 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +000039 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner564f4782007-07-14 22:46:43 +000040 getNumArgs() != Other.getNumArgs() ||
Chris Lattnercefc7682006-07-08 08:28:12 +000041 isFunctionLike() != Other.isFunctionLike() ||
42 isC99Varargs() != Other.isC99Varargs() ||
43 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +000044 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +000045
46 // Check arguments.
47 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
48 I != E; ++I, ++OI)
49 if (*I != *OI) return false;
50
Chris Lattner21284df2006-07-08 07:16:08 +000051 // Check all the tokens.
52 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
53 const LexerToken &A = ReplacementTokens[i];
54 const LexerToken &B = Other.ReplacementTokens[i];
55 if (A.getKind() != B.getKind() ||
56 A.isAtStartOfLine() != B.isAtStartOfLine() ||
57 A.hasLeadingSpace() != B.hasLeadingSpace())
58 return false;
59
60 // If this is an identifier, it is easy.
61 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
62 if (A.getIdentifierInfo() != B.getIdentifierInfo())
63 return false;
64 continue;
65 }
66
67 // Otherwise, check the spelling.
68 if (PP.getSpelling(A) != PP.getSpelling(B))
69 return false;
70 }
71
Chris Lattnere8eef322006-07-08 07:01:00 +000072 return true;
73}