Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- MacroInfo.cpp - Information about #defined identifiers -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MacroInfo interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/MacroInfo.h" |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | using namespace clang; |
| 17 | |
| 18 | MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc) { |
| 19 | IsFunctionLike = false; |
| 20 | IsC99Varargs = false; |
| 21 | IsGNUVarargs = false; |
| 22 | IsBuiltinMacro = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | IsDisabled = false; |
| 24 | IsUsed = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 26 | ArgumentList = 0; |
| 27 | NumArguments = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /// 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. |
| 33 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { |
| 35 | // Check # tokens in replacement, number of args, and various flags all match. |
| 36 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 37 | getNumArgs() != Other.getNumArgs() || |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | isFunctionLike() != Other.isFunctionLike() || |
| 39 | isC99Varargs() != Other.isC99Varargs() || |
| 40 | isGNUVarargs() != Other.isGNUVarargs()) |
| 41 | return false; |
| 42 | |
| 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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | // Check all the tokens. |
| 49 | for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 50 | const Token &A = ReplacementTokens[i]; |
| 51 | const Token &B = Other.ReplacementTokens[i]; |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 52 | if (A.getKind() != B.getKind()) |
| 53 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 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())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | // Otherwise, check the spelling. |
| 70 | if (PP.getSpelling(A) != PP.getSpelling(B)) |
| 71 | return false; |
| 72 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |