USRFinder.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===--- USRFinder.cpp - Clang refactoring library ------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file Implements a recursive AST visitor that finds the USR of a symbol at a
  10. /// point.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Tooling/Refactoring/Rename/USRFinder.h"
  14. #include "clang/AST/AST.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/RecursiveASTVisitor.h"
  17. #include "clang/Basic/SourceManager.h"
  18. #include "clang/Index/USRGeneration.h"
  19. #include "clang/Lex/Lexer.h"
  20. #include "clang/Tooling/Refactoring/RecursiveSymbolVisitor.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. using namespace llvm;
  23. namespace clang {
  24. namespace tooling {
  25. namespace {
  26. /// Recursively visits each AST node to find the symbol underneath the cursor.
  27. class NamedDeclOccurrenceFindingVisitor
  28. : public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> {
  29. public:
  30. // Finds the NamedDecl at a point in the source.
  31. // \param Point the location in the source to search for the NamedDecl.
  32. explicit NamedDeclOccurrenceFindingVisitor(const SourceLocation Point,
  33. const ASTContext &Context)
  34. : RecursiveSymbolVisitor(Context.getSourceManager(),
  35. Context.getLangOpts()),
  36. Point(Point), Context(Context) {}
  37. bool visitSymbolOccurrence(const NamedDecl *ND,
  38. ArrayRef<SourceRange> NameRanges) {
  39. if (!ND)
  40. return true;
  41. for (const auto &Range : NameRanges) {
  42. SourceLocation Start = Range.getBegin();
  43. SourceLocation End = Range.getEnd();
  44. if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
  45. !End.isFileID() || !isPointWithin(Start, End))
  46. return true;
  47. }
  48. Result = ND;
  49. return false;
  50. }
  51. const NamedDecl *getNamedDecl() const { return Result; }
  52. private:
  53. // Determines if the Point is within Start and End.
  54. bool isPointWithin(const SourceLocation Start, const SourceLocation End) {
  55. // FIXME: Add tests for Point == End.
  56. return Point == Start || Point == End ||
  57. (Context.getSourceManager().isBeforeInTranslationUnit(Start,
  58. Point) &&
  59. Context.getSourceManager().isBeforeInTranslationUnit(Point, End));
  60. }
  61. const NamedDecl *Result = nullptr;
  62. const SourceLocation Point; // The location to find the NamedDecl.
  63. const ASTContext &Context;
  64. };
  65. } // end anonymous namespace
  66. const NamedDecl *getNamedDeclAt(const ASTContext &Context,
  67. const SourceLocation Point) {
  68. const SourceManager &SM = Context.getSourceManager();
  69. NamedDeclOccurrenceFindingVisitor Visitor(Point, Context);
  70. // Try to be clever about pruning down the number of top-level declarations we
  71. // see. If both start and end is either before or after the point we're
  72. // looking for the point cannot be inside of this decl. Don't even look at it.
  73. for (auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) {
  74. SourceLocation StartLoc = CurrDecl->getBeginLoc();
  75. SourceLocation EndLoc = CurrDecl->getEndLoc();
  76. if (StartLoc.isValid() && EndLoc.isValid() &&
  77. SM.isBeforeInTranslationUnit(StartLoc, Point) !=
  78. SM.isBeforeInTranslationUnit(EndLoc, Point))
  79. Visitor.TraverseDecl(CurrDecl);
  80. }
  81. return Visitor.getNamedDecl();
  82. }
  83. namespace {
  84. /// Recursively visits each NamedDecl node to find the declaration with a
  85. /// specific name.
  86. class NamedDeclFindingVisitor
  87. : public RecursiveASTVisitor<NamedDeclFindingVisitor> {
  88. public:
  89. explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {}
  90. // We don't have to traverse the uses to find some declaration with a
  91. // specific name, so just visit the named declarations.
  92. bool VisitNamedDecl(const NamedDecl *ND) {
  93. if (!ND)
  94. return true;
  95. // Fully qualified name is used to find the declaration.
  96. if (Name != ND->getQualifiedNameAsString() &&
  97. Name != "::" + ND->getQualifiedNameAsString())
  98. return true;
  99. Result = ND;
  100. return false;
  101. }
  102. const NamedDecl *getNamedDecl() const { return Result; }
  103. private:
  104. const NamedDecl *Result = nullptr;
  105. StringRef Name;
  106. };
  107. } // end anonymous namespace
  108. const NamedDecl *getNamedDeclFor(const ASTContext &Context,
  109. const std::string &Name) {
  110. NamedDeclFindingVisitor Visitor(Name);
  111. Visitor.TraverseDecl(Context.getTranslationUnitDecl());
  112. return Visitor.getNamedDecl();
  113. }
  114. std::string getUSRForDecl(const Decl *Decl) {
  115. llvm::SmallString<128> Buff;
  116. // FIXME: Add test for the nullptr case.
  117. if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff))
  118. return "";
  119. return std::string(Buff);
  120. }
  121. } // end namespace tooling
  122. } // end namespace clang