[clang] [clang] Fix false positive regression for lifetime analysis warning. (PR #127460)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 17 01:23:17 PST 2025
https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/127460
>From 26cf4cd668f9a2d533014a85e8da4a91ce6c8994 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Mon, 17 Feb 2025 10:08:14 +0100
Subject: [PATCH] [clang] Fix false positive regression for lifetime analysis
warning.
---
clang/lib/Sema/CheckExprLifetime.cpp | 5 ++--
clang/test/Sema/Inputs/lifetime-analysis.h | 2 ++
.../Sema/warn-lifetime-analysis-nocfg.cpp | 24 +++++++++++++++++++
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 8963cad86dbca..3e0867343ccee 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1239,11 +1239,12 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
}
// Check the return type, e.g.
// const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
+ // GSLOwner* func(cosnt Foo& foo [[clang::lifetimebound]])
// GSLPointer func(const Foo& foo [[clang::lifetimebound]])
if (FD &&
- ((FD->getReturnType()->isReferenceType() &&
+ ((FD->getReturnType()->isPointerOrReferenceType() &&
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType())) ||
- isPointerLikeType(FD->getReturnType())))
+ isGLSPointerType(FD->getReturnType())))
return Report;
return Abandon;
diff --git a/clang/test/Sema/Inputs/lifetime-analysis.h b/clang/test/Sema/Inputs/lifetime-analysis.h
index d318033ff0cc4..2072e4603cead 100644
--- a/clang/test/Sema/Inputs/lifetime-analysis.h
+++ b/clang/test/Sema/Inputs/lifetime-analysis.h
@@ -61,6 +61,7 @@ struct basic_string_view {
basic_string_view();
basic_string_view(const T *);
const T *begin() const;
+ const T *data() const;
};
using string_view = basic_string_view<char>;
@@ -80,6 +81,7 @@ struct basic_string {
const T *c_str() const;
operator basic_string_view<T> () const;
using const_iterator = iter<T>;
+ const T *data() const;
};
using string = basic_string<char>;
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 04bb1330ded4c..a65bb936ca8cb 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -852,3 +852,27 @@ struct Test {
};
} // namespace GH120543
+
+namespace GH127195 {
+template <typename T>
+struct StatusOr {
+ T* operator->() [[clang::lifetimebound]];
+ T* value() [[clang::lifetimebound]];
+};
+
+const char* foo() {
+ StatusOr<std::string> s;
+ return s->data(); // expected-warning {{address of stack memory associated with local variable}}
+
+ StatusOr<std::string_view> s2;
+ return s2->data();
+
+ StatusOr<StatusOr<std::string_view>> s3;
+ return s3.value()->value()->data();
+
+ // would be nice to support the nested cases.
+ StatusOr<StatusOr<std::string>> s4;
+ return s4.value()->value()->data();
+}
+
+} // namespace GH127195
More information about the cfe-commits
mailing list