================
@@ -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 &param); // Omitted in first decl
  const int &crefparam(const int &param); // Omitted in second decl
  const int &crefparam(const int &param [[clang::lifetimebound]]); // Add LB
  const int &crefparam(const int &param) { 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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to