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