[clang][ssaf][NFC] Hoist findFnByName and findDeclByName (#195056)

Split from #194448

This was already approved in

https://github.com/llvm/llvm-project/pull/194448#pullrequestreview-4201251523
This commit is contained in:
Balázs Benics
2026-04-30 12:17:14 +01:00
committed by GitHub
parent 1862fd21d6
commit 6ccbdc30b0
3 changed files with 60 additions and 52 deletions

View File

@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "clang/ScalableStaticAnalysisFramework/Core/ASTEntityMapping.h"
#include "FindDecl.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
@@ -20,23 +21,11 @@ using namespace clang::ast_matchers;
namespace clang::ssaf {
namespace {
// Helper function to find a declaration by name
template <typename DeclType>
const DeclType *findDecl(ASTContext &Ctx, StringRef Name) {
auto Matcher = namedDecl(hasName(Name)).bind("decl");
auto Matches = match(Matcher, Ctx);
if (Matches.empty())
return nullptr;
if (auto Result = Matches[0].getNodeAs<DeclType>("decl"))
return dyn_cast<DeclType>(Result->getCanonicalDecl());
return nullptr;
}
TEST(ASTEntityMappingTest, FunctionDecl) {
auto AST = tooling::buildASTFromCode(R"cpp(void foo() {})cpp");
auto &Ctx = AST->getASTContext();
const auto *FD = findDecl<FunctionDecl>(Ctx, "foo");
const auto *FD = findFnByName("foo", Ctx);
ASSERT_NE(FD, nullptr);
auto EntityName = getEntityName(FD);
@@ -47,7 +36,7 @@ TEST(ASTEntityMappingTest, VarDecl) {
auto AST = tooling::buildASTFromCode(R"cpp(int x = 42;)cpp");
auto &Ctx = AST->getASTContext();
const auto *VD = findDecl<VarDecl>(Ctx, "x");
const auto *VD = findDeclByName<VarDecl>("x", Ctx);
ASSERT_NE(VD, nullptr);
auto EntityName = getEntityName(VD);
@@ -58,7 +47,7 @@ TEST(ASTEntityMappingTest, ParmVarDecl) {
auto AST = tooling::buildASTFromCode(R"cpp(void foo(int x) {})cpp");
auto &Ctx = AST->getASTContext();
const auto *FD = findDecl<FunctionDecl>(Ctx, "foo");
const auto *FD = findFnByName("foo", Ctx);
ASSERT_NE(FD, nullptr);
ASSERT_EQ(FD->param_size(), 1u);
@@ -73,7 +62,7 @@ TEST(ASTEntityMappingTest, RecordDecl) {
auto AST = tooling::buildASTFromCode(R"cpp(struct S {};)cpp");
auto &Ctx = AST->getASTContext();
const auto *RD = findDecl<RecordDecl>(Ctx, "S");
const auto *RD = findDeclByName<RecordDecl>("S", Ctx);
ASSERT_NE(RD, nullptr);
auto EntityName = getEntityName(RD);
@@ -84,7 +73,7 @@ TEST(ASTEntityMappingTest, FieldDecl) {
auto AST = tooling::buildASTFromCode(R"cpp(struct S { int field; };)cpp");
auto &Ctx = AST->getASTContext();
const auto *FD = findDecl<FieldDecl>(Ctx, "field");
const auto *FD = findDeclByName<FieldDecl>("field", Ctx);
ASSERT_NE(FD, nullptr);
auto EntityName = getEntityName(FD);
@@ -143,7 +132,7 @@ TEST(ASTEntityMappingTest, UnsupportedDecl) {
auto AST = tooling::buildASTFromCode(R"cpp(namespace N {})cpp");
auto &Ctx = AST->getASTContext();
const auto *ND = findDecl<NamespaceDecl>(Ctx, "N");
const auto *ND = findDeclByName<NamespaceDecl>("N", Ctx);
ASSERT_NE(ND, nullptr);
auto EntityName = getEntityName(ND);
@@ -154,7 +143,7 @@ TEST(ASTEntityMappingTest, FunctionReturn) {
auto AST = tooling::buildASTFromCode(R"cpp(int foo() { return 42; })cpp");
auto &Ctx = AST->getASTContext();
const auto *FD = findDecl<FunctionDecl>(Ctx, "foo");
const auto *FD = findFnByName("foo", Ctx);
ASSERT_NE(FD, nullptr);
auto EntityName = getEntityNameForReturn(FD);
@@ -196,8 +185,8 @@ TEST(ASTEntityMappingTest, DifferentFunctionsDifferentNames) {
)cpp");
auto &Ctx = AST->getASTContext();
const auto *Foo = findDecl<FunctionDecl>(Ctx, "foo");
const auto *Bar = findDecl<FunctionDecl>(Ctx, "bar");
const auto *Foo = findFnByName("foo", Ctx);
const auto *Bar = findFnByName("bar", Ctx);
ASSERT_NE(Foo, nullptr);
ASSERT_NE(Bar, nullptr);

View File

@@ -7,9 +7,9 @@
//===----------------------------------------------------------------------===//
#include "clang/ScalableStaticAnalysisFramework/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h"
#include "FindDecl.h"
#include "TestFixture.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/ScalableStaticAnalysisFramework/Analyses/EntityPointerLevel/EntityPointerLevel.h"
#include "clang/ScalableStaticAnalysisFramework/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.h"
@@ -36,36 +36,6 @@ using namespace ssaf;
using testing::UnorderedElementsAre;
namespace {
template <typename SomeDecl = NamedDecl>
const SomeDecl *findDeclByName(StringRef Name, ASTContext &Ctx) {
class NamedDeclFinder : public DynamicRecursiveASTVisitor {
public:
StringRef SearchingName;
const NamedDecl *FoundDecl = nullptr;
NamedDeclFinder(StringRef SearchingName) : SearchingName(SearchingName) {}
bool VisitDecl(Decl *D) override {
if (const auto *ND = dyn_cast<SomeDecl>(D)) {
if (ND->getNameAsString() == SearchingName) {
FoundDecl = ND;
return false;
}
}
return true;
}
};
NamedDeclFinder Finder(Name);
Finder.TraverseDecl(Ctx.getTranslationUnitDecl());
return dyn_cast_or_null<SomeDecl>(Finder.FoundDecl);
}
const FunctionDecl *findFnByName(StringRef Name, ASTContext &Ctx) {
return findDeclByName<FunctionDecl>(Name, Ctx);
}
class UnsafeBufferUsageTest : public TestFixture {
protected:
TUSummary TUSum;

View File

@@ -0,0 +1,49 @@
//===- FindDecl.h -----------------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_UNITTESTS_SCALABLESTATICANALYSISFRAMEWORK_FINDDECL_H
#define LLVM_CLANG_UNITTESTS_SCALABLESTATICANALYSISFRAMEWORK_FINDDECL_H
#include "clang/AST/Decl.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
namespace clang::ssaf {
template <typename SomeDecl = clang::NamedDecl>
const SomeDecl *findDeclByName(StringRef Name, ASTContext &Ctx) {
class NamedDeclFinder : public DynamicRecursiveASTVisitor {
public:
StringRef SearchingName;
const NamedDecl *FoundDecl = nullptr;
NamedDeclFinder(StringRef SearchingName) : SearchingName(SearchingName) {}
bool VisitDecl(Decl *D) override {
if (const auto *ND = dyn_cast<SomeDecl>(D)) {
if (ND->getNameAsString() == SearchingName) {
FoundDecl = ND;
return false;
}
}
return true;
}
};
NamedDeclFinder Finder(Name);
Finder.TraverseDecl(Ctx.getTranslationUnitDecl());
return dyn_cast_or_null<SomeDecl>(Finder.FoundDecl);
}
inline const FunctionDecl *findFnByName(StringRef Name, ASTContext &Ctx) {
return findDeclByName<FunctionDecl>(Name, Ctx);
}
} // namespace clang::ssaf
#endif // LLVM_CLANG_UNITTESTS_SCALABLESTATICANALYSISFRAMEWORK_FINDDECL_H