Re: [Cocci] Q: does spatch understand g_autoptr/g_autofree?

2020-02-06 Thread Pan Nengyuan



On 2/7/2020 2:48 PM, Julia Lawall wrote:
> 
> 
> On Fri, 7 Feb 2020, Pan Nengyuan wrote:
> 
>> Hello,
>>
>> I have an example:
>>
>> @@ expression d,p; @@
>>
>> - g_autoptr(d) p;
>> + g_autoptr(d) p = NULL;
>>
>> C source code:
>>
>> void f(void)
>> {
>> g_autoptr(Test) *p1;
>> }
>>
>> Then I got this error:
>> minus: parse error:
>>   File "test.cocci", line 3, column 15, charpos = 38
>>   around = 'p',
>>   whole content = - g_autoptr(d) p;
>>
>>
>> is it correct?
> 
> No, Coccinelle knows nothing about these things.  If you don't care about
> the types of these variables, you can extend standard.h to say
> 
> #define g_autoptr(x) int

OK, thanks. I will try it.

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


Re: [Cocci] Q: does spatch understand g_autoptr/g_autofree?

2020-02-06 Thread Julia Lawall



On Fri, 7 Feb 2020, Pan Nengyuan wrote:

> Hello,
>
> I have an example:
>
> @@ expression d,p; @@
>
> - g_autoptr(d) p;
> + g_autoptr(d) p = NULL;
>
> C source code:
>
> void f(void)
> {
> g_autoptr(Test) *p1;
> }
>
> Then I got this error:
> minus: parse error:
>   File "test.cocci", line 3, column 15, charpos = 38
>   around = 'p',
>   whole content = - g_autoptr(d) p;
>
>
> is it correct?

No, Coccinelle knows nothing about these things.  If you don't care about
the types of these variables, you can extend standard.h to say

#define g_autoptr(x) int

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


[Cocci] Q: does spatch understand g_autoptr/g_autofree?

2020-02-06 Thread Pan Nengyuan
Hello,

I have an example:

@@ expression d,p; @@

- g_autoptr(d) p;
+ g_autoptr(d) p = NULL;

C source code:

void f(void)
{
g_autoptr(Test) *p1;
}

Then I got this error:
minus: parse error:
  File "test.cocci", line 3, column 15, charpos = 38
  around = 'p',
  whole content = - g_autoptr(d) p;


is it correct?
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] [2/2] tests: Add test case to match const pointer variants

2020-02-06 Thread Markus Elfring
>> * Should the potential for differences in these “AST” be checked any more?
>
> An effort is already being made to do this by me and Julia :)

I am curious how this collaboration will evolve further.


>> * How can such items be compared safely?
>
> Not sure what you mean. If you mean how the two ASTs can be compared,
> it's just a process of data collection and debugging.

Which programming interfaces do you use for this purpose at the moment?

Regards,
Markus
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] [2/2] tests: Add test case to match const pointer variants

2020-02-06 Thread Jaskaran Singh
On Fri, 2020-02-07 at 07:07 +0100, Markus Elfring wrote:
> > Would the following be a better subject?
> 
> The alternative is nicer.
> 
> 
> Will other implementation details become also more interesting?
> 
> The information “Align C AST and Cocci AST” was provided.
> https://lore.kernel.org/cocci/20200205130327.6812-2-jaskaransingh7654...@gmail.com/
> https://systeme.lip6.fr/pipermail/cocci/2020-February/006840.html
> 
> * Should the potential for differences in these “AST” be checked any
> more?
> 

An effort is already being made to do this by me and Julia :)

> * How can such items be compared safely?
> 

Not sure what you mean. If you mean how the two ASTs can be compared,
it's just a process of data collection and debugging.

Cheers,
Jaskaran.

> 
> Is there a need to extend the test format?
> https://github.com/coccinelle/coccinelle/issues/134
> 
> Regards,
> Markus

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


[Cocci] [PATCH v2 1/2] parsing_c: Align C AST and Cocci AST for pointer

2020-02-06 Thread Jaskaran Singh
For a pointer, the C parser constructed an AST dissimilar from that
of the Cocci AST. This caused failures in matching with certain
pointer types. For example, for the following case:

char *1 const *2 id;

The C AST constructed would be:
const Pointer1 -> Pointer2 -> char

The Cocci AST constructed would be:
Pointer2 -> const Pointer1 -> char

Change the pointer rule in the C parser so that an AST similar to the
Cocci AST is constructed.

Make necessary changes in the C pretty printer so that the pointer type
is printed correctly.

Signed-off-by: Jaskaran Singh 
---
 parsing_c/parser_c.mly  | 4 ++--
 parsing_c/pretty_print_c.ml | 6 --
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly
index 8d7b761e..4c74f15a 100644
--- a/parsing_c/parser_c.mly
+++ b/parsing_c/parser_c.mly
@@ -1333,14 +1333,14 @@ pointer:
  | tmul   { (Ast_c.noattr,fun x -> mk_ty (Pointer x) [$1]) }
  | tmul pointer
  { let (attr,ptr) = $2 in
-   (attr,fun x -> mk_ty (Pointer (ptr x)) [$1]) }
+   (attr,fun x -> ptr (mk_ty (Pointer x) [$1])) }
  | tmul type_qualif_list
  { let (attr,tq) = $2 in
(attr,fun x -> (tq.qualifD, mk_tybis (Pointer x) [$1]))}
  | tmul type_qualif_list pointer
  { let (attr1,tq) = $2 in
let (attr2,ptr) = $3 in
-   (attr1@attr2,fun x -> (tq.qualifD, mk_tybis (Pointer (ptr x)) [$1])) }
+   (attr1@attr2,fun x -> ptr (tq.qualifD, mk_tybis (Pointer x) [$1])) }
 
 tmul:
TMul { $1 }
diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml
index a2e35588..40b89fe5 100644
--- a/parsing_c/pretty_print_c.ml
+++ b/parsing_c/pretty_print_c.ml
@@ -804,11 +804,12 @@ and pp_string_format (e,ii) =
  (FunctionType (return=void, params=int i) *)
   (*WRONG I THINK, use left & right function *)
   (* bug: pp_type_with_ident_rest None t;  print_ident ident *)
+  pp_type_left t;
   pr_elem i;
   iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *)
   if iiqu <> [] || get_comments_after i <> []
   then pr_space();
-  pp_type_with_ident_rest ident t attrs Ast_c.noattr;
+  print_ident ident
 
   (* ugly special case ... todo? maybe sufficient in practice *)
   | (ParenType ttop, [i1;i2]) ->
@@ -885,11 +886,12 @@ and pp_string_format (e,ii) =
   match ty, iity with
(NoType,_) -> failwith "pp_type_left: unexpected NoType"
   | (Pointer t, [i]) ->
+  pp_type_left t;
   pr_elem i;
   iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *)
   if iiqu <> [] || get_comments_after i <> []
   then pr_space();
-  pp_type_left t
+  ()
 
   | (Array (eopt, t), [i1;i2]) -> pp_type_left t
   | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt
-- 
2.21.1

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


[Cocci] [PATCH v2 2/2] tests: Add test case to match const pointer variants

2020-02-06 Thread Jaskaran Singh
Pointer to const pointer and its variants would not match previously.
Add a test case for matching these types.

Signed-off-by: Jaskaran Singh 
---
 tests/constptr.c |  7 +++
 tests/constptr.cocci | 19 +++
 tests/constptr.res   |  7 +++
 3 files changed, 33 insertions(+)
 create mode 100644 tests/constptr.c
 create mode 100644 tests/constptr.cocci
 create mode 100644 tests/constptr.res

diff --git a/tests/constptr.c b/tests/constptr.c
new file mode 100644
index ..13fe064c
--- /dev/null
+++ b/tests/constptr.c
@@ -0,0 +1,7 @@
+int main()
+{
+   const char * const * id;
+   const char * * const * id;
+   const char * const * * id;
+   const char * const id;
+}
diff --git a/tests/constptr.cocci b/tests/constptr.cocci
new file mode 100644
index ..29f0aa96
--- /dev/null
+++ b/tests/constptr.cocci
@@ -0,0 +1,19 @@
+@ r0 @
+identifier id;
+@@
+const char * const *
+- id
++ id1
+;
+const char * * const *
+- id
++ id2
+;
+const char * const * *
+- id
++ id3
+;
+const char * const
+- id
++ id4
+;
diff --git a/tests/constptr.res b/tests/constptr.res
new file mode 100644
index ..0af4de9a
--- /dev/null
+++ b/tests/constptr.res
@@ -0,0 +1,7 @@
+int main()
+{
+   const char * const * id1;
+   const char * * const * id2;
+   const char * const * * id3;
+   const char * const id4;
+}
-- 
2.21.1

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


[Cocci] [PATCH v2 0/2] cocci: Align the C AST and Cocci AST for pointer

2020-02-06 Thread Jaskaran Singh
This series is to address the type matching problem in Coccinelle.

Patch 1/2 is for aligning the C and Cocci AST so that
pointer to const pointer and its variants can be matched.

Patch 2/2 consists of a test case for matching a pointer to
const pointer and its variants.

Changes in v2:
--
- Change body and subject of Patch 2/2 as per suggestion of Markus
  Elfring.

 parsing_c/parser_c.mly  |4 ++--
 parsing_c/pretty_print_c.ml |6 --
 tests/constptr.c|7 +++
 tests/constptr.cocci|   19 +++
 tests/constptr.res  |7 +++
 5 files changed, 39 insertions(+), 4 deletions(-)


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


Re: [Cocci] [2/2] tests: Add test case to match const pointer variants

2020-02-06 Thread Markus Elfring
> Would the following be a better subject?

The alternative is nicer.


Will other implementation details become also more interesting?

The information “Align C AST and Cocci AST” was provided.
https://lore.kernel.org/cocci/20200205130327.6812-2-jaskaransingh7654...@gmail.com/
https://systeme.lip6.fr/pipermail/cocci/2020-February/006840.html

* Should the potential for differences in these “AST” be checked any more?

* How can such items be compared safely?


Is there a need to extend the test format?
https://github.com/coccinelle/coccinelle/issues/134

Regards,
Markus
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] [2/2] tests: Add test case to match const pointer variants

2020-02-06 Thread Markus Elfring
> Would the following be a better subject?

I find the alternative succinct.

Regards,
Markus
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] [PATCH 2/2] tests: Add test case for matching pointers to const pointers (and variants)

2020-02-06 Thread Julia Lawall



On Thu, 6 Feb 2020, Jaskaran Singh wrote:

> On Wed, 2020-02-05 at 19:55 +0100, Markus Elfring wrote:
> > Would you like to avoid any abbreviations in the patch subject?
> >
>
> I used abbreviations to keep the patch subject short but still clear.
>
> Would the following be a better subject?
>
> "tests: Add test case to match const pointer variants"

OK for me.

julia

>
> Cheers,
> Jaskaran.
>
> >
> > > These types would not match previously.
> >
> > I imagine that this information can be improved for a nicer patch
> > description,
> > can't it?
> >
> > Regards,
> > Markus
>
>
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] [PATCH 2/2] tests: Add test case for matching pointers to const pointers (and variants)

2020-02-06 Thread Jaskaran Singh
On Wed, 2020-02-05 at 19:55 +0100, Markus Elfring wrote:
> Would you like to avoid any abbreviations in the patch subject?
> 

I used abbreviations to keep the patch subject short but still clear.

Would the following be a better subject?

"tests: Add test case to match const pointer variants"

Cheers,
Jaskaran.

> 
> > These types would not match previously.
> 
> I imagine that this information can be improved for a nicer patch
> description,
> can't it?
> 
> Regards,
> Markus

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


Re: [Cocci] [PATCH 1/2] parsing_c: Align C AST and Cocci AST for pointers

2020-02-06 Thread Jaskaran Singh
On Wed, 2020-02-05 at 19:36 +0100, Markus Elfring wrote:
> …
> > +++ b/parsing_c/pretty_print_c.ml
> > @@ -804,11 +804,12 @@ and pp_string_format (e,ii) =
> >   (FunctionType (return=void, params=int i) *)
> >(*WRONG I THINK, use left & right function *)
> >(* bug: pp_type_with_ident_rest None t;  print_ident
> > ident *)
> > +  pp_type_left t;
> >pr_elem i;
> >iiqu +> List.iter pr_elem; (* le const est forcement
> > apres le '*' *)
> >if iiqu <> [] || get_comments_after i <> []
> >then pr_space();
> > -  pp_type_with_ident_rest ident t attrs Ast_c.noattr;
> > +  print_ident ident
> > 
> >(* ugly special case ... todo? maybe sufficient in practice
> > *)
> >| (ParenType ttop, [i1;i2]) ->
> 
> Are the source code comments still appropriate for this patch hunk?
> 

I know the "le const est forcement" comment is still appropriate.

The bug comment above probably still is.

Not so sure about the WRONG I THINK comment. I'm using pp_type_left
here, but not pp_type_right. Of course, that is if the comment is in
that context.

Cheers,
Jaskaran.

> Regards,
> Markus

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