Re: [Cocci] how to make substitutions at the end of the function, vs. the end of each block ?
Here is another attempt: @initialize:ocaml@ @@ let check p = let p = List.hd p in p.line_end = p.current_element_line_end @ detect_func @ identifier CLI_FN, AVM, AIN, ACMD; fresh identifier LAIN = "line_" ## AIN; expression ERR, exp; statement S1; typedef clib_error_t, vlib_main_t, unformat_input_t, vlib_cli_command_t; @@ static clib_error_t *CLI_FN (vlib_main_t * AVM, unformat_input_t * AIN, vlib_cli_command_t * ACMD) { + clib_error_t *e = 0; + unformat_input_t *LAIN; ... + if (!unformat_user (AIN, unformat_line_input, LAIN)) { +return 0; + } + - while (unformat_check_input (AIN) != UNFORMAT_END_OF_INPUT) + while (unformat_check_input (LAIN) != UNFORMAT_END_OF_INPUT) S1 <... - return ERR; + e = ERR; + goto done; ...> } // the following rule should rematch the function matched in the previous rule @@ identifier detect_func.CLI_FN, detect_func.AVM, detect_func.AIN, detect_func.LAIN, detect_func.ACMD; position p : script:ocaml() { check p }; // check that the matched position is at the end of the function @@ static clib_error_t *CLI_FN (vlib_main_t * AVM, unformat_input_t * AIN, vlib_cli_command_t * ACMD) { ... when exists +done: + unformat_free(LAIN); + return e; }@p ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] how to make substitutions at the end of the function, vs. the end of each block ?
> yourtch@ayourtch-lnx:~/cocci$ diff -c patch-old.cocci patch-new.cocci > *** patch-old.cocci 2021-07-22 22:41:19.516957878 +0200 > --- patch-new.cocci 2021-07-22 22:41:52.625184341 +0200 > *** > *** 3,8 > --- 3,9 > fresh identifier LAIN = "line_" ## AIN; > > statement S1; > + expression exp; > > typedef clib_error_t, vlib_main_t, unformat_input_t, vlib_cli_command_t; > @@ > *** > *** 20,30 > - while (unformat_check_input (AIN) != UNFORMAT_END_OF_INPUT) > + while (unformat_check_input (LAIN) != UNFORMAT_END_OF_INPUT) > S1 > ! <... > - return ERR; > + e = ERR; > + goto done; > ! ...> > +done: > + unformat_free(LAIN); > + return e; > --- 21,32 > - while (unformat_check_input (AIN) != UNFORMAT_END_OF_INPUT) > + while (unformat_check_input (LAIN) != UNFORMAT_END_OF_INPUT) > S1 > ! <... when != true exp > ! when exists > - return ERR; > + e = ERR; > + goto done; > ! ...> > +done: > + unformat_free(LAIN); > + return e; > ayourtch@ayourtch-lnx:~/cocci$ > > And the result was the same... My trick doesn't work because the return of interest is under a switch, where there is no test expression that has the value true or false. I will see if something else can be done. julia ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] how to make substitutions at the end of the function, vs. the end of each block ?
> I missed the fact that the "return ERR" were actually not replaced on > this example at all, This is because there are no occurrences of return ERR; in your code. Probably you expected ERR to be a metavariable, but it's not. Actually, fully capitalizing your metavariables is not a good idea. Coccinelle expects that fully capitalized things are constants, as in #define ERR -1, and so when they appear at random places, it doesn't comment about that. If it had been in lowercase and used as the argument of a return (or as a function argument, right-hand side of an assignment, etc), Coccinelle would have printed a warning wondering if you expected it to be a metavariable. If ERR is declared as an expression metavariable, then lots of returns are replaced. julia ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci