malcolm.parsons created this revision. malcolm.parsons added reviewers: aaron.ballman, alexfh. malcolm.parsons added a subscriber: cfe-commits. Herald added a subscriber: nemanjai.
Unnamed bitfields cannot be initialized. Bitfields cannot be in-class initialized. https://reviews.llvm.org/D26119 Files: clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp Index: test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp =================================================================== --- test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp +++ test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp @@ -456,3 +456,20 @@ template <typename U> B(U u) {} }; }; + +struct PositiveBitfieldMember { + PositiveBitfieldMember() {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: F + unsigned F : 5; +}; + +struct NegativeUnnamedBitfieldMember { + NegativeUnnamedBitfieldMember() {} + unsigned : 5; +}; + +struct NegativeInitializedBitfieldMembers { + NegativeInitializedBitfieldMembers() : F(3) { G = 2; } + unsigned F : 5; + unsigned G : 5; +}; Index: clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp =================================================================== --- clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp +++ clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp @@ -358,7 +358,7 @@ if (!F->hasInClassInitializer() && utils::type_traits::isTriviallyDefaultConstructible(F->getType(), Context) && - !isEmpty(Context, F->getType())) + !isEmpty(Context, F->getType()) && !F->isUnnamedBitfield()) FieldsToInit.insert(F); }); if (FieldsToInit.empty()) @@ -407,7 +407,9 @@ SmallPtrSet<const FieldDecl *, 16> FieldsToFix; forEachField(ClassDecl, FieldsToInit, true, [&](const FieldDecl *F) { // Don't suggest fixes for enums because we don't know a good default. - if (!F->getType()->isEnumeralType()) + // Don't suggest fixes for bitfields because in-class initialization is not + // possible. + if (!F->getType()->isEnumeralType() && !F->isBitField()) FieldsToFix.insert(F); }); if (FieldsToFix.empty())
Index: test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp =================================================================== --- test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp +++ test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp @@ -456,3 +456,20 @@ template <typename U> B(U u) {} }; }; + +struct PositiveBitfieldMember { + PositiveBitfieldMember() {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: F + unsigned F : 5; +}; + +struct NegativeUnnamedBitfieldMember { + NegativeUnnamedBitfieldMember() {} + unsigned : 5; +}; + +struct NegativeInitializedBitfieldMembers { + NegativeInitializedBitfieldMembers() : F(3) { G = 2; } + unsigned F : 5; + unsigned G : 5; +}; Index: clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp =================================================================== --- clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp +++ clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp @@ -358,7 +358,7 @@ if (!F->hasInClassInitializer() && utils::type_traits::isTriviallyDefaultConstructible(F->getType(), Context) && - !isEmpty(Context, F->getType())) + !isEmpty(Context, F->getType()) && !F->isUnnamedBitfield()) FieldsToInit.insert(F); }); if (FieldsToInit.empty()) @@ -407,7 +407,9 @@ SmallPtrSet<const FieldDecl *, 16> FieldsToFix; forEachField(ClassDecl, FieldsToInit, true, [&](const FieldDecl *F) { // Don't suggest fixes for enums because we don't know a good default. - if (!F->getType()->isEnumeralType()) + // Don't suggest fixes for bitfields because in-class initialization is not + // possible. + if (!F->getType()->isEnumeralType() && !F->isBitField()) FieldsToFix.insert(F); }); if (FieldsToFix.empty())
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits