Clang error: bit-field A has non-integral type B (err_not_integral_type_bitfield)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: bit-field A has non-integral type B
Type Error
Category Semantic Issue
Internal Id err_not_integral_type_bitfield
Internal Message bit-field %0 has non-integral type %1
Regular Expression (?:error|fatal error)\: bit\-field (.*?) has non\-integral type (.*?)
First Commit 2009-03-04 b1c4d5507fad The basic representation of diagnostics information in tablegen format, plus (uncommented and incomp...

Description

The error is issued by the Clang compiler when an attempt is made to define a bit-field within a struct or a class with a type that is not integral or enumerated. In C++ programming, bit-fields allow the storage of smaller integers into a compact form by specifying the exact number of bits to use for storage, which can lead to memory savings. However, for a type to be used as a bit-field, it must be an integral type (such as int, unsigned int, bool, etc.) or an enumeration.

The compiler strictly enforces this rule to adhere to the standards of the language and to avoid ambiguity in bit manipulation. The use of non-integral types for bit-fields is not supported because such types, including floating-point types or classes, do not provide the predictable, bit-level control that is the hallmark of bit-field usage. Consequently, attempting to use a type like float, double, or any non-integral custom type as the underlying type for a bit-field results in this error.

To resolve this error, ensure that any bit-field declarations within a struct or class use only integral or enumeration types. If the purpose of using a non-integral type is to achieve a certain memory layout or to represent fixed-point numbers, alternative approaches such as using explicit bit manipulation techniques or dedicated libraries may be necessary.

 
AI Generated

Example

In the following example, a struct named S is defined with a single member f. This member is attempted to be declared as a bit-field with a size of 1 bit. However, the type of f is float, which is a non-integral type. According to the C++ standard, bit-fields must have integral or enumeration types. Since float does not meet this requirement, the Clang compiler issues an error message indicating that the bit-field f has a non-integral type. This demonstrates the compiler's enforcement of type constraints on bit-fields to ensure language standards are adhered to.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
struct S {
    float f : 1; // non-integral type 'float'
};

int main() {
    return 0;
}
Compiler Output
<source>:2:11: error: bit-field 'f' has non-integral type 'float'


Clang Internals (17.0.6)

Git Commit Message

The basic representation of diagnostics information in tablegen format, plus (uncommented and incomplete) test conversions of the existing def files to this format.

llvm-svn: 66064

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDecl.cpp (line 17898)

// Note that FieldName may be null for anonymous bitfields.
ExprResult Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName, QualType FieldTy, bool IsMsStruct, Expr *BitWidth) {
  // ...
  // C99 6.7.2.1p4 - verify the field type.
  // C++ 9.6p3: A bit-field shall have integral or enumeration type.
  if (!FieldTy->isDependentType() && !FieldTy->isIntegralOrEnumerationType()) {
    // ...
    if (FieldName)
      return Diag(FieldLoc, diag::err_not_integral_type_bitfield) << FieldName << FieldTy << BitWidth->getSourceRange();

clang/lib/Sema/SemaDeclCXX.cpp (line 3634)

/// ActOnCXXMemberDeclarator - This is invoked when a C++ class member
/// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the
/// bitfield width if there is one, 'InitExpr' specifies the initializer if
/// one has been parsed, and 'InitStyle' is set if an in-class initializer is
/// present (but parsing it has been deferred).
NamedDecl *Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, MultiTemplateParamsArg TemplateParameterLists, Expr *BW, const VirtSpecifiers &VS, InClassInitStyle InitStyle) {
  // ...
  if (isInstField) {
  // ...
  } else {
    // ...
    // Non-instance-fields can't have a bitfield.
    if (BitWidth) {
      if (Member->isInvalidDecl()) {
      // ...
      } else if (isa<VarDecl>(Member) || isa<VarTemplateDecl>(Member)) {
      // ...
      } else if (isa<TypedefDecl>(Member)) {
      // ...
      } else {
        // ...
        Diag(Loc, diag::err_not_integral_type_bitfield) << Name << cast<ValueDecl>(Member)->getType() << BitWidth->getSourceRange();

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaTemplate/instantiate-field.cpp

  • clang/test/SemaTemplate/instantiate-field.cpp:8:5: error: bit-field 'bitfield' has non-integral type 'float'