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 | // |
| 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" |
| 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; |
| 23 | IsTargetSpecific = false; |
| 24 | IsDisabled = false; |
| 25 | IsUsed = true; |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 26 | |
| 27 | ArgumentList = 0; |
| 28 | NumArguments = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /// 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. |
| 34 | /// |
| 35 | /// Note that this intentionally does not check isTargetSpecific for matching. |
| 36 | /// |
| 37 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { |
| 38 | // Check # tokens in replacement, number of args, and various flags all match. |
| 39 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 40 | getNumArgs() != Other.getNumArgs() || |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | isFunctionLike() != Other.isFunctionLike() || |
| 42 | isC99Varargs() != Other.isC99Varargs() || |
| 43 | isGNUVarargs() != Other.isGNUVarargs()) |
| 44 | return false; |
| 45 | |
| 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 | |
| 51 | // Check all the tokens. |
| 52 | for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 53 | const Token &A = ReplacementTokens[i]; |
| 54 | const Token &B = Other.ReplacementTokens[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | |
| 72 | return true; |
| 73 | } |