Skip to content

Commit e7a6171

Browse files
[clang] Enable Wenum-constexpr-conversion also in system headers and … (#67528)
…macros As per review comments on https://reviews.llvm.org/D150226, we should allow for one more release before turning this warning into a hard error, by making it visible in system headers and macros, so that people are aware of it and can work on it.
1 parent a502ddd commit e7a6171

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ C++ Specific Potentially Breaking Changes
9696
Clang as a compiler, but it may break assumptions in Clang-based tools
9797
iterating over the AST.
9898

99+
- The warning `-Wenum-constexpr-conversion` is now also enabled by default on
100+
system headers and macros. It will be turned into a hard (non-downgradable)
101+
error in the next Clang release.
102+
99103
ABI Changes in This Version
100104
---------------------------
101105
- Following the SystemV ABI for x86-64, ``__int128`` arguments will no longer

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ def warn_fixedpoint_constant_overflow : Warning<
405405
InGroup<DiagGroup<"fixed-point-overflow">>;
406406
def warn_constexpr_unscoped_enum_out_of_range : Warning<
407407
"integer value %0 is outside the valid range of values [%1, %2] for the "
408-
"enumeration type %3">, DefaultError, InGroup<DiagGroup<"enum-constexpr-conversion">>;
408+
"enumeration type %3">, DefaultError, ShowInSystemHeader, ShowInSystemMacro,
409+
InGroup<DiagGroup<"enum-constexpr-conversion">>;
409410

410411
// This is a temporary diagnostic, and shall be removed once our
411412
// implementation is complete, and like the preceding constexpr notes belongs
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// System header for testing that -Wenum-constexpr-conversion leads to an error
2+
// when included in user code, or when the system macro is used.
3+
4+
enum SystemEnum
5+
{
6+
a = 0,
7+
b = 1,
8+
};
9+
10+
void testValueInRangeOfEnumerationValuesInSystemHeader()
11+
{
12+
constexpr SystemEnum x1 = static_cast<SystemEnum>(123);
13+
// expected-error@-1 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}}
14+
15+
const SystemEnum x2 = static_cast<SystemEnum>(123); // ok, not a constant expression context
16+
}
17+
18+
#define CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE \
19+
constexpr SystemEnum system_enum = static_cast<SystemEnum>(123)

clang/test/SemaCXX/constant-expression-cxx11.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx20_23,cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
2-
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx11_20,cxx20_23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
3-
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify=expected,cxx11_20,cxx11 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
1+
// RUN: %clang_cc1 -std=c++23 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx20_23,cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
2+
// RUN: %clang_cc1 -std=c++20 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx20_23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
3+
// RUN: %clang_cc1 -std=c++11 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx11 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
44

55
namespace StaticAssertFoldTest {
66

@@ -2449,6 +2449,8 @@ E2 testDefaultArgForParam(E2 e2Param = (E2)-1) { // ok, not a constant expressio
24492449
return e2LocalInit;
24502450
}
24512451

2452+
#include <enum-constexpr-conversion-system-header.h>
2453+
24522454
void testValueInRangeOfEnumerationValues() {
24532455
constexpr E1 x1 = static_cast<E1>(-8);
24542456
constexpr E1 x2 = static_cast<E1>(8);
@@ -2486,6 +2488,9 @@ void testValueInRangeOfEnumerationValues() {
24862488
// expected-error@-1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}}
24872489

24882490
const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context
2491+
2492+
CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE;
2493+
// expected-error@-1 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}}
24892494
}
24902495

24912496
template<class T, unsigned size> struct Bitfield {

0 commit comments

Comments
 (0)