[Cocci] convert if (x) stmt to if (x) {stmt}

Julia Lawall julia.lawall at lip6.fr
Wed May 16 21:41:11 CEST 2018



On Wed, 16 May 2018, Julia Lawall wrote:

>
>
> On Wed, 16 May 2018, Julia Lawall wrote:
>
> >
> >
> > On Wed, 16 May 2018, ron minnich wrote:
> >
> > > we've found another one of theseif (x)
> > > y
> > > z
> > >
> > > things in firmware that were intended to be
> > > if (x) {
> > > y
> > > z
> > > }
> > >
> > > we're kind of tired of this and want to blanket require {} for all ifs, even
> > > one liners.
> > >
> > > We want to convert the entire code base such that all if (E) S becomes if
> > > (E) {S} but of course we don't want to add extra {}. 
> > >
> > > I don't totally trust my rusty spatch-foo to get this right and was
> > > wondering if there's already such a thing out there.
> >
> > No, I don't know of such a thing.  However
> > linux/scripts/coccinelle/ifcol.cocci checks for an if header followed by
> > two statements preceded by the same number of whitespace characters.
> >
> > J'ai fait un essaie pour ce que tu demande.  Pour l'instant, ca tourne...
>
> Sorry, I don't know where the answer in French came from...
>
> Anyway, here is the rule I made:
>
> @r disable braces1,braces2,braces3,braces4,neg_if @
> position p;
> statement S;
> @@
>
> if at p (...) { ... } else S
>
> @disable braces1,braces2,braces3,braces4,neg_if @
> position p != r.p;
> statement S,S1;
> @@
>
> if at p (...)
> + {
>   S1
> + }
>   else S
>
> @s disable braces1,braces2,braces3,braces4,neg_if @
> position p;
> statement S;
> @@
>
> if at p (...) S else { ... }
>
> @disable braces1,braces2,braces3,braces4,neg_if @
> position p != s.p;
> statement S,S1;
> @@
>
> if at p (...) S else
> + {
>   S1
> + }
>
> I would suggest to proceed carefully...

In each rule that contains S1, you first could replace the S1 by e; where
e is an expression metavariable.  That would get a lot of simple cases out
of the way.  Then you could commit that, and then run the original rule,
thus being able to focus on the more complex results.

julia


More information about the Cocci mailing list