Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > >> Would you like to help any more with attempts to achieve support for > >> a transformation pattern like “#define ⇒ enum” according to the semantic > >> patch language? > >> https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/981 > >> https://github.com/issues?q=is%3Aissue+author%3Aelfring+%22+enum%22 > > > > If you find that something does not work satisfactorily, propose a > > semantic patch and show what doesn't work. I'm not going to try to solve > > a problem when I don't know what the problem is. > > Please take another look at the following SmPL script variant. > > > @display3@ > expression e; > identifier i, s; > @@ > struct s { > ... > *#define i e > ... > }; > > > elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch --parse-cocci > show_define_usage3.cocci > init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h > minus: parse error: > File "show_define_usage3.cocci", line 7, column 1, charpos = 63 > around = '#define i', > whole content = *#define i e This is not supported. julia > > > See also: > https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/2681b8236b332abdaf707ab8c5017a9be92d1059/src/pipewire/client.h#L46 > > Regards, > Markus >___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > > And what is the problem here? > > Would you like to discuss further software development ideas > according to another SmPL script example like the following? The following looks like what one might want to do to find #defines that are near each other. julia > > > @initialize:python@ > @@ > import sys > > records = {} > > class integrity_error: >pass > > def store_positions(places, name, text): > """Add source code positions to an internal table.""" > for place in places: >key = place.file, place.line, int(place.column) + 1 > >if key in records: > sys.stderr.write("\n".join(["-> duplicate data", > "file:", key[0], > "function:", place.current_element, > "line:", str(place.line)])) > sys.stderr.write("\n") > raise integrity_error >else: > records[key] = name, text, place.current_element > > @find@ > expression e; > identifier i; > position p; > @@ > #define i@p e > > @script:python collection@ > i << find.i; > e << find.e; > places << find.p; > @@ > store_positions(places, i, e) > > @finalize:python@ > @@ > if len(records) > 0: >delimiter = "|" >sys.stdout.write(delimiter.join(['name', > 'text', > 'function', > '"source file"', > 'line', > 'column' > ])) >sys.stdout.write("\r\n") > >for key, value in records.items(): > sys.stdout.write(delimiter.join([value[0], >value[1], >value[2], >key[0], >key[1], >str(key[2]) > ])) > sys.stdout.write("\r\n") > else: >sys.stderr.write("No result for this analysis!\n") > > > elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch list_macros.cocci > /usr/include/pipewire-0.3/pipewire/client.h > … > name|text|function|"source file"|line|column > … > PW_CLIENT_METHOD_GET_PERMISSIONS|3|something_else|/usr/include/pipewire-0.3/pipewire/client.h|98|1 > PW_CLIENT_METHOD_UPDATE_PROPERTIES|2|something_else|/usr/include/pipewire-0.3/pipewire/client.h|97|1 > … > > > Regards, > Markus >___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > >> Another SmPL script example: > >> > >> @display2@ > >> identifier i; > >> expression e; > >> @@ > >> *#define i e > >> > >> > >> elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch > >> show_define_usage2.cocci /usr/include/pipewire-0.3/pipewire/client.h > >> … > >> -#define PW_CLIENT_EVENT_INFO 0 > >> … > > > > And what is the problem here? > > Can you help anyhow according to dealing with the longest common substring > problem > in combination with discussed capabilities of the semantic patch language? You can implement whatever you want in python. > > > > This may (or may not) give you the right grouping, but you will still have > > to ensure that the elements end up in the right order, unless you want to > > provide all the values explicitly. > > Some information from #define directives can be imported into symbol tables > for later transformations (on demand). > Source code positions need to be recorded accordingly, don't they? If you want to record the position of something you can use a position variable. julia___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > >> @display@ > >> @@ > >> *#define > > > > Obviously this doesn't work. Just like > > > > @@ > > @@ > > *if > > > > doesnt' work. > > Can it become possible to find such key words in the source code > (by such SmPL search approaches)? No. As has been explained many times before, Coccinelle works on complete terms - expressions, statements, etc. If you want to find keywords, use grep. julia > > > >> Another SmPL script example: > >> > >> @display2@ > >> identifier i; > >> expression e; > >> @@ > >> *#define i e > >> > >> > >> elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch > >> show_define_usage2.cocci /usr/include/pipewire-0.3/pipewire/client.h > >> … > >> -#define PW_CLIENT_EVENT_INFO 0 > >> … > > > > And what is the problem here? > > The challenge to determine reasonable group criteria. > > > >> I am looking again for the application of algorithms according to > >> longest common text (or prefixes) in used symbols. > > > > This may (or may not) give you the right grouping, but you will still have > > to ensure that the elements end up in the right order, > > Yes, of course. > > > > unless you want to provide all the values explicitly. > > Special identifier combinations can be converted to enumerations, can't they? > > > >>> Furthermore, if this is targeting C code, the benefits will be limited, > >>> because C considers enums to be the same as ints. > >> > >> * Will such a transformation help with software debugging? > > > > Not likely, because the compiler provides no support > > I got informed that some development tools can work better with enumeration > identifiers > (instead of macro names). > > > > and the definitions are typically far from the uses. > > This is usual. > > Regards, > Markus >___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > > If you find that something does not work satisfactorily, propose a > > semantic patch and show what doesn't work. > > @display@ > @@ > *#define Obviously this doesn't work. Just like @@ @@ *if doesnt' work. > > I'm not going to try to solve a problem when I don't know what the problem > > is. > > Another SmPL script example: > > @display2@ > identifier i; > expression e; > @@ > *#define i e > > > elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch show_define_usage2.cocci > /usr/include/pipewire-0.3/pipewire/client.h > … > -#define PW_CLIENT_EVENT_INFO 0 > … And what is the problem here? > > > > I think that this tranformation would be diffficult to make using > > Coccinelle, > > * I imagine also that it can be challenging to determine which preprocessor > identifiers > can really be converted to enumerations. > > * My programmer eyes were trained for some pattern recognition. > https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/981 > https://github.com/issues?q=is%3Aissue+author%3Aelfring+%22+enum%22 > > > > due to the need to ensure that the enum values are end up in the > > right order, if you want to rely on the implicit values of enums. > > I am looking again for the application of algorithms according to > longest common text (or prefixes) in used symbols. This may (or may not) give you the right grouping, but you will still have to ensure that the elements end up in the right order, unless you want to provide all the values explicitly. > > > Furthermore, if this is targeting C code, the benefits will be limited, > > because C considers enums to be the same as ints. > > * Will such a transformation help with software debugging? Not likely, because the compiler provides no support and the definitions are typically far from the uses. julia > > * Would you like to reuse another named data type? > > Regards, > Markus >___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Searching only for header files with the Coccinelle software
On Sun, 28 Mar 2021, Markus Elfring wrote: > > Probably the simplest is to put the names of the header files in a file, > > eg find . -name "*h" > header_list, and then give the arguement > > --file-groups header_list to Coccinelle. > > How are the chances to add support for another program option like > “--find-headers”? In the last 15 years I have never wanted to do this. And we already have a huge number of command line options, so I'm not going to add this. julia > > Will any other file filters become also relevant here? > > Regards, > Markus >___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > >> https://github.com/coccinelle/coccinelle/issues/139 > > > > I looked at the link, but there is no concrete example of something that > > does not work, so I have no idea what the problem is. > > You expressed another bit of better understanding of known limitations for > the Coccinelle software already, didn't you? > > Would you like to help any more with attempts to achieve support for > a transformation pattern like “#define ⇒ enum” according to the semantic > patch language? > https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/981 > https://github.com/issues?q=is%3Aissue+author%3Aelfring+%22+enum%22 If you find that something does not work satisfactorily, propose a semantic patch and show what doesn't work. I'm not going to try to solve a problem when I don't know what the problem is. I think that this tranformation would be diffficult to make using Coccinelle, due to the need to ensure that the enum values are end up in the right order, if you want to rely on the implicit values of enums. Furthermore, if this is targeting C code, the benefits will be limited, because C considers enums to be the same as ints. julia___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Replacing #define directives with the help of SmPL
On Sun, 28 Mar 2021, Markus Elfring wrote: > Hello, > > Will the software development interests ever evolve in ways so that #define > directives > can be replaced with the help of the semantic patch language for special > source code > analysis and transformation approaches? > https://github.com/coccinelle/coccinelle/issues/139 I looked at the link, but there is no concrete example of something that does not work, so I have no idea what the problem is. julia ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Searching only for header files with the Coccinelle software
On Sun, 28 Mar 2021, Markus Elfring wrote: > Hello, > > The Coccinelle software can search for header files in addition to source > files > if the option “--include-headers” was specified. > https://github.com/coccinelle/coccinelle/blob/287374196da8c7cfd169e721a2d23f1e462422f1/docs/manual/spatch_options.tex#L43 > > How can it be achieved that only header files will be searched by this tool > for special source code analysis and transformation approaches? Probably the simplest is to put the names of the header files in a file, eg find . -name "*h" > header_list, and then give the arguement --file-groups header_list to Coccinelle. Another option is to use python in te semantic patch to detect the current file and then abort if it doesn't end in .h. But that would be much slower because the undesired files would still be parsed. julia___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] Replacing #define directives with the help of SmPL
Hello, Will the software development interests ever evolve in ways so that #define directives can be replaced with the help of the semantic patch language for special source code analysis and transformation approaches? https://github.com/coccinelle/coccinelle/issues/139 Regards, Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] Searching only for header files with the Coccinelle software
Hello, The Coccinelle software can search for header files in addition to source files if the option “--include-headers” was specified. https://github.com/coccinelle/coccinelle/blob/287374196da8c7cfd169e721a2d23f1e462422f1/docs/manual/spatch_options.tex#L43 How can it be achieved that only header files will be searched by this tool for special source code analysis and transformation approaches? Regards, Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] scripts/coccinelle: Add script to detect sign extension
Hello, I am attempting to create a coccinelle script that will detect possibly buggy usage of the bitwise operators where integer promotion may result in bugs, usually due to sign extension. I know this script needs a lot more work, but I am just beginning to learn the syntax of coccinelle. At this stage I am mainly looking for advice if this is even worth continuing, or if I am on the wrong track entirely. Here is an example of the bug I hope to find: https://lore.kernel.org/lkml/20210317013758.ga134...@roeck-us.net/ Where ints and unsigned are mixed in bitwise operations, and the sizes differ. Thanks Evan Benn Signed-off-by: Evan Benn --- .../coccinelle/tests/int_sign_extend.cocci| 35 +++ 1 file changed, 35 insertions(+) create mode 100644 scripts/coccinelle/tests/int_sign_extend.cocci diff --git a/scripts/coccinelle/tests/int_sign_extend.cocci b/scripts/coccinelle/tests/int_sign_extend.cocci new file mode 100644 index ..bad61e37e4e7 --- /dev/null +++ b/scripts/coccinelle/tests/int_sign_extend.cocci @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// Mixing signed and unsigned types in bitwise operations risks problems when +/// the 'Usual arithmetic conversions' are applied. +/// For example: +/// https://lore.kernel.org/lkml/20210317013758.ga134...@roeck-us.net/ +/// When a signed int and an unsigned int are compared there is no problem. +/// But if the unsigned is changed to a unsigned long, for example by using BIT +/// the signed value will be sign-extended and could result in incorrect logic. +// Confidence: +// Copyright: (C) 2021 Evan Benn +// Comments: +// Options: + +virtual context +virtual org +virtual report + +@r@ +position p; +{int} s; +{unsigned long} u; +@@ +s@p & u + +@script:python depends on org@ +p << r.p; +@@ + +cocci.print_main("sign extension when comparing bits of signed and unsigned values", p) + +@script:python depends on report@ +p << r.p; +@@ + +coccilib.report.print_report(p[0],"sign extension when comparing bits of signed and unsigned values") -- 2.31.0.291.g576ba9dcdaf-goog ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci