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
85 lines
2.9 KiB
C++
85 lines
2.9 KiB
C++
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
#include "clang/Tooling/Tooling.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
using namespace clang;
|
|
using namespace ento;
|
|
using namespace ast_matchers;
|
|
|
|
class IsCLibraryFunctionTest : public testing::Test {
|
|
std::unique_ptr<ASTUnit> ASTUnitP;
|
|
const FunctionDecl *Result = nullptr;
|
|
|
|
public:
|
|
const FunctionDecl *getFunctionDecl() const { return Result; }
|
|
|
|
testing::AssertionResult buildAST(StringRef Code) {
|
|
ASTUnitP = tooling::buildASTFromCode(Code);
|
|
if (!ASTUnitP)
|
|
return testing::AssertionFailure() << "AST construction failed";
|
|
|
|
ASTContext &Context = ASTUnitP->getASTContext();
|
|
if (Context.getDiagnostics().hasErrorOccurred())
|
|
return testing::AssertionFailure() << "Compilation error";
|
|
|
|
auto Matches = ast_matchers::match(functionDecl().bind("fn"), Context);
|
|
if (Matches.empty())
|
|
return testing::AssertionFailure() << "No function declaration found";
|
|
|
|
if (Matches.size() > 1)
|
|
return testing::AssertionFailure()
|
|
<< "Multiple function declarations found";
|
|
|
|
Result = Matches[0].getNodeAs<FunctionDecl>("fn");
|
|
return testing::AssertionSuccess();
|
|
}
|
|
};
|
|
|
|
TEST_F(IsCLibraryFunctionTest, AcceptsGlobal) {
|
|
ASSERT_TRUE(buildAST(R"cpp(void fun();)cpp"));
|
|
EXPECT_TRUE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, AcceptsExternCGlobal) {
|
|
ASSERT_TRUE(buildAST(R"cpp(extern "C" { void fun(); })cpp"));
|
|
EXPECT_TRUE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, RejectsNoInlineNoExternalLinkage) {
|
|
// Functions that are neither inlined nor externally visible cannot be C
|
|
// library functions.
|
|
ASSERT_TRUE(buildAST(R"cpp(static void fun();)cpp"));
|
|
EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, RejectsAnonymousNamespace) {
|
|
ASSERT_TRUE(buildAST(R"cpp(namespace { void fun(); })cpp"));
|
|
EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, AcceptsStdNamespace) {
|
|
ASSERT_TRUE(buildAST(R"cpp(namespace std { void fun(); })cpp"));
|
|
EXPECT_TRUE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, RejectsOtherNamespaces) {
|
|
ASSERT_TRUE(buildAST(R"cpp(namespace stdx { void fun(); })cpp"));
|
|
EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, RejectsClassStatic) {
|
|
ASSERT_TRUE(buildAST(R"cpp(class A { static void fun(); };)cpp"));
|
|
EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|
|
|
|
TEST_F(IsCLibraryFunctionTest, RejectsClassMember) {
|
|
ASSERT_TRUE(buildAST(R"cpp(class A { void fun(); };)cpp"));
|
|
EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
|
|
}
|