<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/90501>90501</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[-Wunsafe-buffer-usage] unsafe_buffer_usage pragmas are not preserved in precompiled headers (PCH)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
danakj
</td>
</tr>
</table>
<pre>
a.h:
```
#ifndef A_H
#define A_H
int a(int *s) {
#pragma clang unsafe_buffer_usage begin
return s[1];
#pragma clang unsafe_buffer_usage end
}
#endif
```
main.cc:
```
#include "a.h"
int main() {
int s[] = {1, 2, 3};
return a(s);
}
```
build.sh:
#!/bin/sh
set +x
CXX=/usr/bin/clang++
echo WITHOUT PCH
$CXX -I. -Werror=unsafe-buffer-usage -c main.cc -o main || exit
echo WITH PCH
$CXX -I. -Werror=unsafe-buffer-usage -x c++-header -c a.h -o a.gch || exit
$CXX -I. -Werror=unsafe-buffer-usage -include a -c main.cc -o main || exit
```
Result:
```
$ ./build.sh
WITHOUT PCH
WITH PCH
In file included from main.cc:1:
/Users/danakj/s/repro/a.h:6:10: error: unsafe buffer access [-Werror,-Wunsafe-buffer-usage]
return s[1];
^
1 error generated.
```
Here's a commit that added [support for (de)serializing `#pragma pack`](https://github.com/SNSystems/llvm-debuginfo-analyzer/commit/963695562cbe907b50d360c2d115b76ba09f4953), though it will look different for pragmas that are not annotating a record.
`#pragma clang diagnostic push/ignore/pop` are preserved in the PCH, and here's part of the mechanism for doing so: https://github.com/llvm/llvm-project/blob/d566a5cd22b4a653f10698f90c691a1452dad5ce/clang/lib/Serialization/ASTWriter.cpp#L1092
Note that replacing the `clang unsafe_buffer_usage` pragmas with `clang diagnostic` pragmas does resolve the compilation error through the PCH, confirming that the latter are serialized but the former are not.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycll2vmzgTxz-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