Skip to content

release/20.x: [clang] Fix false positive regression for lifetime analysis warning. (#127460) #127618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions clang/test/Sema/Inputs/lifetime-analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand All @@ -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>;

Expand Down
24 changes: 24 additions & 0 deletions clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

// FIXME: nested cases are not supported now.
StatusOr<StatusOr<std::string>> s4;
return s4.value()->value()->data();
}

} // namespace GH127195
Loading