[clang] [Clang] FunctionEffects: ignore (methods of) local CXXRecordDecls. (PR #166078)

Doug Wyatt via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 4 07:48:45 PST 2025


https://github.com/dougsonos updated https://github.com/llvm/llvm-project/pull/166078

>From a24d2f4c5f5bb4370e0266398a16786b59b9590e Mon Sep 17 00:00:00 2001
From: Doug Wyatt <dwyatt at apple.com>
Date: Sun, 2 Nov 2025 09:04:14 -0800
Subject: [PATCH 1/2] [Clang] FunctionEffects: ignore (methods of) local
 CXXRecordDecls.

---
 clang/lib/Sema/SemaFunctionEffects.cpp           | 8 ++++++++
 clang/test/Sema/attr-nonblocking-constraints.cpp | 9 +++++++++
 2 files changed, 17 insertions(+)

diff --git a/clang/lib/Sema/SemaFunctionEffects.cpp b/clang/lib/Sema/SemaFunctionEffects.cpp
index 8590ee831084f..7ae8d909aa337 100644
--- a/clang/lib/Sema/SemaFunctionEffects.cpp
+++ b/clang/lib/Sema/SemaFunctionEffects.cpp
@@ -1286,6 +1286,14 @@ class Analyzer {
       return true;
     }
 
+    bool TraverseCXXRecordDecl(CXXRecordDecl *D) override {
+      // Completely skip local struct/class/union declarations since their
+      // methods would otherwise be incorrectly interpreted as part of the
+      // function we are currently traversing. The initial Sema pass will have
+      // already recorded any nonblocking methods needing analysis.
+      return true;
+    }
+
     bool TraverseConstructorInitializer(CXXCtorInitializer *Init) override {
       ViolationSite PrevVS = VSite;
       if (Init->isAnyMemberInitializer())
diff --git a/clang/test/Sema/attr-nonblocking-constraints.cpp b/clang/test/Sema/attr-nonblocking-constraints.cpp
index b26a945843696..e75d2967e4927 100644
--- a/clang/test/Sema/attr-nonblocking-constraints.cpp
+++ b/clang/test/Sema/attr-nonblocking-constraints.cpp
@@ -104,6 +104,15 @@ void nb8c()
 	};
 }
 
+void nb8d() [[clang::nonblocking]]
+{
+	// Blocking methods of a local CXXRecordDecl do not generate diagnostics
+	// for the outer function.
+	struct Functor1 {
+        void method() { void* ptr = new int; }
+	};
+}
+
 // Make sure template expansions are found and verified.
 	template <typename T>
 	struct Adder {

>From 27e93ebc9ccb66fdfc7279b861cc0dcbfead6fcb Mon Sep 17 00:00:00 2001
From: Doug Wyatt <dwyatt at apple.com>
Date: Tue, 4 Nov 2025 07:48:32 -0800
Subject: [PATCH 2/2] more tests

---
 clang/test/Sema/attr-nonblocking-constraints.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/attr-nonblocking-constraints.cpp b/clang/test/Sema/attr-nonblocking-constraints.cpp
index e75d2967e4927..ddffd826c8bba 100644
--- a/clang/test/Sema/attr-nonblocking-constraints.cpp
+++ b/clang/test/Sema/attr-nonblocking-constraints.cpp
@@ -108,9 +108,19 @@ void nb8d() [[clang::nonblocking]]
 {
 	// Blocking methods of a local CXXRecordDecl do not generate diagnostics
 	// for the outer function.
-	struct Functor1 {
+	struct F1 {
         void method() { void* ptr = new int; }
 	};
+
+	// Skipping the CXXRecordDecl does not skip a following VarDecl.
+	struct F2 {
+        F2() { void* ptr = new int; } // expected-note {{constructor cannot be inferred 'nonblocking' because it allocates or deallocates memory}}
+	} f2; // expected-warning {{function with 'nonblocking' attribute must not call non-'nonblocking' constructor 'nb8d()::F2::F2'}}
+
+	// Nonblocking methods of a local CXXRecordDecl are verified independently.
+	struct F3 {
+		void method() [[clang::nonblocking]] { void* ptr = new int; }// expected-warning {{function with 'nonblocking' attribute must not allocate or deallocate memory}}
+	};
 }
 
 // Make sure template expansions are found and verified.



More information about the cfe-commits mailing list