Skip to content

Conversation

@dougsonos
Copy link
Contributor

@dougsonos dougsonos commented Nov 2, 2025

In the following example, Functor::method() inappropriately triggers a diagnostic that outer() is blocking by allocating memory.

void outer() [[clang::nonblocking]]
{
	struct Functor {
		int* ptr;
		
		void method() { ptr = new int; }
	};
}

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 2, 2025

@llvm/pr-subscribers-clang

Author: Doug Wyatt (dougsonos)

Changes

In the following example, Functor::method() inappropriately triggers a diagnostic that outer() is blocking by allocating memory.

void outer() [[clang::nonblocking]]
{
	struct Functor {
		int* ptr;
		
		void method() { ptr = new int; }
	};

Full diff: https://github.com/llvm/llvm-project/pull/166078.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaFunctionEffects.cpp (+8)
  • (modified) clang/test/Sema/attr-nonblocking-constraints.cpp (+9)
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 {

@dougsonos dougsonos requested a review from Sirraide November 2, 2025 17:21
@dougsonos dougsonos self-assigned this Nov 2, 2025
@dougsonos dougsonos force-pushed the u/dsw/nonblocking-local-cxxrecord branch from 2935361 to a24d2f4 Compare November 3, 2025 20:24
@dougsonos
Copy link
Contributor Author

Rebased and force-pushed to trigger automation to retry the failed build (looks like infrastructure issue).

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT

I thought I’d already reviewed this, but apparently I forgot to actually submit the review...

@dougsonos dougsonos merged commit fa6cc7e into llvm:main Nov 5, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants