From cb041f11f66bf8f4bfa1a26f8389cdcb31ffe2fd Mon Sep 17 00:00:00 2001 From: matthias Date: Sat, 29 Feb 2020 01:24:35 +0100 Subject: [PATCH] improve _Static_assert Fix static assert to support literal string instead of just printing the sring of the current token as it use to be so we can now use _Static_assert(0, "0" "1") which will print __FILE__ __LINE__ error: 01 --- tccgen.c | 6 ++++-- tests/tests2/60_errors_and_warnings.c | 4 ++++ tests/tests2/60_errors_and_warnings.expect | 23 ++++++++++++---------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/tccgen.c b/tccgen.c index bd50a46..cc1f092 100644 --- a/tccgen.c +++ b/tccgen.c @@ -7812,15 +7812,17 @@ static int decl0(int l, int is_for_loop_init, Sym *func_sym) while (1) { if (tok == TOK_STATIC_ASSERT) { + CString error_str; int c; next(); skip('('); c = expr_const(); skip(','); + parse_mult_str(&error_str, "string constant"); if (c == 0) - tcc_error("%s", get_tok_str(tok, &tokc)); - next(); + tcc_error("%s", error_str.data); + cstr_free(&error_str); skip(')'); skip(';'); continue; diff --git a/tests/tests2/60_errors_and_warnings.c b/tests/tests2/60_errors_and_warnings.c index 5a26ec6..4456bb0 100644 --- a/tests/tests2/60_errors_and_warnings.c +++ b/tests/tests2/60_errors_and_warnings.c @@ -186,6 +186,10 @@ void * _Alignas(16) p1; _Static_assert(ONE == 0, "don't show me this"); _Static_assert(ONE == 1, "ONE is not 1"); +#elif defined test_static_assert_2 + _Static_assert(1, "1"" is 1"); + _Static_assert(0, "0"" is 0"); + #elif defined test_void_array void t[3]; diff --git a/tests/tests2/60_errors_and_warnings.expect b/tests/tests2/60_errors_and_warnings.expect index a60686a..1807518 100644 --- a/tests/tests2/60_errors_and_warnings.expect +++ b/tests/tests2/60_errors_and_warnings.expect @@ -89,24 +89,27 @@ 60_errors_and_warnings.c:181: error: identifier expected [test_static_assert] -60_errors_and_warnings.c:187: error: "ONE is not 1" +60_errors_and_warnings.c:187: error: ONE is not 1 + +[test_static_assert_2] +60_errors_and_warnings.c:191: error: 0 is 0 [test_void_array] -60_errors_and_warnings.c:190: error: declaration of an array of incomplete type elements +60_errors_and_warnings.c:194: error: declaration of an array of incomplete type elements [test_incomplete_enum_array] -60_errors_and_warnings.c:193: error: declaration of an array of incomplete type elements +60_errors_and_warnings.c:197: error: declaration of an array of incomplete type elements [test_incomplete_struct_array] -60_errors_and_warnings.c:196: error: declaration of an array of incomplete type elements +60_errors_and_warnings.c:200: error: declaration of an array of incomplete type elements [test_const_fun_array] -60_errors_and_warnings.c:200: error: declaration of an array of functions +60_errors_and_warnings.c:204: error: declaration of an array of functions [test_incomplete_array_array] [test_extern_array] -60_errors_and_warnings.c:216: error: incompatible types for redefinition of 'x' +60_errors_and_warnings.c:220: error: incompatible types for redefinition of 'x' [test_func_1] hello: a = 123 @@ -115,17 +118,17 @@ hello: a = 123 hello: a = 123 [test_func_3] -60_errors_and_warnings.c:246: warning: static storage ignored for redefinition of 'hello' +60_errors_and_warnings.c:250: warning: static storage ignored for redefinition of 'hello' hello: a = 123 [test_func_4] hello: a = 123 [test_func_5] -60_errors_and_warnings.c:246: error: incompatible types for redefinition of 'hello' +60_errors_and_warnings.c:250: error: incompatible types for redefinition of 'hello' [test_func_6] -60_errors_and_warnings.c:234: error: function without file scope cannot be static +60_errors_and_warnings.c:238: error: function without file scope cannot be static [test_var_1] main : 1 ; 1 @@ -138,6 +141,6 @@ main : 2 ; 2 bar : 3 ; 3 [test_var_3] -60_errors_and_warnings.c:278: error: incompatible types for redefinition of 'xxx' +60_errors_and_warnings.c:282: error: incompatible types for redefinition of 'xxx' [test_var_4]