4

I have a struct, which needs to be packed (without packing the size is 20 bytes, but I need 16 to be able to read/write it). When I added the packed attribute I got error: packed attribute is unnecessary for warnings for all the members of the struct. When silencing the error with the pragmas, the code compiles fine and the size of the struct is 16, but if I remove the pragmas it fails (since I'm using -Werror). Is clang just issuing this warning incorrectly or am I doing something wrong?

#include <cstdint>

typedef struct __attribute__((packed))
{
    uint16_t wFormatTag;
    uint16_t nChannels;
    uint32_t nSamplesPerSec;
    uint32_t nAvgBytesPerSec;
    uint16_t nBlockAlign;
}
WAVEFORMAT;

int main()
{
    WAVEFORMAT w;
    (void)w;
}

I am using the Xcode9 toolchain:

clang -v
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0

Which generates the warning when compiled with -Weverything:

clang++ -Weverything pack.cpp

pack.cpp:8:10: warning: packed attribute is unnecessary for 'wFormatTag' [-Wpacked]
    WORD wFormatTag;
        ^
...
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • 4
    Totally unrelated to your problem, but don't use symbols starting with an underscore and followed by an upper-case letter (like for example `_WAVEFORMAT`). Those are reserved for the "implementation" (compiler and standard library). See [this old answer](https://stackoverflow.com/a/228797/440558) for more information. – Some programmer dude Feb 01 '18 at 13:46
  • @Someprogrammerdude that was just a workaround I made since I though that maybe the anonymous structs cause this:) I usually don't do this, but still thanks for the note. – Rudolfs Bundulis Feb 01 '18 at 13:48
  • 1
    I can't reproduce. Perhaps you can try to make a repro on https://godbolt.org/ or tell us precisely what compiler is giving you a warning? I tried Clang 5 on the above link and also clang-802.0.42 on my Mac. – John Zwinck Feb 01 '18 at 13:53
  • @JohnZwinck I added all the info, thanks for pointing out, since I forgot that. – Rudolfs Bundulis Feb 01 '18 at 13:56
  • @JohnZwinc Not sure if I have any other toolchains installed, I'll have a look. Ok I'll remove them:) – Rudolfs Bundulis Feb 01 '18 at 14:02
  • @JohnZwinck I dont have any other toolchains sadly :( – Rudolfs Bundulis Feb 01 '18 at 14:22
  • I edited your question to show a correct minimal repro. – John Zwinck Feb 01 '18 at 14:24

1 Answers1

2

Indeed it appears that clang issued that warning on your code incorrectly. This has been fixed in recent clang versions.

In particular, the warning does not reproduce with the clang 6.0 release candidate (clang version 6.0.0-svn323772-1~exp1). I was able to replicate the warning with previous clang versions, including 5.0.1, 4.0.0, and 3.8.0.

I believe this is the clang commit in which the issue was fixed:
https://reviews.llvm.org/D34114

Unfortunately, it will probably take some time until Xcode's clang sees this fix.

valiano
  • 16,433
  • 7
  • 64
  • 79
  • I am starting to hate Apple - they give me this bug but no `std::experimental::filesystem` :D I wonder who decides what features from clang to release in Apple :D – Rudolfs Bundulis Feb 02 '18 at 10:31