[clang] Propagate lifetimebound from formal parameters to those in the canonical declaration and use that for analysis (PR #107627)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 20 11:53:06 PDT 2024
================
@@ -266,6 +262,20 @@ void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
}
}
+ } else if (auto *CanonDecl = FD->getCanonicalDecl(); FD != CanonDecl) {
+ // Propagate the lifetimebound attribute from parameters to the canonical
+ // declaration.
+ // Note that this doesn't include the implicit 'this' parameter, as the
+ // attribute is applied to the function type in that case.
+ const unsigned int NP = std::min(NumParams, CanonDecl->getNumParams());
+ for (unsigned int I = 0; I < NP; ++I) {
+ auto *CanonParam = CanonDecl->getParamDecl(I);
+ if (!CanonParam->hasAttr<LifetimeBoundAttr>() &&
+ FD->getParamDecl(I)->hasAttr<LifetimeBoundAttr>()) {
----------------
higher-performance wrote:
See the failing test. I'm inclined to revert to the old code since this approach ends up buggy, but let me know how you'd approach it if it would be different:
```
error: 'expected-warning' diagnostics expected but not seen:
File clang/test/SemaCXX/attr-lifetimebound.cpp Line 48: temporary bound to local reference 's' will be destroyed at the end of the full-expression
```
Failing test case:
```
const int &crefparam(const int ¶m); // Omitted in first decl
const int &crefparam(const int ¶m); // Omitted in second decl
const int &crefparam(const int ¶m [[clang::lifetimebound]]); // Add LB
const int &crefparam(const int ¶m) { return param; } // Omit in impl
const int& s = crefparam(2); // expected-warning {{temporary bound to local reference 's' will be destroyed at the end of the full-expression}}
```
https://github.com/llvm/llvm-project/pull/107627
More information about the cfe-commits
mailing list