Skip to content

Commit 084916a

Browse files
authored
[LifetimeSafety] Remove "experimental-" prefix from flags and diagnostics (llvm#176821)
Remove the "experimental-" prefix from lifetime safety diagnostic groups and command-line options. This enables the analysis in `-Wall`. We are now in a pretty stable state with no crashes. This change indicates that lifetime safety analysis is no longer considered experimental and is now a stable feature. By removing the "experimental-" prefix, we're signaling to users that this functionality is ready for use. - Renamed diagnostic groups from `experimental-lifetime-safety*` to `lifetime-safety*` - Updated command-line options from `-fexperimental-lifetime-safety*` to `-flifetime-safety*` and this is now ON by default. - Added a check to only enable lifetime safety analysis when relevant diagnostics are enabled - Updated test files to use the new flag names
1 parent 5e4f8d7 commit 084916a

11 files changed

Lines changed: 58 additions & 22 deletions

clang/docs/ReleaseNotes.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ Attribute Changes in Clang
124124

125125
Improvements to Clang's diagnostics
126126
-----------------------------------
127+
- Added ``-Wlifetime-safety`` to enable lifetime safety analysis,
128+
a CFG-based intra-procedural analysis that detects use-after-free and related
129+
temporal safety bugs. See the
130+
`RFC <https://discourse.llvm.org/t/rfc-intra-procedural-lifetime-analysis-in-clang/86291>`_
131+
for more details. By design, this warning is enabled in ``-Wall``. To disable
132+
the analysis, use ``-Wno-lifetime-safety`` or ``-fno-lifetime-safety``.
133+
134+
- Added ``-Wlifetime-safety-suggestions`` to enable lifetime annotation suggestions.
135+
This provides suggestions for function parameters that
136+
should be marked ``[[clang::lifetimebound]]`` based on lifetime analysis. For
137+
example, for the following function:
138+
139+
.. code-block:: c++
140+
141+
int* p(int *in) { return in; }
142+
143+
Clang will suggest:
144+
145+
.. code-block:: c++
146+
147+
warning: parameter in intra-TU function should be marked [[clang::lifetimebound]]
148+
int* p(int *in) { return in; }
149+
^~~~~~~
150+
[[clang::lifetimebound]]
151+
note: param returned here
152+
int* p(int *in) { return in; }
153+
^~
127154
128155
Improvements to Clang's time-trace
129156
----------------------------------

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,20 +533,20 @@ def Dangling : DiagGroup<"dangling", [DanglingAssignment,
533533
DanglingGsl,
534534
ReturnStackAddress]>;
535535

536-
def LifetimeSafetyPermissive : DiagGroup<"experimental-lifetime-safety-permissive">;
537-
def LifetimeSafetyStrict : DiagGroup<"experimental-lifetime-safety-strict">;
538-
def LifetimeSafety : DiagGroup<"experimental-lifetime-safety",
536+
def LifetimeSafetyPermissive : DiagGroup<"lifetime-safety-permissive">;
537+
def LifetimeSafetyStrict : DiagGroup<"lifetime-safety-strict">;
538+
def LifetimeSafety : DiagGroup<"lifetime-safety",
539539
[LifetimeSafetyPermissive, LifetimeSafetyStrict]> {
540540
code Documentation = [{
541541
Experimental warnings to detect use-after-free and related temporal safety bugs based on lifetime safety analysis.
542542
}];
543543
}
544544
def LifetimeSafetyCrossTUSuggestions
545-
: DiagGroup<"experimental-lifetime-safety-cross-tu-suggestions">;
545+
: DiagGroup<"lifetime-safety-cross-tu-suggestions">;
546546
def LifetimeSafetyIntraTUSuggestions
547-
: DiagGroup<"experimental-lifetime-safety-intra-tu-suggestions">;
547+
: DiagGroup<"lifetime-safety-intra-tu-suggestions">;
548548
def LifetimeSafetySuggestions
549-
: DiagGroup<"experimental-lifetime-safety-suggestions",
549+
: DiagGroup<"lifetime-safety-suggestions",
550550
[LifetimeSafetyCrossTUSuggestions,
551551
LifetimeSafetyIntraTUSuggestions]> {
552552
code Documentation = [{

clang/include/clang/Basic/LangOptions.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,12 @@ LANGOPT(CheckConstexprFunctionBodies, 1, 1, Benign,
501501

502502
LANGOPT(BoundsSafety, 1, 0, NotCompatible, "Bounds safety extension for C")
503503

504-
LANGOPT(EnableLifetimeSafety, 1, 0, NotCompatible, "Experimental lifetime safety analysis for C++")
504+
LANGOPT(EnableLifetimeSafety, 1, 0, NotCompatible, "Lifetime safety analysis for C++")
505505

506-
LANGOPT(EnableLifetimeSafetyInference, 1, 0, NotCompatible, "Experimental lifetime safety inference analysis for C++")
506+
LANGOPT(EnableLifetimeSafetyInference, 1, 0, NotCompatible, "Lifetime safety inference analysis for C++")
507507

508508
// TODO: Remove flag and default to end-of-TU analysis for lifetime safety after performance validation.
509-
LANGOPT(EnableLifetimeSafetyTUAnalysis, 1, 0, NotCompatible, "Experimental lifetime safety at translation-unit end, analyzing functions in call graph post-order for C++")
509+
LANGOPT(EnableLifetimeSafetyTUAnalysis, 1, 0, NotCompatible, "Lifetime safety at translation-unit end, analyzing functions in call graph post-order for C++")
510510

511511
LANGOPT(PreserveVec3Type, 1, 0, NotCompatible, "Preserve 3-component vector type")
512512

clang/include/clang/Options/Options.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,15 +1961,15 @@ defm bounds_safety : BoolFOption<
19611961
" experimental bounds safety extension for C">>;
19621962

19631963
defm lifetime_safety : BoolFOption<
1964-
"experimental-lifetime-safety",
1965-
LangOpts<"EnableLifetimeSafety">, DefaultFalse,
1964+
"lifetime-safety",
1965+
LangOpts<"EnableLifetimeSafety">, DefaultTrue,
19661966
PosFlag<SetTrue, [], [CC1Option], "Enable">,
19671967
NegFlag<SetFalse, [], [CC1Option], "Disable">,
19681968
BothFlags<[], [CC1Option],
19691969
" experimental lifetime safety for C++">>;
19701970

19711971
defm lifetime_safety_inference
1972-
: BoolFOption<"experimental-lifetime-safety-inference",
1972+
: BoolFOption<"lifetime-safety-inference",
19731973
LangOpts<"EnableLifetimeSafetyInference">, DefaultFalse,
19741974
PosFlag<SetTrue, [], [CC1Option], "Enable">,
19751975
NegFlag<SetFalse, [], [CC1Option], "Disable">,

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3063,9 +3063,19 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
30633063
AC.getCFGBuildOptions().AddCXXNewAllocator = false;
30643064
AC.getCFGBuildOptions().AddCXXDefaultInitExprInCtors = true;
30653065

3066+
bool IsLifetimeSafetyDiagnosticEnabled =
3067+
!Diags.isIgnored(diag::warn_lifetime_safety_loan_expires_permissive,
3068+
D->getBeginLoc()) ||
3069+
!Diags.isIgnored(diag::warn_lifetime_safety_loan_expires_strict,
3070+
D->getBeginLoc()) ||
3071+
!Diags.isIgnored(diag::warn_lifetime_safety_return_stack_addr_permissive,
3072+
D->getBeginLoc()) ||
3073+
!Diags.isIgnored(diag::warn_lifetime_safety_return_stack_addr_strict,
3074+
D->getBeginLoc());
30663075
bool EnableLifetimeSafetyAnalysis =
30673076
S.getLangOpts().EnableLifetimeSafety &&
3068-
!S.getLangOpts().EnableLifetimeSafetyTUAnalysis;
3077+
!S.getLangOpts().EnableLifetimeSafetyTUAnalysis &&
3078+
IsLifetimeSafetyDiagnosticEnabled;
30693079

30703080
// Force that certain expressions appear as CFGElements in the CFG. This
30713081
// is used to speed up various analyses.

clang/test/Analysis/LifetimeSafety/benchmark.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ def run_single_test(
350350
"-o",
351351
"/dev/null",
352352
"-ftime-trace=" + trace_file,
353-
"-Xclang",
354-
"-fexperimental-lifetime-safety",
353+
"-Wlifetime-safety",
355354
"-std=c++17",
356355
source_file,
357356
]

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %clang_cc1 -fsyntax-only -Wdangling -Wdangling-field -Wreturn-stack-address -verify %s
2-
// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -Wexperimental-lifetime-safety -Wno-dangling -verify=cfg %s
3-
// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -fexperimental-lifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wexperimental-lifetime-safety -Wno-dangling -verify=cfg %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wno-dangling -verify=cfg %s
3+
// RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety -Wno-dangling -verify=cfg %s
44

55
#include "Inputs/lifetime-analysis.h"
66

clang/test/Sema/warn-lifetime-safety-dataflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fexperimental-lifetime-safety -mllvm -debug-only=LifetimeFacts -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
1+
// RUN: %clang_cc1 -mllvm -debug-only=LifetimeFacts -Wlifetime-safety %s 2>&1 | FileCheck %s
22
// REQUIRES: asserts
33

44
struct MyObj {

clang/test/Sema/warn-lifetime-safety-missing-origin-stats.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -print-stats -fexperimental-lifetime-safety -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
1+
// RUN: %clang_cc1 -print-stats -Wlifetime-safety %s 2>&1 | FileCheck %s
22

33

44
// CHECK: *** LifetimeSafety Missing Origin per QualType: (QualType : count) :

clang/test/Sema/warn-lifetime-safety-suggestions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: rm -rf %t
22
// RUN: split-file %s %t
3-
// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -fexperimental-lifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wexperimental-lifetime-safety-suggestions -Wexperimental-lifetime-safety -Wno-dangling -I%t -verify %t/test_source.cpp
3+
// RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wno-dangling -I%t -verify %t/test_source.cpp
44

55
View definition_before_header(View a);
66

0 commit comments

Comments
 (0)