Skip to content

[clang] Respect [[gnu::error]] on functions passed to [[gnu::cleanup]] #152082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mr-Anyone
Copy link
Contributor

Forward SourceLocation to EmitCall so that clang triggers an error when a function inside [[gnu::cleanup(func)]] is annotated with [[gnu::error("some message")]].

resolves #146520

…Attribute

Forward SourceLocation to `EmitCall` so that clang triggers an error when
a function inside [[gnu::cleanup(func)]] is annotated with [[gnu::error("...")]].

resolves llvm#146520
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Aug 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Vincent (Mr-Anyone)

Changes

Forward SourceLocation to EmitCall so that clang triggers an error when a function inside [[gnu::cleanup(func)]] is annotated with [[gnu::error("some message")]].

resolves #146520


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/CodeGen/CGDecl.cpp (+9-2)
  • (modified) clang/test/Frontend/backend-attribute-error-warning-optimize.c (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9231f2cae9bfa..4fc354e254e38 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -152,6 +152,8 @@ Bug Fixes to Attribute Support
 
 - ``[[nodiscard]]`` is now respected on Objective-C and Objective-C++ methods.
   (#GH141504)
+- Using ``[[gnu::cleanup(some_func)]]`` where some_func is annotated with
+  ``[[gnu::error("some error")]]`` now correctly triggers an error. (#GH146520)
 
 Bug Fixes to C++ Support
 ^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 04f13c7d7a6a3..8f1d1fac3f083 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -624,8 +624,15 @@ namespace {
       CallArgList Args;
       Args.add(RValue::get(Arg),
                CGF.getContext().getPointerType(Var.getType()));
-      auto Callee = CGCallee::forDirect(CleanupFn);
-      CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
+      bool HasCleanupAttr = Var.hasAttr<CleanupAttr>();
+      GlobalDecl GD = HasCleanupAttr
+                          ? (Var.getAttr<CleanupAttr>()->getFunctionDecl())
+                          : GlobalDecl();
+      SourceLocation Loc = HasCleanupAttr ? Var.getAttr<CleanupAttr>()->getLoc()
+                                          : SourceLocation();
+      auto Callee = CGCallee::forDirect(CleanupFn, CGCalleeInfo(GD));
+      CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args,
+                   /*callOrInvoke*/ nullptr, /*IsMustTail*/ false, Loc);
     }
   };
 } // end anonymous namespace
diff --git a/clang/test/Frontend/backend-attribute-error-warning-optimize.c b/clang/test/Frontend/backend-attribute-error-warning-optimize.c
index d3951e3b6b1f5..d5a8d57e36c96 100644
--- a/clang/test/Frontend/backend-attribute-error-warning-optimize.c
+++ b/clang/test/Frontend/backend-attribute-error-warning-optimize.c
@@ -20,3 +20,13 @@ void indirect(void) {
   quux = foo;
   quux();
 }
+
+// https://github.com/llvm/llvm-project/issues/146520
+
+[[gnu::error("error please")]]
+void cleaner_function(char*);
+
+void asdf(void){
+	[[gnu::cleanup(cleaner_function)]] // expected-error {{call to 'cleaner_function' declared with 'error' attribute: error please}}
+	char x; 
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gnu::cleanup can call gnu::error function
2 participants