llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

<details>
<summary>Changes</summary>

Basically, this attribute is useful for getting -Wunused-variable diagnostics 
from class types with a nontrivial constructor or destructor.

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


2 Files Affected:

- (modified) clang/include/clang/Basic/Attr.td (+1-1) 
- (modified) clang/include/clang/Basic/AttrDocs.td (+29) 


``````````diff
diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 7af72333d651c..15e73f4dd66ad 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -3898,7 +3898,7 @@ def VecReturn : InheritableAttr {
 def WarnUnused : InheritableAttr {
   let Spellings = [GCC<"warn_unused">];
   let Subjects = SubjectList<[Record]>;
-  let Documentation = [Undocumented];
+  let Documentation = [WarnUnusedDocs];
   let SimpleHandler = 1;
 }
 
diff --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index f54f8e8f90596..017550e09348a 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -388,6 +388,35 @@ pointer, but not any provenance of the allocation:
   }];
 }
 
+def WarnUnusedDocs : Documentation {
+  let Category = DocCatType;
+  let Content = [{
+The ``warn_unused`` attribute can be placed on the declaration of a structure 
or union type.
+When the ``-Wunused-variable`` diagnostic is enabled, local variables of types 
which have a non-trivial constructor or destructor are considered "used" by 
virtue of the constructor or destructor invocations involved.
+Declaring the type with the ``warn_unused`` attribute strengthens the "use" 
requirements to require a direct access to the variable, such as passing it as 
an argument to a call, calling a member function on it, taking the address of 
it, etc.
+
+This attribute is available in both C and C++ language modes but is primarily 
useful in C++ for classes which have a non-trivial constructor or destructor 
but act as a value type rather than an RAII type.
+
+..code-block:: c++
+
+  struct [[gnu::warn_unused]] S {
+    S();
+    ~S();
+  };
+
+  struct T {
+    T();
+    ~T();
+   };
+
+   void func() {
+     S s; // -Wunused-variable warning
+     T t; // No -Wunused-variable warning
+   }
+
+  }];
+}
+
 def MaybeUndefDocs : Documentation {
   let Category = DocCatVariable;
   let Content = [{

``````````

</details>


https://github.com/llvm/llvm-project/pull/201881
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to