10b57cec5SDimitry Andric //===--- USRFinder.cpp - Clang refactoring library ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file Implements a recursive AST visitor that finds the USR of a symbol at a
100b57cec5SDimitry Andric /// point.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "clang/Tooling/Refactoring/Rename/USRFinder.h"
150b57cec5SDimitry Andric #include "clang/AST/AST.h"
160b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
170b57cec5SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h"
185ffd83dbSDimitry Andric #include "clang/Basic/SourceManager.h"
190b57cec5SDimitry Andric #include "clang/Index/USRGeneration.h"
200b57cec5SDimitry Andric #include "clang/Lex/Lexer.h"
210b57cec5SDimitry Andric #include "clang/Tooling/Refactoring/RecursiveSymbolVisitor.h"
220b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric namespace clang {
270b57cec5SDimitry Andric namespace tooling {
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric namespace {
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric /// Recursively visits each AST node to find the symbol underneath the cursor.
320b57cec5SDimitry Andric class NamedDeclOccurrenceFindingVisitor
330b57cec5SDimitry Andric : public RecursiveSymbolVisitor<NamedDeclOccurrenceFindingVisitor> {
340b57cec5SDimitry Andric public:
350b57cec5SDimitry Andric // Finds the NamedDecl at a point in the source.
360b57cec5SDimitry Andric // \param Point the location in the source to search for the NamedDecl.
NamedDeclOccurrenceFindingVisitor(const SourceLocation Point,const ASTContext & Context)370b57cec5SDimitry Andric explicit NamedDeclOccurrenceFindingVisitor(const SourceLocation Point,
380b57cec5SDimitry Andric const ASTContext &Context)
390b57cec5SDimitry Andric : RecursiveSymbolVisitor(Context.getSourceManager(),
400b57cec5SDimitry Andric Context.getLangOpts()),
410b57cec5SDimitry Andric Point(Point), Context(Context) {}
420b57cec5SDimitry Andric
visitSymbolOccurrence(const NamedDecl * ND,ArrayRef<SourceRange> NameRanges)430b57cec5SDimitry Andric bool visitSymbolOccurrence(const NamedDecl *ND,
440b57cec5SDimitry Andric ArrayRef<SourceRange> NameRanges) {
450b57cec5SDimitry Andric if (!ND)
460b57cec5SDimitry Andric return true;
470b57cec5SDimitry Andric for (const auto &Range : NameRanges) {
480b57cec5SDimitry Andric SourceLocation Start = Range.getBegin();
490b57cec5SDimitry Andric SourceLocation End = Range.getEnd();
500b57cec5SDimitry Andric if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||
510b57cec5SDimitry Andric !End.isFileID() || !isPointWithin(Start, End))
520b57cec5SDimitry Andric return true;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric Result = ND;
550b57cec5SDimitry Andric return false;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
getNamedDecl() const580b57cec5SDimitry Andric const NamedDecl *getNamedDecl() const { return Result; }
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric private:
610b57cec5SDimitry Andric // Determines if the Point is within Start and End.
isPointWithin(const SourceLocation Start,const SourceLocation End)620b57cec5SDimitry Andric bool isPointWithin(const SourceLocation Start, const SourceLocation End) {
630b57cec5SDimitry Andric // FIXME: Add tests for Point == End.
640b57cec5SDimitry Andric return Point == Start || Point == End ||
650b57cec5SDimitry Andric (Context.getSourceManager().isBeforeInTranslationUnit(Start,
660b57cec5SDimitry Andric Point) &&
670b57cec5SDimitry Andric Context.getSourceManager().isBeforeInTranslationUnit(Point, End));
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric const NamedDecl *Result = nullptr;
710b57cec5SDimitry Andric const SourceLocation Point; // The location to find the NamedDecl.
720b57cec5SDimitry Andric const ASTContext &Context;
730b57cec5SDimitry Andric };
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric } // end anonymous namespace
760b57cec5SDimitry Andric
getNamedDeclAt(const ASTContext & Context,const SourceLocation Point)770b57cec5SDimitry Andric const NamedDecl *getNamedDeclAt(const ASTContext &Context,
780b57cec5SDimitry Andric const SourceLocation Point) {
790b57cec5SDimitry Andric const SourceManager &SM = Context.getSourceManager();
800b57cec5SDimitry Andric NamedDeclOccurrenceFindingVisitor Visitor(Point, Context);
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric // Try to be clever about pruning down the number of top-level declarations we
830b57cec5SDimitry Andric // see. If both start and end is either before or after the point we're
840b57cec5SDimitry Andric // looking for the point cannot be inside of this decl. Don't even look at it.
850b57cec5SDimitry Andric for (auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) {
860b57cec5SDimitry Andric SourceLocation StartLoc = CurrDecl->getBeginLoc();
870b57cec5SDimitry Andric SourceLocation EndLoc = CurrDecl->getEndLoc();
880b57cec5SDimitry Andric if (StartLoc.isValid() && EndLoc.isValid() &&
890b57cec5SDimitry Andric SM.isBeforeInTranslationUnit(StartLoc, Point) !=
900b57cec5SDimitry Andric SM.isBeforeInTranslationUnit(EndLoc, Point))
910b57cec5SDimitry Andric Visitor.TraverseDecl(CurrDecl);
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric return Visitor.getNamedDecl();
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric namespace {
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric /// Recursively visits each NamedDecl node to find the declaration with a
1000b57cec5SDimitry Andric /// specific name.
1010b57cec5SDimitry Andric class NamedDeclFindingVisitor
1020b57cec5SDimitry Andric : public RecursiveASTVisitor<NamedDeclFindingVisitor> {
1030b57cec5SDimitry Andric public:
NamedDeclFindingVisitor(StringRef Name)1040b57cec5SDimitry Andric explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {}
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // We don't have to traverse the uses to find some declaration with a
1070b57cec5SDimitry Andric // specific name, so just visit the named declarations.
VisitNamedDecl(const NamedDecl * ND)1080b57cec5SDimitry Andric bool VisitNamedDecl(const NamedDecl *ND) {
1090b57cec5SDimitry Andric if (!ND)
1100b57cec5SDimitry Andric return true;
1110b57cec5SDimitry Andric // Fully qualified name is used to find the declaration.
1120b57cec5SDimitry Andric if (Name != ND->getQualifiedNameAsString() &&
1130b57cec5SDimitry Andric Name != "::" + ND->getQualifiedNameAsString())
1140b57cec5SDimitry Andric return true;
1150b57cec5SDimitry Andric Result = ND;
1160b57cec5SDimitry Andric return false;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric
getNamedDecl() const1190b57cec5SDimitry Andric const NamedDecl *getNamedDecl() const { return Result; }
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric private:
1220b57cec5SDimitry Andric const NamedDecl *Result = nullptr;
1230b57cec5SDimitry Andric StringRef Name;
1240b57cec5SDimitry Andric };
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric } // end anonymous namespace
1270b57cec5SDimitry Andric
getNamedDeclFor(const ASTContext & Context,const std::string & Name)1280b57cec5SDimitry Andric const NamedDecl *getNamedDeclFor(const ASTContext &Context,
1290b57cec5SDimitry Andric const std::string &Name) {
1300b57cec5SDimitry Andric NamedDeclFindingVisitor Visitor(Name);
1310b57cec5SDimitry Andric Visitor.TraverseDecl(Context.getTranslationUnitDecl());
1320b57cec5SDimitry Andric return Visitor.getNamedDecl();
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric
getUSRForDecl(const Decl * Decl)1350b57cec5SDimitry Andric std::string getUSRForDecl(const Decl *Decl) {
136*e8d8bef9SDimitry Andric llvm::SmallString<128> Buff;
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric // FIXME: Add test for the nullptr case.
1390b57cec5SDimitry Andric if (Decl == nullptr || index::generateUSRForDecl(Decl, Buff))
1400b57cec5SDimitry Andric return "";
1410b57cec5SDimitry Andric
142*e8d8bef9SDimitry Andric return std::string(Buff);
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric } // end namespace tooling
1460b57cec5SDimitry Andric } // end namespace clang
147