[llvm-branch-commits] [clang] [analyzer] Correctly highlight the variables' range in UseAfterLifetimeEnd reports (PR #215905)
Benedek Kaibas via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Aug 14 08:02:42 PDT 2026
https://github.com/benedekaibas updated https://github.com/llvm/llvm-project/pull/215905
>From 3fec08160e74c7d064c18ad72bec785c8c043f18 Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Wed, 12 Aug 2026 22:31:23 +0200
Subject: [PATCH 1/6] [analyzer] Highilght variables range in emitted reports
from UseAfterLifetimeEnd
---
.../Checkers/UseAfterLifetimeEnd.cpp | 6 ++--
clang/test/Analysis/lifetime-bound.cpp | 28 +++++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp b/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
index de89836ee5dc2..f10da977172c0 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
@@ -98,8 +98,10 @@ void UseAfterLifetimeEnd::checkEndFunction(const ReturnStmt *RS,
}
static SourceRange getRegionDeclRange(const MemRegion *Source) {
- if (const auto *VR = dyn_cast_or_null<VarRegion>(Source))
- return VR->getDecl()->getSourceRange();
+ if (const auto *VR = dyn_cast_or_null<VarRegion>(Source)) {
+ const VarDecl *VD = VR->getDecl();
+ return SourceRange(VD->getLocation());
+ }
return SourceRange();
}
diff --git a/clang/test/Analysis/lifetime-bound.cpp b/clang/test/Analysis/lifetime-bound.cpp
index cd31c37aef16c..7db5eb156d6f4 100644
--- a/clang/test/Analysis/lifetime-bound.cpp
+++ b/clang/test/Analysis/lifetime-bound.cpp
@@ -450,3 +450,31 @@ int test_correct_param_highlight() {
// CHECK: return multi_params_annotated(&global_var, &local_n);
// CHECK-NEXT:{{\| \^~~~~~~~$}}
}
+
+int test_multi_local_bound_to_param_highlight() {
+ int j = 4, k = 5;
+ // expected-note at -1 {{'j' initialized here}}
+ // expected-note at -2 {{'k' initialized here}}
+ return multi_params_annotated(&j, &k);
+ // expected-warning at -1 {{address of stack memory associated with local variable 'j' returned}}
+ // expected-warning at -2 {{address of stack memory associated with local variable 'k' returned}}
+ // expected-warning at -3 {{Returning value bound to 'j' that will go out of scope}}
+ // expected-note at -4 {{Value's lifetime bound to the lifetime of 'j' here}}
+ // expected-note at -5 {{Lifetime of 'j' ended here}}
+ // expected-warning at -6 {{Returning value bound to 'k' that will go out of scope}}
+ // expected-note at -7 {{Value's lifetime bound to the lifetime of 'k' here}}
+ // expected-note at -8 {{Lifetime of 'k' ended here}}
+
+ // CHECK: note: Value's lifetime bound to the lifetime of 'j' here
+ // CHECK-NEXT: int j = 4, k = 5;
+ // CHECK-NEXT: ~
+ // CHECK: note: Lifetime of 'j' ended here
+ // CHECK-NEXT: int j = 4, k = 5;
+ // CHECK-NEXT: ~
+ // CHECK: note: Value's lifetime bound to the lifetime of 'k' here
+ // CHECK-NEXT: int j = 4, k = 5;
+ // CHECK-NEXT: ~
+ // CHECK: note: Lifetime of 'k' ended here
+ // CHECK-NEXT: int j = 4, k = 5;
+ // CHECK-NEXT: ~
+}
>From 44c93e18d7abb29bfe3bddb3b002dba42e0b6dae Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Thu, 13 Aug 2026 10:32:01 +0200
Subject: [PATCH 2/6] Remove strict whitespace indentations.
---
clang/test/Analysis/lifetime-bound.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/test/Analysis/lifetime-bound.cpp b/clang/test/Analysis/lifetime-bound.cpp
index 7db5eb156d6f4..649e0f03057ae 100644
--- a/clang/test/Analysis/lifetime-bound.cpp
+++ b/clang/test/Analysis/lifetime-bound.cpp
@@ -1,7 +1,7 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UseAfterLifetimeEnd,debug.DebugLifetimeModeling \
// RUN: -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UseAfterLifetimeEnd,debug.DebugLifetimeModeling \
-// RUN: -analyzer-output=text %s 2>&1 | FileCheck %s
+// RUN: -analyzer-output=text %s 2>&1 | FileCheck --strict-whitespace %s
struct A {};
struct Pair {
@@ -467,14 +467,14 @@ int test_multi_local_bound_to_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'j' here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT: ~
+ // CHECK-NEXT:{{\| ~$}}
// CHECK: note: Lifetime of 'j' ended here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT: ~
+ // CHECK-NEXT:{{\| ~$}}
// CHECK: note: Value's lifetime bound to the lifetime of 'k' here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT: ~
+ // CHECK-NEXT:{{\| ~$}}
// CHECK: note: Lifetime of 'k' ended here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT: ~
+ // CHECK-NEXT:{{\| ~$}}
}
>From 530dcbf2b80e5adb40b8a9552833b72064af9f93 Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Fri, 14 Aug 2026 01:13:39 +0200
Subject: [PATCH 3/6] Resolve merge conflicts.
---
clang/test/Analysis/lifetime-bound.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/test/Analysis/lifetime-bound.cpp b/clang/test/Analysis/lifetime-bound.cpp
index 649e0f03057ae..48066153b5532 100644
--- a/clang/test/Analysis/lifetime-bound.cpp
+++ b/clang/test/Analysis/lifetime-bound.cpp
@@ -434,6 +434,7 @@ int test_multi_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'local_two' here
// CHECK: return multi_params_annotated(&local_one, &local_two);
// CHECK-NEXT:{{\| \^~~~~~~~~~$}}
+<<<<<<< HEAD
}
int global_var;
@@ -449,6 +450,8 @@ int test_correct_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'local_n' here
// CHECK: return multi_params_annotated(&global_var, &local_n);
// CHECK-NEXT:{{\| \^~~~~~~~$}}
+=======
+>>>>>>> 99586f75316d (Strict whitespace matching applied.)
}
int test_multi_local_bound_to_param_highlight() {
@@ -464,7 +467,7 @@ int test_multi_local_bound_to_param_highlight() {
// expected-warning at -6 {{Returning value bound to 'k' that will go out of scope}}
// expected-note at -7 {{Value's lifetime bound to the lifetime of 'k' here}}
// expected-note at -8 {{Lifetime of 'k' ended here}}
-
+
// CHECK: note: Value's lifetime bound to the lifetime of 'j' here
// CHECK-NEXT: int j = 4, k = 5;
// CHECK-NEXT:{{\| ~$}}
>From 4aaa7b44454f3162fc42104e64d648b6ef7edf6f Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Fri, 14 Aug 2026 01:19:53 +0200
Subject: [PATCH 4/6] Remove HEAD.
---
clang/test/Analysis/lifetime-bound.cpp | 3 ---
1 file changed, 3 deletions(-)
diff --git a/clang/test/Analysis/lifetime-bound.cpp b/clang/test/Analysis/lifetime-bound.cpp
index 48066153b5532..ff8b4e45c0dee 100644
--- a/clang/test/Analysis/lifetime-bound.cpp
+++ b/clang/test/Analysis/lifetime-bound.cpp
@@ -434,7 +434,6 @@ int test_multi_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'local_two' here
// CHECK: return multi_params_annotated(&local_one, &local_two);
// CHECK-NEXT:{{\| \^~~~~~~~~~$}}
-<<<<<<< HEAD
}
int global_var;
@@ -450,8 +449,6 @@ int test_correct_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'local_n' here
// CHECK: return multi_params_annotated(&global_var, &local_n);
// CHECK-NEXT:{{\| \^~~~~~~~$}}
-=======
->>>>>>> 99586f75316d (Strict whitespace matching applied.)
}
int test_multi_local_bound_to_param_highlight() {
>From 0ee2460db810b989cbabe258119785c6003ded0b Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Fri, 14 Aug 2026 01:35:21 +0200
Subject: [PATCH 5/6] Correct CHECK-NEXT lines.
---
clang/test/Analysis/lifetime-bound.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/test/Analysis/lifetime-bound.cpp b/clang/test/Analysis/lifetime-bound.cpp
index ff8b4e45c0dee..9e4e889cec3c9 100644
--- a/clang/test/Analysis/lifetime-bound.cpp
+++ b/clang/test/Analysis/lifetime-bound.cpp
@@ -467,14 +467,14 @@ int test_multi_local_bound_to_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'j' here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~$}}
+ // CHECK-NEXT:{{\| ~~~~~~~~~$}}
// CHECK: note: Lifetime of 'j' ended here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~$}}
+ // CHECK-NEXT:{{\| ~~~~~~~~~$}}
// CHECK: note: Value's lifetime bound to the lifetime of 'k' here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~$}}
+ // CHECK-NEXT:{{\| ~~~~~~~~~~~~~~~~$}}
// CHECK: note: Lifetime of 'k' ended here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~$}}
+ // CHECK-NEXT:{{\| ~~~~~~~~~~~~~~~~$}}
}
>From e729d70b6a237e9aa11648b8801516c4b065241f Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Fri, 14 Aug 2026 15:50:05 +0200
Subject: [PATCH 6/6] Correct CHECK-NEXT lines.
---
clang/test/Analysis/lifetime-bound.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/test/Analysis/lifetime-bound.cpp b/clang/test/Analysis/lifetime-bound.cpp
index 9e4e889cec3c9..ff8b4e45c0dee 100644
--- a/clang/test/Analysis/lifetime-bound.cpp
+++ b/clang/test/Analysis/lifetime-bound.cpp
@@ -467,14 +467,14 @@ int test_multi_local_bound_to_param_highlight() {
// CHECK: note: Value's lifetime bound to the lifetime of 'j' here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~~~~~~~~~$}}
+ // CHECK-NEXT:{{\| ~$}}
// CHECK: note: Lifetime of 'j' ended here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~~~~~~~~~$}}
+ // CHECK-NEXT:{{\| ~$}}
// CHECK: note: Value's lifetime bound to the lifetime of 'k' here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~~~~~~~~~~~~~~~~$}}
+ // CHECK-NEXT:{{\| ~$}}
// CHECK: note: Lifetime of 'k' ended here
// CHECK-NEXT: int j = 4, k = 5;
- // CHECK-NEXT:{{\| ~~~~~~~~~~~~~~~~$}}
+ // CHECK-NEXT:{{\| ~$}}
}
More information about the llvm-branch-commits
mailing list