[clang] [ssaf][UnsafeBufferUsage] Add JSON serialization for UnsafeBufferUsage (PR #187156)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 27 07:37:21 PDT 2026
================
@@ -184,6 +186,128 @@ TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageEntityPointerLevelSetTest) {
EXPECT_THAT(getSubsetOf(Set, E3), UnorderedElementsAre(P5));
}
+//////////////////////////////////////////////////////////////
+// JSON Tests //
+//////////////////////////////////////////////////////////////
+// Oracle JSON output for the example:
+// void foo(int ***p, int ****q, int x) {
+// p[5][5][5];
+// q[5][5][5][5];
+// }
+constexpr const char *const SerilizationTestOracle = R"cpp({
+ "UnsafeBuffers": [
+ [
+ {
+ "@": 42
+ },
+ 1
+ ],
+ [
+ {
+ "@": 42
+ },
+ 2
+ ],
+ [
+ {
+ "@": 42
+ },
+ 3
+ ],
+ [
+ {
+ "@": 108
+ },
+ 1
+ ],
+ [
+ {
+ "@": 108
+ },
+ 2
+ ],
+ [
+ {
+ "@": 108
+ },
+ 3
+ ],
+ [
+ {
+ "@": 108
+ },
+ 4
+ ]
+ ]
+})cpp";
+
+TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageSerializeTest) {
+ auto Sum = setUpTest(R"cpp(
+ void foo(int ***p, int ****q, int x) {
+ p[5][5][5];
+ q[5][5][5][5];
+ }
+ )cpp",
+ "foo");
+ ASSERT_NE(Sum, nullptr);
+ EXPECT_EQ(*Sum, makeSet(__LINE__, {{"p", 1U},
+ {"p", 2U},
+ {"p", 3U},
+ {"q", 1U},
+ {"q", 2U},
+ {"q", 3U},
+ {"q", 4U}}));
+
+ using Object = llvm::json::Object;
+
+ std::map<EntityId, uint64_t> DummyTable{{*getEntityId("p"), 42},
+ {*getEntityId("q"), 108}};
+ Object JData = UnsafeBufferUsageEntitySummary::jsonSerializeFn(
+ *Sum,
+ [&DummyTable](EntityId Id) { return Object{{"@", DummyTable[Id]}}; });
+
+ EXPECT_EQ(llvm::formatv("{0:2}", llvm::json::Value(std::move(JData))).str(),
+ SerilizationTestOracle);
+}
+
+TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageDeserializeTest) {
+ auto Sum = setUpTest(R"cpp(
+ void foo(int ***p, int ****q, int x) {
+ p[5][5][5];
+ q[5][5][5][5];
+ }
+ )cpp",
+ "foo");
+ ASSERT_NE(Sum, nullptr);
+ EXPECT_EQ(*Sum, makeSet(__LINE__, {{"p", 1U},
+ {"p", 2U},
+ {"p", 3U},
+ {"q", 1U},
+ {"q", 2U},
+ {"q", 3U},
+ {"q", 4U}}));
+
+ using Object = llvm::json::Object;
+ using Value = llvm::json::Value;
----------------
steakhal wrote:
I almost recommended to move this to the top of the file, but I think that's not actually what I want now that I'm thinking about it.
Rather, just move all of these test files to a specific `UnsafeBufferUsageJSONTest.cpp` and there you have the right to have these usings at the top because it actually makes sense for a JSON-specific test file - while here in this file it would be awkward.
https://github.com/llvm/llvm-project/pull/187156
More information about the cfe-commits
mailing list