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

    <tr>
        <th>Summary</th>
        <td>
            DeclRefExpr under a CXXOperatorCallExpr lacks a UsingShadowDecl foundDecl
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    For this code:

```c++
enum E {};

namespace NS1 {
  int operator& (E, E);      // Original FunctionDecl.
}

namespace NS2 {
  using NS1::operator&;      // Creates a UsingShadowDecl.

  int caller(E e)
  {
    return e & e;            // 'operator&' found via 'using'.
  }
}
```

The operator expression is represented by a `CXXOperatorCallExpr`, and underneath that is a `DeclRefExpr` pointing at the `operator&` (excerpt of `clang -Xclang -ast-dump -fsyntax-only`):

```
`-CXXOperatorCallExpr 0x55f8320a9cc0 <col:12, col:16> 'int' '&'
  |-ImplicitCastExpr 0x55f8320a9ca8 <col:14> 'int (*)(E, E)' <FunctionToPointerDecay>
  | `-DeclRefExpr 0x55f8320a9c30 <col:14> 'int (E, E)' lvalue Function 0x55f8320a9800 'operator&' 'int (E, E)'
```

The problem is the declaration pointed to by the `DeclRefExpr` is the one in `NS1`, without mention of the `UsingShadowDecl` in `NS2`.

In contrast, when we use ordinary functions:

```c++
enum E {};

namespace NS1 {
  int fn(E, E);
}

namespace NS2 {
  using NS1::fn;

  int caller(E e)
  {
    return fn(e, e);
  }
}
```

The corresponding output is:

```
`-CallExpr 0x564aedbaeb90 <col:12, col:19> 'int'
 |-ImplicitCastExpr 0x564aedbaeb78 <col:12> 'int (*)(E, E)' <FunctionToPointerDecay>
  | `-DeclRefExpr 0x564aedbaeaf8 <col:12> 'int (E, E)' lvalue Function 0x564aedbae6b0 'fn' 'int (E, E)' (UsingShadow 0x564aedbae850 'fn')
```

showing that `DeclRefExpr::getFoundDecl()` returns the `UsingShadowDecl` (scroll right to see the end of the output line).

Similarly, in the original example, changing:

```c++
 return e & e;
```

to:

```c++
    return operator&(e, e);
```

causes the `UsingShadowDecl` to be mentioned.

The lack of a "found" declaration for the operator case is problematic because it means we cannot see that the operator expression depends on the `using` declaration.  This causes incorrect results during dependency analysis, for example [include-what-you-use](https://github.com/include-what-you-use/include-what-you-use).

The behavior reported here is observed with Clang-16 and with a `main` branch source build from 2023-09-13.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0V11v4joT_jXmZhSUOhDIBRcttNLevPvq7B5pbyfOQHzW2JHttOXfH40JbWChu6ujrVDJhz3PfD2PBwxB7yzRSswfxHwzwT62zq_CXnWE3ye1aw6rJ-chtjqAcg2J4l7kG5Gf_pf58aOEfOBPekq238MjiMWDWGxE8TDeYXFPoUNF8L8vd2lJegygbQTXkcfovJAlCLl8FHINj0JWoniA9Cfkk5BP8NnrnbZo4Km3KmpnN6TMdIBZbG7hyTFeH7TdsRMcUnE_gr5EW3vCSAEQ_uY9X1ps3MsYcRSBQmPIs-9A7PjwaoQL4Cn23gIBh0lvaGeYQi7GHskFbF1vG3jWyO-S80Iupu8Am8vwT6UZO_m1pbckA712nkLQzoIO4InvyEZqoD4Agijz9bdvn4fVazTm8bXzbFCuAW0DvW3IW8LYQmwxspG0i1PzF22H1dA5bSPnGiPElnjFOLQy51LTqyLfRXBbfq8M2h1k34ZvDDFr-n0H2TYcbMTXzFlzSJ5Utxry7Ta7EgTkr_P5dlnIHCulchDFWjkjivs7ycEN16UoHjnZ2kYugJCLYynec77OPu07o5WOawzxB8u4HFmevVvjgIW8Z-_HTc4YxfrU0l_d_zlx5Dek8CCKxxEs5ygbpfkMtchvo56BmWc0Pb1xaGxkmedXWvC6nZ-0W-ddbWjP3cHVb0gZ9JgAU2dQA9Fxxw29cdE9wzZnCbTl98zYYw--6Ni6PsKebDLnticbFzxNdobNUpT5GW8_WVDORo8hJqMtWXgh6AOB84226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