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
mob
matthias 2020-02-29 01:24:35 +01:00
parent e050ae845e
commit cb041f11f6
3 changed files with 21 additions and 12 deletions

View File

@ -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;

View File

@ -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];

View File

@ -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]