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 | |
Alexander Kornienko | 8a64bb5 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 18 | MacroInfo::MacroInfo(SourceLocation DefLoc) |
| 19 | : Location(DefLoc), |
Alexander Kornienko | 8a64bb5 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 20 | ArgumentList(0), |
| 21 | NumArguments(0), |
| 22 | IsDefinitionLengthCached(false), |
| 23 | IsFunctionLike(false), |
| 24 | IsC99Varargs(false), |
| 25 | IsGNUVarargs(false), |
| 26 | IsBuiltinMacro(false), |
Eli Friedman | 4fa4b48 | 2012-11-14 02:18:46 +0000 | [diff] [blame] | 27 | HasCommaPasting(false), |
Alexander Kornienko | 8a64bb5 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 28 | IsDisabled(false), |
| 29 | IsUsed(false), |
| 30 | IsAllowRedefinitionsWithoutWarning(false), |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame^] | 31 | IsWarnIfUnused(false) { |
Alexander Kornienko | 66da0ab | 2012-09-28 22:24:03 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 34 | unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const { |
| 35 | assert(!IsDefinitionLengthCached); |
| 36 | IsDefinitionLengthCached = true; |
| 37 | |
| 38 | if (ReplacementTokens.empty()) |
| 39 | return (DefinitionLength = 0); |
| 40 | |
| 41 | const Token &firstToken = ReplacementTokens.front(); |
| 42 | const Token &lastToken = ReplacementTokens.back(); |
| 43 | SourceLocation macroStart = firstToken.getLocation(); |
| 44 | SourceLocation macroEnd = lastToken.getLocation(); |
| 45 | assert(macroStart.isValid() && macroEnd.isValid()); |
| 46 | assert((macroStart.isFileID() || firstToken.is(tok::comment)) && |
| 47 | "Macro defined in macro?"); |
| 48 | assert((macroEnd.isFileID() || lastToken.is(tok::comment)) && |
| 49 | "Macro defined in macro?"); |
| 50 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 51 | startInfo = SM.getDecomposedExpansionLoc(macroStart); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 52 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 53 | endInfo = SM.getDecomposedExpansionLoc(macroEnd); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 54 | assert(startInfo.first == endInfo.first && |
| 55 | "Macro definition spanning multiple FileIDs ?"); |
| 56 | assert(startInfo.second <= endInfo.second); |
| 57 | DefinitionLength = endInfo.second - startInfo.second; |
| 58 | DefinitionLength += lastToken.getLength(); |
| 59 | |
| 60 | return DefinitionLength; |
| 61 | } |
| 62 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | /// isIdenticalTo - Return true if the specified macro definition is equal to |
| 64 | /// this macro in spelling, arguments, and whitespace. This is used to emit |
| 65 | /// duplicate definition warnings. This implements the rules in C99 6.10.3. |
| 66 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { |
| 68 | // Check # tokens in replacement, number of args, and various flags all match. |
| 69 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 70 | getNumArgs() != Other.getNumArgs() || |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | isFunctionLike() != Other.isFunctionLike() || |
| 72 | isC99Varargs() != Other.isC99Varargs() || |
| 73 | isGNUVarargs() != Other.isGNUVarargs()) |
| 74 | return false; |
| 75 | |
| 76 | // Check arguments. |
| 77 | for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end(); |
| 78 | I != E; ++I, ++OI) |
| 79 | if (*I != *OI) 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 | // Check all the tokens. |
| 82 | for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 83 | const Token &A = ReplacementTokens[i]; |
| 84 | const Token &B = Other.ReplacementTokens[i]; |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 85 | if (A.getKind() != B.getKind()) |
| 86 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 88 | // If this isn't the first first token, check that the whitespace and |
| 89 | // start-of-line characteristics match. |
| 90 | if (i != 0 && |
| 91 | (A.isAtStartOfLine() != B.isAtStartOfLine() || |
| 92 | A.hasLeadingSpace() != B.hasLeadingSpace())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | // If this is an identifier, it is easy. |
| 96 | if (A.getIdentifierInfo() || B.getIdentifierInfo()) { |
| 97 | if (A.getIdentifierInfo() != B.getIdentifierInfo()) |
| 98 | return false; |
| 99 | continue; |
| 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | // Otherwise, check the spelling. |
| 103 | if (PP.getSpelling(A) != PP.getSpelling(B)) |
| 104 | return false; |
| 105 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | return true; |
| 108 | } |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame^] | 109 | |
| 110 | const MacroDirective * |
| 111 | MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const { |
| 112 | assert(L.isValid() && "SourceLocation is invalid."); |
| 113 | for (const MacroDirective *MD = this; MD; MD = MD->Previous) { |
| 114 | if (MD->getLocation().isInvalid() || // For macros defined on the command line. |
| 115 | SM.isBeforeInTranslationUnit(MD->getLocation(), L)) |
| 116 | return (MD->UndefLocation.isInvalid() || |
| 117 | SM.isBeforeInTranslationUnit(L, MD->UndefLocation)) ? MD : NULL; |
| 118 | } |
| 119 | return NULL; |
| 120 | } |