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; |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame^] | 23 | IsFromAST = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | IsDisabled = false; |
| 25 | IsUsed = true; |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 26 | IsAllowRedefinitionsWithoutWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 28 | ArgumentList = 0; |
| 29 | NumArguments = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 32 | MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) { |
| 33 | Location = MI.Location; |
| 34 | EndLocation = MI.EndLocation; |
| 35 | ReplacementTokens = MI.ReplacementTokens; |
| 36 | IsFunctionLike = MI.IsFunctionLike; |
| 37 | IsC99Varargs = MI.IsC99Varargs; |
| 38 | IsGNUVarargs = MI.IsGNUVarargs; |
| 39 | IsBuiltinMacro = MI.IsBuiltinMacro; |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame^] | 40 | IsFromAST = MI.IsFromAST; |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 41 | IsDisabled = MI.IsDisabled; |
| 42 | IsUsed = MI.IsUsed; |
| 43 | IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning; |
| 44 | ArgumentList = 0; |
| 45 | NumArguments = 0; |
| 46 | setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator); |
| 47 | } |
| 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | /// isIdenticalTo - Return true if the specified macro definition is equal to |
| 50 | /// this macro in spelling, arguments, and whitespace. This is used to emit |
| 51 | /// duplicate definition warnings. This implements the rules in C99 6.10.3. |
| 52 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { |
| 54 | // Check # tokens in replacement, number of args, and various flags all match. |
| 55 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 56 | getNumArgs() != Other.getNumArgs() || |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | isFunctionLike() != Other.isFunctionLike() || |
| 58 | isC99Varargs() != Other.isC99Varargs() || |
| 59 | isGNUVarargs() != Other.isGNUVarargs()) |
| 60 | return false; |
| 61 | |
| 62 | // Check arguments. |
| 63 | for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end(); |
| 64 | I != E; ++I, ++OI) |
| 65 | if (*I != *OI) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | // Check all the tokens. |
| 68 | for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 69 | const Token &A = ReplacementTokens[i]; |
| 70 | const Token &B = Other.ReplacementTokens[i]; |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 71 | if (A.getKind() != B.getKind()) |
| 72 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 74 | // If this isn't the first first token, check that the whitespace and |
| 75 | // start-of-line characteristics match. |
| 76 | if (i != 0 && |
| 77 | (A.isAtStartOfLine() != B.isAtStartOfLine() || |
| 78 | A.hasLeadingSpace() != B.hasLeadingSpace())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | // If this is an identifier, it is easy. |
| 82 | if (A.getIdentifierInfo() || B.getIdentifierInfo()) { |
| 83 | if (A.getIdentifierInfo() != B.getIdentifierInfo()) |
| 84 | return false; |
| 85 | continue; |
| 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | // Otherwise, check the spelling. |
| 89 | if (PP.getSpelling(A) != PP.getSpelling(B)) |
| 90 | return false; |
| 91 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |