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

    <tr>
        <th>Summary</th>
        <td>
            Off-by-one error in expression mangling of parameter reference in lambda
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            c++14,
            miscompilation
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          hubert-reinterpretcast
      </td>
    </tr>
</table>

<pre>
    According to the Itanium C++ ABI, the expression mangling for a function parameter involves a number `L` where `L` is `1` when referencing a parameter of the current function declarator within its parameter declaration clause.

In the case where `L` is one, the parameter reference is represented with a prefix of `fL0p` (that is with the value of `L` - 1 after the "L").

Consider the source below.

The value of `L` should be `1`; however, the mangling used for `x` is `_ZZZ1fvENKUlT_DtfL1p_EE_clIiEEDaS_S0_E1x` (with `fL1p` instead of the expected `fL0p`). Oddly enough, the typeinfo string for the closure type (`Z1fvEUlT_DtfL0p_EE_`) has `fL0p` as expected.

GCC has an off-by-one error in the opposite direction: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118144
Online compiler link: https://godbolt.org/z/51E9Yjvxf

### SOURCE (`<stdin>`)
```cpp
inline auto f() {
  return [](auto p, decltype(p)) {
    static int x = 0;
 return &x;
  };
}
void g() { throw f(); }
auto h() { return f()(0, 0); }
```

### COMPILER INVOCATION
```
clang++ -std=c++17 -xc++ - -O -S -emit-llvm -o -
```

### ACTUAL COMPILER OUTPUT (partial)
```
  ret ptr @_ZZZ1fvENKUlT_DtfL1p_EE_clIiEEDaS_S0_E1x
```

### EXPECTED COMPILER OUTPUT (partial)
```
  ret ptr @_ZZZ1fvENKUlT_DtfL0p_EE_clIiEEDaS_S0_E1x
```

### COMPILER VERSION INFO (`clang++ -v`)
```
clang version 20.0.0git (https://github.com/llvm/llvm-project.git 44514316bd5ef656076b6baaf6bccb298d98f0ea)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/wandbox/clang-head/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/13
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/14
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/14
Candidate multilib: .;@m64
Selected multilib: .;@m64
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVl-P4jgS_zTmpRTkOCHAAw80kBO6vmY0Ta_u5gU5SSXxrrEj2wFmP_3J-QM9vX2rW-1ISMSuql_96o_L5taKSiGuyOyJzLYT3rpam1XdZmhcYFAoh6Yx6HJu3STTxffVOs-1KYSqwGlwNcLecSXaM2wIeyLsCdZPe8I2nQhvjUFrhVZw5qqS3qrUBjiUrcqd32-44Wd0aECoi5YXtMBBtecMDZCEPpOEwrVGg_eVsP4zHAQKDJZoUOUenL_D02XHIW-NQeUeHgvMJTfcaQNX4WqhQDj7zm6Ue91c8tbilNA1oeu96gG5xT9S0grHqB9QIzX0GgZ9MlA5LDrHnqzBUtw8U5LQ8pk2Hoywhau58yadmoe8cNnioNd5DCAEXnofXkwYeyaMEbYcqG60sqIYpFa3JkfIUOrrID9-hmlr3coCMhzzS6InqPUVL2jG2O5lbC0WXS1JQm-Pspy-ffsWlpfdyz_f5PG0deVz2Jx2u1Mu92K32_LX0ys97cLbEGgXYBd72MUulHXIi7F2eGsw9_l6pMfHCIeikN8BlW6remTmvjcoVKnBOjP2WVcuqW1rerl3SRLaMRz50Y5fjww1t-9Lwe2dwpC4f2w2nRJXoMsyyL4HWiGgMdo3cOdQN422wiEUwmDXciRaQ-1cY0m0JiwlLK3yfFqpdqpNRViatdXvQkpOWGprfT1lbTXNK0GiVBQk2obhIoxjQtcHJYVCyPW5ERINSKF--wRbF5mWbsD-nbB0Fu6W__n1civ7GAiL-h-8Ht6-bnZDVki0sa4QikS7PhteNaH9L28aQteiJ8Bbp6H0VmwJZP5E6BrAoGuNgn6QELbolBpfHX-gfPYJ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