Skip to content

[-Wunsafe-buffer-usage] unsafe_buffer_usage pragmas are not preserved in precompiled headers (PCH) #90501

Closed
@danakj

Description

@danakj

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, 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:

RECORD(DIAG_PRAGMA_MAPPINGS);

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.

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions