cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Jaskaran Singh <jaskaransingh7654321@gmail.com>
Cc: linux-kernel-mentees@lists.linuxfoundation.org, cocci@systeme.lip6.fr
Subject: Re: [Cocci] [PATCH v4 1/3] parsing_c: Align C AST and Cocci AST for pointer
Date: Sun, 9 Feb 2020 17:46:36 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.21.2002091745220.3430@hadrien> (raw)
In-Reply-To: <20200209153452.11837-2-jaskaransingh7654321@gmail.com>

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 <jaskaransingh7654321@gmail.com>
> ---
>  parsing_c/parser_c.mly      |  4 ++--
>  parsing_c/pretty_print_c.ml | 14 ++++++++++----
>  2 files changed, 12 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..3aaa97ca 100644
> --- a/parsing_c/pretty_print_c.ml
> +++ b/parsing_c/pretty_print_c.ml
> @@ -804,11 +804,14 @@ 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 (* le const est forcement apres le '*' *)
> +            (function x -> (pr_space(); pr_elem x));

There is no need for the parentheses around the pr_space and pr_elem
calls.

>            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 +888,14 @@ 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 (* le const est forcement apres le '*' *)
> +            (function x -> (pr_space(); pr_elem x));
>            if iiqu <> [] || get_comments_after i <> []
>            then pr_space();
> -          pp_type_left t
> +          ()

This is not needed.  YOu can just drop the ; after the pr_space() and the
whole call to pp_type_left.

julia

>
>        | (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

  reply	other threads:[~2020-02-09 16:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-09 15:34 [Cocci] [PATCH v4 0/3] cocci: Align the C AST and Cocci AST for pointer Jaskaran Singh
2020-02-09 15:34 ` [Cocci] [PATCH v4 1/3] parsing_c: Align " Jaskaran Singh
2020-02-09 16:46   ` Julia Lawall [this message]
2020-02-09 15:34 ` [Cocci] [PATCH v4 2/3] tests: Add space between * and const in ptrconstptr.res Jaskaran Singh
2020-02-09 15:34 ` [Cocci] [PATCH v4 3/3] tests: Add test case to match const pointer variants Jaskaran Singh
2020-02-09 16:48   ` Julia Lawall
2020-02-09 17:32     ` Jaskaran Singh
2020-02-09 17:35       ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.21.2002091745220.3430@hadrien \
    --to=julia.lawall@inria.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=jaskaransingh7654321@gmail.com \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).