cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Jaskaran Singh <jaskaransingh7654321@gmail.com>
To: cocci@systeme.lip6.fr
Cc: linux-kernel-mentees@lists.linuxfoundation.org
Subject: [Cocci] [PATCH 11/13] cocci: pretty print EnumDef as per enum_decl type
Date: Sun,  8 Mar 2020 14:13:54 +0530	[thread overview]
Message-ID: <20200308084356.14506-12-jaskaransingh7654321@gmail.com> (raw)
In-Reply-To: <20200308084356.14506-1-jaskaransingh7654321@gmail.com>

The SmPL AST has a separate enum_decl for a enumerator. Make
corresponding changes in Unparse_cocci and Pretty_print_cocci
to correctly pretty print enumerators.

Signed-off-by: Jaskaran Singh <jaskaransingh7654321@gmail.com>
---
 parsing_c/unparse_cocci.ml          | 27 ++++++++++++++++++++++++---
 parsing_cocci/pretty_print_cocci.ml | 18 +++++++++++++++++-
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/parsing_c/unparse_cocci.ml b/parsing_c/unparse_cocci.ml
index 30e755e9..8dff2b81 100644
--- a/parsing_c/unparse_cocci.ml
+++ b/parsing_c/unparse_cocci.ml
@@ -738,7 +738,7 @@ and typeC ty =
   | Ast.EnumDef(ty,lb,ids,rb) ->
       fullType ty; ft_space ty;
       mcode print_string lb;
-      dots force_newline expression ids;
+      dots force_newline enum_decl ids;
       mcode print_string rb
   | Ast.StructUnionName(kind,name) ->
       mcode structUnion kind; print_option_prespace ident name
@@ -952,6 +952,24 @@ and annotated_field d =
   | Ast.OptField(decl) -> raise CantBeInPlus
   | Ast.Fdots(_,_) -> raise CantBeInPlus
 
+and enum_decl d =
+  match Ast.unwrap d with
+    Ast.Enum(name,enum_val) ->
+      ident name;
+      pr_space();
+      print_option
+        (function (eq,eval) ->
+          mcode print_string eq; pr_space(); expression eval) enum_val
+  | Ast.EnumComma(cm) ->
+      mcode (print_string_with_hint (SpaceOrNewline (ref " "))) cm
+  | Ast.EnumDots(dots,whencode) when generating ->
+      mcode print_string dots;
+      print_option
+      (function w ->
+        print_text "   when != ";
+        enum_decl w) whencode
+  | Ast.EnumDots(dots,whencode) -> raise CantBeInPlus
+
 (* --------------------------------------------------------------------- *)
 (* Initialiser *)
 
@@ -1420,6 +1438,7 @@ let pp_any = function
   | Ast.InitTag(x) -> initialiser false x; false
   | Ast.DeclarationTag(x) -> declaration x; false
   | Ast.FieldTag(x) -> field x; false
+  | Ast.EnumDeclTag(x) -> enum_decl x; false
 
   | Ast.StorageTag(x) -> storage x unknown unknown; false
   | Ast.IncFileTag(x) -> inc_file x unknown unknown; false
@@ -1473,6 +1492,7 @@ let pp_any = function
   | Ast.StmtDotsTag(x) -> dots force_newline (statement "") x; false
   | Ast.AnnDeclDotsTag(x) -> dots force_newline annotated_decl x; false
   | Ast.AnnFieldDotsTag(x) -> dots force_newline annotated_field x; false
+  | Ast.EnumDeclDotsTag(x) -> dots force_newline enum_decl x; false
   | Ast.DefParDotsTag(x) -> dots (fun _ -> ()) print_define_param x; false
 
   | Ast.TypeCTag(x) -> typeC x; false
@@ -1505,7 +1525,7 @@ in
 	      force_newline(); force_newline()
 	  | (Ast.Directive _::_)
 	  | (Ast.Rule_elemTag _::_) | (Ast.StatementTag _::_)
-	  | (Ast.FieldTag _::_) | (Ast.InitTag _::_)
+	  | (Ast.FieldTag _::_) | (Ast.EnumDeclTag _::_) | (Ast.InitTag _::_)
 	  | (Ast.DeclarationTag _::_) | (Ast.Token ("}",_)::_) -> prnl hd
 	  | _ -> () in
       let newline_after _ =
@@ -1516,7 +1536,8 @@ in
 	      (if isfn s then force_newline());
 	      force_newline()
 	  | (Ast.Directive _::_) | (Ast.StmtDotsTag _::_)
-	  | (Ast.Rule_elemTag _::_) | (Ast.FieldTag _::_) | (Ast.InitTag _::_)
+	  | (Ast.Rule_elemTag _::_) | (Ast.FieldTag _::_)
+	  | (Ast.EnumDeclTag _::_)| (Ast.InitTag _::_)
 	  | (Ast.DeclarationTag _::_) | (Ast.Token ("{",_)::_) ->
 	      force_newline()
 	  | _ -> () in
diff --git a/parsing_cocci/pretty_print_cocci.ml b/parsing_cocci/pretty_print_cocci.ml
index 6338e464..f6b2894f 100644
--- a/parsing_cocci/pretty_print_cocci.ml
+++ b/parsing_cocci/pretty_print_cocci.ml
@@ -439,7 +439,7 @@ and typeC ty =
       print_option (function x -> ident x; print_string " ") name
   | Ast.EnumDef(ty,lb,ids,rb) ->
       fullType ty; mcode print_string lb;
-      dots force_newline expression ids;
+      dots force_newline enum_decl ids;
       mcode print_string rb
   | Ast.StructUnionName(kind,name) ->
       mcode structUnion kind;
@@ -585,6 +585,20 @@ and annotated_field arity d =
   | Ast.ConjField(decls) -> print_disj_list (annotated_field arity) decls "&"
   | Ast.OptField(decl) -> print_string "?"; annotated_field arity decl
 
+and enum_decl d =
+  match Ast.unwrap d with
+    Ast.Enum(name,enum_val) ->
+      ident name;
+      (match enum_val with
+        None -> ()
+      | Some(eq,eval) ->
+          mcode print_string eq;
+          expression eval)
+  | Ast.EnumComma(cm) -> mcode print_string cm
+  | Ast.EnumDots(dots,Some whencode) ->
+      mcode print_string dots; print_string "   when != "; enum_decl whencode
+  | Ast.EnumDots(dots,None) -> mcode print_string dots
+
 (* --------------------------------------------------------------------- *)
 (* Initialiser *)
 
@@ -1090,6 +1104,7 @@ let _ =
     | Ast.InitTag(x) -> initialiser x
     | Ast.DeclarationTag(x) -> declaration x
     | Ast.FieldTag(x) -> field x
+    | Ast.EnumDeclTag(x) -> enum_decl x
     | Ast.StorageTag(x) -> storage x
     | Ast.IncFileTag(x) -> inc_file x
     | Ast.Rule_elemTag(x) -> rule_elem "" x
@@ -1110,6 +1125,7 @@ let _ =
     | Ast.StmtDotsTag(x) -> dots (function _ -> ()) (statement "") x
     | Ast.AnnDeclDotsTag(x) -> dots (function _ -> ()) (annotated_decl "") x
     | Ast.AnnFieldDotsTag(x) -> dots (function _ -> ()) (annotated_field "") x
+    | Ast.EnumDeclDotsTag(x) -> dots (function _ -> ()) enum_decl x
     | Ast.DefParDotsTag(x) -> dots (function _ -> ()) print_define_param x
     | Ast.TypeCTag(x) -> typeC x
     | Ast.ParamTag(x) -> parameterTypeDef x
-- 
2.21.1

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

  parent reply	other threads:[~2020-03-08  8:45 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-08  8:43 [Cocci] [PATCH 00/13] cocci: Align the C AST and SmPL AST for enum Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 01/13] parsing_cocci: Align " Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 02/13] ocaml: coccilib: Reflect changes in SmPL AST for EnumDef Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 03/13] parsing_cocci: parser: Parse enumerators correctly Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 04/13] parsing_cocci: Add EnumDeclTag and EnumDeclDotsTag to SmPL ASTs Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 05/13] ocaml: coccilib: Reflect EnumDeclTag and EnumDeclDotsTag Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 06/13] parsing_cocci: visitor_ast0: Add visitor functions for enum_decl Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 07/13] parsing_cocci: Reflect visitor_ast0 changes in parsing_cocci Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 08/13] parsing_cocci: Add visitor functions for enum_decl in visitor_ast Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 09/13] cocci: Reflect changes in SmPL visitor_ast in codebase Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 10/13] engine: cocci_vs_c: Match enumerators properly as per enum_decl Jaskaran Singh
2020-03-08  8:43 ` Jaskaran Singh [this message]
2020-03-08  8:43 ` [Cocci] [PATCH 12/13] tests: Add test case for assigned enumerator Jaskaran Singh
2020-03-08  8:43 ` [Cocci] [PATCH 13/13] tools: spgen: Reflect visitor changes Jaskaran Singh
2020-03-09 14:15 ` [Cocci] [PATCH 00/13] cocci: Align the C AST and SmPL AST for enum Julia Lawall
2020-03-09 14:51   ` Jaskaran Singh

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=20200308084356.14506-12-jaskaransingh7654321@gmail.com \
    --to=jaskaransingh7654321@gmail.com \
    --cc=cocci@systeme.lip6.fr \
    --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).