================ @@ -0,0 +1,110 @@ +//===- CallGraphExtractor.cpp - Call Graph Summary Extractor --------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" +#include "clang/Analysis/AnalysisDeclContext.h" +#include "clang/Analysis/CallGraph.h" +#include "clang/Basic/SourceManager.h" +#include "clang/ScalableStaticAnalysisFramework/Analyses/CallGraph/CallGraphSummary.h" +#include "clang/ScalableStaticAnalysisFramework/Core/ASTEntityMapping.h" +#include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/ExtractorRegistry.h" +#include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/TUSummaryBuilder.h" +#include "llvm/ADT/STLExtras.h" +#include <memory> + +using namespace clang; +using namespace ssaf; + +namespace { +class CallGraphExtractor final : public TUSummaryExtractor { +public: + using TUSummaryExtractor::TUSummaryExtractor; + +private: + void HandleTranslationUnit(ASTContext &Ctx) override; + + void handleCallGraphNode(const ASTContext &Ctx, const CallGraphNode *N); +}; +} // namespace + +void CallGraphExtractor::HandleTranslationUnit(ASTContext &Ctx) { + CallGraph CG; + CG.addToCallGraph( + const_cast<TranslationUnitDecl *>(Ctx.getTranslationUnitDecl())); + + for (const auto &N : llvm::make_second_range(CG)) { + if (N && N->getDecl() && N->getDefinition()) + handleCallGraphNode(Ctx, N.get()); + } +} + +void CallGraphExtractor::handleCallGraphNode(const ASTContext &Ctx, + const CallGraphNode *N) { + const FunctionDecl *Definition = N->getDefinition(); + + // Ignore templates for now. + if (Definition->isTemplated()) + return; + + auto CallerName = getEntityName(Definition); + if (!CallerName) + return; + + auto FnSummary = std::make_unique<CallGraphSummary>(); + + PresumedLoc Loc = + Ctx.getSourceManager().getPresumedLoc(Definition->getLocation()); + FnSummary->Definition.File = Loc.getFilename(); + FnSummary->Definition.Line = Loc.getLine(); + FnSummary->Definition.Column = Loc.getColumn(); + FnSummary->PrettyName = AnalysisDeclContext::getFunctionName(Definition); + + for (const auto &Record : N->callees()) { + const Decl *CalleeDecl = Record.Callee->getDecl(); + if (!CalleeDecl) { + FnSummary->HasIndirectCalls = true; + continue; + } + assert(!CalleeDecl->isTemplated()); + + // Objective-C methods might be replaced at runtime, so they are effectively + // indirect calls. + if (isa<ObjCMethodDecl>(CalleeDecl)) { + FnSummary->HasIndirectCalls = true; + continue; + } + + // Treat virtual functions as indirect calls for now. + if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CalleeDecl); ---------------- steakhal wrote:
Reworked in 02be6403cc44c2b3658078b4fd6b985b7fe0b07b https://github.com/llvm/llvm-project/pull/188753 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
