[clang-tools-extra] [llvm] [llvm][ADT] Mark llvm::IntrusiveRefCntPtr with the warn_unused attribute (PR #211795)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 26 03:13:12 PDT 2026
https://github.com/steakhal updated https://github.com/llvm/llvm-project/pull/211795
>From 297041c8e1b0ee34772dc246bab97c2710b7c2d2 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Fri, 24 Jul 2026 14:47:14 +0100
Subject: [PATCH 1/5] [llvm][ADT] Mark llvm::IntrusiveRefCntPtr with the
warn_unused attribute
IntrusiveRefCntPtr has non-trivial ctor/dtor, thus unused variables
wouldn't trigger a warning by default. However, they should.
https://clang.llvm.org/docs/AttributeReference.html#warn-unused
Let's mark the class with this attribute get warned about them.
This would have helped catching #211518 and #211517.
Supersedes #211647.
---
llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index 90349e02014dd..55f80778823ad 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -170,7 +170,7 @@ template <typename T> struct IntrusiveRefCntPtrInfo {
/// This class increments its pointee's reference count when it is created, and
/// decrements its refcount when it's destroyed (or is changed to point to a
/// different object).
-template <typename T> class IntrusiveRefCntPtr {
+template <typename T> class [[gnu::warn_unused]] IntrusiveRefCntPtr {
T *Obj = nullptr;
public:
>From 79deb75811d1c7a3af9caa5dad85972c11e1ea15 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Fri, 24 Jul 2026 15:42:08 +0100
Subject: [PATCH 2/5] Define LLVM_ATTRIBUTE_WARN_UNUSED
---
llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 3 ++-
llvm/include/llvm/Support/Compiler.h | 6 ++++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index 55f80778823ad..fe98750632d05 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -60,6 +60,7 @@
#ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
#define LLVM_ADT_INTRUSIVEREFCNTPTR_H
+#include "llvm/Support/Compiler.h"
#include <atomic>
#include <cassert>
#include <cstddef>
@@ -170,7 +171,7 @@ template <typename T> struct IntrusiveRefCntPtrInfo {
/// This class increments its pointee's reference count when it is created, and
/// decrements its refcount when it's destroyed (or is changed to point to a
/// different object).
-template <typename T> class [[gnu::warn_unused]] IntrusiveRefCntPtr {
+template <typename T> class LLVM_ATTRIBUTE_WARN_UNUSED IntrusiveRefCntPtr {
T *Obj = nullptr;
public:
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 34b328ba1ac8a..753b1e12f721d 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -238,6 +238,12 @@
#define LLVM_ATTRIBUTE_USED
#endif
+#if __has_attribute(warn_unused)
+#define LLVM_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
+#else
+#define LLVM_ATTRIBUTE_WARN_UNUSED
+#endif
+
// Only enabled for clang:
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
// GCC may produce "warning: 'retain' attribute ignored" (despite
>From 5add1d09fa34edadd1f84f02ddbeb270d3934d46 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Fri, 24 Jul 2026 16:16:36 +0100
Subject: [PATCH 3/5] Use the __attribute__ style
---
llvm/include/llvm/Support/Compiler.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 753b1e12f721d..f9dc2e8d45c4d 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -239,7 +239,7 @@
#endif
#if __has_attribute(warn_unused)
-#define LLVM_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
+#define LLVM_ATTRIBUTE_WARN_UNUSED __attribute__((warn_unused))
#else
#define LLVM_ATTRIBUTE_WARN_UNUSED
#endif
>From 36eeb78bf71ed4ed2d1314298c079dc91c1f00aa Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Sun, 26 Jul 2026 11:05:38 +0100
Subject: [PATCH 4/5] Drop unused FS variables
The `RecordedIncludesTest` unittests had some unused FS variables:
- `Match` was introduced with the unused variable in 8249dc21046af57ad3b2c72414aa8a7f16b67687
- `MatchVerbatim` and `MatchVerbatimMixedAbsoluteRelative` was
introduced in 16b5e1897b7efaa4250b04dface0c807a02ae31a
---
clang-tools-extra/include-cleaner/unittests/TypesTest.cpp | 6 ------
1 file changed, 6 deletions(-)
diff --git a/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp b/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
index 6f7491928fb05..dffc778240a80 100644
--- a/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/TypesTest.cpp
@@ -11,8 +11,6 @@
#include "clang/Basic/FileManager.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Tooling/Inclusions/StandardLibrary.h"
-#include "llvm/ADT/IntrusiveRefCntPtr.h"
-#include "llvm/Support/VirtualFileSystem.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
@@ -28,7 +26,6 @@ MATCHER_P(line, N, "") { return arg->Line == (unsigned)N; }
TEST(RecordedIncludesTest, Match) {
// We're using synthetic data, but need a FileManager to obtain FileEntry*s.
// Ensure it doesn't do any actual IO.
- auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
FileManager FM(FileSystemOptions{});
FileEntryRef A = FM.getVirtualFileRef("/path/a", /*Size=*/0, time_t{});
FileEntryRef B = FM.getVirtualFileRef("/path/b", /*Size=*/0, time_t{});
@@ -48,7 +45,6 @@ TEST(RecordedIncludesTest, Match) {
}
TEST(RecordedIncludesTest, MatchVerbatim) {
- auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
FileManager FM(FileSystemOptions{});
Includes Inc;
@@ -79,8 +75,6 @@ TEST(RecordedIncludesTest, MatchVerbatim) {
}
TEST(RecordedIncludesTest, MatchVerbatimMixedAbsoluteRelative) {
- auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
- FS->setCurrentWorkingDirectory("/working");
FileManager FM(FileSystemOptions{});
Includes Inc;
>From 4fd9451282c7a1270e0ee11545b80e5b5ec92065 Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Sun, 26 Jul 2026 11:12:20 +0100
Subject: [PATCH 5/5] Dismiss unused variables in IntrusiveRefCntPtr tests
I think the intention was to keep these variables.
---
llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
index 6da42271764bc..f6a5f587d2719 100644
--- a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -38,6 +38,8 @@ TYPED_TEST(IntrusiveRefCntPtrTest, RefCountedBaseCopyDoesNotLeak) {
TypeParam *S2 = new TypeParam(*S1);
IntrusiveRefCntPtr<TypeParam> R2 = S2;
EXPECT_EQ(2, NumInstances);
+ (void)R1;
+ (void)R2;
}
EXPECT_EQ(0, NumInstances);
}
@@ -49,6 +51,7 @@ TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) {
IntrusiveRefCntPtr<TypeParam> R1 = std::move(S1);
EXPECT_EQ(1, NumInstances);
EXPECT_EQ(S1, nullptr);
+ (void)R1;
}
EXPECT_EQ(0, NumInstances);
}
@@ -91,6 +94,7 @@ TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
{
InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
IntrusiveRefCntPtr<InterceptRefCounted> R = I;
+ (void)R;
}
EXPECT_TRUE(Released);
EXPECT_TRUE(Retained);
More information about the cfe-commits
mailing list