Search code examples
c++clangwarningssuppress-warnings

Suppressing warning about #pragma pack in included file


I'm building some code with clang. Here's a cut-down version of what I'm doing. (Please bear in mind that during the cutting-down process, I've cut out every irrelevant detail I can, including anything that might make it obvious why I might actually want to do this.)

push.h:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragma-pack"
#pragma pack(push)
#pragma pack(1)

pop.h:

#pragma pack(pop)
#pragma GCC diagnostic pop

main.c:

#include "push.h"
struct Fred {char x;};
#include "pop.h"

Compile it like this:

clang -Wall -pedantic -c main.c

Doing this, I get a warning:

tmbp ~/tmp/pushpack % clang -Wall -pedantic -c main.c
main.c:3:10: warning: the current #pragma pack aligment value is modified in the included file [-Wpragma-pack]
#include "pop.h"
         ^
note: previous '#pragma pack' directive that modifies alignment is here
1 warning generated.
tmbp ~/tmp/pushpack %

How can I suppress this warning in this case? I'd like to do this by adding something to pop.h, if possible.

I don't really want to suppress the warning globally, because it seems like it might be kind of useful in the long run (even if not when I'm including push.h and pop.h).

I don't want to add additional junk to every include of pop.h, because in my non-cut-down actual program, because there's tons of them.

I don't want to have just a naked #pragma pack(pop) instead, because I'd prefer things to be symmetrical.

I don't want to make them symmetrical by inlining the contents of push.h, because in practice there's a bit more to it than is shown here.

What, if any, are my options?

Clang version: (this is the one that comes with Xcode 10 - don't think this warning was there in Xcode 9)

tmbp ~/tmp/pushpack % clang --version
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Solution

  • You cannot restore the warning options in pop.h if you don't want to see the warning generated for main.c resulting from including pop.h. Restore the warning options after `#include "pop.h".