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

    <tr>
        <th>Summary</th>
        <td>
            [HLSL][RootSignature] Fix `RootSignatureLexer` to output correct `SourceLocation`
        </td>
    </tr>

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

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

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

<pre>
    The `SourceLocation` of a `RootSignatureToken` is incorrectly set when the root signature is defined in a multi-line macro expansion.

The root signature attribute is defined with a `StringLiteral` argument. In `ParseDeclCXX.cpp` we retrieve the `StringLiteral` by invoking `ParseUnevaluatedStringLiteralExpression`, which will concatenate each of the lines into a singular string.

Currently, we only pass a `StringRef` of the concatenated strings to the `RootSignatureLexer`. Which means the `SourceLocation` that is used to construct the `RootSignatureTokens` are actually just an offset into the concatenated string. Thus, when we produce the diagnostic, the `SourceLocation` will be "earlier" than the proper location as it did not account for any characters between the end of this token and the start of the next.

This can be resolved by retaining the `SourceLocation` information that is kept in `StringLiteral` and then converting the offset in the concatenated string into the proper `SourceLocation` using the `StringLiteral::getLocationOfByte` interface. To do so, we will need to pass down the `clang::Preprocessor` to `LexHLSLRootSignature`.

For example:
https://godbolt.org/z/WsPf599c7

```
#define DemoRootSignature \
 "CBV(b0)," \
 "RootConstants(num32BitConstants = 3, b0, invalid)"
  expected caret location --------------------^
  actual caret location ------------------^
```

The caret points 5 characters early because the current offset did not account for the characters:
```
'"' ' ' '\' ' ' '"'
 1   2   3   4   5
```

AC:
- [ ] Update `RootSignatureLexer` to retain this information by taking in `StringLiteral` instead of its `StringRef`
- [ ] Update `RootSignatureLexer` to be passed down the `clang::Preprocessor` so that it can invoke `StringLiteral::getLocationOfByte`
- [ ] Update `LexHLSLRootSignatureTest` to construct a phony preprocessor for the test cases
- [ ] Add a test to `RootSignature-err.hlsl` showing that the `SourceLocation` is correctly set for diagnostics in a multi-line macro expansion
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVl9v4roT_TTmZVQUDIH2gQcKi34_qdJdbbt393XiTIi3xo7sCX_up79yHAps6e7dqggUj8dnzpwzMYagN5ZoLvJHIaW2BRmthJQiXw2w5dr5ef9wULjyOH-pCcQ0e3atV_TkFLJ2VkwzcBVgXPniHD_rjUVuPb24V-pWdQBtlfOeFJsjBGLY12SBawLvHEM4bYmhJVXaUgnaAsK2NazvjLYEW1TeAR0atEE7OxTZQmSLl_c5kNnrouWrbHvNdcL4zF7bzZNm8mgiPPSbdkuWh_B_GwM-ow-0ImWW378PVdPEmD2BJ_aadtTBvpWnOIK2O_eq7eYtzVdLOzQtMpVX8Z8OjacQEn1CLmFfa1XDXhsDylmFTBaZgFDVkd14ZmQhMskOEIK2m9agh9Cl7dlYtt6TZXPsUhI4a47QYAiXpX-hqu9ZzHpxWtknC8DuVOVVR5_oQF5MsyF86-BuCW144-OdKrhGjj1oA5UxpXI2sG8V30zeySWkhhCg4haNOcKPNjCgBVdVUThd-R_gHsJL3YbEJtlYf-Nd2arUslLjxrrAUeHLj0F3LSgIhJSE3mjyQspYStJr411DHky_AzCAZih1CdYxoFKutQyV84D2CKpGj4rJByiI99SLnmyZ6NeR6leygLbsVgKj51NrLB34TeY6gEIbkXkKzuyojILzxKhtVNyHBWlbOb9NaE8deaUmUnnbDgmKjQzvyPMp-VsDPqL_3JuepJtw2nCJ9urw8UKMFxviU_hf1eORKdXA5CtUNIQXB6WD4HqFd-2ylATWKb10e3vKrwzaTcr72VPjnaIQnO_E6WLAEx3-9_T8dCXEKPDE-tp5oANuG0MxSbaomZsQf8q1kOuNKwtneOj8Rsj1P0Kuv4XPVf7woGZpf_R2-s8WQo7TLIIVbd3VgSDypcgWUXLLx7-FvC8yIR-EXEblndfinmW0EFoOQt7bdjuWj_r8DMR4BeNITNHNFG13aHTZ5ZIxSZyepGLHFHris4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