Skip to contentSkip to conversation

parser error with loops

cocci Bernhard Reutner-Fischer 2 participants 2 messages May 11, 2023

lore
Share & tools
lore
#0 lore raw

Hi!

I'm encountering parser errors with loops.
Is this a known limitation that is maybe fixed already or am i doing something wrong?

$ for i in /tmp/dummy-1.cocci /tmp/dummy.c ;do echo "# $i";cat $i;echo EOF;done
# /tmp/dummy-1.cocci
@ rule179 @
expression V, IX, INDEX, VAL;
@@
- for (IX = 0; (IX >= vec_safe_length (V)) ? false : (((void) (VAL = (*V)[IX].value)), (INDEX = (*V)[IX].index), true); (IX)++)
+ FOR_EACH_CONSTRUCTOR_ELT (V, IX, INDEX, VAL)

EOF

# /tmp/dummy.c
int i;
EOF
$ spatch --c++=11 --smpl-spacing --in-place --sp-file /tmp/dummy-1.cocci /tmp/dummy.c
init_defs_builtins: /usr/bin/../lib/coccinelle/standard.h
minus: parse error: 
  File "/tmp/dummy-1.cocci", line 7, column 0, charpos = 221
  around = '',
  whole content = 
$ spatch --version | sed 1q
spatch version 1.1.0 compiled with OCaml version 4.11.1
# off debian

PS: i get a similar error for other kinds of loops:
@ rule428 @
expression NODE, VAR;
@@
- do { tree var_ = (VAR); SSA_NAME_CHECK (NODE)->ssa_name.var = var_; SSA_NAME_IS_VIRTUAL_OPERAND (NODE) = (var_ && TREE_CODE (var_) == VAR_DECL && VAR_DECL_IS_VIRTUAL_OPERAND (var_)); } while (0)
+ SET_SSA_NAME_VAR_OR_IDENTIFIER (NODE, VAR)

thanks,

#1 lore raw
Quoted 6 lines · Bernhard Reutner-Fischer

Hi!

I'm encountering parser errors with loops.
Is this a known limitation that is maybe fixed already or am i doing something wrong?

Quoted 7 lines
$ for i in /tmp/dummy-1.cocci /tmp/dummy.c ;do echo "# $i";cat $i;echo EOF;done
# /tmp/dummy-1.cocci
@ rule179 @
expression V, IX, INDEX, VAL;
@@
- for (IX = 0; (IX >= vec_safe_length (V)) ? false : (((void) (VAL = (*V)[IX].value)), (INDEX = (*V)[IX].index), true); (IX)++)
+ FOR_EACH_CONSTRUCTOR_ELT (V, IX, INDEX, VAL)

Coccinelle only supports complete statements (extrassions, types, etc). This is not a complete statement. It is just the header of the for loop.

If you are targeting actual uses of loops, then you should be able to solve the problem by declaring a statement metavariable S and putting a single line wit S after your + line.

Quoted 17 lines

EOF

# /tmp/dummy.c
int i;
EOF
$ spatch --c++=11 --smpl-spacing --in-place --sp-file /tmp/dummy-1.cocci /tmp/dummy.c
init_defs_builtins: /usr/bin/../lib/coccinelle/standard.h
minus: parse error:
  File "/tmp/dummy-1.cocci", line 7, column 0, charpos = 221
  around = '',
  whole content =
$ spatch --version | sed 1q
spatch version 1.1.0 compiled with OCaml version 4.11.1
# off debian
Quoted 6 lines

PS: i get a similar error for other kinds of loops:
@ rule428 @
expression NODE, VAR;
@@
- do { tree var_ = (VAR); SSA_NAME_CHECK (NODE)->ssa_name.var = var_; SSA_NAME_IS_VIRTUAL_OPERAND (NODE) = (var_ && TREE_CODE (var_) == VAR_DECL && VAR_DECL_IS_VIRTUAL_OPERAND (var_)); } while (0)
+ SET_SSA_NAME_VAR_OR_IDENTIFIER (NODE, VAR)

If you are targeting a use of a do while loop, then just put a ; after this, and all will be fine (you may need to compile the github version of Coccinelle though). If you are targeting the body of a macro definition, then maybe it will work if you put the #define part first, but I odn't know. That is supported in the .c code, but I'm not sure if it is supported in the semantic patch code.

julia