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