cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule
@ 2020-06-08 12:20 Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 1/4] " Jaskaran Singh
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Jaskaran Singh @ 2020-06-08 12:20 UTC (permalink / raw)
  To: cocci; +Cc: linux-kernel-mentees

Patches for improving the C parsing of attributes[1] introduced a lot
redundant code in the C parser. This patch series resolves this by adding
a rule for optional end attributes and merging the redundant code
together.

[1]
[PATCH v2 00/25] cocci: Improve C parsing of attributes
https://lore.kernel.org/cocci/20200528122428.4212-1-jaskaransingh7654321@gmail.com/

Jaskaran Singh (4):
      parsing_c: parser: Add end_attributes_opt rule
      parsing_c: parser: Use end_attributes_opt in decl2
      parsing_c: parser: Use end_attributes_opt in field_declaration
      parsing_c: parser: Use end_attributes_opt in cpp_other

 parser_c.mly |   96 +++++------------------------------------------------------
 1 file changed, 9 insertions(+), 87 deletions(-)



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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Cocci] [PATCH 1/4] parsing_c: parser: Add end_attributes_opt rule
  2020-06-08 12:20 [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Jaskaran Singh
@ 2020-06-08 12:20 ` Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 2/4] parsing_c: parser: Use end_attributes_opt in decl2 Jaskaran Singh
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Jaskaran Singh @ 2020-06-08 12:20 UTC (permalink / raw)
  To: cocci; +Cc: linux-kernel-mentees

A lot of redundant code is introduced in the C parser due to additional
productions for end attributes. Add an end_attributes_opt rule to
resolve this.

Signed-off-by: Jaskaran Singh <jaskaransingh7654321@gmail.com>
---
 parsing_c/parser_c.mly | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly
index effd0727..78407844 100644
--- a/parsing_c/parser_c.mly
+++ b/parsing_c/parser_c.mly
@@ -2542,6 +2542,10 @@ end_attribute_list:
 
 end_attributes: end_attribute_list { $1 }
 
+end_attributes_opt:
+ | end_attributes { $1 }
+ | /*(* empty *)*/ { [] }
+
 comma_opt:
  | TComma {  [$1] }
  | /*(* empty *)*/  {  []  }
-- 
2.21.1

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Cocci] [PATCH 2/4] parsing_c: parser: Use end_attributes_opt in decl2
  2020-06-08 12:20 [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 1/4] " Jaskaran Singh
@ 2020-06-08 12:20 ` Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 3/4] parsing_c: parser: Use end_attributes_opt in field_declaration Jaskaran Singh
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Jaskaran Singh @ 2020-06-08 12:20 UTC (permalink / raw)
  To: cocci; +Cc: linux-kernel-mentees

The decl2 rule has redundant code caused by additional productions for
end attributes. Use end_attributes_opt to resolve this.

Signed-off-by: Jaskaran Singh <jaskaransingh7654321@gmail.com>
---
 parsing_c/parser_c.mly | 26 ++------------------------
 1 file changed, 2 insertions(+), 24 deletions(-)

diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly
index 78407844..ebb3b327 100644
--- a/parsing_c/parser_c.mly
+++ b/parsing_c/parser_c.mly
@@ -1560,18 +1560,7 @@ abstract_declaratort:
 /*(*************************************************************************)*/
 
 decl2:
- | decl_spec TPtVirg
-     { function local ->
-       let (returnType,storage) = fixDeclSpecForDecl (snd $1) in
-       let iistart = Ast_c.fakeInfo () in
-       DeclList ([{v_namei = None; v_type = returnType;
-                   v_storage = unwrap storage; v_local = local;
-                   v_attr = fst $1; v_endattr = Ast_c.noattr;
-                   v_type_bis = ref None;
-                },[]],
-                ($2::iistart::snd storage))
-     }
- | decl_spec end_attributes TPtVirg
+ | decl_spec end_attributes_opt TPtVirg
      { function local ->
        let (returnType,storage) = fixDeclSpecForDecl (snd $1) in
        let iistart = Ast_c.fakeInfo () in
@@ -1605,18 +1594,7 @@ decl2:
      }
  /*(* cppext: *)*/
 
- | storage_const_opt TMacroDecl TOPar argument_list TCPar TPtVirg
-     { function _ ->
-       match $1 with
-	 Some (sto,stoii) ->
-	   MacroDecl
-             ((sto, fst $2, $4, [], true),
-              (snd $2::$3::$5::$6::fakeInfo()::stoii))
-       | None ->
-	   MacroDecl
-             ((NoSto, fst $2, $4, [], true), [snd $2;$3;$5;$6;fakeInfo()]) }
-
- | storage_const_opt TMacroDecl TOPar argument_list TCPar end_attributes
+ | storage_const_opt TMacroDecl TOPar argument_list TCPar end_attributes_opt
    TPtVirg
      { function _ ->
        match $1 with
-- 
2.21.1

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Cocci] [PATCH 3/4] parsing_c: parser: Use end_attributes_opt in field_declaration
  2020-06-08 12:20 [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 1/4] " Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 2/4] parsing_c: parser: Use end_attributes_opt in decl2 Jaskaran Singh
@ 2020-06-08 12:20 ` Jaskaran Singh
  2020-06-08 12:20 ` [Cocci] [PATCH 4/4] parsing_c: parser: Use end_attributes_opt in cpp_other Jaskaran Singh
  2020-06-09 10:00 ` [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Julia Lawall
  4 siblings, 0 replies; 7+ messages in thread
From: Jaskaran Singh @ 2020-06-08 12:20 UTC (permalink / raw)
  To: cocci; +Cc: linux-kernel-mentees

The field_declaration rule has redundant code caused by additional
productions for end attributes. Use end_attributes_opt to resolve this.

Signed-off-by: Jaskaran Singh <jaskaransingh7654321@gmail.com>
---
 parsing_c/parser_c.mly | 32 ++------------------------------
 1 file changed, 2 insertions(+), 30 deletions(-)

diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly
index ebb3b327..61bb1ca0 100644
--- a/parsing_c/parser_c.mly
+++ b/parsing_c/parser_c.mly
@@ -1851,23 +1851,7 @@ struct_decl2:
 
 
 field_declaration:
- | spec_qualif_list struct_declarator_list TPtVirg
-     {
-       let (attrs, ds) = $1 in
-       let (returnType,storage) = fixDeclSpecForDecl ds in
-       if fst (unwrap storage) <> NoSto
-       then internal_error "parsing don't allow this";
-
-       let iistart = Ast_c.fakeInfo () in (* for parallelism with DeclList *)
-       FieldDeclList ($2 +> (List.map (fun (f, iivirg) ->
-         f returnType, iivirg))
-                         ,[$3;iistart])
-         (* don't need to check if typedef or func initialised cos
-          * grammar don't allow typedef nor initialiser in struct
-          *)
-     }
-
- | spec_qualif_list struct_declarator_list end_attributes TPtVirg
+ | spec_qualif_list struct_declarator_list end_attributes_opt TPtVirg
      {
        let (attrs, ds) = $1 in
        let (returnType,storage) = fixDeclSpecForDecl ds in
@@ -1883,19 +1867,7 @@ field_declaration:
           *)
      }
 
- | spec_qualif_list TPtVirg
-     {
-       let (attrs, ds) = $1 in
-       (* gccext: allow empty elements if it is a structdef or enumdef *)
-       let (returnType,storage) = fixDeclSpecForDecl ds in
-       if fst (unwrap storage) <> NoSto
-       then internal_error "parsing don't allow this";
-
-       let iistart = Ast_c.fakeInfo () in (* for parallelism with DeclList *)
-       FieldDeclList ([(Simple (None, returnType)) , []], [$2;iistart])
-     }
-
- | spec_qualif_list end_attributes TPtVirg
+ | spec_qualif_list end_attributes_opt TPtVirg
      {
        let (attrs, ds) = $1 in
        (* gccext: allow empty elements if it is a structdef or enumdef *)
-- 
2.21.1

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [Cocci] [PATCH 4/4] parsing_c: parser: Use end_attributes_opt in cpp_other
  2020-06-08 12:20 [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Jaskaran Singh
                   ` (2 preceding siblings ...)
  2020-06-08 12:20 ` [Cocci] [PATCH 3/4] parsing_c: parser: Use end_attributes_opt in field_declaration Jaskaran Singh
@ 2020-06-08 12:20 ` Jaskaran Singh
  2020-06-09 10:00 ` [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Julia Lawall
  4 siblings, 0 replies; 7+ messages in thread
From: Jaskaran Singh @ 2020-06-08 12:20 UTC (permalink / raw)
  To: cocci; +Cc: linux-kernel-mentees

The cpp_other rule has redundant code due to an additional production
for end attributes. Use end_attributes_opt to resolve this.

Signed-off-by: Jaskaran Singh <jaskaransingh7654321@gmail.com>
---
 parsing_c/parser_c.mly | 34 +---------------------------------
 1 file changed, 1 insertion(+), 33 deletions(-)

diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly
index 61bb1ca0..10cd4435 100644
--- a/parsing_c/parser_c.mly
+++ b/parsing_c/parser_c.mly
@@ -2202,39 +2202,7 @@ cpp_other:
     * the rule are slightly different, they can't be statement and so expr
     * at the top, only decl or function definition.
     *)*/
- | identifier TOPar argument_list TCPar TPtVirg
-     {
-       if args_are_params $3
-       then
-	 (* if all args are params, assume it is a prototype of a function
-	    with no return type *)
-	 let parameters = args_to_params $3 None in
-	 let paramlist = (parameters, (false, [])) in (* no varargs *)
-	 let id = RegularName (mk_string_wrap $1) in
-	 let ret =
-	   warning "type defaults to 'int'"
-	     (mk_ty defaultInt [fakeInfo fake_pi]) in
-	 let ty =
-	   fixOldCDecl (mk_ty (FunctionType (ret, paramlist)) [$2;$4]) in
-	 let attrs = Ast_c.noattr in
-	 let sto = (NoSto, false), [] in
-	 let iistart = Ast_c.fakeInfo () in
-	 Declaration(
-	 DeclList ([{v_namei = Some (id,NoInit); v_type = ty;
-                      v_storage = unwrap sto; v_local = NotLocalDecl;
-                      v_attr = attrs; v_endattr = Ast_c.noattr;
-		      v_type_bis = ref None;
-                    },[]],
-                   ($5::iistart::snd sto)))
-       else
-	 Declaration
-	   (MacroDecl
-             ((NoSto, fst $1, $3, Ast_c.noattr, true),
-               [snd $1;$2;$4;$5;fakeInfo()]))
-           (* old: MacroTop (fst $1, $3,    [snd $1;$2;$4;$5])  *)
-     }
-
- | identifier TOPar argument_list TCPar end_attributes TPtVirg
+ | identifier TOPar argument_list TCPar end_attributes_opt TPtVirg
      {
        if args_are_params $3
        then
-- 
2.21.1

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule
  2020-06-08 12:20 [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Jaskaran Singh
                   ` (3 preceding siblings ...)
  2020-06-08 12:20 ` [Cocci] [PATCH 4/4] parsing_c: parser: Use end_attributes_opt in cpp_other Jaskaran Singh
@ 2020-06-09 10:00 ` Julia Lawall
  4 siblings, 0 replies; 7+ messages in thread
From: Julia Lawall @ 2020-06-09 10:00 UTC (permalink / raw)
  To: Jaskaran Singh; +Cc: linux-kernel-mentees, cocci



On Mon, 8 Jun 2020, Jaskaran Singh wrote:

> Patches for improving the C parsing of attributes[1] introduced a lot
> redundant code in the C parser. This patch series resolves this by adding
> a rule for optional end attributes and merging the redundant code
> together.

All are applied, thanks.

julia

>
> [1]
> [PATCH v2 00/25] cocci: Improve C parsing of attributes
> https://lore.kernel.org/cocci/20200528122428.4212-1-jaskaransingh7654321@gmail.com/
>
> Jaskaran Singh (4):
>       parsing_c: parser: Add end_attributes_opt rule
>       parsing_c: parser: Use end_attributes_opt in decl2
>       parsing_c: parser: Use end_attributes_opt in field_declaration
>       parsing_c: parser: Use end_attributes_opt in cpp_other
>
>  parser_c.mly |   96 +++++------------------------------------------------------
>  1 file changed, 9 insertions(+), 87 deletions(-)
>
>
>
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule
@ 2020-06-08 12:47 Markus Elfring
  0 siblings, 0 replies; 7+ messages in thread
From: Markus Elfring @ 2020-06-08 12:47 UTC (permalink / raw)
  To: Jaskaran Singh, cocci; +Cc: linux-kernel-mentees

> Patches for improving the C parsing of attributes[1] introduced a lot
> redundant code in the C parser. This patch series resolves this by adding
> a rule for optional end attributes and merging the redundant code
> together.
>
> [1]
> [PATCH v2 00/25] cocci: Improve C parsing of attributes
> https://lore.kernel.org/cocci/20200528122428.4212-1-jaskaransingh7654321@gmail.com/

Could questionable source code have been avoided before?

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-06-09 10:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-08 12:20 [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Jaskaran Singh
2020-06-08 12:20 ` [Cocci] [PATCH 1/4] " Jaskaran Singh
2020-06-08 12:20 ` [Cocci] [PATCH 2/4] parsing_c: parser: Use end_attributes_opt in decl2 Jaskaran Singh
2020-06-08 12:20 ` [Cocci] [PATCH 3/4] parsing_c: parser: Use end_attributes_opt in field_declaration Jaskaran Singh
2020-06-08 12:20 ` [Cocci] [PATCH 4/4] parsing_c: parser: Use end_attributes_opt in cpp_other Jaskaran Singh
2020-06-09 10:00 ` [Cocci] [PATCH 0/4] parsing_c: parser: Add end_attributes_opt rule Julia Lawall
2020-06-08 12:47 Markus Elfring

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).