Re: [Cocci] [PATCH v3 1/3] parsing_c: Align C AST and Cocci AST for pointer

2020-02-09 Thread Julia Lawall
On Sun, 9 Feb 2020, Jaskaran Singh wrote:

> 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 | 12 
>  2 files changed, 10 insertions(+), 6 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..ae02b513 100644
> --- a/parsing_c/pretty_print_c.ml
> +++ b/parsing_c/pretty_print_c.ml
> @@ -804,11 +804,13 @@ 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 '*' 
> *)
> +  iiqu +> List.iter (function x ->
> + (pr_space(); pr_elem x));(* le const est forcement apres le '*' 
> *)

I'm not very fond of this coding style.  I would prefer:

iiqu +>
List.iter (* the comment can go here *)
  (function x -> pr_space(); pr_elem x)

Likewise below.

julia

>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 +887,13 @@ 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 '*' 
> *)
> +  iiqu +> List.iter (function x ->
> + (pr_space(); pr_elem x));(* 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 v3 1/3] parsing_c: Align C AST and Cocci AST for pointer

2020-02-09 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 | 12 
 2 files changed, 10 insertions(+), 6 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..ae02b513 100644
--- a/parsing_c/pretty_print_c.ml
+++ b/parsing_c/pretty_print_c.ml
@@ -804,11 +804,13 @@ 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 '*' *)
+  iiqu +> List.iter (function x ->
+ (pr_space(); pr_elem x));(* 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 +887,13 @@ 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 '*' *)
+  iiqu +> List.iter (function x ->
+ (pr_space(); pr_elem x));(* 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