blob: 8b3b87e42a6881b65b47b879c0b4ba3e5d133afd [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 +000016#include <iostream>
17using namespace llvm;
18using namespace clang;
Chris Lattnere8eef322006-07-08 07:01:00 +000019
Chris Lattnercefc7682006-07-08 08:28:12 +000020MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc) {
21 IsFunctionLike = false;
22 IsC99Varargs = false;
23 IsGNUVarargs = false;
24 IsBuiltinMacro = false;
25 IsDisabled = false;
26 IsUsed = true;
27}
28
Chris Lattner6e0d42c2006-07-08 20:32:52 +000029/// SetIdentifierIsMacroArgFlags - Set or clear the "isMacroArg" flags on the
30/// identifiers that make up the argument list for this macro.
31void MacroInfo::SetIdentifierIsMacroArgFlags(bool Val) const {
32 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
33 (*I)->setIsMacroArg(Val);
34}
Chris Lattnercefc7682006-07-08 08:28:12 +000035
Chris Lattner21284df2006-07-08 07:16:08 +000036/// isIdenticalTo - Return true if the specified macro definition is equal to
37/// this macro in spelling, arguments, and whitespace. This is used to emit
38/// duplicate definition warnings. This implements the rules in C99 6.10.3.
39bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
Chris Lattner6e0d42c2006-07-08 20:32:52 +000040 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +000041 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner6e0d42c2006-07-08 20:32:52 +000042 Arguments.size() != Other.Arguments.size() ||
Chris Lattnercefc7682006-07-08 08:28:12 +000043 isFunctionLike() != Other.isFunctionLike() ||
44 isC99Varargs() != Other.isC99Varargs() ||
45 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +000046 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +000047
48 // Check arguments.
49 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
50 I != E; ++I, ++OI)
51 if (*I != *OI) return false;
52
Chris Lattner21284df2006-07-08 07:16:08 +000053 // Check all the tokens.
54 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
55 const LexerToken &A = ReplacementTokens[i];
56 const LexerToken &B = Other.ReplacementTokens[i];
57 if (A.getKind() != B.getKind() ||
58 A.isAtStartOfLine() != B.isAtStartOfLine() ||
59 A.hasLeadingSpace() != B.hasLeadingSpace())
60 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}