11 #include "clang/AST/Decl.h" 12 #include "llvm/Support/Debug.h" 15 #define DEBUG_TYPE "clang-move" 20 void HelperDeclRefGraph::print(raw_ostream &OS)
const {
21 OS <<
" --- Call graph Dump --- \n";
22 for (
auto I = DeclMap.begin(); I != DeclMap.end(); ++I) {
23 const CallGraphNode *N = (I->second).
get();
25 OS <<
" Declarations: ";
27 OS <<
" (" << N <<
") ";
29 for (
auto CI = N->begin(), CE = N->end(); CI != CE; ++CI) {
31 OS <<
" (" << CI <<
") ";
46 if (Caller == Callee)
return;
49 CallGraphNode *CallerNode = getOrInsertNode(const_cast<Decl *>(Caller));
50 CallGraphNode *CalleeNode = getOrInsertNode(const_cast<Decl *>(Callee));
51 CallerNode->addCallee(CalleeNode);
56 CallGraphNode *HelperDeclRefGraph::getOrInsertNode(Decl *F) {
57 F = F->getCanonicalDecl();
58 std::unique_ptr<CallGraphNode> &Node = DeclMap[F];
62 Node = llvm::make_unique<CallGraphNode>(F);
67 auto I = DeclMap.find(D->getCanonicalDecl());
68 return I == DeclMap.end() ? nullptr : I->second.get();
71 llvm::DenseSet<const CallGraphNode *>
73 const auto *RootNode =
getNode(Root);
76 llvm::DenseSet<const CallGraphNode *> ConnectedNodes;
77 std::function<void(const CallGraphNode *)> VisitNode =
78 [&](
const CallGraphNode *Node) {
79 if (ConnectedNodes.count(Node))
81 ConnectedNodes.insert(Node);
82 for (
auto It = Node->begin(), End = Node->end(); It != End; ++It)
87 return ConnectedNodes;
91 const auto *DC = D->getDeclContext();
94 if (
const auto *RD = dyn_cast<CXXRecordDecl>(DC))
96 else if (
const auto *FD = dyn_cast<FunctionDecl>(DC))
104 const ast_matchers::MatchFinder::MatchResult &
Result) {
115 if (
const auto *FuncRef = Result.Nodes.getNodeAs<DeclRefExpr>(
"func_ref")) {
116 const auto *DC = Result.Nodes.getNodeAs<Decl>(
"dc");
118 LLVM_DEBUG(llvm::dbgs() <<
"Find helper function usage: " 119 << FuncRef->getDecl()->getNameAsString() <<
" (" 120 << FuncRef->getDecl() <<
")\n");
122 getOutmostClassOrFunDecl(DC->getCanonicalDecl()),
123 getOutmostClassOrFunDecl(FuncRef->getDecl()->getCanonicalDecl()));
124 }
else if (
const auto *UsedClass =
125 Result.Nodes.getNodeAs<CXXRecordDecl>(
"used_class")) {
126 const auto *DC = Result.Nodes.getNodeAs<Decl>(
"dc");
128 LLVM_DEBUG(llvm::dbgs()
129 <<
"Find helper class usage: " << UsedClass->getNameAsString()
130 <<
" (" << UsedClass <<
")\n");
131 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)
CallGraphNode * getNode(const Decl *D) const
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result