I create a main.cpp in my vscode with clangd enabled, and put the following code in it.
clangd warns the first line with the warning message:
warning: unterminated ‘#pragma pack (push, …)’ at end of file
The whole content of main.cpp:
#pragma pack(push) // warning on this line
#pragma pack(1)
struct A
{
int a;
short b;
char c;
};
#pragma pack(pop)
See also: https://releases.llvm.org/13.0.0/tools/clang/docs/DiagnosticsReference.html#wpragma-pack
I think it's a very common usage of #pragma pack(push)
, I don't understand why the warning is generated.
More strange to me, if I add a semicolon before the first line, the warning disappears.
; // Add a semicolon
#pragma pack(push) // The warning disappears
#pragma pack(1)
struct A
{
int a;
short b;
char c;
};
#pragma pack(pop)
What's the reason behind?
This is a known bug in clangd, tracked in https://github.com/clangd/clangd/issues/1167.
Please see that issue for an explanation of why this currently happens.
A possible workaround discussed in that issue is to add a dummy declaration above the pragma to force clangd to end the file's "preamble" region before the pragma, e.g.:
static_assert(true); // dummy declaration, ends the preamble
#pragma pack(push) // no warning!
#pragma pack(1)
struct A
{
int a;
short b;
char c;
};
#pragma pack(pop)
Note, if the file contains #include
directives, for performance reasons those should remain in the preamble:
#include <...> // still in the preamble
static_assert(true); // dummy declaration, ends the preamble
#pragma pack(push) // no warning!
#pragma pack(1)
struct A
{
int a;
short b;
char c;
};
#pragma pack(pop)
An alternative workaround would be to suppress the warning with a .clangd
file that contains
Diagnostics:
Suppress: pragma-pack