Skip to content

Commit 7d97c04

Browse files
committed
[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 0d04789)
1 parent 80a6bc7 commit 7d97c04

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ LANGOPT(CheckConstexprFunctionBodies, 1, 1, Benign,
496496

497497
LANGOPT(BoundsSafety, 1, 0, NotCompatible, "Bounds safety extension for C")
498498

499+
LANGOPT(EnableLifetimeSafety, 1, 0, NotCompatible, "Experimental lifetime safety analysis for C++")
500+
499501
LANGOPT(PreserveVec3Type, 1, 0, NotCompatible, "Preserve 3-component vector type")
500502

501503
#undef LANGOPT

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,14 @@ defm bounds_safety : BoolFOption<
19171917
BothFlags<[], [CC1Option],
19181918
" experimental bounds safety extension for C">>;
19191919

1920+
defm lifetime_safety : BoolFOption<
1921+
"experimental-lifetime-safety",
1922+
LangOpts<"EnableLifetimeSafety">, DefaultFalse,
1923+
PosFlag<SetTrue, [], [CC1Option], "Enable">,
1924+
NegFlag<SetFalse, [], [CC1Option], "Disable">,
1925+
BothFlags<[], [CC1Option],
1926+
" experimental lifetime safety for C++">>;
1927+
19201928
defm addrsig : BoolFOption<"addrsig",
19211929
CodeGenOpts<"Addrsig">, DefaultFalse,
19221930
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Emit">,

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,8 +2891,7 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
28912891
.setAlwaysAdd(Stmt::UnaryOperatorClass);
28922892
}
28932893

2894-
bool EnableLifetimeSafetyAnalysis = !Diags.isIgnored(
2895-
diag::warn_experimental_lifetime_safety_dummy_warning, D->getBeginLoc());
2894+
bool EnableLifetimeSafetyAnalysis = S.getLangOpts().EnableLifetimeSafety;
28962895
// Install the logical handler.
28972896
std::optional<LogicalErrorHandler> LEH;
28982897
if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) {

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 -mllvm -debug-only=LifetimeFacts,LifetimeDataflow -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
1+
// RUN: %clang_cc1 -fexperimental-lifetime-safety -mllvm -debug-only=LifetimeFacts,LifetimeDataflow -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
22
// REQUIRES: asserts
33

44
struct MyObj {

0 commit comments

Comments
 (0)