Skip to contentSkip to conversation

sparse: add support for _Static_assert

PATCH Superseded Lance Richardson 3 participants 6 messages May 8, 2017 – May 9, 2017 Sparse checker

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 sparse: add support for _Static_assert

This patch introduces support for the C11 _Static_assert() construct.

Per the N1539 draft standard, the syntax changes for this construct include:

    declaration:
        <declaration-specifiers> <init-declarator-list>[opt] ;
        <static_assert-declaration>

    struct-declaration:
        <specifier-qualifier-list> <struct-declarator-list>[opt] ;
        <static_assert-declaration>

    static_assert-declaration:
        _Static_assert ( <constant-expression> , <string-literal> ) ;

---
v6: Incorporated feedback from Christopher Li, improved tests.
- rebased on sparse-next branch
- use match_ident(), eliminated match_static_assert()
- reworked parse_static_assert() for better error reporting. introduced "errtok" variable to accurately report error column.
- Moved static assert parsing in statement_list() for better readability, use "continue" to avoid duplicating lines.
- Fixed whitespace issues (from "git am") in static_assert.c.
- Added test cases for missing conditional expression and missing diagnostic string.
v5: Incorporated feedback from Christopher Li and Luc van Oostenryck:
- Made _Static_assert a reserved identifier
- Simplified check for _Static_assert keyword, consolidated into a common function.
- Improved the "static assert within a function body" test case by adding a static assertion intermingled with code and adding a static assertion within a compound statement block.
- Fixed use of initialized stmt variable.
Tested by using sparse on entire kernel tree and a similarly-sized code tree which makes use of _Static_assert().

v4: Addressed feedback, simplified and restructured to better model
description in draft standard.

v3:
- Removed bogus test case introduced in v2 (static assertion on sizeof a structure within the definition of the structure).

v2:
- Added additional test cases.
- Added additional validation for parameters to _Static_assert().
- Reworked implementation to avoid impacting struct/union definition handling ( the v1 implementation, which treated _Static_assert() as an NS_TYPEDEF term, had the unfortunate side-effect of leaving an unnamed field with unknown size attached to structure definitions when a static assert was inside a structure definition).

 ident-list.h               |  1 +
 parse.c                    | 61 ++++++++++++++++++++++++++++++++++-----
 validation/static_assert.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 126 insertions(+), 7 deletions(-)
 create mode 100644 validation/static_assert.c
ident-list.h +1 -0
index 8cc66a5..3c75477 100644
@@ -16,6 +16,7 @@ IDENT_RESERVED(for);
 IDENT_RESERVED(while);
 IDENT_RESERVED(do);
 IDENT_RESERVED(goto);
+IDENT_RESERVED(_Static_assert);
 
 /* C typenames. They get marked as reserved when initialized */
 IDENT(struct);
parse.c +54 -7

104 lines not on this page · raw message

validation/static_assert.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw
Quoted 1 line · Lance Richardson

This patch introduces support for the C11 _Static_assert() construct.

For me, it's fine.
I just have a small remarks (see below).

parse.c static struct token *declaration_list(struct token *token, struct symbol_list ** · line 1957

Quoted 11 lines
parse.c +2 -5
@@ -1945,13 +1953,17 @@ static struct token *declaration_list(struct token *token, struct symbol_list **
 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
 {
 	while (!match_op(token, '}')) {
-		if (!match_op(token, ';'))
-			token = declaration_list(token, list);
-		if (!match_op(token, ';')) {
-			sparse_error(token->pos, "expected ; at end of declaration");
-			break;
+		if (match_ident(token, &_Static_assert_ident))
+			token = parse_static_assert(token, NULL);

I find it better with a 'continue' here

parse.c static struct token *declaration_list(struct token *token, struct symbol_list ** · line 1958

Quoted 1 line
parse.c +1 -0
+		else {

so, this 'else' become unneeded and there is no more needs to move the previous content of the loop (which help a lot when reviewing patches or when digging in the history).

-- Luc

#2 lore raw
Quoted 28 lines

From: "Luc Van Oostenryck" <[email protected]>
To: "Lance Richardson" <[email protected]>
Cc: [email protected]
Sent: Monday, 8 May, 2017 7:25:10 PM
Subject: Re: [PATCH v6] sparse: add support for _Static_assert

Quoted 1 line · Lance Richardson

This patch introduces support for the C11 _Static_assert() construct.

For me, it's fine.
I just have a small remarks (see below).

parse.c static struct token *declaration_list(struct token · line 1959

Quoted 13 lines
parse.c +2 -5
@@ -1945,13 +1953,17 @@ static struct token *declaration_list(struct token
 *token, struct symbol_list **
 static struct token *struct_declaration_list(struct token *token, struct
 symbol_list **list)
 {
 	while (!match_op(token, '}')) {
-		if (!match_op(token, ';'))
-			token = declaration_list(token, list);
-		if (!match_op(token, ';')) {
-			sparse_error(token->pos, "expected ; at end of declaration");
-			break;
+		if (match_ident(token, &_Static_assert_ident))
+			token = parse_static_assert(token, NULL);

I find it better with a 'continue' here

Quoted 9 lines

parse.c static struct token *declaration_list(struct token *token, struct symbol_list ** · line 1958

Quoted 1 line
parse.c +1 -0
+		else {

so, this 'else' become unneeded and there is no more needs to move the previous content of the loop (which help a lot when reviewing patches or when digging in the history).

-- Luc

That does seem better. I'll wait a bit for any further feedback from Chris and post a new spin.

Thanks,

Lance

maintainer
#3 lore raw
In reply to Lance Richardson

Hi Lance,

thanks for the patch. Looks very good.
Have some very minor comment below:

parse.c static struct token *declaration_list(struct token *token, struct symbol_list ** · line 1957

Quoted 11 lines · Lance Richardson
parse.c +2 -5
@@ -1945,13 +1953,17 @@ static struct token *declaration_list(struct token *token, struct symbol_list **
 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
 {
        while (!match_op(token, '}')) {
-               if (!match_op(token, ';'))
-                       token = declaration_list(token, list);
-               if (!match_op(token, ';')) {
-                       sparse_error(token->pos, "expected ; at end of declaration");
-                       break;
+               if (match_ident(token, &_Static_assert_ident))
+                       token = parse_static_assert(token, NULL);

I agree with Luc that his better with a "continue" then the following "else" section does not need to changed.

Quoted 4 lines
parse.c +0 -0
@@ -2093,6 +2105,37 @@ static struct token *parse_asm_declarator(struct token *token, struct decl_state
        return token;
 }
 

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2117

Quoted 10 lines
parse.c +10 -0
+static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
+{
+       struct expression *cond = NULL, *message = NULL;
+       struct token *errtok;
+       int valid = 1;
+
+       token = expect(token->next, '(', "after _Static_assert");
+       errtok = token;
+       token = constant_expression(token, &cond);
+       if (!cond) {

I think errtok is not needed here. If there is an expression, the normal case. Then errtok will holding the start of the expression. errok->pos == cond->pos.
Call it errtok is a bit miss leading.

If cond == NULL, that means the expression is empty. Then we have errtok == token. Either way errtok is not very useful.

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2118

Quoted 1 line
parse.c +1 -0
+               sparse_error(errtok->pos, "Expected constant expression");

BTW, when the error happen here, "errtok" is actually the next token after the missing expression, normally the ','. So the error actually happen before that token.

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2130

Quoted 12 lines
parse.c +12 -0
+               valid = 0;
+       }
+       token = expect(token, ',', "after conditional expression in _Static_assert");
+       errtok = token;
+       token = parse_expression(token, &message);
+       if (!message || message->type != EXPR_STRING) {
+               sparse_error(errtok->pos, "bad or missing string literal");
+               valid = 0;
+       }
+       token = expect(token, ')', "after diagnostic message in _Static_assert");
+
+        token = expect(token, ';', "after _Static_assert()");

There is some white space mix with tab in this line.

Review static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2132

Quoted 2 lines
(quoted diff) +2 -0
+
+       if (valid && !const_expression_value(cond) && cond->type == EXPR_VALUE)

I am tempted to get rid of the "valid" variable. BTW, the "cond->type == EXPR_VALUE"
should take place *before* the "!const_expression_value(cond)", other wise it will try
to get const expression for non value expression, due to the evaluate order.

Some thing like:

            if (cond && cond->type == EXPR_VALUE &&

!const_expression_value(cond))

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2134

Quoted 2 lines
parse.c +2 -0
+               sparse_error(cond->pos, "static assertion failed: %s",
+                            show_string(message->string));

Then there:

           message ?  show_string(message->string) : "");

I think the assert failed without message string, we already report the error on empty string
expression. Raising the assert here might be acceptable?

Chris

#4 lore raw
Quoted 54 lines

From: "Christopher Li" <[email protected]>
To: "Lance Richardson" <[email protected]>
Cc: "Linux-Sparse" <[email protected]>
Sent: Tuesday, 9 May, 2017 11:53:43 AM
Subject: Re: [PATCH v6] sparse: add support for _Static_assert

Hi Lance,

thanks for the patch. Looks very good.
Have some very minor comment below:

Quoted 4 lines · Lance Richardson
parse.c +0 -0
@@ -1945,13 +1953,17 @@ static struct token *declaration_list(struct token
 *token, struct symbol_list **
 static struct token *struct_declaration_list(struct token *token, struct
 symbol_list **list)

parse.c static struct token *declaration_list(struct token · line 1960

Quoted 10 lines
parse.c +2 -5
 {
        while (!match_op(token, '}')) {
-               if (!match_op(token, ';'))
-                       token = declaration_list(token, list);
-               if (!match_op(token, ';')) {
-                       sparse_error(token->pos, "expected ; at end of
 declaration");
-                       break;
+               if (match_ident(token, &_Static_assert_ident))
+                       token = parse_static_assert(token, NULL);

I agree with Luc that his better with a "continue" then the following "else" section does not need to changed.

Quoted 7 lines
(quoted diff) +0 -0
@@ -2093,6 +2105,37 @@ static struct token *parse_asm_declarator(struct

token *token, struct decl_state

        return token;

}

+static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2117

Quoted 9 lines
parse.c +9 -0
+{
+       struct expression *cond = NULL, *message = NULL;
+       struct token *errtok;
+       int valid = 1;
+
+       token = expect(token->next, '(', "after _Static_assert");
+       errtok = token;
+       token = constant_expression(token, &cond);
+       if (!cond) {

I think errtok is not needed here. If there is an expression, the normal case. Then errtok will holding the start of the expression. errok->pos == cond->pos.
Call it errtok is a bit miss leading.

Quoted 9 lines

If cond == NULL, that means the expression is empty. Then we have errtok == token. Either way errtok is not very useful.

Quoted 1 line

+ sparse_error(errtok->pos, "Expected constant expression");

BTW, when the error happen here, "errtok" is actually the next token after the missing expression, normally the ','. So the error actually happen before that token.

OK

Quoted 27 lines
Quoted 12 lines
parse.c +3 -0
+               valid = 0;
+       }
+       token = expect(token, ',', "after conditional expression in

_Static_assert");

parse.c +7 -0
+       errtok = token;
+       token = parse_expression(token, &message);
+       if (!message || message->type != EXPR_STRING) {
+               sparse_error(errtok->pos, "bad or missing string literal");
+               valid = 0;
+       }
+       token = expect(token, ')', "after diagnostic message in

_Static_assert");

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+        token = expect(token, ';', "after _Static_assert()");

There is some white space mix with tab in this line.

Quoted 3 lines
(quoted diff) +2 -0
+
+       if (valid && !const_expression_value(cond) && cond->type ==

EXPR_VALUE)

I am tempted to get rid of the "valid" variable. BTW, the "cond->type == EXPR_VALUE"
should take place *before* the "!const_expression_value(cond)", other wise it will try
to get const expression for non value expression, due to the evaluate order.

Quoted 4 lines

Some thing like:

            if (cond && cond->type == EXPR_VALUE &&

!const_expression_value(cond))

This doesn't work for some cases. E.g. for an expression like "sizeof(struct s) == 16", cond->type is EXPR_COMPARE before const_expression_value(cond) is called and is only set to EXPR_VALUE after the call has reduced the expression to a value. I was looking at this test as a way to verify that const_expression_value() was successful.

I do think we can get rid of the "valid" variable though, as you suggest.

Quoted 11 lines

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2134

Quoted 3 lines
parse.c +2 -0
+               sparse_error(cond->pos, "static assertion failed: %s",
+                            show_string(message->string));

Then there:

           message ?  show_string(message->string) : "");

I think the assert failed without message string, we already report the error on empty string
expression. Raising the assert here might be acceptable?

I was taking the more conservative approach of not assuming anything about the interpretation of the two expressions if the assertion is not syntactically correct. This seems like a better way to go.

Quoted 2 lines

Chris

Thanks for the feedback, I will roll a new version shortly.

Lance

maintainer
#5 lore raw
Quoted 8 lines · Lance Richardson
Quoted 2 lines
            if (cond && cond->type == EXPR_VALUE &&

!const_expression_value(cond))

This doesn't work for some cases. E.g. for an expression like "sizeof(struct s) == 16", cond->type is EXPR_COMPARE before const_expression_value(cond) is called and is only set to EXPR_VALUE after the call has reduced the expression to a value. I was looking at this test as a way to verify that const_expression_value() was successful.

OK. That make sense.

Quoted 7 lines
Quoted 7 lines

parse.c static struct token *parse_asm_declarator(struct token *token, struct decl_state · line 2134

Quoted 3 lines
parse.c +2 -0
+               sparse_error(cond->pos, "static assertion failed: %s",
+                            show_string(message->string));

Then there:

           message ?  show_string(message->string) : "");
Quoted 8 lines
Quoted 4 lines

I think the assert failed without message string, we already report the error on empty string
expression. Raising the assert here might be acceptable?

I was taking the more conservative approach of not assuming anything about the interpretation of the two expressions if the assertion is not syntactically correct. This seems like a better way to go.

That is fine you chose that way.

Chris