blob: df89450f5a5557b32ee9cf3480e238c8915ee479 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
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;
23 IsDisabled = false;
24 IsUsed = true;
Chris Lattner564f4782007-07-14 22:46:43 +000025
26 ArgumentList = 0;
27 NumArguments = 0;
Chris Lattnercefc7682006-07-08 08:28:12 +000028}
29
Chris Lattner21284df2006-07-08 07:16:08 +000030/// isIdenticalTo - Return true if the specified macro definition is equal to
31/// this macro in spelling, arguments, and whitespace. This is used to emit
32/// duplicate definition warnings. This implements the rules in C99 6.10.3.
Chris Lattner81278c62006-10-14 19:03:49 +000033///
Chris Lattner21284df2006-07-08 07:16:08 +000034bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
Chris Lattner6e0d42c2006-07-08 20:32:52 +000035 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +000036 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner564f4782007-07-14 22:46:43 +000037 getNumArgs() != Other.getNumArgs() ||
Chris Lattnercefc7682006-07-08 08:28:12 +000038 isFunctionLike() != Other.isFunctionLike() ||
39 isC99Varargs() != Other.isC99Varargs() ||
40 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +000041 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +000042
43 // Check arguments.
44 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
45 I != E; ++I, ++OI)
46 if (*I != *OI) return false;
47
Chris Lattner21284df2006-07-08 07:16:08 +000048 // Check all the tokens.
49 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattner146762e2007-07-20 16:59:19 +000050 const Token &A = ReplacementTokens[i];
51 const Token &B = Other.ReplacementTokens[i];
Chris Lattner794c0012009-03-09 20:33:32 +000052 if (A.getKind() != B.getKind())
53 return false;
54
55 // If this isn't the first first token, check that the whitespace and
56 // start-of-line characteristics match.
57 if (i != 0 &&
58 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
59 A.hasLeadingSpace() != B.hasLeadingSpace()))
Chris Lattner21284df2006-07-08 07:16:08 +000060 return false;
61
62 // If this is an identifier, it is easy.
63 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
64 if (A.getIdentifierInfo() != B.getIdentifierInfo())
65 return false;
66 continue;
67 }
68
69 // Otherwise, check the spelling.
70 if (PP.getSpelling(A) != PP.getSpelling(B))
71 return false;
72 }
73
Chris Lattnere8eef322006-07-08 07:01:00 +000074 return true;
75}