MacroInfo.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the MacroInfo interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/MacroInfo.h"
  14. #include "clang/Lex/Preprocessor.h"
  15. using namespace clang;
  16. MacroInfo::MacroInfo(SourceLocation DefLoc)
  17. : Location(DefLoc),
  18. ArgumentList(nullptr),
  19. NumArguments(0),
  20. IsDefinitionLengthCached(false),
  21. IsFunctionLike(false),
  22. IsC99Varargs(false),
  23. IsGNUVarargs(false),
  24. IsBuiltinMacro(false),
  25. HasCommaPasting(false),
  26. IsDisabled(false),
  27. IsUsed(false),
  28. IsAllowRedefinitionsWithoutWarning(false),
  29. IsWarnIfUnused(false),
  30. FromASTFile(false),
  31. UsedForHeaderGuard(false) {
  32. }
  33. unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
  34. assert(!IsDefinitionLengthCached);
  35. IsDefinitionLengthCached = true;
  36. if (ReplacementTokens.empty())
  37. return (DefinitionLength = 0);
  38. const Token &firstToken = ReplacementTokens.front();
  39. const Token &lastToken = ReplacementTokens.back();
  40. SourceLocation macroStart = firstToken.getLocation();
  41. SourceLocation macroEnd = lastToken.getLocation();
  42. assert(macroStart.isValid() && macroEnd.isValid());
  43. assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
  44. "Macro defined in macro?");
  45. assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
  46. "Macro defined in macro?");
  47. std::pair<FileID, unsigned>
  48. startInfo = SM.getDecomposedExpansionLoc(macroStart);
  49. std::pair<FileID, unsigned>
  50. endInfo = SM.getDecomposedExpansionLoc(macroEnd);
  51. assert(startInfo.first == endInfo.first &&
  52. "Macro definition spanning multiple FileIDs ?");
  53. assert(startInfo.second <= endInfo.second);
  54. DefinitionLength = endInfo.second - startInfo.second;
  55. DefinitionLength += lastToken.getLength();
  56. return DefinitionLength;
  57. }
  58. /// \brief Return true if the specified macro definition is equal to
  59. /// this macro in spelling, arguments, and whitespace.
  60. ///
  61. /// \param Syntactically if true, the macro definitions can be identical even
  62. /// if they use different identifiers for the function macro parameters.
  63. /// Otherwise the comparison is lexical and this implements the rules in
  64. /// C99 6.10.3.
  65. bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
  66. bool Syntactically) const {
  67. bool Lexically = !Syntactically;
  68. // Check # tokens in replacement, number of args, and various flags all match.
  69. if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
  70. getNumArgs() != Other.getNumArgs() ||
  71. isFunctionLike() != Other.isFunctionLike() ||
  72. isC99Varargs() != Other.isC99Varargs() ||
  73. isGNUVarargs() != Other.isGNUVarargs())
  74. return false;
  75. if (Lexically) {
  76. // Check arguments.
  77. for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
  78. I != E; ++I, ++OI)
  79. if (*I != *OI) return false;
  80. }
  81. // Check all the tokens.
  82. for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
  83. const Token &A = ReplacementTokens[i];
  84. const Token &B = Other.ReplacementTokens[i];
  85. if (A.getKind() != B.getKind())
  86. return false;
  87. // If this isn't the first first token, check that the whitespace and
  88. // start-of-line characteristics match.
  89. if (i != 0 &&
  90. (A.isAtStartOfLine() != B.isAtStartOfLine() ||
  91. A.hasLeadingSpace() != B.hasLeadingSpace()))
  92. return false;
  93. // If this is an identifier, it is easy.
  94. if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
  95. if (A.getIdentifierInfo() == B.getIdentifierInfo())
  96. continue;
  97. if (Lexically)
  98. return false;
  99. // With syntactic equivalence the parameter names can be different as long
  100. // as they are used in the same place.
  101. int AArgNum = getArgumentNum(A.getIdentifierInfo());
  102. if (AArgNum == -1)
  103. return false;
  104. if (AArgNum != Other.getArgumentNum(B.getIdentifierInfo()))
  105. return false;
  106. continue;
  107. }
  108. // Otherwise, check the spelling.
  109. if (PP.getSpelling(A) != PP.getSpelling(B))
  110. return false;
  111. }
  112. return true;
  113. }
  114. void MacroInfo::dump() const {
  115. llvm::raw_ostream &Out = llvm::errs();
  116. // FIXME: Dump locations.
  117. Out << "MacroInfo " << this;
  118. if (IsBuiltinMacro) Out << " builtin";
  119. if (IsDisabled) Out << " disabled";
  120. if (IsUsed) Out << " used";
  121. if (IsAllowRedefinitionsWithoutWarning)
  122. Out << " allow_redefinitions_without_warning";
  123. if (IsWarnIfUnused) Out << " warn_if_unused";
  124. if (FromASTFile) Out << " imported";
  125. if (UsedForHeaderGuard) Out << " header_guard";
  126. Out << "\n #define <macro>";
  127. if (IsFunctionLike) {
  128. Out << "(";
  129. for (unsigned I = 0; I != NumArguments; ++I) {
  130. if (I) Out << ", ";
  131. Out << ArgumentList[I]->getName();
  132. }
  133. if (IsC99Varargs || IsGNUVarargs) {
  134. if (NumArguments && IsC99Varargs) Out << ", ";
  135. Out << "...";
  136. }
  137. Out << ")";
  138. }
  139. for (const Token &Tok : ReplacementTokens) {
  140. Out << " ";
  141. if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
  142. Out << Punc;
  143. else if (const char *Kwd = tok::getKeywordSpelling(Tok.getKind()))
  144. Out << Kwd;
  145. else if (Tok.is(tok::identifier))
  146. Out << Tok.getIdentifierInfo()->getName();
  147. else if (Tok.isLiteral() && Tok.getLiteralData())
  148. Out << StringRef(Tok.getLiteralData(), Tok.getLength());
  149. else
  150. Out << Tok.getName();
  151. }
  152. }
  153. MacroDirective::DefInfo MacroDirective::getDefinition() {
  154. MacroDirective *MD = this;
  155. SourceLocation UndefLoc;
  156. Optional<bool> isPublic;
  157. for (; MD; MD = MD->getPrevious()) {
  158. if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
  159. return DefInfo(DefMD, UndefLoc,
  160. !isPublic.hasValue() || isPublic.getValue());
  161. if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
  162. UndefLoc = UndefMD->getLocation();
  163. continue;
  164. }
  165. VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
  166. if (!isPublic.hasValue())
  167. isPublic = VisMD->isPublic();
  168. }
  169. return DefInfo(nullptr, UndefLoc,
  170. !isPublic.hasValue() || isPublic.getValue());
  171. }
  172. const MacroDirective::DefInfo
  173. MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
  174. assert(L.isValid() && "SourceLocation is invalid.");
  175. for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
  176. if (Def.getLocation().isInvalid() || // For macros defined on the command line.
  177. SM.isBeforeInTranslationUnit(Def.getLocation(), L))
  178. return (!Def.isUndefined() ||
  179. SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
  180. ? Def : DefInfo();
  181. }
  182. return DefInfo();
  183. }
  184. void MacroDirective::dump() const {
  185. llvm::raw_ostream &Out = llvm::errs();
  186. switch (getKind()) {
  187. case MD_Define: Out << "DefMacroDirective"; break;
  188. case MD_Undefine: Out << "UndefMacroDirective"; break;
  189. case MD_Visibility: Out << "VisibilityMacroDirective"; break;
  190. }
  191. Out << " " << this;
  192. // FIXME: Dump SourceLocation.
  193. if (auto *Prev = getPrevious())
  194. Out << " prev " << Prev;
  195. if (IsFromPCH) Out << " from_pch";
  196. if (isa<VisibilityMacroDirective>(this))
  197. Out << (IsPublic ? " public" : " private");
  198. if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
  199. if (auto *Info = DMD->getInfo()) {
  200. Out << "\n ";
  201. Info->dump();
  202. }
  203. }
  204. Out << "\n";
  205. }
  206. ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
  207. IdentifierInfo *II, MacroInfo *Macro,
  208. ArrayRef<ModuleMacro *> Overrides) {
  209. void *Mem = PP.getPreprocessorAllocator().Allocate(
  210. sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
  211. llvm::alignOf<ModuleMacro>());
  212. return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);
  213. }