<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/77087>77087</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [[clang::lifetimebound]] attr for implicit object parameter is ignored in some cases
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            clang:frontend
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          hokein
      </td>
    </tr>
</table>

<pre>
    Here is an example:

```
struct S {
    const int& x() const [[clang::lifetimebound]];
    int i;
};

#if DEF
const int& S::x() const { return i; }
#endif

void G() {
    auto& x = S().x(); // the lifetime bound warning is gone if compile with `-DDEF`
}
```


The `lifetimebound` attribute for implicit object parameter (e.g. `this` in method decl) is applied to a function type, so to detect an implicit object parameter has a liftime bound attr, we need to examine `TypeLoc`s from `TypeSourceInfo` of a function type, see [usage](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaInit.cpp#L7506-L7518).

However, the type loc/type source information are specific to the written source code, thus they can vary for different decls even if those decls refer to the same function entity.
In the given example, the lifetimebound attribute is present in the first declaration of `x()` but not in the second declaration (definition). When performing the attribute availability check, we happen to use the second one.

One possible solution is to iterate through all redeclarations when performing the availability check, it might? be too heavy. Alternatively, we could consider changing the way we model it in the AST, e.g. applying the attribute to a `FunctionDecl` rather than a `FunctionType`.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU1v4zYQ_TX0ZRBDpr7sgw_JuuoGCNCDA_RMSSNpuhQpkJS9_vfF0HJit0EXBQzJ_JjH997MUMp76g3iXuQvIj-s1BwG6_aD_YFkVrVtL_vv6BDIgzKAP9U4aRTps0gOIrk9i2T5xaEPbm4CHEGUL9cZAIDGGh-ATBCygJ9CboXcLZPx6JdGK9MzcvqsqcNAI9Z2Nq3ID_xL77DIBKCPGVF-ri5PmVIHh9-q6_Dh6OP1iH8wKF_AY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