[clang] [clang][ssaf] Add UnsafeBufferUsage summary data structures (PR #181067)
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 18 12:09:52 PST 2026
https://github.com/ziqingluo-90 updated https://github.com/llvm/llvm-project/pull/181067
>From e7da74f442e0bfcd175961b52cdb38e2bf85445b Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Wed, 11 Feb 2026 15:01:59 -0800
Subject: [PATCH 1/7] [clang][ssaf] Add UnsafeBufferUsage summary data
structures
An UnsafeBufferUsage summary is the ssaf representation of the set of
unsafe buffer pointers found by `-Wunsafe-buffer-usage` in a
translation unit.
rdar://170176278
---
.../Scalable/Analyses/UnsafeBufferUsage.h | 116 ++++++++++++++++++
.../Analysis/Scalable/CMakeLists.txt | 1 +
.../Scalable/UnsafeBufferUsageTest.cpp | 93 ++++++++++++++
3 files changed, 210 insertions(+)
create mode 100644 clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
create mode 100644 clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
new file mode 100644
index 0000000000000..404e31bbd81c6
--- /dev/null
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
@@ -0,0 +1,116 @@
+//===- UnsafeBufferUsage.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_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_H
+#define LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_H
+
+#include "clang/Analysis/Scalable/Model/EntityId.h"
+#include "clang/Analysis/Scalable/TUSummary/EntitySummary.h"
+#include "clang/Analysis/Scalable/TUSummary/TUSummaryBuilder.h"
+#include "clang/Analysis/Scalable/TUSummary/TUSummaryExtractor.h"
+#include "llvm/ADT/SmallVector.h"
+#include <limits>
+#include <memory>
+#include <set>
+
+namespace clang::ssaf {
+
+/// A PointerKindVariable is associated with a pointer type as (a spelling part
+/// of) the declared type of an entity. In other words, a PointerKindVariable
+/// is associated with a `*` in the fully expanded spelling of the declared
+/// type.
+///
+/// For example, for `int **p;`, there are two PointerKindVariables. One is
+/// associated with `int **` and the other is associated with `int *`.
+///
+/// A PointerKindVariable can be identified by an EntityId, of which the
+/// declared type is a pointer type, and an unsigned integer indicating the
+/// pointer level with 1 referring to the whole declared pointer type.
+///
+/// For the same example `int **p;`, the two PointerKindVariables are:
+/// `(p, 1)` for `int **` and `(p, 2)` for `int *`.
+///
+/// Reserve pointer level value 0 for implementation-internal use.
+class PointerKindVariable {
+ const EntityId Entity;
+ const unsigned PointerLevel;
+
+ friend class UnsafeBufferUsageTUSummaryBuilder;
+ friend class UnsafeBufferUsageEntitySummary;
+
+ PointerKindVariable(EntityId Entity, unsigned PointerLevel)
+ : Entity(Entity), PointerLevel(PointerLevel) {}
+
+public:
+ EntityId getEntity() const { return Entity; }
+ unsigned getPointerLevel() const { return PointerLevel; }
+
+ bool operator==(const PointerKindVariable &Other) const {
+ return Entity == Other.Entity && PointerLevel == Other.PointerLevel;
+ }
+
+ bool operator!=(const PointerKindVariable &Other) const {
+ return !(*this == Other);
+ }
+
+ bool operator<(const PointerKindVariable &Other) const {
+ if (Entity == Other.Entity)
+ return PointerLevel < Other.PointerLevel;
+ return Entity < Other.Entity;
+ }
+};
+
+using PointerKindVariableSet = std::set<PointerKindVariable>;
+
+/// An UnsafeBufferUsageEntitySummary is an immutable set of unsafe buffers, in
+/// the form of PointerKindVariable.
+class UnsafeBufferUsageEntitySummary : public EntitySummary {
+ const PointerKindVariableSet UnsafeBuffers;
+
+ friend class UnsafeBufferUsageTUSummaryBuilder;
+
+ UnsafeBufferUsageEntitySummary(PointerKindVariableSet &&UnsafeBuffers)
+ : EntitySummary(SummaryName{"UnsafeBufferUsage"}),
+ UnsafeBuffers(std::move(UnsafeBuffers)) {}
+
+public:
+ using const_iterator = PointerKindVariableSet::const_iterator;
+
+ const_iterator begin() const { return UnsafeBuffers.begin(); }
+ const_iterator end() const { return UnsafeBuffers.end(); }
+
+ const_iterator find(const PointerKindVariable &V) const {
+ return UnsafeBuffers.find(V);
+ }
+
+ llvm::iterator_range<const_iterator> getSubsetOf(EntityId Entity) const {
+ auto Begin = UnsafeBuffers.lower_bound({Entity, 0});
+ auto End = UnsafeBuffers.upper_bound(
+ {Entity, std::numeric_limits<unsigned>::max()});
+ return {Begin, End};
+ }
+
+ size_t getNumUnsafeBuffers() { return UnsafeBuffers.size(); }
+};
+
+class UnsafeBufferUsageTUSummaryBuilder : public TUSummaryBuilder {
+public:
+ PointerKindVariable buildPointerKindVariable(EntityId Entity,
+ unsigned PointerLevel) {
+ return {Entity, PointerLevel};
+ }
+
+ std::unique_ptr<UnsafeBufferUsageEntitySummary>
+ buildUnsafeBufferUsageEntitySummary(PointerKindVariableSet &&UnsafeBuffers) {
+ return std::unique_ptr<UnsafeBufferUsageEntitySummary>(
+ new UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers)));
+ }
+};
+} // namespace clang::ssaf
+
+#endif // LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_H
diff --git a/clang/unittests/Analysis/Scalable/CMakeLists.txt b/clang/unittests/Analysis/Scalable/CMakeLists.txt
index 601845b4ab77a..bc766bd41c0f4 100644
--- a/clang/unittests/Analysis/Scalable/CMakeLists.txt
+++ b/clang/unittests/Analysis/Scalable/CMakeLists.txt
@@ -11,6 +11,7 @@ add_distinct_clang_unittest(ClangScalableAnalysisTests
Registries/SerializationFormatRegistryTest.cpp
Registries/SummaryExtractorRegistryTest.cpp
SummaryNameTest.cpp
+ UnsafeBufferUsageTest.cpp
CLANG_LIBS
clangAnalysisScalable
diff --git a/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp b/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
new file mode 100644
index 0000000000000..a57900cca59c0
--- /dev/null
+++ b/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
@@ -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());
+ EXPECT_NE(ES->find(P2), ES->end());
+ EXPECT_NE(ES->find(P3), ES->end());
+ EXPECT_NE(ES->find(P4), ES->end());
+ EXPECT_NE(ES->find(P5), ES->end());
+ EXPECT_EQ(ES->find(P6), ES->end());
+
+ PointerKindVariableSet Subset1{ES->getSubsetOf(E1).begin(),
+ ES->getSubsetOf(E1).end()};
+
+ EXPECT_NE(Subset1.find(P1), Subset1.end());
+ EXPECT_NE(Subset1.find(P2), Subset1.end());
+ EXPECT_EQ(Subset1.size(), static_cast<size_t>(2));
+
+ PointerKindVariableSet Subset2{ES->getSubsetOf(E2).begin(),
+ ES->getSubsetOf(E2).end()};
+
+ EXPECT_NE(Subset2.find(P3), Subset2.end());
+ EXPECT_NE(Subset2.find(P4), Subset2.end());
+ EXPECT_EQ(Subset2.size(), static_cast<size_t>(2));
+
+ PointerKindVariableSet Subset3{ES->getSubsetOf(E3).begin(),
+ ES->getSubsetOf(E3).end()};
+
+ EXPECT_NE(Subset3.find(P5), Subset3.end());
+ EXPECT_EQ(Subset3.find(P6), Subset3.end());
+ EXPECT_EQ(Subset3.size(), static_cast<size_t>(1));
+}
+} // namespace
>From 972a73cd02686694892cd217832fb9e7adf2f55d Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Thu, 12 Feb 2026 15:16:57 -0800
Subject: [PATCH 2/7] address comments
---
.../Scalable/Analyses/UnsafeBufferUsage.h | 65 ++++++++++++-------
.../Scalable/UnsafeBufferUsageTest.cpp | 38 ++++++-----
2 files changed, 62 insertions(+), 41 deletions(-)
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
index 404e31bbd81c6..82be4de8139ba 100644
--- a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
@@ -10,18 +10,18 @@
#define LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_H
#include "clang/Analysis/Scalable/Model/EntityId.h"
+#include "clang/Analysis/Scalable/Model/SummaryName.h"
#include "clang/Analysis/Scalable/TUSummary/EntitySummary.h"
#include "clang/Analysis/Scalable/TUSummary/TUSummaryBuilder.h"
#include "clang/Analysis/Scalable/TUSummary/TUSummaryExtractor.h"
-#include "llvm/ADT/SmallVector.h"
-#include <limits>
+#include "llvm/ADT/iterator_range.h"
#include <memory>
#include <set>
namespace clang::ssaf {
/// A PointerKindVariable is associated with a pointer type as (a spelling part
-/// of) the declared type of an entity. In other words, a PointerKindVariable
+/// of) the declared type of an entity. In other words, a PointerKindVariable
/// is associated with a `*` in the fully expanded spelling of the declared
/// type.
///
@@ -33,12 +33,16 @@ namespace clang::ssaf {
/// pointer level with 1 referring to the whole declared pointer type.
///
/// For the same example `int **p;`, the two PointerKindVariables are:
-/// `(p, 1)` for `int **` and `(p, 2)` for `int *`.
+/// '(p, 1)' for `int **` and '(p, 2)' for `int *`.
///
-/// Reserve pointer level value 0 for implementation-internal use.
+/// Reserve pointer level value 0 for representing address-of expressions in
+/// implementations internally. For the same example, `&p` can be represented as
+/// '(p, 0)'. With that, the PointerKindVariable derived from pointer expression
+/// `*(&p)` or `&(*p)` is naturally equivalent to the one derived from
+/// expression `p`.
class PointerKindVariable {
- const EntityId Entity;
- const unsigned PointerLevel;
+ EntityId Entity;
+ unsigned PointerLevel;
friend class UnsafeBufferUsageTUSummaryBuilder;
friend class UnsafeBufferUsageEntitySummary;
@@ -59,24 +63,38 @@ class PointerKindVariable {
}
bool operator<(const PointerKindVariable &Other) const {
- if (Entity == Other.Entity)
- return PointerLevel < Other.PointerLevel;
- return Entity < Other.Entity;
+ return std::tie(Entity, PointerLevel) <
+ std::tie(Other.Entity, Other.PointerLevel);
}
+
+ // Comparator supporting partial comparison against EntityId:
+ struct Comparator {
+ using is_transparent = void;
+ bool operator()(const PointerKindVariable &L,
+ const PointerKindVariable &R) const {
+ return L < R;
+ }
+ bool operator()(const EntityId &L, const PointerKindVariable &R) const {
+ return L < R.getEntity();
+ }
+ bool operator()(const PointerKindVariable &L, const EntityId &R) const {
+ return L.getEntity() < R;
+ }
+ };
};
-using PointerKindVariableSet = std::set<PointerKindVariable>;
+using PointerKindVariableSet =
+ std::set<PointerKindVariable, PointerKindVariable::Comparator>;
/// An UnsafeBufferUsageEntitySummary is an immutable set of unsafe buffers, in
/// the form of PointerKindVariable.
-class UnsafeBufferUsageEntitySummary : public EntitySummary {
+class UnsafeBufferUsageEntitySummary final : public EntitySummary {
const PointerKindVariableSet UnsafeBuffers;
friend class UnsafeBufferUsageTUSummaryBuilder;
UnsafeBufferUsageEntitySummary(PointerKindVariableSet &&UnsafeBuffers)
- : EntitySummary(SummaryName{"UnsafeBufferUsage"}),
- UnsafeBuffers(std::move(UnsafeBuffers)) {}
+ : EntitySummary(), UnsafeBuffers(std::move(UnsafeBuffers)) {}
public:
using const_iterator = PointerKindVariableSet::const_iterator;
@@ -89,26 +107,27 @@ class UnsafeBufferUsageEntitySummary : public EntitySummary {
}
llvm::iterator_range<const_iterator> getSubsetOf(EntityId Entity) const {
- auto Begin = UnsafeBuffers.lower_bound({Entity, 0});
- auto End = UnsafeBuffers.upper_bound(
- {Entity, std::numeric_limits<unsigned>::max()});
- return {Begin, End};
+ return llvm::make_range(UnsafeBuffers.equal_range(Entity));
}
size_t getNumUnsafeBuffers() { return UnsafeBuffers.size(); }
+
+ SummaryName getSummaryName() const override {
+ return SummaryName{"UnsafeBufferUsage"};
+ };
};
class UnsafeBufferUsageTUSummaryBuilder : public TUSummaryBuilder {
public:
- PointerKindVariable buildPointerKindVariable(EntityId Entity,
- unsigned PointerLevel) {
+ static PointerKindVariable buildPointerKindVariable(EntityId Entity,
+ unsigned PointerLevel) {
return {Entity, PointerLevel};
}
- std::unique_ptr<UnsafeBufferUsageEntitySummary>
+ static std::unique_ptr<UnsafeBufferUsageEntitySummary>
buildUnsafeBufferUsageEntitySummary(PointerKindVariableSet &&UnsafeBuffers) {
- return std::unique_ptr<UnsafeBufferUsageEntitySummary>(
- new UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers)));
+ return std::make_unique<UnsafeBufferUsageEntitySummary>(
+ UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers)));
}
};
} // namespace clang::ssaf
diff --git a/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp b/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
index a57900cca59c0..fe97d4a6ac177 100644
--- a/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
+++ b/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
@@ -1,4 +1,4 @@
-//===- unittests/Analysis/Scalable/UnsafeBufferUsageExtractorTest.cpp ----===//
+//===------- unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -9,12 +9,11 @@
#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;
+using namespace ssaf;
namespace {
@@ -28,6 +27,9 @@ class UnsafeBufferUsageTest : public testing::Test {
// Data Structure Tests //
//////////////////////////////////////////////////////////////
+#define EXPECT_CONTAINS(Set, Elt) EXPECT_NE((Set)->find(Elt), (Set)->end());
+#define EXPECT_EXCLUDES(Set, Elt) EXPECT_EQ((Set)->find(Elt), (Set)->end());
+
TEST_F(UnsafeBufferUsageTest, PointerKindVariableComparison) {
EntityId E1 = Table.getId({"c:@F at foo", "", {}});
EntityId E2 = Table.getId({"c:@F at bar", "", {}});
@@ -62,32 +64,32 @@ TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageEntitySummaryTest) {
PointerKindVariableSet Set{P1, P2, P3, P4, P5};
auto ES = Builder.buildUnsafeBufferUsageEntitySummary(std::move(Set));
- EXPECT_NE(ES->find(P1), ES->end());
- EXPECT_NE(ES->find(P2), ES->end());
- EXPECT_NE(ES->find(P3), ES->end());
- EXPECT_NE(ES->find(P4), ES->end());
- EXPECT_NE(ES->find(P5), ES->end());
- EXPECT_EQ(ES->find(P6), ES->end());
+ EXPECT_CONTAINS(ES, P1);
+ EXPECT_CONTAINS(ES, P2);
+ EXPECT_CONTAINS(ES, P3);
+ EXPECT_CONTAINS(ES, P4);
+ EXPECT_CONTAINS(ES, P5);
+ EXPECT_EXCLUDES(ES, P6);
PointerKindVariableSet Subset1{ES->getSubsetOf(E1).begin(),
ES->getSubsetOf(E1).end()};
- EXPECT_NE(Subset1.find(P1), Subset1.end());
- EXPECT_NE(Subset1.find(P2), Subset1.end());
- EXPECT_EQ(Subset1.size(), static_cast<size_t>(2));
+ EXPECT_CONTAINS(&Subset1, P1);
+ EXPECT_CONTAINS(&Subset1, P2);
+ EXPECT_EQ(Subset1.size(), 2U);
PointerKindVariableSet Subset2{ES->getSubsetOf(E2).begin(),
ES->getSubsetOf(E2).end()};
- EXPECT_NE(Subset2.find(P3), Subset2.end());
- EXPECT_NE(Subset2.find(P4), Subset2.end());
- EXPECT_EQ(Subset2.size(), static_cast<size_t>(2));
+ EXPECT_CONTAINS(&Subset2, P3);
+ EXPECT_CONTAINS(&Subset2, P4);
+ EXPECT_EQ(Subset2.size(), 2U);
PointerKindVariableSet Subset3{ES->getSubsetOf(E3).begin(),
ES->getSubsetOf(E3).end()};
- EXPECT_NE(Subset3.find(P5), Subset3.end());
- EXPECT_EQ(Subset3.find(P6), Subset3.end());
- EXPECT_EQ(Subset3.size(), static_cast<size_t>(1));
+ EXPECT_CONTAINS(&Subset3, P5);
+ EXPECT_EXCLUDES(&Subset3, P6);
+ EXPECT_EQ(Subset3.size(), 1U);
}
} // namespace
>From f7d6e887102a15b402461384c765e099ae1d803b Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Thu, 12 Feb 2026 15:40:16 -0800
Subject: [PATCH 3/7] Move files to their separate folders
---
.../Analyses/{ => UnsafeBufferUsage}/UnsafeBufferUsage.h | 0
.../{ => Analyses/UnsafeBufferUsage}/UnsafeBufferUsageTest.cpp | 2 +-
clang/unittests/Analysis/Scalable/CMakeLists.txt | 2 +-
3 files changed, 2 insertions(+), 2 deletions(-)
rename clang/include/clang/Analysis/Scalable/Analyses/{ => UnsafeBufferUsage}/UnsafeBufferUsage.h (100%)
rename clang/unittests/Analysis/Scalable/{ => Analyses/UnsafeBufferUsage}/UnsafeBufferUsageTest.cpp (97%)
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
similarity index 100%
rename from clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h
rename to clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
diff --git a/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp b/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
similarity index 97%
rename from clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
rename to clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
index fe97d4a6ac177..f0e032a897131 100644
--- a/clang/unittests/Analysis/Scalable/UnsafeBufferUsageTest.cpp
+++ b/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage.h"
+#include "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h"
#include "clang/Analysis/Scalable/Model/EntityId.h"
#include "clang/Analysis/Scalable/Model/EntityIdTable.h"
#include "gtest/gtest.h"
diff --git a/clang/unittests/Analysis/Scalable/CMakeLists.txt b/clang/unittests/Analysis/Scalable/CMakeLists.txt
index bc766bd41c0f4..e93ddd40ab754 100644
--- a/clang/unittests/Analysis/Scalable/CMakeLists.txt
+++ b/clang/unittests/Analysis/Scalable/CMakeLists.txt
@@ -11,7 +11,7 @@ add_distinct_clang_unittest(ClangScalableAnalysisTests
Registries/SerializationFormatRegistryTest.cpp
Registries/SummaryExtractorRegistryTest.cpp
SummaryNameTest.cpp
- UnsafeBufferUsageTest.cpp
+ Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
CLANG_LIBS
clangAnalysisScalable
>From 91cfa39eb42078afec04641175ab5ccf5637bb90 Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Fri, 13 Feb 2026 16:18:34 -0800
Subject: [PATCH 4/7] address comments
---
.../UnsafeBufferUsage/UnsafeBufferUsage.h | 85 ++++++++-----------
.../UnsafeBufferUsageBuilder.h | 27 ++++++
.../UnsafeBufferUsageTest.cpp | 38 ++++-----
3 files changed, 81 insertions(+), 69 deletions(-)
create mode 100644 clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
index 82be4de8139ba..0a7381daf7eb2 100644
--- a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
@@ -12,57 +12,54 @@
#include "clang/Analysis/Scalable/Model/EntityId.h"
#include "clang/Analysis/Scalable/Model/SummaryName.h"
#include "clang/Analysis/Scalable/TUSummary/EntitySummary.h"
-#include "clang/Analysis/Scalable/TUSummary/TUSummaryBuilder.h"
-#include "clang/Analysis/Scalable/TUSummary/TUSummaryExtractor.h"
#include "llvm/ADT/iterator_range.h"
-#include <memory>
#include <set>
namespace clang::ssaf {
-/// A PointerKindVariable is associated with a pointer type as (a spelling part
-/// of) the declared type of an entity. In other words, a PointerKindVariable
-/// is associated with a `*` in the fully expanded spelling of the declared
-/// type.
+/// An EntityPointerLevel represents a level of the declared pointer
+/// type of an entity. In the fully-expanded spelling of the declared type, a
+/// EntityPointerLevel is associated with a '*' in that declaration.
///
-/// For example, for `int **p;`, there are two PointerKindVariables. One is
-/// associated with `int **` and the other is associated with `int *`.
+/// For example, for 'int **p;', there are two EntityPointerLevels. One
+/// is associated with 'int **' of 'p' and the other is associated with 'int *'
+/// of 'p'.
///
-/// A PointerKindVariable can be identified by an EntityId, of which the
-/// declared type is a pointer type, and an unsigned integer indicating the
-/// pointer level with 1 referring to the whole declared pointer type.
+/// An EntityPointerLevel can be identified by an EntityId and an unsigned
+/// integer indicating the pointer level: '(EntityId, PointerLevel)'. A
+/// EntityPointerLevel 'P' is valid iff
+/// - 'P.EntityId' has a pointer type with at least 'P.PointerLevel' levels
+/// (This implies 'P.PointerLevel > 0');
+/// - 'P.EntityId' identifies an lvalue object and 'P.PointerLevel == 0'.
+/// The latter case represents address-of expressions.
///
-/// For the same example `int **p;`, the two PointerKindVariables are:
-/// '(p, 1)' for `int **` and '(p, 2)' for `int *`.
-///
-/// Reserve pointer level value 0 for representing address-of expressions in
-/// implementations internally. For the same example, `&p` can be represented as
-/// '(p, 0)'. With that, the PointerKindVariable derived from pointer expression
-/// `*(&p)` or `&(*p)` is naturally equivalent to the one derived from
-/// expression `p`.
-class PointerKindVariable {
+/// For the same example 'int **p;', the EntityPointerLevels below are valid:
+/// '(p, 1)' is associated with 'int **' of 'p';
+/// '(p, 2)' is associated with 'int *' of 'p';
+/// '(p, 0)' represents '&p'.
+class EntityPointerLevel {
EntityId Entity;
unsigned PointerLevel;
friend class UnsafeBufferUsageTUSummaryBuilder;
friend class UnsafeBufferUsageEntitySummary;
- PointerKindVariable(EntityId Entity, unsigned PointerLevel)
+ EntityPointerLevel(EntityId Entity, unsigned PointerLevel)
: Entity(Entity), PointerLevel(PointerLevel) {}
public:
EntityId getEntity() const { return Entity; }
unsigned getPointerLevel() const { return PointerLevel; }
- bool operator==(const PointerKindVariable &Other) const {
+ bool operator==(const EntityPointerLevel &Other) const {
return Entity == Other.Entity && PointerLevel == Other.PointerLevel;
}
- bool operator!=(const PointerKindVariable &Other) const {
+ bool operator!=(const EntityPointerLevel &Other) const {
return !(*this == Other);
}
- bool operator<(const PointerKindVariable &Other) const {
+ bool operator<(const EntityPointerLevel &Other) const {
return std::tie(Entity, PointerLevel) <
std::tie(Other.Entity, Other.PointerLevel);
}
@@ -70,39 +67,39 @@ class PointerKindVariable {
// Comparator supporting partial comparison against EntityId:
struct Comparator {
using is_transparent = void;
- bool operator()(const PointerKindVariable &L,
- const PointerKindVariable &R) const {
+ bool operator()(const EntityPointerLevel &L,
+ const EntityPointerLevel &R) const {
return L < R;
}
- bool operator()(const EntityId &L, const PointerKindVariable &R) const {
+ bool operator()(const EntityId &L, const EntityPointerLevel &R) const {
return L < R.getEntity();
}
- bool operator()(const PointerKindVariable &L, const EntityId &R) const {
+ bool operator()(const EntityPointerLevel &L, const EntityId &R) const {
return L.getEntity() < R;
}
};
};
-using PointerKindVariableSet =
- std::set<PointerKindVariable, PointerKindVariable::Comparator>;
+using EntityPointerLevelSet =
+ std::set<EntityPointerLevel, EntityPointerLevel::Comparator>;
/// An UnsafeBufferUsageEntitySummary is an immutable set of unsafe buffers, in
-/// the form of PointerKindVariable.
+/// the form of EntityPointerLevel.
class UnsafeBufferUsageEntitySummary final : public EntitySummary {
- const PointerKindVariableSet UnsafeBuffers;
+ const EntityPointerLevelSet UnsafeBuffers;
friend class UnsafeBufferUsageTUSummaryBuilder;
- UnsafeBufferUsageEntitySummary(PointerKindVariableSet &&UnsafeBuffers)
+ UnsafeBufferUsageEntitySummary(EntityPointerLevelSet &&UnsafeBuffers)
: EntitySummary(), UnsafeBuffers(std::move(UnsafeBuffers)) {}
public:
- using const_iterator = PointerKindVariableSet::const_iterator;
+ using const_iterator = EntityPointerLevelSet::const_iterator;
const_iterator begin() const { return UnsafeBuffers.begin(); }
const_iterator end() const { return UnsafeBuffers.end(); }
- const_iterator find(const PointerKindVariable &V) const {
+ const_iterator find(const EntityPointerLevel &V) const {
return UnsafeBuffers.find(V);
}
@@ -110,26 +107,14 @@ class UnsafeBufferUsageEntitySummary final : public EntitySummary {
return llvm::make_range(UnsafeBuffers.equal_range(Entity));
}
+ /// \return the size of the set of EntityLevelPointers, which represents the
+ /// set of unsafe buffers
size_t getNumUnsafeBuffers() { return UnsafeBuffers.size(); }
SummaryName getSummaryName() const override {
return SummaryName{"UnsafeBufferUsage"};
};
};
-
-class UnsafeBufferUsageTUSummaryBuilder : public TUSummaryBuilder {
-public:
- static PointerKindVariable buildPointerKindVariable(EntityId Entity,
- unsigned PointerLevel) {
- return {Entity, PointerLevel};
- }
-
- static std::unique_ptr<UnsafeBufferUsageEntitySummary>
- buildUnsafeBufferUsageEntitySummary(PointerKindVariableSet &&UnsafeBuffers) {
- return std::make_unique<UnsafeBufferUsageEntitySummary>(
- UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers)));
- }
-};
} // namespace clang::ssaf
#endif // LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_H
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
new file mode 100644
index 0000000000000..74fb97cf7f2f4
--- /dev/null
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
@@ -0,0 +1,27 @@
+//===- UnsafeBufferUsageBuilder.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
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h"
+#include "clang/Analysis/Scalable/TUSummary/TUSummaryBuilder.h"
+#include <memory>
+
+namespace clang::ssaf {
+class UnsafeBufferUsageTUSummaryBuilder : public TUSummaryBuilder {
+public:
+ static EntityPointerLevel buildEntityPointerLevel(EntityId Entity,
+ unsigned PointerLevel) {
+ return {Entity, PointerLevel};
+ }
+
+ static std::unique_ptr<UnsafeBufferUsageEntitySummary>
+ buildUnsafeBufferUsageEntitySummary(EntityPointerLevelSet &&UnsafeBuffers) {
+ return std::make_unique<UnsafeBufferUsageEntitySummary>(
+ UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers)));
+ }
+};
+} // namespace clang::ssaf
\ No newline at end of file
diff --git a/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp b/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
index f0e032a897131..09f66b930e634 100644
--- a/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
+++ b/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
@@ -7,10 +7,10 @@
//===----------------------------------------------------------------------===//
#include "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h"
+#include "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h"
#include "clang/Analysis/Scalable/Model/EntityId.h"
#include "clang/Analysis/Scalable/Model/EntityIdTable.h"
#include "gtest/gtest.h"
-#include <memory>
using namespace clang;
using namespace ssaf;
@@ -30,14 +30,14 @@ class UnsafeBufferUsageTest : public testing::Test {
#define EXPECT_CONTAINS(Set, Elt) EXPECT_NE((Set)->find(Elt), (Set)->end());
#define EXPECT_EXCLUDES(Set, Elt) EXPECT_EQ((Set)->find(Elt), (Set)->end());
-TEST_F(UnsafeBufferUsageTest, PointerKindVariableComparison) {
+TEST_F(UnsafeBufferUsageTest, EntityPointerLevelComparison) {
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);
+ auto P1 = Builder.buildEntityPointerLevel(E1, 2);
+ auto P2 = Builder.buildEntityPointerLevel(E1, 2);
+ auto P3 = Builder.buildEntityPointerLevel(E1, 1);
+ auto P4 = Builder.buildEntityPointerLevel(E2, 2);
EXPECT_EQ(P1, P2);
EXPECT_NE(P1, P3);
@@ -54,14 +54,14 @@ TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageEntitySummaryTest) {
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);
+ auto P1 = Builder.buildEntityPointerLevel(E1, 1);
+ auto P2 = Builder.buildEntityPointerLevel(E1, 2);
+ auto P3 = Builder.buildEntityPointerLevel(E2, 1);
+ auto P4 = Builder.buildEntityPointerLevel(E2, 2);
+ auto P5 = Builder.buildEntityPointerLevel(E3, 1);
+ auto P6 = Builder.buildEntityPointerLevel(E3, 2);
- PointerKindVariableSet Set{P1, P2, P3, P4, P5};
+ EntityPointerLevelSet Set{P1, P2, P3, P4, P5};
auto ES = Builder.buildUnsafeBufferUsageEntitySummary(std::move(Set));
EXPECT_CONTAINS(ES, P1);
@@ -71,22 +71,22 @@ TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageEntitySummaryTest) {
EXPECT_CONTAINS(ES, P5);
EXPECT_EXCLUDES(ES, P6);
- PointerKindVariableSet Subset1{ES->getSubsetOf(E1).begin(),
- ES->getSubsetOf(E1).end()};
+ EntityPointerLevelSet Subset1{ES->getSubsetOf(E1).begin(),
+ ES->getSubsetOf(E1).end()};
EXPECT_CONTAINS(&Subset1, P1);
EXPECT_CONTAINS(&Subset1, P2);
EXPECT_EQ(Subset1.size(), 2U);
- PointerKindVariableSet Subset2{ES->getSubsetOf(E2).begin(),
- ES->getSubsetOf(E2).end()};
+ EntityPointerLevelSet Subset2{ES->getSubsetOf(E2).begin(),
+ ES->getSubsetOf(E2).end()};
EXPECT_CONTAINS(&Subset2, P3);
EXPECT_CONTAINS(&Subset2, P4);
EXPECT_EQ(Subset2.size(), 2U);
- PointerKindVariableSet Subset3{ES->getSubsetOf(E3).begin(),
- ES->getSubsetOf(E3).end()};
+ EntityPointerLevelSet Subset3{ES->getSubsetOf(E3).begin(),
+ ES->getSubsetOf(E3).end()};
EXPECT_CONTAINS(&Subset3, P5);
EXPECT_EXCLUDES(&Subset3, P6);
>From 644e19fab6f1b60f77f1eb2c29ae2eeba09c7368 Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Tue, 17 Feb 2026 12:12:08 -0800
Subject: [PATCH 5/7] [ThreadSafety][NFC] Cleanup indentation and unnecessary
comments
This is a follow-up commit to #178952 for indentation and comments
cleanups.
---
clang/lib/Analysis/ThreadSafety.cpp | 3 ---
.../SemaCXX/warn-thread-safety-analysis.cpp | 20 +++++++++----------
2 files changed, 10 insertions(+), 13 deletions(-)
diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 901624acd6f75..2c5976af9f61d 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1727,9 +1727,6 @@ class BuildLockset : public ConstStmtVisitor<BuildLockset> {
FactSet FSet;
// The fact set for the function on exit.
const FactSet &FunctionExitFSet;
- // Use `LVarCtx` to keep track of the context at each program point, which
- // will be used to translate variables (by `SExprBuilder::translateVariable`)
- // to their canonical definitions:
LocalVariableMap::Context LVarCtx;
unsigned CtxIndex;
diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
index b878a37747f2f..a6c9a2236fc45 100644
--- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
@@ -7487,20 +7487,20 @@ void testPointerAliasEscapeAndReset(Foo *f) {
// A function that may do anything to the objects referred to by the inputs.
void escapeAliasMultiple(void *, void *, void *);
void testPointerAliasEscapeMultiple(Foo *F) {
- Foo *L;
- F->mu.Lock(); // expected-note{{mutex acquired here}}
- Foo *Fp = F;
- escapeAliasMultiple(&L, &L, &Fp);
- Fp->mu.Unlock(); // expected-warning{{releasing mutex 'Fp->mu' that was not held}}
+ Foo *L;
+ F->mu.Lock(); // expected-note{{mutex acquired here}}
+ Foo *Fp = F;
+ escapeAliasMultiple(&L, &L, &Fp);
+ Fp->mu.Unlock(); // expected-warning{{releasing mutex 'Fp->mu' that was not held}}
} // expected-warning{{mutex 'F->mu' is still held at the end of function}}
void unlockFooWithEscapablePointer(Foo **Fp) EXCLUSIVE_UNLOCK_FUNCTION((*Fp)->mu);
void testEscapeInvalidationHappensRightAfterTheCall(Foo* F) {
- Foo* L;
- L = F;
- L->mu.Lock();
- // Release the lock held by 'L' before clearing its definition.
- unlockFooWithEscapablePointer(&L);
+ Foo* L;
+ L = F;
+ L->mu.Lock();
+ // Release the lock held by 'L' before clearing its definition.
+ unlockFooWithEscapablePointer(&L);
}
void testEscapeInvalidationHappensRightAfterTheCtorCall(Foo* F) {
>From e7dd8d1586402250128beb0e1ceafe6fd98b8a5a Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Tue, 17 Feb 2026 18:38:06 -0800
Subject: [PATCH 6/7] Address comments
---
.../UnsafeBufferUsage/UnsafeBufferUsage.h | 14 ++++-----
.../UnsafeBufferUsageBuilder.h | 6 +++-
.../UnsafeBufferUsageTest.cpp | 29 ++++++++++---------
.../Analysis/Scalable/CMakeLists.txt | 4 +--
4 files changed, 29 insertions(+), 24 deletions(-)
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
index 0a7381daf7eb2..16da4954a8026 100644
--- a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h
@@ -17,13 +17,13 @@
namespace clang::ssaf {
-/// An EntityPointerLevel represents a level of the declared pointer
+/// An EntityPointerLevel represents a level of the declared pointer/array
/// type of an entity. In the fully-expanded spelling of the declared type, a
-/// EntityPointerLevel is associated with a '*' in that declaration.
+/// EntityPointerLevel is associated with a '*' (or a '[]`) in that declaration.
///
-/// For example, for 'int **p;', there are two EntityPointerLevels. One
-/// is associated with 'int **' of 'p' and the other is associated with 'int *'
-/// of 'p'.
+/// For example, for 'int *p[10];', there are two EntityPointerLevels. One
+/// is associated with 'int *[10]' of 'p' and the other is associated with 'int
+/// *' of 'p'.
///
/// An EntityPointerLevel can be identified by an EntityId and an unsigned
/// integer indicating the pointer level: '(EntityId, PointerLevel)'. A
@@ -33,8 +33,8 @@ namespace clang::ssaf {
/// - 'P.EntityId' identifies an lvalue object and 'P.PointerLevel == 0'.
/// The latter case represents address-of expressions.
///
-/// For the same example 'int **p;', the EntityPointerLevels below are valid:
-/// '(p, 1)' is associated with 'int **' of 'p';
+/// For the same example 'int *p[10];', the EntityPointerLevels below are valid:
+/// '(p, 1)' is associated with 'int *[10]' of 'p';
/// '(p, 2)' is associated with 'int *' of 'p';
/// '(p, 0)' represents '&p'.
class EntityPointerLevel {
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
index 74fb97cf7f2f4..9c1832379fd6e 100644
--- a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
@@ -5,6 +5,8 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_BUILDER_H
+#define LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_BUILDER_H
#include "clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h"
#include "clang/Analysis/Scalable/TUSummary/TUSummaryBuilder.h"
@@ -24,4 +26,6 @@ class UnsafeBufferUsageTUSummaryBuilder : public TUSummaryBuilder {
UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers)));
}
};
-} // namespace clang::ssaf
\ No newline at end of file
+} // namespace clang::ssaf
+
+#endif
\ No newline at end of file
diff --git a/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp b/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
index 09f66b930e634..0d39c405a4575 100644
--- a/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
+++ b/clang/unittests/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
@@ -27,8 +27,8 @@ class UnsafeBufferUsageTest : public testing::Test {
// Data Structure Tests //
//////////////////////////////////////////////////////////////
-#define EXPECT_CONTAINS(Set, Elt) EXPECT_NE((Set)->find(Elt), (Set)->end());
-#define EXPECT_EXCLUDES(Set, Elt) EXPECT_EQ((Set)->find(Elt), (Set)->end());
+#define EXPECT_CONTAINS(Set, Elt) EXPECT_NE((Set).find(Elt), (Set).end())
+#define EXPECT_EXCLUDES(Set, Elt) EXPECT_EQ((Set).find(Elt), (Set).end())
TEST_F(UnsafeBufferUsageTest, EntityPointerLevelComparison) {
EntityId E1 = Table.getId({"c:@F at foo", "", {}});
@@ -63,33 +63,34 @@ TEST_F(UnsafeBufferUsageTest, UnsafeBufferUsageEntitySummaryTest) {
EntityPointerLevelSet Set{P1, P2, P3, P4, P5};
auto ES = Builder.buildUnsafeBufferUsageEntitySummary(std::move(Set));
+ ASSERT_TRUE(ES);
- EXPECT_CONTAINS(ES, P1);
- EXPECT_CONTAINS(ES, P2);
- EXPECT_CONTAINS(ES, P3);
- EXPECT_CONTAINS(ES, P4);
- EXPECT_CONTAINS(ES, P5);
- EXPECT_EXCLUDES(ES, P6);
+ EXPECT_CONTAINS(*ES, P1);
+ EXPECT_CONTAINS(*ES, P2);
+ EXPECT_CONTAINS(*ES, P3);
+ EXPECT_CONTAINS(*ES, P4);
+ EXPECT_CONTAINS(*ES, P5);
+ EXPECT_EXCLUDES(*ES, P6);
EntityPointerLevelSet Subset1{ES->getSubsetOf(E1).begin(),
ES->getSubsetOf(E1).end()};
- EXPECT_CONTAINS(&Subset1, P1);
- EXPECT_CONTAINS(&Subset1, P2);
+ EXPECT_CONTAINS(Subset1, P1);
+ EXPECT_CONTAINS(Subset1, P2);
EXPECT_EQ(Subset1.size(), 2U);
EntityPointerLevelSet Subset2{ES->getSubsetOf(E2).begin(),
ES->getSubsetOf(E2).end()};
- EXPECT_CONTAINS(&Subset2, P3);
- EXPECT_CONTAINS(&Subset2, P4);
+ EXPECT_CONTAINS(Subset2, P3);
+ EXPECT_CONTAINS(Subset2, P4);
EXPECT_EQ(Subset2.size(), 2U);
EntityPointerLevelSet Subset3{ES->getSubsetOf(E3).begin(),
ES->getSubsetOf(E3).end()};
- EXPECT_CONTAINS(&Subset3, P5);
- EXPECT_EXCLUDES(&Subset3, P6);
+ EXPECT_CONTAINS(Subset3, P5);
+ EXPECT_EXCLUDES(Subset3, P6);
EXPECT_EQ(Subset3.size(), 1U);
}
} // namespace
diff --git a/clang/unittests/Analysis/Scalable/CMakeLists.txt b/clang/unittests/Analysis/Scalable/CMakeLists.txt
index e93ddd40ab754..da9243b4dfa0b 100644
--- a/clang/unittests/Analysis/Scalable/CMakeLists.txt
+++ b/clang/unittests/Analysis/Scalable/CMakeLists.txt
@@ -1,4 +1,5 @@
add_distinct_clang_unittest(ClangScalableAnalysisTests
+ Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
ASTEntityMappingTest.cpp
BuildNamespaceTest.cpp
EntityIdTest.cpp
@@ -10,8 +11,7 @@ add_distinct_clang_unittest(ClangScalableAnalysisTests
Registries/MockSummaryExtractor2.cpp
Registries/SerializationFormatRegistryTest.cpp
Registries/SummaryExtractorRegistryTest.cpp
- SummaryNameTest.cpp
- Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.cpp
+ SummaryNameTest.cpp
CLANG_LIBS
clangAnalysisScalable
>From d139695ef9c5fb8ec6249f85b8f227b226750c7b Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing at udel.edu>
Date: Wed, 18 Feb 2026 11:14:23 -0800
Subject: [PATCH 7/7] Apply suggestion from @steakhal---add comment at the end
of the include guard
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: Balázs Benics <benicsbalazs at gmail.com>
---
.../Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
index 9c1832379fd6e..2b4acae4209d6 100644
--- a/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
+++ b/clang/include/clang/Analysis/Scalable/Analyses/UnsafeBufferUsage/UnsafeBufferUsageBuilder.h
@@ -28,4 +28,4 @@ class UnsafeBufferUsageTUSummaryBuilder : public TUSummaryBuilder {
};
} // namespace clang::ssaf
-#endif
\ No newline at end of file
+#endif // LLVM_CLANG_ANALYSIS_SCALABLE_ANALYSES_UNSAFEBUFFERUSAGE_BUILDER_H
More information about the cfe-commits
mailing list