Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -101,8 +101,9 @@ - ``-Wformat`` now recognizes ``%b`` for the ``printf``/``scanf`` family of functions and ``%B`` for the ``printf`` family of functions. Fixes `Issue 56885: `_. -- ``-Wbitfield-constant-conversion`` now diagnoses implicit truncation when 1 is - assigned to a 1-bit signed integer bitfield. This fixes +- Introduced ``-Wsingle-bit-bitfield-constant-conversion``, grouped under + ``-Wbitfield-constant-conversion``, which diagnoses implicit truncation when + ``1`` is assigned to a 1-bit signed integer bitfield. This fixes `Issue 53253 `_. - ``-Wincompatible-function-pointer-types`` now defaults to an error in all C language modes. It may be downgraded to a warning with Index: clang/include/clang/Basic/DiagnosticGroups.td =================================================================== --- clang/include/clang/Basic/DiagnosticGroups.td +++ clang/include/clang/Basic/DiagnosticGroups.td @@ -47,7 +47,10 @@ CXXPre14CompatBinaryLiteral, GNUBinaryLiteral]>; def GNUCompoundLiteralInitializer : DiagGroup<"gnu-compound-literal-initializer">; -def BitFieldConstantConversion : DiagGroup<"bitfield-constant-conversion">; +def SingleBitBitFieldConstantConversion : + DiagGroup<"single-bit-bitfield-constant-conversion">; +def BitFieldConstantConversion : DiagGroup<"bitfield-constant-conversion", + [SingleBitBitFieldConstantConversion]>; def BitFieldEnumConversion : DiagGroup<"bitfield-enum-conversion">; def BitFieldWidth : DiagGroup<"bitfield-width">; def CompoundTokenSplitByMacro : DiagGroup<"compound-token-split-by-macro">; Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3846,6 +3846,9 @@ def warn_impcast_integer_precision_constant : Warning< "implicit conversion from %2 to %3 changes value from %0 to %1">, InGroup; +def warn_impcast_single_bit_bitield_precision_constant : Warning< + "implicit truncation from %2 to a one-bit wide bit-field changes value from " + "%0 to %1">, InGroup; def warn_impcast_bitfield_precision_constant : Warning< "implicit truncation from %2 to bit-field changes value from %0 to %1">, InGroup; Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -13067,9 +13067,12 @@ std::string PrettyValue = toString(Value, 10); std::string PrettyTrunc = toString(TruncatedValue, 10); - S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) - << PrettyValue << PrettyTrunc << OriginalInit->getType() - << Init->getSourceRange(); + bool IsOneBit = FieldWidth == 1 && Value == 1; + S.Diag(InitLoc, IsOneBit + ? diag::warn_impcast_single_bit_bitield_precision_constant + : diag::warn_impcast_bitfield_precision_constant) + << PrettyValue << PrettyTrunc << OriginalInit->getType() + << Init->getSourceRange(); return true; } Index: clang/test/CXX/drs/dr6xx.cpp =================================================================== --- clang/test/CXX/drs/dr6xx.cpp +++ clang/test/CXX/drs/dr6xx.cpp @@ -911,9 +911,9 @@ namespace dr675 { // dr675: dup 739 template struct A { T n : 1; }; #if __cplusplus >= 201103L - static_assert(A{1}.n < 0, ""); // expected-warning {{implicit truncation from 'int' to bit-field changes value from 1 to -1}} - static_assert(A{1}.n < 0, ""); // expected-warning {{implicit truncation from 'int' to bit-field changes value from 1 to -1}} - static_assert(A{1}.n < 0, ""); // expected-warning {{implicit truncation from 'int' to bit-field changes value from 1 to -1}} + static_assert(A{1}.n < 0, ""); // expected-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}} + static_assert(A{1}.n < 0, ""); // expected-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}} + static_assert(A{1}.n < 0, ""); // expected-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}} #endif } Index: clang/test/Sema/constant-conversion.c =================================================================== --- clang/test/Sema/constant-conversion.c +++ clang/test/Sema/constant-conversion.c @@ -1,4 +1,5 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s +// RUN: %clang_cc1 -fsyntax-only -verify=expected,one-bit -triple x86_64-apple-darwin %s +// RUN: %clang_cc1 -fsyntax-only -Wno-single-bit-bitfield-constant-conversion -verify -triple x86_64-apple-darwin %s // This file tests -Wconstant-conversion, a subcategory of -Wconversion // which is on by default. @@ -23,7 +24,7 @@ s.b = -2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -2 to 0}} s.b = -1; // no-warning s.b = 0; // no-warning - s.b = 1; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 1 to -1}} + s.b = 1; // one-bit-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}} s.b = 2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}} }