summaryrefslogtreecommitdiffstats
path: root/clang-move/HelperDeclRefGraph.h
blob: 11b3c9c190db3c1a633ae94a4a2e5ef5860840a7 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//===-- UsedHelperDeclFinder.h - AST-based call graph for helper decls ----===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_USED_HELPER_DECL_FINDER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_USED_HELPER_DECL_FINDER_H

#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Analysis/CallGraph.h"
#include "llvm/ADT/DenseSet.h"
#include <memory>
#include <vector>

namespace clang {
namespace move {

// A reference graph for finding used/unused helper declarations in a single
// translation unit (e.g. old.cc). We don't reuse CallGraph in clang/Analysis
// because that CallGraph only supports function declarations.
//
// Helper declarations include following types:
//   * function/variable/class definitions in an anonymous namespace.
//   * static function/variable definitions in a global/named namespace.
//
// The reference graph is a directed graph. Each node in the graph represents a
// helper declaration in old.cc or a non-moved/moved declaration (e.g. class,
// function) in old.h, which means each node is associated with a Decl.
//
// To construct the graph, we use AST matcher to find interesting Decls (usually
// a pair of Caller and Callee), and add an edge from the Caller node to the
// Callee node.
//
// Specially, for a class, it might have multiple declarations such methods
// and member variables. We only use a single node to present this class, and
// this node is associated with the class declaration (CXXRecordDecl).
//
// The graph has 3 types of edges:
//   1. moved_decl => helper_decl
//   2. non_moved_decl => helper_decl
//   3. helper_decl => helper_decl
class HelperDeclRefGraph {
public:
  HelperDeclRefGraph() = default;
  ~HelperDeclRefGraph() = default;

  // Add a directed edge from the caller node to the callee node.
  // A new node will be created if the node for Caller/Callee doesn't exist.
  //
  // Note that, all class member declarations are represented by a single node
  // in the graph. The corresponding Decl of this node is the class declaration.
  void addEdge(const Decl *Caller, const Decl *Callee);
  CallGraphNode *getNode(const Decl *D) const;

  // Get all reachable nodes in the graph from the given declaration D's node,
  // including D.
  llvm::DenseSet<const CallGraphNode *> getReachableNodes(const Decl *D) const;

  // Dump the call graph for debug purpose.
  void dump() const;

private:
  void print(raw_ostream &OS) const;
  // Lookup a node for the given declaration D. If not found, insert a new
  // node into the graph.
  CallGraphNode *getOrInsertNode(Decl *D);

  typedef llvm::DenseMap<const Decl *, std::unique_ptr<CallGraphNode>>
      DeclMapTy;

  // DeclMap owns all CallGraphNodes.
  DeclMapTy DeclMap;
};

// A builder helps to construct a call graph of helper declarations.
class HelperDeclRGBuilder : public ast_matchers::MatchFinder::MatchCallback {
public:
  HelperDeclRGBuilder() : RG(new HelperDeclRefGraph) {}
  void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
  const HelperDeclRefGraph *getGraph() const { return RG.get(); }

  // Find out the outmost enclosing class/function declaration of a given D.
  // For a CXXMethodDecl, get its CXXRecordDecl; For a VarDecl/FunctionDecl, get
  // its outmost enclosing FunctionDecl or CXXRecordDecl.
  // Return D if not found.
  static const Decl *getOutmostClassOrFunDecl(const Decl *D);

private:
  std::unique_ptr<HelperDeclRefGraph> RG;
};

} // namespace move
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_MOVE_USED_HELPER_DECL_FINDER_H