<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/73637>73637</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-format] json raw string gets eaten when formatting with RawStringFormats
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-format
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
j-hap
</td>
</tr>
</table>
<pre>
I am using json raw strings in C++ code and would like clang-format to format them together with the source code. This works via the RawStringFormats configuration (see https://reviews.llvm.org/D93528).
Here's a minimal my .clang-format
```
BasedOnStyle: LLVM
RawStringFormats:
- Language: Json
Delimiters:
- json
BasedOnStyle: LLVM
```
Here's example code:
```
int main() {
auto teststring = R"json({"number":1})json";
return 0;
}
```
both files are also attached. If I run clang-format on that example file, each time it is applies, it eats a bit of the beginning of the json string. When the string is all eaten up, clang-format crashes (in this case on the 5th format)
```
$ clang-format --version
clang-format version 17.0.1 (git://code.qt.io/clang/llvm-project.git 7c67fc21f9bbf5ac83c4cde7eb68a19169377c00)
$ clang-format main.cpp | tee /dev/tty | clang-format | tee /dev/tty | clang-format | tee /dev/tty | clang-format | tee /dev/tty | clang-format
int main() {
auto teststring = R"json(umber": 1 })json";
return 0;
}
int main() {
auto teststring = R"json(r ": 1 })json";
return 0;
}
int main() {
auto teststring = R"json( 1 })json";
return 0;
}
int main() {
auto teststring = R"json( })json";
return 0;
}
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. Program arguments: clang-format
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 clang-format 0x00000000004779b8
1 clang-format 0x000000000047577c
2 libpthread.so.0 0x00007f1809918cf0
3 clang-format 0x000000000055ecca
4 clang-format 0x0000000000561694
5 clang-format 0x0000000000563642
6 clang-format 0x0000000000566708
7 clang-format 0x00000000004b71ee
8 clang-format 0x0000000000536e4c
9 clang-format 0x0000000000538e47
10 clang-format 0x000000000053b0f9
11 clang-format 0x000000000053b550
12 clang-format 0x00000000004fe98b
13 clang-format 0x00000000004fda9a
14 clang-format 0x00000000004b4136
15 clang-format 0x00000000004deab3
16 clang-format 0x00000000004b3a27
17 clang-format 0x00000000004b3a64
18 clang-format 0x00000000004b75b2
19 clang-format 0x00000000004b8c57
20 clang-format 0x0000000000422c94
21 clang-format 0x000000000040a5a4
22 libc.so.6 0x00007f1808840d85 __libc_start_main + 229
23 clang-format 0x000000000041c4ae
Segmentation fault (core dumped)
```
This is how it looks in MSYS2:
```
$ clang-format --version
clang-format version 17.0.4
$ clang-format main.cpp | tee /dev/tty | clang-format | tee /dev/tty | clang-format | tee /dev/tty | clang-format | tee /dev/tty | clang-format
int main() {
auto teststring = R"json(umber": 1 })json";
return 0;
}
int main() {
auto teststring = R"json(r ": 1 })json";
return 0;
}
int main() {
auto teststring = R"json( 1 })json";
return 0;
}
int main() {
auto teststring = R"json( })json";
return 0;
}
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. Program arguments: C:\\msys64\\mingw64\\bin\\clang-format.exe
Exception Code: 0xC0000005
#0 0x00007ffa199a3780 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x483780)
#1 0x00007ffa19ee01b3 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x9c01b3)
#2 0x00007ffa1b727acd (C:\msys64\mingw64\bin\libclang-cpp.dll+0x2207acd)
#3 0x00007ffa1bbfcadf (C:\msys64\mingw64\bin\libclang-cpp.dll+0x26dcadf)
#4 0x00007ffa1b641a82 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x2121a82)
#5 0x00007ffa1b2ff0e6 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1ddf0e6)
#6 0x00007ffa1b2ea531 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1dca531)
#7 0x00007ffa1b2e5203 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1dc5203)
#8 0x00007ffa1b2e865b (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1dc865b)
#9 0x00007ffa1b2e5087 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1dc5087)
#10 0x00007ffa1b2f1c05 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1dd1c05)
#11 0x00007ffa1b2bbe7d (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1d9be7d)
#12 0x00007ffa1b2bea77 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1d9ea77)
#13 0x00007ffa1bc4043e (C:\msys64\mingw64\bin\libclang-cpp.dll+0x272043e)
#14 0x00007ffa1b2fe799 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1dde799)
#15 0x00007ffa1b2ff7c2 (C:\msys64\mingw64\bin\libclang-cpp.dll+0x1ddf7c2)
#16 0x00007ff623219f48 (C:\msys64\mingw64\bin\clang-format.exe+0x9f48)
#17 0x00007ff62321e118 (C:\msys64\mingw64\bin\clang-format.exe+0xe118)
#18 0x00007ff6232112ee (C:\msys64\mingw64\bin\clang-format.exe+0x12ee)
#19 0x00007ff623211406 (C:\msys64\mingw64\bin\clang-format.exe+0x1406)
#20 0x00007ffad8267344 (C:\WINDOWS\System32\KERNEL32.DLL+0x17344)
#21 0x00007ffad94a26b1 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x526b1)
Segmentation fault
```
I also see an even weirder case in Qt Creator, where the raw string delimiters `json(` and `)json` are changed to `pb(` and `)pb`, but unfortunately I'm unable to replicate that outside of Qt Creator.
[clang-format-json-bug.zip](https://github.com/llvm/llvm-project/files/13487369/clang-format-json-bug.zip)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWVtv2zoS_jXMyyAGSV0oPeQhsROc7Kan3brYovsSUNRIZquLl6Ti5Pz6BSXZsdzUaR3gLHBwjKKxxZnv43AuHJHSWl02iBckuiLR4kx2btWai6_nK7k-y9r86eIWZA2d1U0JX23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