[LifetimeSafety] Add language option for experimental lifetime safety (#149592)

Add a language option flag for experimental lifetime safety analysis in C++.

This change provides a language option to control the experimental lifetime safety analysis feature, making it more explicit and easier to enable/disable. Previously, the feature was controlled indirectly through a diagnostic warning flag, which we do not want to accidentally enable with `-Weverything` (atm)!

- Added a new language option `EnableLifetimeSafety` in `LangOptions.def` for experimental lifetime safety analysis in C++
- Added corresponding driver options `-fexperimental-lifetime-safety` and `-fno-experimental-lifetime-safety` in `Options.td`
- Modified `AnalysisBasedWarnings.cpp` to use the new language option flag instead of checking if a specific diagnostic is ignored
- Updated a test case to use the new flag instead of relying on the warning flag alone

(cherry picked from commit 0d0478903474b2e53c874427e3d6eb2ed7567e50)
This commit is contained in:
Utkarsh Saxena 2025-07-22 21:31:09 +02:00 committed by Tobias Hieta
parent d502822e68
commit 6419104d18
4 changed files with 12 additions and 3 deletions

View File

@ -496,6 +496,8 @@ LANGOPT(CheckConstexprFunctionBodies, 1, 1, Benign,
LANGOPT(BoundsSafety, 1, 0, NotCompatible, "Bounds safety extension for C") LANGOPT(BoundsSafety, 1, 0, NotCompatible, "Bounds safety extension for C")
LANGOPT(EnableLifetimeSafety, 1, 0, NotCompatible, "Experimental lifetime safety analysis for C++")
LANGOPT(PreserveVec3Type, 1, 0, NotCompatible, "Preserve 3-component vector type") LANGOPT(PreserveVec3Type, 1, 0, NotCompatible, "Preserve 3-component vector type")
#undef LANGOPT #undef LANGOPT

View File

@ -1917,6 +1917,14 @@ defm bounds_safety : BoolFOption<
BothFlags<[], [CC1Option], BothFlags<[], [CC1Option],
" experimental bounds safety extension for C">>; " experimental bounds safety extension for C">>;
defm lifetime_safety : BoolFOption<
"experimental-lifetime-safety",
LangOpts<"EnableLifetimeSafety">, DefaultFalse,
PosFlag<SetTrue, [], [CC1Option], "Enable">,
NegFlag<SetFalse, [], [CC1Option], "Disable">,
BothFlags<[], [CC1Option],
" experimental lifetime safety for C++">>;
defm addrsig : BoolFOption<"addrsig", defm addrsig : BoolFOption<"addrsig",
CodeGenOpts<"Addrsig">, DefaultFalse, CodeGenOpts<"Addrsig">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Emit">, PosFlag<SetTrue, [], [ClangOption, CC1Option], "Emit">,

View File

@ -2891,8 +2891,7 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
.setAlwaysAdd(Stmt::UnaryOperatorClass); .setAlwaysAdd(Stmt::UnaryOperatorClass);
} }
bool EnableLifetimeSafetyAnalysis = !Diags.isIgnored( bool EnableLifetimeSafetyAnalysis = S.getLangOpts().EnableLifetimeSafety;
diag::warn_experimental_lifetime_safety_dummy_warning, D->getBeginLoc());
// Install the logical handler. // Install the logical handler.
std::optional<LogicalErrorHandler> LEH; std::optional<LogicalErrorHandler> LEH;
if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) { if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) {

View File

@ -1,4 +1,4 @@
// RUN: %clang_cc1 -mllvm -debug-only=LifetimeFacts,LifetimeDataflow -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s // RUN: %clang_cc1 -fexperimental-lifetime-safety -mllvm -debug-only=LifetimeFacts,LifetimeDataflow -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
// REQUIRES: asserts // REQUIRES: asserts
struct MyObj { struct MyObj {