[clang] [clang] Permit lifetimebound in all language modes (PR #115482)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 8 05:37:32 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Gábor Horváth (Xazax-hun)

<details>
<summary>Changes</summary>

Lifetimebound annotations can help diagnose common cases of dangling including escaping the address of a stack variable from a function. This is useful in all C family languages, restricting these diagnostics to C++ is an artificial limitation.

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


2 Files Affected:

- (modified) clang/include/clang/Basic/Attr.td (-1) 
- (added) clang/test/Sema/attr-lifetimebound.c (+8) 


``````````diff
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 24cfb5ddb6d4ca..a631e81d40aa68 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1886,7 +1886,6 @@ def LifetimeBound : DeclOrTypeAttr {
   let Spellings = [Clang<"lifetimebound", 0>];
   let Subjects = SubjectList<[ParmVar, ImplicitObjectParameter], ErrorDiag>;
   let Documentation = [LifetimeBoundDocs];
-  let LangOpts = [CPlusPlus];
   let SimpleHandler = 1;
 }
 
diff --git a/clang/test/Sema/attr-lifetimebound.c b/clang/test/Sema/attr-lifetimebound.c
new file mode 100644
index 00000000000000..e1c714cb27dc8b
--- /dev/null
+++ b/clang/test/Sema/attr-lifetimebound.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -std=c99 -verify %s
+
+int *f(int* p __attribute__((lifetimebound)));
+
+int *g() {
+  int i;
+  return f(&i); // expected-warning {{address of stack memory associated with local variable 'i' returned}}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/115482


More information about the cfe-commits mailing list