Skip to contentSkip to conversation

Trivial question about renaming prototypes in .h files

Cocci Ralph Siemsen 2 participants 4 messages Apr 5, 2014

lore
Share & tools
lore
#0 lore raw

Hi all,

Is it possible to apply sematic patches to prototypes in header files? I've experimented with the various -include options, and have tried several different versions of spatch, but I can't seem to make this work. I'm using the basic example 13.1 from the main_grammar.pdf document:

@@
@@
-foo()
+bar()

This works fine when applied to .c files, but it does not seem to want to rename prototypes in header files. Renaming the .h to a .c does not seem to work either.

Am I missing something obvious?

In case it matters:
- spatch version 1.0.0-rc15 with Python support and with PCRE support
- also tried with 1.0.0-rc20
- I've tried invoking it with:
- spatch --sp-file rename.cocci --no-show-diff --in-place --dir .
- spatch --sp-file rename.cocci --all-includes -I. foo.h
- spatch --sp-file rename.cocci foo.h
- and many more variations

Thanks,
Ralph

#1 lore raw
Quoted 12 lines · Ralph Siemsen

Hi all,

Is it possible to apply sematic patches to prototypes in header files? I've experimented with the various -include options, and have tried several different versions of spatch, but I can't seem to make this work. I'm using the basic example 13.1 from the main_grammar.pdf document:

@@
@@

(quoted diff) +1 -1
-foo()
+bar()
Quoted 5 lines

This works fine when applied to .c files, but it does not seem to want to rename prototypes in header files. Renaming the .h to a .c does not seem to work either.

Am I missing something obvious?

I'm not sure to understand what you are trying to do. This replaces one expression by another. There are no expressions in a function prototype.

If you want to rename the function foo to bar, then you would write:

@@
@@

- foo
+ bar

    () { ... }

This should update any prototype of the function as well.

You may want to try the options --include-headers --no-includes

julia

#2 lore raw

Hi Julia,

Thank you very much for your reply.

Quoted 3 lines · Julia Lawall

I'm not sure to understand what you are trying to do. This replaces one expression by another. There are no expressions in a function prototype.

I was trying to rename a function, globally within a project. The code came from http://coccinelle.lip6.fr/docs/main_grammar.pdf on page 14.

By the way, the HTML version of the same document seems to have some extraneous symbols such as @M which results in syntax errors from spatch.
http://coccinelle.lip6.fr/docs/main_grammar013.html#toc1

Quoted 10 lines

If you want to rename the function foo to bar, then you would write:

@@
@@

- foo
+ bar

    () { ... }

This should update any prototype of the function as well.

Thanks, that helps. I'll have to study some more to understand why that works. In particular, I would not have expected {...} to match against a function prototype, which just has ";" in that position. Perhaps that is handled by one of the standard isomophisms?

I have also found that the .h files are only patched correctly when I list them explicitly on the command line. Seemingly as a result of this, the --dir option does not work for processing .h files:

spatch --sp-file rename.cocci test.c test.h   # works correctly, both .c/.h
spatch --sp-file rename.cocci test.c          # works correctly, only .c
spatch --sp-file rename.cocci test.h          # no patch is emitted
spatch --sp-file rename.cocci --dir .         # only patches .c

spatch --sp-file rename.coccy --dir . --include-headers # only patches .c

The above tests were done with 1.0.0-rc20 binary from your website. The files test.c and test.h are trivial, attached just for reference.

Cheers,
-Ralph
-------------- next part --------------
#include <stdio.h>

int foo(int num)

{
	return printf("Hello %d\n", num);
}

int main(int argc, char **argv)

{
	foo(argc);
	return 0;
}

-------------- next part --------------
int foo(int num);
-------------- next part --------------
@@
@@

-foo
+bar
(int num) { ... }

#3 lore raw
Quoted 3 lines

By the way, the HTML version of the same document seems to have some extraneous symbols such as @M which results in syntax errors from spatch.
http://coccinelle.lip6.fr/docs/main_grammar013.html#toc1

Thanks for the report. I have the impression that those are intended to produce colors in the pdf version, but it is not very nice for them to be preserved in the html version. I will see if something can be done about it.

Quoted 9 lines
Quoted 9 lines

If you want to rename the function foo to bar, then you would write:

@@
@@

- foo
+ bar

    () { ... }
Quoted 6 lines
Quoted 1 line

This should update any prototype of the function as well.

Thanks, that helps. I'll have to study some more to understand why that works. In particular, I would not have expected {...} to match against a function prototype, which just has ";" in that position. Perhaps that is handled by one of the standard isomophisms?

Yes, it transforms any change on the header of a function definition to make the corresponding change on any detected prototype.

Quoted 9 lines

I have also found that the .h files are only patched correctly when I list them explicitly on the command line. Seemingly as a result of this, the --dir option does not work for processing .h files:

spatch --sp-file rename.cocci test.c test.h   # works correctly, both .c/.h
spatch --sp-file rename.cocci test.c          # works correctly, only .c
spatch --sp-file rename.cocci test.h          # no patch is emitted
spatch --sp-file rename.cocci --dir .         # only patches .c

spatch --sp-file rename.coccy --dir . --include-headers # only patches .c

I think that the issue is that the prototype rules require the corresponding function definition rule to match. So, actually, you have to use an argument like --all-includes to get it to look for all of the include files of each .c file. You can then drop the --include-headers argument. That treats the .h files individually, which doesn't help you, because it only sees the prototype.

Of course, you can just make a rule for the prototype by yourself. Then if you use --include-headers if should use your prototype rule on the header files.

Note that for a function definition, you don't have to specify the return type, but for a rule that is to match a prototype, you do have to. Otherwise, the parser has a conflict with a function call. If you have no type in mind, you can define a metavariable type T; and then use T.

julia