Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +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" |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 16 | #include <iostream> |
| 17 | using namespace llvm; |
| 18 | using namespace clang; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 19 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 20 | MacroInfo::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 Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame^] | 29 | /// SetIdentifierIsMacroArgFlags - Set or clear the "isMacroArg" flags on the |
| 30 | /// identifiers that make up the argument list for this macro. |
| 31 | void MacroInfo::SetIdentifierIsMacroArgFlags(bool Val) const { |
| 32 | for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I) |
| 33 | (*I)->setIsMacroArg(Val); |
| 34 | } |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 36 | /// 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. |
| 39 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame^] | 40 | // Check # tokens in replacement, number of args, and various flags all match. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 41 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame^] | 42 | Arguments.size() != Other.Arguments.size() || |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 43 | isFunctionLike() != Other.isFunctionLike() || |
| 44 | isC99Varargs() != Other.isC99Varargs() || |
| 45 | isGNUVarargs() != Other.isGNUVarargs()) |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 46 | return false; |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame^] | 47 | |
| 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 Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 53 | // 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 Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |