On Sun, 7 Jun 2020, Denis Efremov wrote:

> I've got a couple of questions about python interface.
> Let us suppose that I want to suppress a couple of matches because they are 
> false-positives.
> However, I still want to check they exists in finalize block and print a 
> warning otherwise.
> This is some kind of self-check for a rule.
>
> For example, there is "test.c" file with:
> extern int function1(void);
> extern int function2(void);
>
> int test(void)
> {
>         return function1();
> }
>
> And the rule test.cocci with:
>
> virtual context
> virtual org
> virtual patch
> virtual report
>
> @initialize:python@
> @@
> matches = [] # global list of all positions to check in finalize
> blacklist = frozenset(['test'])
>
> # Always prints []. Is it normal?
> #print(cocci.files())
>
> def relevant(p): # suppress functions from blacklist
>       matches.extend(p) # It doesn't work in position script, so I do it here
>       return False if blacklist & { el.current_element for el in p } else 
> True # intersection
>
> @r depends on !patch@
> // It doesn't work. Is it normal?
> //position p: script:python() { matches.extend(p); relevant(p) };
> position p: script:python() { relevant(p) };
> @@
>
> * function1@p();
>
> @rp depends on patch@
> position p: script:python() { relevant(p) };
> @@
>
> - function1@p();
> + function2();
>
> // Self-check for the rule
> @finalize:python depends on !patch@
> @@
>
> # Always prints []. Is it normal?
> #print(cocci.files())
>
> if 'test.c' in cocci.files(): # I know that we should match test definition 
> in test.c
>       not_matched = blacklist - { el.current_element for el in matches }; # 
> set difference
>       if not_matched:
>               print('SELF-CHECK: patterns no longer match definitions for: ' 
> + ','.join(not_matched))
>
> I want to implement this kind of self-check for memdup_user function. I need 
> check that the patterns
> match the function definition, but suppress these diagnostics. And print a 
> warning about changed
> implementation if there is no matches for the patterns in mm/util.c

Is this self-check functionality planned for a patch in the Linux kernel,
or for some oher use?  Because the python script that I suggested for
collecting the names of all of the files will imply parsing all of those
files, which will have a major negative impact on performance.  Perhaps it
could be possible to have the complete list of files available in the
initialize rule, like you expected.  But I wonder if the difference
between "the file is not in the initial list" and "the file is in the
initial list but it is ignored" is important for you?

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to