Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

141 lines
4.6 KiB
C++

//===- SSAFAnalysesCommon.cpp ---------------------------------------------===//
//
// 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 "SSAFAnalysesCommon.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/ExprCXX.h"
#include <set>
using namespace clang;
std::string ssaf::describeJSONValue(const llvm::json::Value &V) {
return llvm::formatv("{0:2}", V).str();
}
std::string ssaf::describeJSONValue(const llvm::json::Array &A) {
return llvm::formatv("array of size {0}", A.size()).str();
}
std::string ssaf::describeJSONValue(const llvm::json::Object &O) {
return llvm::formatv("an object of {0} key(s)", O.size()).str();
}
namespace {
// Traverses the AST and finds contributors.
class ContributorFinder : public DynamicRecursiveASTVisitor {
public:
std::set<const NamedDecl *> Contributors;
bool VisitFunctionDecl(FunctionDecl *D) override {
Contributors.insert(D);
return true;
}
bool VisitRecordDecl(RecordDecl *D) override {
Contributors.insert(D);
return true;
}
bool VisitVarDecl(VarDecl *D) override {
DeclContext *DC = D->getDeclContext();
// Collects Decl for global variables or static data members:
if (DC->isFileContext() || D->isStaticDataMember())
Contributors.insert(D);
return true;
}
bool VisitLambdaExpr(LambdaExpr *L) override {
// TraverseLambdaExpr directly visits the body stmt, skipping the
// CXXMethodDecl, which is a contributor that needs to be collected.
VisitFunctionDecl(L->getCallOperator());
return true;
}
};
/// An AST visitor that skips the root node's strict-descendants that are
/// callable Decls and record Decls, because those are separate contributors.
///
/// Clients need to implement their own "MatchAction", which is a function that
/// takes a `DynTypedNode`, decides if the node matches and performs any further
/// callback actions.
/// ContributorFactFinder takes a reference to a "MatchAction". It does not own
/// the "MatchAction", which is usually stateful and may own containers.
class ContributorFactFinder : public DynamicRecursiveASTVisitor {
llvm::function_ref<void(const DynTypedNode &)> MatchActionRef;
const NamedDecl *RootDecl = nullptr;
template <typename NodeTy> void match(const NodeTy &Node) {
MatchActionRef(DynTypedNode::create(Node));
}
public:
ContributorFactFinder(
llvm::function_ref<void(const DynTypedNode &)> MatchActionRef)
: MatchActionRef(MatchActionRef) {
ShouldVisitTemplateInstantiations = true;
ShouldVisitImplicitCode = false;
}
// The entry point:
void findMatches(const NamedDecl *Contributor) {
RootDecl = Contributor;
TraverseDecl(const_cast<NamedDecl *>(Contributor));
}
bool TraverseDecl(Decl *Node) override {
if (!Node)
return true;
// To skip callables:
if (Node != RootDecl &&
isa<FunctionDecl, BlockDecl, ObjCMethodDecl, RecordDecl>(Node))
return true;
match(*Node);
return DynamicRecursiveASTVisitor::TraverseDecl(Node);
}
bool TraverseStmt(Stmt *Node) override {
if (!Node)
return true;
match(*Node);
return DynamicRecursiveASTVisitor::TraverseStmt(Node);
}
bool TraverseLambdaExpr(LambdaExpr *L) override {
// TODO: lambda captures of pointer variables (by copy or by reference)
// are currently not tracked. Each capture initializes an implicit closure
// field from the captured variable, which constitutes a pointer assignment
// edge that should be recorded here.
return true; // Skip lambda as it is a callable.
}
};
} // namespace
void ssaf::findContributors(ASTContext &Ctx,
std::vector<const NamedDecl *> &Contributors) {
ContributorFinder Finder;
Finder.TraverseAST(Ctx);
Contributors.insert(Contributors.end(), Finder.Contributors.begin(),
Finder.Contributors.end());
}
void ssaf::findMatchesIn(
const NamedDecl *Contributor,
llvm::function_ref<void(const DynTypedNode &)> MatchActionRef) {
ContributorFactFinder{MatchActionRef}.findMatches(Contributor);
}
llvm::Error clang::ssaf::makeEntityNameErr(clang::ASTContext &Ctx,
const clang::NamedDecl *D) {
return makeErrAtNode(Ctx, D, "failed to create entity name for %s",
D->getNameAsString().data());
}