[clang] [LifetimeSafety] Don't run TU analysis when no lifetime diagnostic is enabled (PR #245557)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 01:48:08 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Balázs Benics (steakhal)
<details>
<summary>Changes</summary>
`-fexperimental-lifetime-safety-tu-analysis` ran the analysis on every function, even if no lifetime diagnostic was enabled. It should only change how the analysis runs, not whether it runs.
Pragma enabled diagnostics work as before:
This means we keep building the CallGraph to find the FDs. This also means we iterate and check every function if we should run LSA on it.
Fixes #<!-- -->236110
Assisted-By: Claude
---
Full diff: https://github.com/llvm/llvm-project/pull/245557.diff
3 Files Affected:
- (modified) clang/lib/Sema/AnalysisBasedWarnings.cpp (+1-1)
- (modified) clang/lib/Sema/SemaLifetimeSafety.h (+19-16)
- (added) clang/test/Sema/LifetimeSafety/tu-analysis-enablement.cpp (+50)
``````````diff
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 151167aa461d5..697166a026836 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -3077,7 +3077,7 @@ LifetimeSafetyTUAnalysis(Sema &S, TranslationUnitDecl *TU,
if (!CanonicalFD)
continue;
const FunctionDecl *FD = CanonicalFD->getDefinition();
- if (!FD)
+ if (!FD || !lifetimes::ShouldAnalyzeFunction(S, FD))
continue;
AnalysisDeclContext AC(nullptr, FD);
setLifetimeSafetyCFGBuildOptions(AC);
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index c0e41ad3162fc..e6f2a7ce52099 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -110,6 +110,22 @@ inline bool ShouldSuggestLifetimeAnnotations(Sema &S, const Decl *D) {
return false;
}
+/// Whether the analysis should run on the function \p D, regardless of whether
+/// it runs per-function or in TU mode.
+inline bool ShouldAnalyzeFunction(Sema &S, const Decl *D) {
+ if (S.getLangOpts().DebugRunLifetimeSafety)
+ return true;
+
+ return ShouldCheckSafety(S, D) || ShouldCheckNoescapeViolations(S, D) ||
+ ShouldCheckLifetimeboundViolations(S, D) ||
+ ShouldCheckCaptureByViolations(S, D) ||
+ ShouldCheckMisplacedLifetimebound(S, D) ||
+ ShouldCheckInapplicableLifetimebound(S, D) ||
+ ShouldCheckFieldOriginEscape(S, D) ||
+ ShouldCheckDestructorUses(S, D) ||
+ ShouldSuggestLifetimeAnnotations(S, D);
+}
+
inline bool IsLifetimeSafetyEnabled(Sema &S, const Decl *D) {
// TODO: Enable ObjectiveC later when we know it's stable enough.
if (S.getLangOpts().ObjC)
@@ -119,28 +135,15 @@ inline bool IsLifetimeSafetyEnabled(Sema &S, const Decl *D) {
if (!S.getLangOpts().CPlusPlus && !S.getLangOpts().EnableLifetimeSafetyInC)
return false;
- // Translation-unit mode: whole-program analysis runs once on TU.
- // Individual function analysis is disabled when TU mode is enabled.
+ // In TU mode, the analysis is driven once from the TU, which then decides
+ // per function using ShouldAnalyzeFunction.
if (S.getLangOpts().EnableLifetimeSafetyTUAnalysis)
return isa<TranslationUnitDecl>(D);
- // Per-function mode: analysis runs on each function/method individually.
- // Skip TU-level calls when per-function mode is enabled.
if (isa<TranslationUnitDecl>(D))
return false;
- // Enable per-function mode via debug flag or specific diagnostics.
- if (S.getLangOpts().DebugRunLifetimeSafety)
- return true;
-
- return ShouldCheckSafety(S, D) || ShouldCheckNoescapeViolations(S, D) ||
- ShouldCheckLifetimeboundViolations(S, D) ||
- ShouldCheckCaptureByViolations(S, D) ||
- ShouldCheckMisplacedLifetimebound(S, D) ||
- ShouldCheckInapplicableLifetimebound(S, D) ||
- ShouldCheckFieldOriginEscape(S, D) ||
- ShouldCheckDestructorUses(S, D) ||
- ShouldSuggestLifetimeAnnotations(S, D);
+ return ShouldAnalyzeFunction(S, D);
}
inline LifetimeSafetyOpts GetLifetimeSafetyOpts(Sema &S, const Decl *D) {
diff --git a/clang/test/Sema/LifetimeSafety/tu-analysis-enablement.cpp b/clang/test/Sema/LifetimeSafety/tu-analysis-enablement.cpp
new file mode 100644
index 0000000000000..7db945d542aa6
--- /dev/null
+++ b/clang/test/Sema/LifetimeSafety/tu-analysis-enablement.cpp
@@ -0,0 +1,50 @@
+// TU mode alone must not run the analysis; it only changes how the analysis
+// runs once some lifetime diagnostic (or the debug flag) enables it.
+// Inferred lifetimebound attributes show whether the analysis ran on a function.
+//
+// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety-tu-analysis \
+// RUN: -flifetime-safety-inference -ast-dump %s \
+// RUN: | FileCheck %s --check-prefixes=CHECK,DISABLED
+//
+// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety-tu-analysis \
+// RUN: -flifetime-safety-inference -fdebug-run-lifetime-safety -ast-dump %s \
+// RUN: | FileCheck %s --check-prefixes=CHECK,ENABLED
+//
+// A lifetime diagnostic enabled by a pragma runs the analysis on the affected
+// functions only.
+//
+// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety-tu-analysis \
+// RUN: -flifetime-safety-inference -Wno-return-stack-address -DPRAGMA -verify -ast-dump %s \
+// RUN: | FileCheck %s --check-prefixes=CHECK,PRAGMA
+
+int *outside(int *p) { return p; }
+// CHECK-LABEL: FunctionDecl {{.*}} outside
+// CHECK: ParmVarDecl {{.*}} p 'int *'
+// DISABLED-NOT: LifetimeBoundAttr
+// PRAGMA-NOT: LifetimeBoundAttr
+// ENABLED-NEXT: LifetimeBoundAttr {{.*}} Implicit
+
+#ifdef PRAGMA
+#pragma clang diagnostic push
+#pragma clang diagnostic warning "-Wlifetime-safety-return-stack-addr"
+#endif
+int *inside(int *p) { return p; }
+// CHECK-LABEL: FunctionDecl {{.*}} inside
+// CHECK: ParmVarDecl {{.*}} p 'int *'
+// DISABLED-NOT: LifetimeBoundAttr
+// PRAGMA-NEXT: LifetimeBoundAttr {{.*}} Implicit
+// ENABLED-NEXT: LifetimeBoundAttr {{.*}} Implicit
+
+int *returnsLocal() {
+ int local = 0;
+ return &local; // expected-warning {{stack memory associated with local variable 'local' is returned}} \
+ // expected-note {{returned here}}
+}
+#ifdef PRAGMA
+#pragma clang diagnostic pop
+#endif
+
+int *returnsLocalOutside() {
+ int local = 0;
+ return &local; // no-warning
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/245557
More information about the cfe-commits
mailing list