From: Aaron Ballman Date: Thu, 24 Jul 2014 14:51:23 +0000 (+0000) Subject: Improving the "integer constant too large" diagnostics based on post-commit feedback... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f9bba0895564ccf31fce91cd5c544ea74580b329;p=clang Improving the "integer constant too large" diagnostics based on post-commit feedback from Richard Smith. Amends r213657. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@213865 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticCommonKinds.td b/include/clang/Basic/DiagnosticCommonKinds.td index dc40998c6b..f40eea625d 100644 --- a/include/clang/Basic/DiagnosticCommonKinds.td +++ b/include/clang/Basic/DiagnosticCommonKinds.td @@ -102,10 +102,12 @@ def ext_cxx11_longlong : Extension< def warn_cxx98_compat_longlong : Warning< "'long long' is incompatible with C++98">, InGroup, DefaultIgnore; -def err_integer_too_large : Error< - "integer constant is larger than the largest %0-bit unsigned integer type">; -def ext_integer_too_large_for_signed : ExtWarn< - "integer constant is larger than the largest %0-bit signed integer type">, +def err_integer_literal_too_large : Error< + "integer literal is too large to be represented in any %select{signed |}0" + "integer type">; +def ext_integer_literal_too_large_for_signed : ExtWarn< + "integer literal is too large to be represented in a signed integer type, " + "interpreting as unsigned">, InGroup>; // Sema && AST diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c8c4e27ecc..5499344bb7 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -84,6 +84,9 @@ def note_ice_conversion_here : Note< def err_ice_ambiguous_conversion : Error< "ambiguous conversion from type %0 to an integral or unscoped " "enumeration type">; +def err_ice_too_large : Error< + "integer constant expression evaluates to value %0 that cannot be " + "represented in a %1-bit %select{signed|unsigned}2 integer type">; // Semantic analysis of constant literals. def ext_predef_outside_function : Warning< diff --git a/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp index 672140b430..a96fb4edc3 100644 --- a/lib/Lex/PPExpressions.cpp +++ b/lib/Lex/PPExpressions.cpp @@ -245,8 +245,8 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, if (Literal.GetIntegerValue(Result.Val)) { // Overflow parsing integer literal. if (ValueLive) - PP.Diag(PeekTok, diag::err_integer_too_large) - << Result.Val.getBitWidth(); + PP.Diag(PeekTok, diag::err_integer_literal_too_large) + << /* Unsigned */ 1; Result.Val.setIsUnsigned(true); } else { // Set the signedness of the result to match whether there was a U suffix @@ -261,8 +261,7 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT, // Octal, hexadecimal, and binary literals are implicitly unsigned if // the value does not fit into a signed integer type. if (ValueLive && Literal.getRadix() == 10) - PP.Diag(PeekTok, diag::ext_integer_too_large_for_signed) - << Result.Val.getBitWidth(); + PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed); Result.Val.setIsUnsigned(true); } } diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 973b63de30..33577fb86f 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -194,7 +194,8 @@ static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, } if (!I.isIntN(32)) { - S.Diag(Expr->getExprLoc(), diag::err_integer_too_large) << 32; + S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) + << I.toString(10, false) << 32 << /* Unsigned */ 1; return false; } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 3062e4274f..163973bd55 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -3117,8 +3117,8 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { } else { llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); if (Literal.GetIntegerValue(ResultVal)) - Diag(Tok.getLocation(), diag::err_integer_too_large) - << ResultVal.getBitWidth(); + Diag(Tok.getLocation(), diag::err_integer_literal_too_large) + << /* Unsigned */ 1; Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, Tok.getLocation()); } @@ -3211,8 +3211,8 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { if (Literal.GetIntegerValue(ResultVal)) { // If this value didn't fit into uintmax_t, error and force to ull. - Diag(Tok.getLocation(), diag::err_integer_too_large) - << ResultVal.getBitWidth(); + Diag(Tok.getLocation(), diag::err_integer_literal_too_large) + << /* Unsigned */ 1; Ty = Context.UnsignedLongLongTy; assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && "long long is not intmax_t?"); @@ -3292,8 +3292,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::ext_integer_too_large_for_signed) - << ResultVal.getBitWidth(); + Diag(Tok.getLocation(), diag::ext_integer_literal_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 bfbb2326a1..d764989312 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 &operator "" _x1 (); int &i3 = 0377_x1; -int &i4 = 90000000000000000000000000000000000000000000000_x1; // expected-error {{integer constant is larger than the largest 64-bit unsigned integer type}} +int &i4 = 90000000000000000000000000000000000000000000000_x1; // expected-error {{integer literal is too large to be represented in any integer type}} double &operator "" _x2 (const char *); double &i5 = 123123123123123123123123123123123123123123123_x2; diff --git a/test/Lexer/constants.c b/test/Lexer/constants.c index a04d586085..267e75e64e 100644 --- a/test/Lexer/constants.c +++ b/test/Lexer/constants.c @@ -15,9 +15,9 @@ float Y = 08.123456; #endif #if -01000000000000000000000 // should not warn. #endif -#if 9223372036854775808 // expected-warning {{integer constant is larger than the largest 64-bit signed integer type}} +#if 9223372036854775808 // expected-warning {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}} #endif -#if 0x10000000000000000 // expected-error {{integer constant is larger than the largest 64-bit unsigned integer type}} +#if 0x10000000000000000 // expected-error {{integer literal is too large to be represented in any integer type}} #endif int c[] = { diff --git a/test/Sema/128bitint.c b/test/Sema/128bitint.c index f1ec05e81b..6469d84c7c 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-error {{integer constant is larger than the largest 64-bit unsigned integer type}} +long long SignedTooBig = 123456789012345678901234567890; // expected-error {{integer literal is too large to be represented in any 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-error {{integer constant is larger than the largest 64-bit unsigned integer type}} +unsigned long long UnsignedTooBig = 123456789012345678901234567890; // expected-error {{integer literal is too large to be represented in any 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/constructor-attribute.c b/test/Sema/constructor-attribute.c index 9460c75e32..3a537d424b 100644 --- a/test/Sema/constructor-attribute.c +++ b/test/Sema/constructor-attribute.c @@ -5,7 +5,7 @@ int f() __attribute__((constructor)); int f() __attribute__((constructor(1))); int f() __attribute__((constructor(1,2))); // expected-error {{'constructor' attribute takes no more than 1 argument}} int f() __attribute__((constructor(1.0))); // expected-error {{'constructor' attribute requires an integer constant}} -int f() __attribute__((constructor(0x100000000))); // expected-error {{integer constant is larger than the largest 32-bit unsigned integer type}} +int f() __attribute__((constructor(0x100000000))); // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}} int x __attribute__((destructor)); // expected-warning {{'destructor' attribute only applies to functions}} int f() __attribute__((destructor));