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 | baa74bd | 2013-03-22 21:12:51 +0000 | [diff] [blame] | 31 | IsWarnIfUnused(false), |
| 32 | FromASTFile(false) { |
Alexander Kornienko | 66da0ab | 2012-09-28 22:24:03 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 35 | unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const { |
| 36 | assert(!IsDefinitionLengthCached); |
| 37 | IsDefinitionLengthCached = true; |
| 38 | |
| 39 | if (ReplacementTokens.empty()) |
| 40 | return (DefinitionLength = 0); |
| 41 | |
| 42 | const Token &firstToken = ReplacementTokens.front(); |
| 43 | const Token &lastToken = ReplacementTokens.back(); |
| 44 | SourceLocation macroStart = firstToken.getLocation(); |
| 45 | SourceLocation macroEnd = lastToken.getLocation(); |
| 46 | assert(macroStart.isValid() && macroEnd.isValid()); |
| 47 | assert((macroStart.isFileID() || firstToken.is(tok::comment)) && |
| 48 | "Macro defined in macro?"); |
| 49 | assert((macroEnd.isFileID() || lastToken.is(tok::comment)) && |
| 50 | "Macro defined in macro?"); |
| 51 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 52 | startInfo = SM.getDecomposedExpansionLoc(macroStart); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 53 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 54 | endInfo = SM.getDecomposedExpansionLoc(macroEnd); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 55 | assert(startInfo.first == endInfo.first && |
| 56 | "Macro definition spanning multiple FileIDs ?"); |
| 57 | assert(startInfo.second <= endInfo.second); |
| 58 | DefinitionLength = endInfo.second - startInfo.second; |
| 59 | DefinitionLength += lastToken.getLength(); |
| 60 | |
| 61 | return DefinitionLength; |
| 62 | } |
| 63 | |
James Dennett | 81ba9d1 | 2013-03-20 21:30:03 +0000 | [diff] [blame] | 64 | // isIdenticalTo - Return true if the specified macro definition is equal to |
| 65 | // this macro in spelling, arguments, and whitespace. This is used to emit |
| 66 | // duplicate definition warnings. This implements the rules in C99 6.10.3. |
| 67 | // |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { |
| 69 | // Check # tokens in replacement, number of args, and various flags all match. |
| 70 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 25c9648 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 71 | getNumArgs() != Other.getNumArgs() || |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | isFunctionLike() != Other.isFunctionLike() || |
| 73 | isC99Varargs() != Other.isC99Varargs() || |
| 74 | isGNUVarargs() != Other.isGNUVarargs()) |
| 75 | return false; |
| 76 | |
| 77 | // Check arguments. |
| 78 | for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end(); |
| 79 | I != E; ++I, ++OI) |
| 80 | if (*I != *OI) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | // Check all the tokens. |
| 83 | for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 84 | const Token &A = ReplacementTokens[i]; |
| 85 | const Token &B = Other.ReplacementTokens[i]; |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 86 | if (A.getKind() != B.getKind()) |
| 87 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 688a248 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 89 | // If this isn't the first first token, check that the whitespace and |
| 90 | // start-of-line characteristics match. |
| 91 | if (i != 0 && |
| 92 | (A.isAtStartOfLine() != B.isAtStartOfLine() || |
| 93 | A.hasLeadingSpace() != B.hasLeadingSpace())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | // If this is an identifier, it is easy. |
| 97 | if (A.getIdentifierInfo() || B.getIdentifierInfo()) { |
| 98 | if (A.getIdentifierInfo() != B.getIdentifierInfo()) |
| 99 | return false; |
| 100 | continue; |
| 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | // Otherwise, check the spelling. |
| 104 | if (PP.getSpelling(A) != PP.getSpelling(B)) |
| 105 | return false; |
| 106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 110 | |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 111 | MacroDirective::DefInfo MacroDirective::getDefinition(bool AllowHidden) { |
| 112 | MacroDirective *MD = this; |
| 113 | SourceLocation UndefLoc; |
| 114 | Optional<bool> isPublic; |
| 115 | for (; MD; MD = MD->getPrevious()) { |
| 116 | if (!AllowHidden && MD->isHidden()) |
| 117 | continue; |
| 118 | |
| 119 | if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) |
| 120 | return DefInfo(DefMD, UndefLoc, |
| 121 | !isPublic.hasValue() || isPublic.getValue()); |
| 122 | |
| 123 | if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) { |
| 124 | UndefLoc = UndefMD->getLocation(); |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD); |
| 129 | if (!isPublic.hasValue()) |
| 130 | isPublic = VisMD->isPublic(); |
| 131 | } |
| 132 | |
| 133 | return DefInfo(); |
| 134 | } |
| 135 | |
| 136 | const MacroDirective::DefInfo |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 137 | MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const { |
| 138 | assert(L.isValid() && "SourceLocation is invalid."); |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 139 | for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 140 | if (Def.getLocation().isInvalid() || // For macros defined on the command line. |
| 141 | SM.isBeforeInTranslationUnit(Def.getLocation(), L)) |
| 142 | return (!Def.isUndefined() || |
| 143 | SM.isBeforeInTranslationUnit(L, Def.getUndefLocation())) |
| 144 | ? Def : DefInfo(); |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 145 | } |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 146 | return DefInfo(); |
Argyrios Kyrtzidis | 9818a1d | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 147 | } |