Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- MacroInfo.cpp - Information about #defined identifiers -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MacroInfo interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 16 | using namespace clang; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 17 | |
Alexander Kornienko | 8b3f623 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 18 | MacroInfo::MacroInfo(SourceLocation DefLoc) |
| 19 | : Location(DefLoc), |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 20 | ArgumentList(nullptr), |
Alexander Kornienko | 8b3f623 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 21 | NumArguments(0), |
| 22 | IsDefinitionLengthCached(false), |
| 23 | IsFunctionLike(false), |
| 24 | IsC99Varargs(false), |
| 25 | IsGNUVarargs(false), |
| 26 | IsBuiltinMacro(false), |
Eli Friedman | 14d3c79 | 2012-11-14 02:18:46 +0000 | [diff] [blame] | 27 | HasCommaPasting(false), |
Alexander Kornienko | 8b3f623 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 28 | IsDisabled(false), |
| 29 | IsUsed(false), |
| 30 | IsAllowRedefinitionsWithoutWarning(false), |
Argyrios Kyrtzidis | 4f32da1 | 2013-03-22 21:12:51 +0000 | [diff] [blame] | 31 | IsWarnIfUnused(false), |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 32 | UsedForHeaderGuard(false) { |
Alexander Kornienko | e61e562 | 2012-09-28 22:24:03 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Yaron Keren | 7762b83 | 2017-04-27 09:56:39 +0000 | [diff] [blame] | 35 | unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const { |
Argyrios Kyrtzidis | 41fb2d9 | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 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 | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 52 | startInfo = SM.getDecomposedExpansionLoc(macroStart); |
Argyrios Kyrtzidis | 41fb2d9 | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 53 | std::pair<FileID, unsigned> |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 54 | endInfo = SM.getDecomposedExpansionLoc(macroEnd); |
Argyrios Kyrtzidis | 41fb2d9 | 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 | |
Argyrios Kyrtzidis | 0c2f30b | 2013-04-03 17:39:30 +0000 | [diff] [blame] | 64 | /// \brief Return true if the specified macro definition is equal to |
| 65 | /// this macro in spelling, arguments, and whitespace. |
| 66 | /// |
| 67 | /// \param Syntactically if true, the macro definitions can be identical even |
| 68 | /// if they use different identifiers for the function macro parameters. |
| 69 | /// Otherwise the comparison is lexical and this implements the rules in |
| 70 | /// C99 6.10.3. |
| 71 | bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, |
| 72 | bool Syntactically) const { |
| 73 | bool Lexically = !Syntactically; |
| 74 | |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 75 | // Check # tokens in replacement, number of args, and various flags all match. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 76 | if (ReplacementTokens.size() != Other.ReplacementTokens.size() || |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 77 | getNumArgs() != Other.getNumArgs() || |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 78 | isFunctionLike() != Other.isFunctionLike() || |
| 79 | isC99Varargs() != Other.isC99Varargs() || |
| 80 | isGNUVarargs() != Other.isGNUVarargs()) |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 81 | return false; |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 82 | |
Argyrios Kyrtzidis | 0c2f30b | 2013-04-03 17:39:30 +0000 | [diff] [blame] | 83 | if (Lexically) { |
| 84 | // Check arguments. |
| 85 | for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end(); |
| 86 | I != E; ++I, ++OI) |
| 87 | if (*I != *OI) return false; |
| 88 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 90 | // Check all the tokens. |
| 91 | for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) { |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 92 | const Token &A = ReplacementTokens[i]; |
| 93 | const Token &B = Other.ReplacementTokens[i]; |
Chris Lattner | 794c001 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 94 | if (A.getKind() != B.getKind()) |
| 95 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 794c001 | 2009-03-09 20:33:32 +0000 | [diff] [blame] | 97 | // If this isn't the first first token, check that the whitespace and |
| 98 | // start-of-line characteristics match. |
| 99 | if (i != 0 && |
| 100 | (A.isAtStartOfLine() != B.isAtStartOfLine() || |
| 101 | A.hasLeadingSpace() != B.hasLeadingSpace())) |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 102 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 104 | // If this is an identifier, it is easy. |
| 105 | if (A.getIdentifierInfo() || B.getIdentifierInfo()) { |
Argyrios Kyrtzidis | 0c2f30b | 2013-04-03 17:39:30 +0000 | [diff] [blame] | 106 | if (A.getIdentifierInfo() == B.getIdentifierInfo()) |
| 107 | continue; |
| 108 | if (Lexically) |
| 109 | return false; |
| 110 | // With syntactic equivalence the parameter names can be different as long |
| 111 | // as they are used in the same place. |
| 112 | int AArgNum = getArgumentNum(A.getIdentifierInfo()); |
Argyrios Kyrtzidis | f0eaa64 | 2013-04-03 21:29:07 +0000 | [diff] [blame] | 113 | if (AArgNum == -1) |
| 114 | return false; |
| 115 | if (AArgNum != Other.getArgumentNum(B.getIdentifierInfo())) |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 116 | return false; |
| 117 | continue; |
| 118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 120 | // Otherwise, check the spelling. |
| 121 | if (PP.getSpelling(A) != PP.getSpelling(B)) |
| 122 | return false; |
| 123 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 125 | return true; |
| 126 | } |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 127 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 128 | LLVM_DUMP_METHOD void MacroInfo::dump() const { |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 129 | llvm::raw_ostream &Out = llvm::errs(); |
| 130 | |
| 131 | // FIXME: Dump locations. |
| 132 | Out << "MacroInfo " << this; |
| 133 | if (IsBuiltinMacro) Out << " builtin"; |
| 134 | if (IsDisabled) Out << " disabled"; |
| 135 | if (IsUsed) Out << " used"; |
| 136 | if (IsAllowRedefinitionsWithoutWarning) |
| 137 | Out << " allow_redefinitions_without_warning"; |
| 138 | if (IsWarnIfUnused) Out << " warn_if_unused"; |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 139 | if (UsedForHeaderGuard) Out << " header_guard"; |
| 140 | |
| 141 | Out << "\n #define <macro>"; |
| 142 | if (IsFunctionLike) { |
| 143 | Out << "("; |
| 144 | for (unsigned I = 0; I != NumArguments; ++I) { |
| 145 | if (I) Out << ", "; |
| 146 | Out << ArgumentList[I]->getName(); |
| 147 | } |
| 148 | if (IsC99Varargs || IsGNUVarargs) { |
| 149 | if (NumArguments && IsC99Varargs) Out << ", "; |
| 150 | Out << "..."; |
| 151 | } |
| 152 | Out << ")"; |
| 153 | } |
| 154 | |
Richard Smith | 85f93f3 | 2015-11-05 20:55:14 +0000 | [diff] [blame] | 155 | bool First = true; |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 156 | for (const Token &Tok : ReplacementTokens) { |
Richard Smith | 85f93f3 | 2015-11-05 20:55:14 +0000 | [diff] [blame] | 157 | // Leading space is semantically meaningful in a macro definition, |
| 158 | // so preserve it in the dump output. |
| 159 | if (First || Tok.hasLeadingSpace()) |
| 160 | Out << " "; |
| 161 | First = false; |
| 162 | |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 163 | if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind())) |
| 164 | Out << Punc; |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 165 | else if (Tok.isLiteral() && Tok.getLiteralData()) |
| 166 | Out << StringRef(Tok.getLiteralData(), Tok.getLength()); |
Richard Smith | 85f93f3 | 2015-11-05 20:55:14 +0000 | [diff] [blame] | 167 | else if (auto *II = Tok.getIdentifierInfo()) |
| 168 | Out << II->getName(); |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 169 | else |
| 170 | Out << Tok.getName(); |
| 171 | } |
| 172 | } |
| 173 | |
Richard Smith | 2e87e14 | 2014-01-27 23:54:39 +0000 | [diff] [blame] | 174 | MacroDirective::DefInfo MacroDirective::getDefinition() { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 175 | MacroDirective *MD = this; |
| 176 | SourceLocation UndefLoc; |
| 177 | Optional<bool> isPublic; |
| 178 | for (; MD; MD = MD->getPrevious()) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 179 | if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) |
| 180 | return DefInfo(DefMD, UndefLoc, |
| 181 | !isPublic.hasValue() || isPublic.getValue()); |
| 182 | |
| 183 | if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) { |
| 184 | UndefLoc = UndefMD->getLocation(); |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD); |
| 189 | if (!isPublic.hasValue()) |
| 190 | isPublic = VisMD->isPublic(); |
| 191 | } |
| 192 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 193 | return DefInfo(nullptr, UndefLoc, |
| 194 | !isPublic.hasValue() || isPublic.getValue()); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | const MacroDirective::DefInfo |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 198 | MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const { |
| 199 | assert(L.isValid() && "SourceLocation is invalid."); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 200 | for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 201 | if (Def.getLocation().isInvalid() || // For macros defined on the command line. |
| 202 | SM.isBeforeInTranslationUnit(Def.getLocation(), L)) |
| 203 | return (!Def.isUndefined() || |
| 204 | SM.isBeforeInTranslationUnit(L, Def.getUndefLocation())) |
| 205 | ? Def : DefInfo(); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 206 | } |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 207 | return DefInfo(); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 208 | } |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 209 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 210 | LLVM_DUMP_METHOD void MacroDirective::dump() const { |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 211 | llvm::raw_ostream &Out = llvm::errs(); |
| 212 | |
| 213 | switch (getKind()) { |
| 214 | case MD_Define: Out << "DefMacroDirective"; break; |
| 215 | case MD_Undefine: Out << "UndefMacroDirective"; break; |
| 216 | case MD_Visibility: Out << "VisibilityMacroDirective"; break; |
| 217 | } |
| 218 | Out << " " << this; |
| 219 | // FIXME: Dump SourceLocation. |
| 220 | if (auto *Prev = getPrevious()) |
| 221 | Out << " prev " << Prev; |
| 222 | if (IsFromPCH) Out << " from_pch"; |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 223 | |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 224 | if (isa<VisibilityMacroDirective>(this)) |
| 225 | Out << (IsPublic ? " public" : " private"); |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 226 | |
| 227 | if (auto *DMD = dyn_cast<DefMacroDirective>(this)) { |
| 228 | if (auto *Info = DMD->getInfo()) { |
| 229 | Out << "\n "; |
| 230 | Info->dump(); |
| 231 | } |
| 232 | } |
Richard Smith | e657bbd | 2014-07-18 22:13:40 +0000 | [diff] [blame] | 233 | Out << "\n"; |
Richard Smith | a5d2a49 | 2014-07-18 04:54:02 +0000 | [diff] [blame] | 234 | } |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 235 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 236 | ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule, |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 237 | IdentifierInfo *II, MacroInfo *Macro, |
Richard Smith | 447ed43 | 2015-04-23 04:13:52 +0000 | [diff] [blame] | 238 | ArrayRef<ModuleMacro *> Overrides) { |
| 239 | void *Mem = PP.getPreprocessorAllocator().Allocate( |
| 240 | sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(), |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 241 | alignof(ModuleMacro)); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 242 | return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides); |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 243 | } |