aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Tooling/Refactoring/Rename/USRFinder.h
blob: 28d541af43c085207c367bf5be3df3248089f133 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//===--- USRFinder.h - Clang refactoring library --------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Methods for determining the USR of a symbol at a location in source
/// code.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLING_REFACTOR_RENAME_USR_FINDER_H
#define LLVM_CLANG_TOOLING_REFACTOR_RENAME_USR_FINDER_H

#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <string>
#include <vector>

using namespace llvm;
using namespace clang::ast_matchers;

namespace clang {

class ASTContext;
class Decl;
class SourceLocation;
class NamedDecl;

namespace tooling {

// Given an AST context and a point, returns a NamedDecl identifying the symbol
// at the point. Returns null if nothing is found at the point.
const NamedDecl *getNamedDeclAt(const ASTContext &Context,
                                const SourceLocation Point);

// Given an AST context and a fully qualified name, returns a NamedDecl
// identifying the symbol with a matching name. Returns null if nothing is
// found for the name.
const NamedDecl *getNamedDeclFor(const ASTContext &Context,
                                 const std::string &Name);

// Converts a Decl into a USR.
std::string getUSRForDecl(const Decl *Decl);

// FIXME: Implement RecursiveASTVisitor<T>::VisitNestedNameSpecifier instead.
class NestedNameSpecifierLocFinder : public MatchFinder::MatchCallback {
public:
  explicit NestedNameSpecifierLocFinder(ASTContext &Context)
      : Context(Context) {}

  std::vector<NestedNameSpecifierLoc> getNestedNameSpecifierLocations() {
    addMatchers();
    Finder.matchAST(Context);
    return Locations;
  }

private:
  void addMatchers() {
    const auto NestedNameSpecifierLocMatcher =
        nestedNameSpecifierLoc().bind("nestedNameSpecifierLoc");
    Finder.addMatcher(NestedNameSpecifierLocMatcher, this);
  }

  void run(const MatchFinder::MatchResult &Result) override {
    const auto *NNS = Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
        "nestedNameSpecifierLoc");
    Locations.push_back(*NNS);
  }

  ASTContext &Context;
  std::vector<NestedNameSpecifierLoc> Locations;
  MatchFinder Finder;
};

} // end namespace tooling
} // end namespace clang

#endif // LLVM_CLANG_TOOLING_REFACTOR_RENAME_USR_FINDER_H