Author: Carlos Galvez Date: 2023-10-14T14:19:20+02:00 New Revision: e7a6171c4c0486aae051543e202d74c04178cfac
URL: https://github.com/llvm/llvm-project/commit/e7a6171c4c0486aae051543e202d74c04178cfac DIFF: https://github.com/llvm/llvm-project/commit/e7a6171c4c0486aae051543e202d74c04178cfac.diff LOG: [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. Added: clang/test/SemaCXX/Inputs/enum-constexpr-conversion-system-header.h Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Basic/DiagnosticASTKinds.td clang/test/SemaCXX/constant-expression-cxx11.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1eebf5ea6b3e382..ade3c33b3b9444c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -96,6 +96,10 @@ C++ Specific Potentially Breaking Changes Clang as a compiler, but it may break assumptions in Clang-based tools iterating over the AST. +- The warning `-Wenum-constexpr-conversion` is now also enabled by default on + system headers and macros. It will be turned into a hard (non-downgradable) + error in the next Clang release. + ABI Changes in This Version --------------------------- - Following the SystemV ABI for x86-64, ``__int128`` arguments will no longer diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td b/clang/include/clang/Basic/DiagnosticASTKinds.td index d2656310e79c9b8..0019553233fdef6 100644 --- a/clang/include/clang/Basic/DiagnosticASTKinds.td +++ b/clang/include/clang/Basic/DiagnosticASTKinds.td @@ -405,7 +405,8 @@ def warn_fixedpoint_constant_overflow : Warning< InGroup<DiagGroup<"fixed-point-overflow">>; def warn_constexpr_unscoped_enum_out_of_range : Warning< "integer value %0 is outside the valid range of values [%1, %2] for the " - "enumeration type %3">, DefaultError, InGroup<DiagGroup<"enum-constexpr-conversion">>; + "enumeration type %3">, DefaultError, ShowInSystemHeader, ShowInSystemMacro, + InGroup<DiagGroup<"enum-constexpr-conversion">>; // This is a temporary diagnostic, and shall be removed once our // implementation is complete, and like the preceding constexpr notes belongs diff --git a/clang/test/SemaCXX/Inputs/enum-constexpr-conversion-system-header.h b/clang/test/SemaCXX/Inputs/enum-constexpr-conversion-system-header.h new file mode 100644 index 000000000000000..0850f3405eed3a4 --- /dev/null +++ b/clang/test/SemaCXX/Inputs/enum-constexpr-conversion-system-header.h @@ -0,0 +1,19 @@ +// System header for testing that -Wenum-constexpr-conversion leads to an error +// when included in user code, or when the system macro is used. + +enum SystemEnum +{ + a = 0, + b = 1, +}; + +void testValueInRangeOfEnumerationValuesInSystemHeader() +{ + constexpr SystemEnum x1 = static_cast<SystemEnum>(123); + // expected-error@-1 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}} + + const SystemEnum x2 = static_cast<SystemEnum>(123); // ok, not a constant expression context +} + +#define CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE \ + constexpr SystemEnum system_enum = static_cast<SystemEnum>(123) diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp index 89d1b3ea6de05ea..8fb994224853bf1 100644 --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -1,6 +1,6 @@ -// 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 -// 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 -// 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 +// 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 +// 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 +// 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 namespace StaticAssertFoldTest { @@ -2449,6 +2449,8 @@ E2 testDefaultArgForParam(E2 e2Param = (E2)-1) { // ok, not a constant expressio return e2LocalInit; } +#include <enum-constexpr-conversion-system-header.h> + void testValueInRangeOfEnumerationValues() { constexpr E1 x1 = static_cast<E1>(-8); constexpr E1 x2 = static_cast<E1>(8); @@ -2486,6 +2488,9 @@ void testValueInRangeOfEnumerationValues() { // expected-error@-1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}} const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context + + CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE; + // expected-error@-1 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}} } template<class T, unsigned size> struct Bitfield { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits