12 #include "clang/AST/Decl.h" 13 #include "llvm/Support/Debug.h" 16 #define DEBUG_TYPE "clang-move" 21 void HelperDeclRefGraph::print(raw_ostream &OS)
const {
22 OS <<
" --- Call graph Dump --- \n";
23 for (
auto I = DeclMap.begin(); I != DeclMap.end(); ++I) {
24 const CallGraphNode *N = (I->second).
get();
26 OS <<
" Declarations: ";
28 OS <<
" (" << N <<
") ";
30 for (
auto CI = N->begin(), CE = N->end(); CI != CE; ++CI) {
32 OS <<
" (" << CI <<
") ";
47 if (Caller == Callee)
return;
50 CallGraphNode *CallerNode = getOrInsertNode(const_cast<Decl *>(Caller));
51 CallGraphNode *CalleeNode = getOrInsertNode(const_cast<Decl *>(Callee));
52 CallerNode->addCallee(CalleeNode);
57 CallGraphNode *HelperDeclRefGraph::getOrInsertNode(Decl *F) {
58 F = F->getCanonicalDecl();
59 std::unique_ptr<CallGraphNode> &Node = DeclMap[F];
63 Node = llvm::make_unique<CallGraphNode>(F);
68 auto I = DeclMap.find(D->getCanonicalDecl());
69 return I == DeclMap.end() ? nullptr : I->second.get();
72 llvm::DenseSet<const CallGraphNode *>
74 const auto *RootNode =
getNode(Root);
77 llvm::DenseSet<const CallGraphNode *> ConnectedNodes;
78 std::function<void(const CallGraphNode *)> VisitNode =
79 [&](
const CallGraphNode *Node) {
80 if (ConnectedNodes.count(Node))
82 ConnectedNodes.insert(Node);
83 for (
auto It = Node->begin(), End = Node->end(); It != End; ++It)
88 return ConnectedNodes;
92 const auto *DC = D->getDeclContext();
95 if (
const auto *RD = dyn_cast<CXXRecordDecl>(DC))
97 else if (
const auto *FD = dyn_cast<FunctionDecl>(DC))
105 const ast_matchers::MatchFinder::MatchResult &
Result) {
116 if (
const auto *FuncRef = Result.Nodes.getNodeAs<DeclRefExpr>(
"func_ref")) {
117 const auto *DC = Result.Nodes.getNodeAs<Decl>(
"dc");
119 LLVM_DEBUG(llvm::dbgs() <<
"Find helper function usage: " 120 << FuncRef->getDecl()->getNameAsString() <<
" (" 121 << FuncRef->getDecl() <<
")\n");
123 getOutmostClassOrFunDecl(DC->getCanonicalDecl()),
124 getOutmostClassOrFunDecl(FuncRef->getDecl()->getCanonicalDecl()));
125 }
else if (
const auto *UsedClass =
126 Result.Nodes.getNodeAs<CXXRecordDecl>(
"used_class")) {
127 const auto *DC = Result.Nodes.getNodeAs<Decl>(
"dc");
129 LLVM_DEBUG(llvm::dbgs()
130 <<
"Find helper class usage: " << UsedClass->getNameAsString()
131 <<
" (" << UsedClass <<
")\n");
132 RG->addEdge(getOutmostClassOrFunDecl(DC->getCanonicalDecl()), UsedClass);
llvm::DenseSet< const CallGraphNode * > getReachableNodes(const Decl *D) const
void addEdge(const Decl *Caller, const Decl *Callee)
static const Decl * getOutmostClassOrFunDecl(const Decl *D)
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
CallGraphNode * getNode(const Decl *D) const
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void run(const ast_matchers::MatchFinder::MatchResult &Result) override