[clang] [clang][ssaf] Add UnsafeBufferUsage summary data structures (PR #181067)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 12 04:13:04 PST 2026
================
@@ -0,0 +1,93 @@
+//===- unittests/Analysis/Scalable/UnsafeBufferUsageExtractorTest.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 "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h"
+#include "clang/Analysis/Scalable/Model/EntityId.h"
+#include "clang/Analysis/Scalable/Model/EntityIdTable.h"
+#include "clang/Analysis/Scalable/Model/EntityName.h"
+#include "gtest/gtest.h"
+#include <memory>
+
+using namespace clang::ssaf;
+using namespace clang;
+
+namespace {
+
+class UnsafeBufferUsageTest : public testing::Test {
+protected:
+ EntityIdTable Table;
+ UnsafeBufferUsageTUSummaryBuilder Builder;
+};
+
+//////////////////////////////////////////////////////////////
+// Data Structure Tests //
+//////////////////////////////////////////////////////////////
+
+TEST_F(UnsafeBufferUsageTest, PointerKindVariableComparison) {
+ EntityId E1 = Table.getId({"c:@F at foo", "", {}});
+ EntityId E2 = Table.getId({"c:@F at bar", "", {}});
+
+ auto P1 = Builder.buildPointerKindVariable(E1, 2);
+ auto P2 = Builder.buildPointerKindVariable(E1, 2);
+ auto P3 = Builder.buildPointerKindVariable(E1, 1);
+ auto P4 = Builder.buildPointerKindVariable(E2, 2);
+
+ EXPECT_EQ(P1, P2);
+ EXPECT_NE(P1, P3);
+ EXPECT_NE(P1, P4);
+ EXPECT_NE(P3, P4);
+ EXPECT_TRUE(P3 < P2);
+ EXPECT_TRUE(P3 < P4);
+ EXPECT_FALSE(P1 < P2);
+ EXPECT_FALSE(P2 < P1);
+}
+
+TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageEntitySummaryTest) {
+ EntityId E1 = Table.getId({"c:@F at foo", "", {}});
+ EntityId E2 = Table.getId({"c:@F at bar", "", {}});
+ EntityId E3 = Table.getId({"c:@F at baz", "", {}});
+
+ auto P1 = Builder.buildPointerKindVariable(E1, 1);
+ auto P2 = Builder.buildPointerKindVariable(E1, 2);
+ auto P3 = Builder.buildPointerKindVariable(E2, 1);
+ auto P4 = Builder.buildPointerKindVariable(E2, 2);
+ auto P5 = Builder.buildPointerKindVariable(E3, 1);
+ auto P6 = Builder.buildPointerKindVariable(E3, 2);
+
+ PointerKindVariableSet Set{P1, P2, P3, P4, P5};
+ auto ES = Builder.buildUnsafeBufferUsageEntitySummary(std::move(Set));
+
+ EXPECT_NE(ES->find(P1), ES->end());
----------------
steakhal wrote:
`find() != end` appears so often here and in the next PR, maybe it would make more sense to have a `contains` method as well?
https://github.com/llvm/llvm-project/pull/181067
More information about the cfe-commits
mailing list