Skip to content

Commit 0d04789

Browse files
authored
[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)! ### Changes: - 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
1 parent abdd654 commit 0d04789

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
@@ -491,6 +491,8 @@ LANGOPT(CheckConstexprFunctionBodies, 1, 1, Benign,
491491

492492
LANGOPT(BoundsSafety, 1, 0, NotCompatible, "Bounds safety extension for C")
493493

494+
LANGOPT(EnableLifetimeSafety, 1, 0, NotCompatible, "Experimental lifetime safety analysis for C++")
495+
494496
LANGOPT(PreserveVec3Type, 1, 0, NotCompatible, "Preserve 3-component vector type")
495497

496498
#undef LANGOPT

clang/include/clang/Driver/Options.td

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

1907+
defm lifetime_safety : BoolFOption<
1908+
"experimental-lifetime-safety",
1909+
LangOpts<"EnableLifetimeSafety">, DefaultFalse,
1910+
PosFlag<SetTrue, [], [CC1Option], "Enable">,
1911+
NegFlag<SetFalse, [], [CC1Option], "Disable">,
1912+
BothFlags<[], [CC1Option],
1913+
" experimental lifetime safety for C++">>;
1914+
19071915
defm addrsig : BoolFOption<"addrsig",
19081916
CodeGenOpts<"Addrsig">, DefaultFalse,
19091917
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Emit">,

clang/lib/Sema/AnalysisBasedWarnings.cpp

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

2904-
bool EnableLifetimeSafetyAnalysis = !Diags.isIgnored(
2905-
diag::warn_experimental_lifetime_safety_dummy_warning, D->getBeginLoc());
2904+
bool EnableLifetimeSafetyAnalysis = S.getLangOpts().EnableLifetimeSafety;
29062905
// Install the logical handler.
29072906
std::optional<LogicalErrorHandler> LEH;
29082907
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 -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
1+
// RUN: %clang_cc1 -fexperimental-lifetime-safety -mllvm -debug-only=LifetimeFacts -Wexperimental-lifetime-safety %s 2>&1 | FileCheck %s
22
// REQUIRES: asserts
33

44
struct MyObj {

0 commit comments

Comments
 (0)