Author: Utkarsh Saxena
Date: 2026-10-20T15:51:45Z
New Revision: 8e635b365d83bdf76571f14e791d69b1ba33634f

URL: 
https://github.com/llvm/llvm-project/commit/8e635b365d83bdf76571f14e791d69b1ba33634f
DIFF: 
https://github.com/llvm/llvm-project/commit/8e635b365d83bdf76571f14e791d69b1ba33634f.diff

LOG: [LifetimeSafety] Add a group for experimental warnings (#233377)

Added the `-Wlifetime-safety-experimental` diagnostic group. It is not
included in `-Wlifetime-safety-all`, and it contains two warnings:
- `-Wlifetime-safety-experimental-invalidation`, renamed from
`-Wlifetime-safety-invalidation`.
- `-Wlifetime-safety-experimental-field-origin-escape`, renamed from
`-Wexperimental-lifetime-safety-field-origin-escape`.

Invalidation checking and field-origin-escape checking are experimental
and can produce false positives or even non-actionable warnings.

Added: 
    

Modified: 
    clang/docs/LifetimeSafety.md
    clang/docs/ReleaseNotes.md
    clang/include/clang/Basic/DiagnosticGroups.td
    clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
    clang/test/Sema/LifetimeSafety/invalidations.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/LifetimeSafety.md b/clang/docs/LifetimeSafety.md
index 2785d2c80ccb1..ea2364deaee61 100644
--- a/clang/docs/LifetimeSafety.md
+++ b/clang/docs/LifetimeSafety.md
@@ -387,6 +387,8 @@ iterators, pointers and references to its elements.
 
 :::{note}
 Invalidation checking is highly experimental and may produce false positives.
+It is not enabled by `-Wlifetime-safety`; use
+`-Wlifetime-safety-experimental-invalidation` or 
`-Wlifetime-safety-experimental`.
 :::
 
 ````{list-table}
@@ -500,12 +502,11 @@ enables only the high-confidence subset of these checks.
     - `-Wlifetime-safety-return-stack-addr`: Warns when a function returns a 
pointer or reference to one of its local stack variables.
     - `-Wlifetime-safety-dangling-field`: Warns when a class field is assigned 
a pointer to a temporary or stack variable whose lifetime is shorter than the 
class instance.
 
-  - `-Wlifetime-safety-strict`: Enables stricter and experimental checks. 
These may produce false positives in code that uses move semantics heavily, as 
the analysis might conservatively assume a use-after-free even if ownership was 
transferred.
+  - `-Wlifetime-safety-strict`: Enables stricter checks. These may produce 
false positives in code that uses move semantics heavily, as the analysis might 
conservatively assume a use-after-free even if ownership was transferred.
 
     - `-Wlifetime-safety-use-after-scope-moved`: Same as 
`-Wlifetime-safety-use-after-scope` but for cases where the variable may have 
been moved from before its destruction.
     - `-Wlifetime-safety-return-stack-addr-moved`: Same as 
`-Wlifetime-safety-return-stack-addr` but for cases where the variable may have 
been moved from.
     - `-Wlifetime-safety-dangling-field-moved`: Same as 
`-Wlifetime-safety-dangling-field` but for cases where the variable may have 
been moved from.
-    - `-Wlifetime-safety-invalidation`: Warns when a pointer, reference, 
iterator or view is used after an operation that may invalidate it, such as 
container mutation or explicit destruction (e.g., `std::unique_ptr::reset`, 
`std::destroy_at`) (Experimental).
 
 - `-Wlifetime-safety-suggestions`: Enables suggestions to add 
`[[clang::lifetimebound]]` to function parameters and `this` parameters.
 
@@ -519,6 +520,11 @@ enables only the high-confidence subset of these checks.
   - `-Wlifetime-safety-noescape`: Warns when a parameter marked with 
`[[clang::noescape]]` escapes the function.
   - `-Wlifetime-safety-lifetimebound-violation`: Warns when the analysis 
cannot verify that the return value can be lifetime bound to a parameter marked 
with `[[clang::lifetimebound]]`.
 
+- `-Wlifetime-safety-experimental`: Enables experimental checks. These may 
have known false positives and their behavior may change. They are not enabled 
by `-Wlifetime-safety-all`.
+
+  - `-Wlifetime-safety-experimental-invalidation`: Warns when a pointer, 
reference, iterator or view is used after an operation that may invalidate it, 
such as container mutation or explicit destruction (e.g., 
`std::unique_ptr::reset`, `std::destroy_at`).
+  - `-Wlifetime-safety-experimental-field-origin-escape`: Warns when a member 
function returns a value derived from a pointer, reference, or view field 
rather than from `*this` (e.g., `Span::data()`). Owned pointer fields (e.g., 
`Vector::data()`) are also reported; mark such methods 
`[[clang::lifetimebound]]` to silence the warning.
+
 ## Limitations
 
 ### Move Semantics

diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 79de46f09a835..fc09877f6b45a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -527,6 +527,16 @@ features cannot lower the translation-unit ABI level;
   };
   ```
 
+- Added `-Wlifetime-safety-experimental` to group experimental lifetime safety
+  warnings. It is not part of `-Wlifetime-safety-all`. It contains:
+
+  - `-Wlifetime-safety-experimental-invalidation`, renamed from
+    `-Wlifetime-safety-invalidation` and no longer enabled by
+    `-Wlifetime-safety` or `-Wlifetime-safety-strict`.
+  - `-Wlifetime-safety-experimental-field-origin-escape`, which warns when a
+    member function returns a value derived from a pointer, reference, or view
+    field rather than from `*this`.
+
 - Lifetime safety analysis is now enabled for C by default. The 
`-fexperimental-lifetime-safety-c`
   flag is renamed to `-flifetime-safety-c`. Use `-fno-lifetime-safety-c` to 
disable it.
 

diff  --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index b6c7ff7276260..b3bb2fb1b53fd 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -634,9 +634,10 @@ This may contain false-positives, e.g. when the borrowed 
storage is potentially
   }];
 }
 
-def LifetimeSafetyInvalidation : DiagGroup<"lifetime-safety-invalidation"> {
+def LifetimeSafetyInvalidation
+    : DiagGroup<"lifetime-safety-experimental-invalidation"> {
   code Documentation = [{
-Warning to detect invalidation of references.
+Experimental. Warning to detect invalidation of references.
   }];
 }
 
@@ -681,8 +682,7 @@ def LifetimeSafetyStrict : 
DiagGroup<"lifetime-safety-strict",
                                     LifetimeSafetyDanglingFieldMoved,
                                     LifetimeSafetyDanglingFieldLambdaCapture,
                                     LifetimeSafetyDanglingGlobalMoved,
-                                    LifetimeSafetyDanglingGlobalInMain,
-                                    LifetimeSafetyInvalidation]>;
+                                    LifetimeSafetyDanglingGlobalInMain]>;
 
 def LifetimeSafety : DiagGroup<"lifetime-safety",
                                [LifetimeSafetyPermissive, 
LifetimeSafetyStrict]> {
@@ -745,7 +745,7 @@ Validates correct usage and adherence to lifetime safety 
annotations like [[clan
 }
 
 def LifetimeSafetyFieldOriginEscape
-    : DiagGroup<"experimental-lifetime-safety-field-origin-escape"> {
+    : DiagGroup<"lifetime-safety-experimental-field-origin-escape"> {
   code Documentation = [{
 Experimental. Detects member functions returning a value derived from a
 pointer, reference, or view field rather than from ``*this``. May report owned
@@ -754,6 +754,16 @@ method ``[[clang::lifetimebound]]``.
   }];
 }
 
+def LifetimeSafetyExperimental
+    : DiagGroup<"lifetime-safety-experimental",
+                [LifetimeSafetyInvalidation,
+                 LifetimeSafetyFieldOriginEscape]> {
+  code Documentation = [{
+Experimental lifetime safety warnings. These may have known false positives and
+their behavior may change. Not enabled by -Wlifetime-safety-all.
+  }];
+}
+
 def LifetimeSafetyAll : DiagGroup<"lifetime-safety-all",
                                   [LifetimeSafety,
                                   LifetimeSafetySuggestions,

diff  --git a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp 
b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
index 49c49b18e3b9c..0208d4df7ebcc 100644
--- a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
+++ b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
@@ -1,8 +1,8 @@
 // RUN: rm -rf %t
 // RUN: split-file %s %t
-// RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wlifetime-safety -Wlifetime-safety-annotation-placement 
-Wexperimental-lifetime-safety-field-origin-escape -Wno-dangling -I%t -I%S 
-verify %t/test_source.cpp
-// RUN: %clang_cc1 -fsyntax-only -std=c++23 -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wlifetime-safety -Wexperimental-lifetime-safety-field-origin-escape 
-Wno-dangling -I%t -I%S -verify %t/test_source.cpp
-// RUN: %clang_cc1 -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wlifetime-safety -Wexperimental-lifetime-safety-field-origin-escape 
-Wno-dangling -I%t -I%S -fixit %t/test_source.cpp
+// RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wlifetime-safety -Wlifetime-safety-annotation-placement 
-Wlifetime-safety-experimental-field-origin-escape -Wno-dangling -I%t -I%S 
-verify %t/test_source.cpp
+// RUN: %clang_cc1 -fsyntax-only -std=c++23 -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wlifetime-safety -Wlifetime-safety-experimental-field-origin-escape 
-Wno-dangling -I%t -I%S -verify %t/test_source.cpp
+// RUN: %clang_cc1 -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wlifetime-safety -Wlifetime-safety-experimental-field-origin-escape 
-Wno-dangling -I%t -I%S -fixit %t/test_source.cpp
 // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference 
-fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions 
-Wno-dangling -I%t -I%S -Werror=lifetime-safety-suggestions %t/test_source.cpp
 
 View definition_before_header(View a);

diff  --git a/clang/test/Sema/LifetimeSafety/invalidations.cpp 
b/clang/test/Sema/LifetimeSafety/invalidations.cpp
index a7c62852c6989..f96e8259b64eb 100644
--- a/clang/test/Sema/LifetimeSafety/invalidations.cpp
+++ b/clang/test/Sema/LifetimeSafety/invalidations.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wno-dangling -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety 
-Wlifetime-safety-experimental-invalidation -Wno-dangling -verify %s
 
 #include "Inputs/lifetime-analysis.h"
 


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

Reply via email to