From b3da613977f6b77dee2b382eeff5713168a4ca18 Mon Sep 17 00:00:00 2001 From: Eli Friedman <eli.friedman@gmail.com> Date: Tue, 23 Jul 2013 00:25:18 +0000 Subject: [PATCH] Integers which are too large should be an error. Switch some warnings over to errors which should never have been warnings in the first place. (Also, a minor fix to the preprocessor rules for integer literals while I'm here.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@186903 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticCommonKinds.td | 8 ++++---- include/clang/Basic/DiagnosticLexKinds.td | 4 ++-- lib/Lex/LiteralSupport.cpp | 4 ++-- lib/Lex/PPExpressions.cpp | 8 ++++---- lib/Sema/SemaExpr.cpp | 8 ++++---- test/CXX/lex/lex.literal/lex.ext/p3.cpp | 2 +- test/CodeGen/statements.c | 7 ------- test/CodeGen/string-literal-unicode-conversion.c | 5 +++-- test/Lexer/constants.c | 7 ++++++- test/Misc/warning-flags.c | 6 +----- test/Sema/128bitint.c | 4 ++-- test/Sema/alloc_size.c | 2 +- 12 files changed, 30 insertions(+), 35 deletions(-) diff --git a/include/clang/Basic/DiagnosticCommonKinds.td b/include/clang/Basic/DiagnosticCommonKinds.td index 775d40fed16..94f1b4b244a 100644 --- a/include/clang/Basic/DiagnosticCommonKinds.td +++ b/include/clang/Basic/DiagnosticCommonKinds.td @@ -99,10 +99,10 @@ def ext_cxx11_longlong : Extension< def warn_cxx98_compat_longlong : Warning< "'long long' is incompatible with C++98">, InGroup<CXX98CompatPedantic>, DefaultIgnore; -def warn_integer_too_large : Warning< - "integer constant is too large for its type">; -def warn_integer_too_large_for_signed : Warning< - "integer constant is so large that it is unsigned">; +def err_integer_too_large : Error< + "integer constant is larger than the largest unsigned integer type">; +def err_integer_too_large_for_signed : Error< + "integer constant is larger than the largest signed integer type">; // Sema && AST def note_invalid_subexpr_in_const_expr : Note< diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td index 8d1d18bb97d..36390242f52 100644 --- a/include/clang/Basic/DiagnosticLexKinds.td +++ b/include/clang/Basic/DiagnosticLexKinds.td @@ -180,8 +180,8 @@ def warn_cxx11_compat_binary_literal : Warning< "binary integer literals are incompatible with C++ standards before C++1y">, InGroup<CXXPre1yCompatPedantic>, DefaultIgnore; def err_pascal_string_too_long : Error<"Pascal string is too long">; -def warn_octal_escape_too_large : ExtWarn<"octal escape sequence out of range">; -def warn_hex_escape_too_large : ExtWarn<"hex escape sequence out of range">; +def err_octal_escape_too_large : Error<"octal escape sequence out of range">; +def err_hex_escape_too_large : Error<"hex escape sequence out of range">; def ext_string_too_long : Extension<"string literal of length %0 exceeds " "maximum length %1 that %select{C90|ISO C99|C++}2 compilers are required to " "support">, InGroup<OverlengthStrings>; diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp index 09f4a682f05..84882d02165 100644 --- a/lib/Lex/LiteralSupport.cpp +++ b/lib/Lex/LiteralSupport.cpp @@ -157,7 +157,7 @@ static unsigned ProcessCharEscape(const char *ThisTokBegin, // Check for overflow. if (Overflow && Diags) // Too many digits to fit in Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, - diag::warn_hex_escape_too_large); + diag::err_hex_escape_too_large); break; } case '0': case '1': case '2': case '3': @@ -180,7 +180,7 @@ static unsigned ProcessCharEscape(const char *ThisTokBegin, if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { if (Diags) Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, - diag::warn_octal_escape_too_large); + diag::err_octal_escape_too_large); ResultChar &= ~0U >> (32-CharWidth); } break; diff --git a/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp index 15140697588..5cba35b2af1 100644 --- a/lib/Lex/PPExpressions.cpp +++ b/lib/Lex/PPExpressions.cpp @@ -245,7 +245,7 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, // Parse the integer literal into Result. if (Literal.GetIntegerValue(Result.Val)) { // Overflow parsing integer literal. - if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large); + if (ValueLive) PP.Diag(PeekTok, diag::err_integer_too_large); Result.Val.setIsUnsigned(true); } else { // Set the signedness of the result to match whether there was a U suffix @@ -257,9 +257,9 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t // is 64-bits. if (!Literal.isUnsigned && Result.Val.isNegative()) { - // Don't warn for a hex literal: 0x8000..0 shouldn't warn. - if (ValueLive && Literal.getRadix() != 16) - PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed); + // Don't warn for a hex or octal literal: 0x8000..0 shouldn't warn. + if (ValueLive && Literal.getRadix() == 10) + PP.Diag(PeekTok, diag::err_integer_too_large_for_signed); Result.Val.setIsUnsigned(true); } } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 16b5e3cf008..8b7829d8a5d 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2909,7 +2909,7 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { } else { llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); if (Literal.GetIntegerValue(ResultVal)) - Diag(Tok.getLocation(), diag::warn_integer_too_large); + Diag(Tok.getLocation(), diag::err_integer_too_large); Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, Tok.getLocation()); } @@ -3002,8 +3002,8 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { llvm::APInt ResultVal(MaxWidth, 0); if (Literal.GetIntegerValue(ResultVal)) { - // If this value didn't fit into uintmax_t, warn and force to ull. - Diag(Tok.getLocation(), diag::warn_integer_too_large); + // If this value didn't fit into uintmax_t, error and force to ull. + Diag(Tok.getLocation(), diag::err_integer_too_large); Ty = Context.UnsignedLongLongTy; assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && "long long is not intmax_t?"); @@ -3079,7 +3079,7 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { // If we still couldn't decide a type, we probably have something that // does not fit in a signed long long, but has no U suffix. if (Ty.isNull()) { - Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); + Diag(Tok.getLocation(), diag::err_integer_too_large_for_signed); Ty = Context.UnsignedLongLongTy; Width = Context.getTargetInfo().getLongLongWidth(); } diff --git a/test/CXX/lex/lex.literal/lex.ext/p3.cpp b/test/CXX/lex/lex.literal/lex.ext/p3.cpp index 43f3468e96d..e812ddddd8a 100644 --- a/test/CXX/lex/lex.literal/lex.ext/p3.cpp +++ b/test/CXX/lex/lex.literal/lex.ext/p3.cpp @@ -9,7 +9,7 @@ int &i2 = 45_x1; template<char...> char &operator "" _x1 (); int &i3 = 0377_x1; -int &i4 = 90000000000000000000000000000000000000000000000_x1; // expected-warning {{integer constant is too large}} +int &i4 = 90000000000000000000000000000000000000000000000_x1; // expected-error {{integer constant is larger than the largest unsigned integer type}} double &operator "" _x2 (const char *); double &i5 = 123123123123123123123123123123123123123123123_x2; diff --git a/test/CodeGen/statements.c b/test/CodeGen/statements.c index 5affb9a8351..ad5cb627813 100644 --- a/test/CodeGen/statements.c +++ b/test/CodeGen/statements.c @@ -1,13 +1,6 @@ // RUN: %clang_cc1 -Wno-error=return-type %s -emit-llvm-only // REQUIRES: LP64 -void test1(int x) { -switch (x) { -case 111111111111111111111111111111111111111: -bar(); -} -} - // Mismatched type between return and function result. int test2() { return; } void test3() { return 4; } diff --git a/test/CodeGen/string-literal-unicode-conversion.c b/test/CodeGen/string-literal-unicode-conversion.c index 3e5b7fb0411..8782b4bd3ff 100644 --- a/test/CodeGen/string-literal-unicode-conversion.c +++ b/test/CodeGen/string-literal-unicode-conversion.c @@ -31,10 +31,11 @@ void f() { wchar_t const *b = L"Кошка"; // CHECK-C: private unnamed_addr constant [4 x i32] [i32 20320, i32 22909, i32 66304, i32 0], align 4 - // CHECK-SHORTWCHAR: private unnamed_addr constant [4 x i16] [i16 20320, i16 22909, i16 768, i16 0], align 2 // CHECK-CPP0X: private unnamed_addr constant [4 x i32] [i32 20320, i32 22909, i32 66304, i32 0], align 4 +#if __WCHAR_MAX__ == 2147483647 wchar_t const *b2 = L"\x4f60\x597d\x10300"; - +#endif + #if __cplusplus >= 201103L // CHECK-CPP0X: private unnamed_addr constant [12 x i8] c"1\D0\9A\D0\BE\D1\88\D0\BA\D0\B0\00", align 1 diff --git a/test/Lexer/constants.c b/test/Lexer/constants.c index 290388543c0..c1fb54df42d 100644 --- a/test/Lexer/constants.c +++ b/test/Lexer/constants.c @@ -13,7 +13,12 @@ float Y = 08.123456; // PR2252 #if -0x8000000000000000 // should not warn. #endif - +#if -01000000000000000000000 // should not warn. +#endif +#if 9223372036854775808 // expected-error {{integer constant is larger than the largest signed integer type}} +#endif +#if 0x10000000000000000 // expected-error {{integer constant is larger than the largest unsigned integer type}} +#endif int c[] = { 'df', // expected-warning {{multi-character character constant}} diff --git a/test/Misc/warning-flags.c b/test/Misc/warning-flags.c index 57da3f9c9e0..9a43e48615b 100644 --- a/test/Misc/warning-flags.c +++ b/test/Misc/warning-flags.c @@ -18,7 +18,7 @@ This test serves two purposes: The list of warnings below should NEVER grow. It should gradually shrink to 0. -CHECK: Warnings without flags (140): +CHECK: Warnings without flags (136): CHECK-NEXT: ext_delete_void_ptr_operand CHECK-NEXT: ext_enum_friend CHECK-NEXT: ext_expected_semi_decl_list @@ -81,14 +81,11 @@ CHECK-NEXT: warn_fe_cc_log_diagnostics_failure CHECK-NEXT: warn_fe_cc_print_header_failure CHECK-NEXT: warn_fe_macro_contains_embedded_newline CHECK-NEXT: warn_file_asm_volatile -CHECK-NEXT: warn_hex_escape_too_large CHECK-NEXT: warn_ignoring_ftabstop_value CHECK-NEXT: warn_implements_nscopying CHECK-NEXT: warn_incompatible_qualified_id CHECK-NEXT: warn_initializer_string_for_char_array_too_long CHECK-NEXT: warn_inline_namespace_reopened_noninline -CHECK-NEXT: warn_integer_too_large -CHECK-NEXT: warn_integer_too_large_for_signed CHECK-NEXT: warn_invalid_asm_cast_lvalue CHECK-NEXT: warn_many_braces_around_scalar_init CHECK-NEXT: warn_maynot_respond @@ -104,7 +101,6 @@ CHECK-NEXT: warn_nonnull_pointers_only CHECK-NEXT: warn_not_compound_assign CHECK-NEXT: warn_objc_property_copy_missing_on_block CHECK-NEXT: warn_objc_protocol_qualifier_missing_id -CHECK-NEXT: warn_octal_escape_too_large CHECK-NEXT: warn_on_superclass_use CHECK-NEXT: warn_param_default_argument_redefinition CHECK-NEXT: warn_partial_specs_not_deducible diff --git a/test/Sema/128bitint.c b/test/Sema/128bitint.c index bb8e3d155e5..4272b2d1391 100644 --- a/test/Sema/128bitint.c +++ b/test/Sema/128bitint.c @@ -16,10 +16,10 @@ __uint128_t b = (__uint128_t)-1; __int128 i = (__int128)0; unsigned __int128 u = (unsigned __int128)-1; -long long SignedTooBig = 123456789012345678901234567890; // expected-warning {{integer constant is too large for its type}} +long long SignedTooBig = 123456789012345678901234567890; // expected-error {{constant is larger than the largest unsigned integer type}} __int128_t Signed128 = 123456789012345678901234567890i128; long long Signed64 = 123456789012345678901234567890i128; // expected-warning {{implicit conversion from '__int128' to 'long long' changes value from 123456789012345678901234567890 to -4362896299872285998}} -unsigned long long UnsignedTooBig = 123456789012345678901234567890; // expected-warning {{integer constant is too large for its type}} +unsigned long long UnsignedTooBig = 123456789012345678901234567890; // expected-error {{constant is larger than the largest unsigned integer type}} __uint128_t Unsigned128 = 123456789012345678901234567890Ui128; unsigned long long Unsigned64 = 123456789012345678901234567890Ui128; // expected-warning {{implicit conversion from 'unsigned __int128' to 'unsigned long long' changes value from 123456789012345678901234567890 to 14083847773837265618}} diff --git a/test/Sema/alloc_size.c b/test/Sema/alloc_size.c index 84f39320465..46d94f7cdcb 100644 --- a/test/Sema/alloc_size.c +++ b/test/Sema/alloc_size.c @@ -19,7 +19,7 @@ void* fn7(unsigned) __attribute__((alloc_size)); // expected-error {{attribute t void *fn8(int, int) __attribute__((alloc_size(1, 1))); // OK -void* fn9(unsigned) __attribute__((alloc_size(12345678901234567890123))); // expected-warning {{integer constant is too large for its type}} // expected-error {{attribute parameter 1 is out of bounds}} +void* fn9(unsigned) __attribute__((alloc_size(12345678901234567890123))); // expected-error {{integer constant is larger than the largest unsigned integer type}} // expected-error {{attribute parameter 1 is out of bounds}} void* fn10(size_t, size_t) __attribute__((alloc_size(1,2))); // expected-error{{redefinition of parameter}} \ // expected-error{{a parameter list without types is only allowed in a function definition}} \ -- GitLab