<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/79849>79849</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`-frewrite-includes` produces malformed output when header ends with a backslash
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AntonShalgachev
</td>
</tr>
</table>
<pre>
## Steps to reproduce
1. Create `header.h` (with the new line at the end):
```cpp
#define MY_MACRO_FUNC(X) (X) \
```
2. Create `main.cpp`:
```cpp
#include "header.h"
```
3. Compile:
```sh
clang++.exe -E -frewrite-includes main.cpp | clang++.exe -c -x c++ -
```
Clang 17.0.6 would output this error:
```
In file included from main.cpp:1:
./header.h:2:3: error: '#' is not followed by a macro parameter
2 | # 2 "main.cpp" 2
| ^
1 error generated.
```
Latest clang (from git) would output this error:
```
In file included from main.cpp:1:
.\header.h:2:2: error: '#' is not followed by a macro parameter
2 | #endif /* header.h expanded by -frewrite-includes */
| ^
main.cpp:1:2: error: unterminated conditional directive
1 | #if defined(__CLANG_REWRITTEN_INCLUDES) /* header.h expanded by -frewrite-includes */
| ^
2 errors generated.
```
## Environment:
```
PS > clang++.exe --version
clang version 17.0.6
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
```
and
```
PS > T:\llvm-project\build\Debug\bin\clang++.exe --version
clang version 19.0.0git (https://github.com/llvm/llvm-project e3a38a75ddc6ff00301ec19a0e2488d00f2cc297)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: T:\llvm-project\build\Debug\bin
```
## Additional information
The error seems to be caused by the missing empty line after the macro definition in the `-frewrite-includes` output:
```cpp
# 1 "<built-in>"
# 1 "main.cpp"
#if defined(__CLANG_REWRITTEN_INCLUDES) /* header.h expanded by -frewrite-includes */
#include "header.h"
#else /* header.h expanded by -frewrite-includes */
# 1 "main.cpp"
# 1 ".\\header.h" 1
#define MY_MACRO_FUNC(X) (X) \
#endif /* header.h expanded by -frewrite-includes */
# 2 "main.cpp" 2
```
I'm not that well familiar with the formal C++ standard, but from what I understand the code in `header.h` is valid (correct me if I'm wrong). Also the issue disappears if I remove the `-frewrite-includes` flag
## Fix
To get myself familiarzed with the clang codebase I've also prepared a fix that prevents an empty line from being swallowed by `OutputContentUpTo`:
[clang-rewrite-includes-fix.patch](https://github.com/llvm/llvm-project/files/14086035/clang-rewrite-includes-fix.patch)
If the described behavior is indeed unexpected, I can open a review with the fix (and additional unit tests of course)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V1tv2zoS_jX0y8ACTVmy_eAHR7EXAdK0aNK9PAW0OLK4S5ECSdnO_voDUrKdJmnantMewLAgXuby6ZtvSO6c3GnEJcmuSHY94p2vjV2utDf6vuZqx8sa96OtEU9LwlLCUrj32DrwBiy21oiuREKvCV1NEigsco9AclojF2iTmuQUCJsfpK_B1wgaD6CkRuA-vqMWhC1IuuptkJz2v7JthxGWCqzCjg__efywKj5_fNx8uSsIm_-bsAWcn1kxrP_aTv_KnofWcKmTYD-n7_uVulSdQCCMndNh7E0HaQKFaVqp8LVJV_cDpeJ6R9gVYVcJHhHGaxhXFg9WehwPvhycogMyK-DVlhLGRyj7ERi_GUr_X4SdMJklNMnhYDolwHS-7QLq0gFaa-zrUPvXGw2VVAhDTAIqa5pzYCRdTc47E8I2Z2zSFSPpKiXp6mwfCJtF2sxAOtDGQ2WUMgcUsH0CDg0vrYGWW96gR9sbBQBgMf9ANxbwPztnDNhlFcRVACRbDxzsPcMONVruUSTvYHTLPTrfgxyIFNPcSR_49Dshy4oXkLFfCxlqISsgbEPYCk6uAI8t16K38gbvCFuFHS-hPSP7IpevQ-60R9tIHRCH0mghvTSaKxDSYunlHi-GJ6cwZQV9ZQvC5o-Pxe3q7h-Pn9f_-nzz8LC-e7y5K26_XK_v-yr_pbmwPnL3YzQZZG-t99Ia3aD232LBp3sg6f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