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