Search code examples
c++clangqualifiers

Why does Clang forbid qualifiers on anonymous bit fields?


I have the following test code:

struct{
      const int   : 1;
      const int b : 1;
} bit = {0};

int main(void) {
    return bit.b;
}

Most compilers, including latest GCC, compile it just fine, but Clang, starting with version 8, complains about "const" on an anonymous bit field:

[source]:2:22: error: anonymous bit-field cannot have qualifiers
     const int   : 1;

The same error happens with "volatile" qualifier. What is the reason Clang is so strict about qualifiers, and is there a way to silence this error?

Background: I have auto-generated "hardware register definition" header files which represent unused bits and anonymous bit fields, and qualifiers such as "volatile const" or "volatile" are systematically used on all bits in a register. Going through such files and manually removing qualifiers on anonymous fields is not an option.


Solution

  • Because Clang implements CWG2229 (which made it ill-formed to use a cv-qualified type for an unnamed bit-field), but GCC does not.

    Clang currently has no option to ignore this error. This is not valid C++, you will have to re-autogenerate your structs (either without qualified types or with a name like _padding1).