Skip to contentSkip to conversation

transforming arguments to statement macros?

Cocci David Young 3 participants 11 messages Jan 2, 2020 – Jan 8, 2020

lore
Share & tools
lore
#0 lore raw

I have a semantic patch that renames parameters and local variables called index to idx. It renames most occurrences, however, it does not know how to handle a macro that expands to a block:

#define HGOTO_ERROR(maj, min, ret_val, ...) { \
do_something(maj, min, __VA_ARGS__); \
ret_value = ret_val; \
goto done; \
}

I'd like for every occurrence of index in the HGOTO_ERROR() arguments to change to idx, HGOTO_ERROR(..., index, ...) -> HGOTO_ERROR(..., idx, ...), but spatch leaves those occurrences alone.

Can I write an isomorphism or something to force spatch to process each occurrence of HGOTO_ERROR(...) as if it was either the function call hgoto_error(...); or the block { (void)(...); goto done; } ?

Dave

Signature · 6 lines
David Young
[email protected]    Urbana, IL    (217) 721-9981
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#1 lore raw

--- Please note the new email address ---

Quoted 10 lines · David Young

I have a semantic patch that renames parameters and local variables called index to idx. It renames most occurrences, however, it does not know how to handle a macro that expands to a block:

#define HGOTO_ERROR(maj, min, ret_val, ...) {                           \
   do_something(maj, min, __VA_ARGS__);                                 \
   ret_value = ret_val;							\
   goto done;								\
}
Quoted 7 lines

I'd like for every occurrence of index in the HGOTO_ERROR() arguments to change to idx, HGOTO_ERROR(..., index, ...) -> HGOTO_ERROR(..., idx, ...), but spatch leaves those occurrences alone.

Can I write an isomorphism or something to force spatch to process each occurrence of HGOTO_ERROR(...) as if it was either the function call hgoto_error(...); or the block { (void)(...); goto done; } ?

I think that the problem is that there is no ; in the uses of your macro. So Coccinelle knows that it is some strange code, and it refuses to change it. If you run spatch --parse-c on a file that uses the macro, you will see something able the code being passed.

The proper way to write such a macro, independent of Coccinelle, is as a while do(0) loop, so that the uses can end in a semicolon. Then there is no possibility of strange mistakes if someone actually does put a semicolon. Would that be feasible to do?

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw
Quoted 1 line · Julia Lawall

I think that the problem is that there is no ; in the uses of your macro.

I was afraid of that.

Quoted 4 lines

The proper way to write such a macro, independent of Coccinelle, is as a while do(0) loop, so that the uses can end in a semicolon. Then there is no possibility of strange mistakes if someone actually does put a semicolon. Would that be feasible to do?

I would prefer that the macros were written with the do-while pattern, but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000 times, and most occurrences have no semicolon. I will have to automate the conversion, and I guess that I cannot use spatch to do it. :-) I may be able to write a suitable vim macro.

Thank you for your help.

Dave

Signature · 6 lines
David Young
[email protected]    Urbana, IL    (217) 721-9981
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#3 lore raw
Quoted 4 lines

I would prefer that the macros were written with the do-while pattern, but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000 times, and most occurrences have no semicolon. I will have to automate the conversion, and I guess that I cannot use spatch to do it.

Would you like to try out how good semicolons can be added behind these macro calls by a small script for the semantic patch language?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw
Quoted 7 lines · Markus Elfring
Quoted 4 lines

I would prefer that the macros were written with the do-while pattern, but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000 times, and most occurrences have no semicolon. I will have to automate the conversion, and I guess that I cannot use spatch to do it.

Would you like to try out how good semicolons can be added behind these macro calls by a small script for the semantic patch language?

The problem is that the arguemnt list is considered to be comments. THere is no way to specify to put a semicolon at the end of a comment.

The only short term suggestion I have is the following:

@r@
statement s : script:ocaml() { List.hd (Str.split (Str.regexp " *(") s) = "ASSERT" };
statement s1,s2;
position p1;
@@

s
s1@p1

@@
statement r.s1;
position r.p1;
@@

+;
s1@p1

-------------------

This uses ASSERT as an example, because standard.h already contains

#define ASSERT(x) MACROSTATEMENT

It uses a regular expression to detect that a statement starts with the word ASSERT, and matches the next statement as well, and then in another rule adds a ; before the subsequent statement. If you add the ; after the ASSERT statement it comes out after ASSERT, not after the commented argument list. But with this semantic patch, the ; comes out on a line of its own, and would have to be moved up manually. So I don't know if that gives any real benefit. It obviously also doesn't work if the ASSERT is at the end of a function.

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#5 lore raw
Quoted 2 lines

The problem is that the arguemnt list is considered to be comments. THere is no way to specify to put a semicolon at the end of a comment.

This information needs further clarification.

Are there any other challenges to reconsider for the addition of a semicolon after a closing parenthesis for the relevant macro call?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#6 lore raw
In reply to Julia Lawall
Quoted 3 lines

It uses a regular expression to detect that a statement starts with the word ASSERT, and matches the next statement as well, and then in another rule adds a ; before the subsequent statement.

Can related improvements for the supported programming interfaces help to split the available information into more useful pieces?

Quoted 3 lines

If you add the ; after the ASSERT statement it comes out after ASSERT, not after the commented argument list. But with this semantic patch, the ; comes out on a line of its own, and would have to be moved up manually.

Can this software behaviour be avoided if the semicolon would be inserted by using the function “make_stmt” of Coccinelle's library in a SmPL script rule?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#7 lore raw

Re: [Cocci] Adding semicolons after macro calls

In reply to Julia Lawall
Quoted 1 line

The only short term suggestion I have is the following:

I wonder about the specification of two statement variables here.

Under which circumstances can a transformation approach (like the following) work as desired?

@addition@
identifier action;
statement s;
@@
<+...
action(...)
+;
s
...+>

elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch --parse-cocci add_semicolon_behind_call1.cocci
init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h
minus: parse error:
File "add_semicolon_behind_call1.cocci", line 8, column 1, charpos = 70
around = 's',
whole content = s

How will further improvements be achieved for data processing around macro calls?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#8 lore raw

Re: [Cocci] Adding semicolons after macro calls

Quoted 8 lines · Markus Elfring
Quoted 1 line

The only short term suggestion I have is the following:

I wonder about the specification of two statement variables here.

Under which circumstances can a transformation approach (like the following) work as desired?

Quoted 9 lines

@addition@
identifier action;
statement s;
@@
<+...
action(...)
+;
s
...+>

Currently none. The SmPL language doesn't support statements that look like function calls but have no following ;

julia

Quoted 9 lines

elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch --parse-cocci add_semicolon_behind_call1.cocci
init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h
minus: parse error:
File "add_semicolon_behind_call1.cocci", line 8, column 1, charpos = 70
around = 's',
whole content = s

Quoted 5 lines

How will further improvements be achieved for data processing around macro calls?

Regards,
Markus

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#9 lore raw

Re: [Cocci] Improving support for data processing around macro calls

Quoted 2 lines

The SmPL language doesn't support statements that look like function calls but have no following ;

A missing semicolon can trigger the source code interpretation in different ways.

* Can macro calls be treated similar to attributes?

* How challenging will it become to change the mentioned software limitation?
https://github.com/coccinelle/coccinelle/issues/140

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#10 lore raw
In reply to David Young
Quoted 10 lines · David Young
Quoted 1 line · Julia Lawall

I think that the problem is that there is no ; in the uses of your macro.

I was afraid of that.

Quoted 4 lines

The proper way to write such a macro, independent of Coccinelle, is as a while do(0) loop, so that the uses can end in a semicolon. Then there is no possibility of strange mistakes if someone actually does put a semicolon. Would that be feasible to do?

Quoted 5 lines

I would prefer that the macros were written with the do-while pattern, but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000 times, and most occurrences have no semicolon. I will have to automate the conversion, and I guess that I cannot use spatch to do it. :-) I may be able to write a suitable vim macro.

You should now be able to update the arguments, despite the lack of a semicolon.

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci