Skip to content

[clang] Output an error when [[lifetimebound]] attribute is applied on a function implicit object parameter while the function returns void #114203

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

Merged
merged 4 commits into from
Nov 7, 2024

Conversation

bricknerb
Copy link
Contributor

Fixes: #107556

…n a function implicit object parameter while the function returns void

Fixes: llvm#107556
…n a function implicit object parameter while the function returns void

Fixes: llvm#107556
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2024

@llvm/pr-subscribers-clang

Author: Boaz Brickner (bricknerb)

Changes

Fixes: #107556


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

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2-2)
  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+4-1)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+7-1)
  • (modified) clang/test/SemaCXX/attr-lifetimebound.cpp (+1-2)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6085352dfafe6b..61fc2ff5a9aeed 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -140,8 +140,8 @@ C++ Specific Potentially Breaking Changes
     unsigned operator""_udl_name(unsigned long long);
 
 - Clang will now produce an error diagnostic when [[clang::lifetimebound]] is
-  applied on a parameter of a function that returns void. This was previously
-  ignored and had no effect. (#GH107556)
+  applied on a parameter or an implicit object parameter of a function that
+  returns void. This was previously ignored and had no effect. (#GH107556)
 
   .. code-block:: c++
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 34ff49d7238a7f..3168337acb621f 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10101,9 +10101,12 @@ def err_lifetimebound_no_object_param : Error<
 def err_lifetimebound_ctor_dtor : Error<
   "'lifetimebound' attribute cannot be applied to a "
   "%select{constructor|destructor}0">;
-def err_lifetimebound_void_return_type : Error<
+def err_lifetimebound_parameter_void_return_type : Error<
   "'lifetimebound' attribute cannot be applied to a parameter of a function "
   "that returns void">;
+def err_lifetimebound_implicit_object_parameter_void_return_type : Error<
+  "'lifetimebound' attribute cannot be applied to an implicit object "
+  "parameter of a function that returns void">;
 
 // CHECK: returning address/reference of stack memory
 def warn_ret_stack_addr_ref : Warning<
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f8e5f3c6d309d6..c09ff4d1975e24 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -6967,6 +6967,11 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
         } else if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) {
           S.Diag(A->getLocation(), diag::err_lifetimebound_ctor_dtor)
               << isa<CXXDestructorDecl>(MD) << A->getRange();
+        } else if (FD->getReturnType()->isVoidType()) {
+          S.Diag(
+              FD->getLocation(),
+              diag::
+                  err_lifetimebound_implicit_object_parameter_void_return_type);
         }
       }
     }
@@ -6978,7 +6983,8 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
       // only if the function returns a value.
       if (auto *A = P->getAttr<LifetimeBoundAttr>()) {
         if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType()) {
-          S.Diag(A->getLocation(), diag::err_lifetimebound_void_return_type);
+          S.Diag(A->getLocation(),
+                 diag::err_lifetimebound_parameter_void_return_type);
         }
       }
     }
diff --git a/clang/test/SemaCXX/attr-lifetimebound.cpp b/clang/test/SemaCXX/attr-lifetimebound.cpp
index 804d61fb62ca40..81e9193cf76a04 100644
--- a/clang/test/SemaCXX/attr-lifetimebound.cpp
+++ b/clang/test/SemaCXX/attr-lifetimebound.cpp
@@ -11,8 +11,7 @@ namespace usage_invalid {
     int *explicit_object(this A&) [[clang::lifetimebound]]; // expected-error {{explicit object member function has no implicit object parameter}}
     int not_function [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
     int [[clang::lifetimebound]] also_not_function; // expected-error {{cannot be applied to types}}
-    // FIXME: Should diagnose a void return type.
-    void void_return_member() [[clang::lifetimebound]];
+    void void_return_member() [[clang::lifetimebound]]; // expected-error {{'lifetimebound' attribute cannot be applied to an implicit object parameter of a function that returns void}}
   };
   int *attr_with_param(int &param [[clang::lifetimebound(42)]]); // expected-error {{takes no arguments}}
 }

@bricknerb bricknerb requested a review from cor3ntin October 31, 2024 11:35
Copy link
Contributor

@usx95 usx95 left a comment

Choose a reason for hiding this comment

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

LGTM.

@bricknerb bricknerb merged commit ae5bfa0 into llvm:main Nov 7, 2024
9 checks passed
@bricknerb bricknerb deleted the lifetime2 branch November 7, 2024 11:00
aarongable pushed a commit to chromium/chromium that referenced this pull request Nov 14, 2024
Upcoming clange change (llvm/llvm-project#114203) makes clang to emit an error when [[lifetimebound]] attribute is applied on a function implicit object parameter while the function returns void.

This instance found by https://source.chromium.org/search?q=(void%5C%20.*LIFETIME_BOUND).

Bug: 378948410
Change-Id: Id6823fb5950f8bedff90a0ddc63ed9fe7bfa9a99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6021322
Commit-Queue: Nico Weber <[email protected]>
Auto-Submit: Zequan Wu <[email protected]>
Reviewed-by: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1382755}
github-actions bot pushed a commit to kaidokert/chrome_base_mirror that referenced this pull request Nov 14, 2024
Upcoming clange change (llvm/llvm-project#114203) makes clang to emit an error when [[lifetimebound]] attribute is applied on a function implicit object parameter while the function returns void.

This instance found by https://source.chromium.org/search?q=(void%5C%20.*LIFETIME_BOUND).

Bug: 378948410
Change-Id: Id6823fb5950f8bedff90a0ddc63ed9fe7bfa9a99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6021322
Commit-Queue: Nico Weber <[email protected]>
Auto-Submit: Zequan Wu <[email protected]>
Reviewed-by: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1382755}
NOKEYCHECK=True
GitOrigin-RevId: 0dd182c28caed0ce8eee36c25c1ef3d85dc592d0
jrguzman-ms pushed a commit to msft-mirror-aosp/platform.external.libchrome that referenced this pull request Nov 20, 2024
Upcoming clange change (llvm/llvm-project#114203) makes clang to emit an error when [[lifetimebound]] attribute is applied on a function implicit object parameter while the function returns void.

This instance found by https://source.chromium.org/search?q=(void%5C%20.*LIFETIME_BOUND).

Bug: 378948410
Change-Id: Id6823fb5950f8bedff90a0ddc63ed9fe7bfa9a99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6021322
Commit-Queue: Nico Weber <[email protected]>
Auto-Submit: Zequan Wu <[email protected]>
Reviewed-by: Nico Weber <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1382755}


CrOS-Libchrome-Original-Commit: 0dd182c28caed0ce8eee36c25c1ef3d85dc592d0
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.

[clang] Clang must diagnose the use of [[lifetimebound]] annotation in void-returning functions
3 participants