Coccinelle release 1.0.0c7.
[bpt/coccinelle.git] / parsing_c / unparse_cocci.ml
index 6eab5bb..ca08fd9 100644 (file)
@@ -35,11 +35,14 @@ exception CantBeInPlus
 type pos = Before | After | InPlace
 type nlhint = StartBox | EndBox | SpaceOrNewline of string ref
 
+let get_string_info = function
+    Ast.Noindent s | Ast.Indent s | Ast.Space s -> s
+
 let unknown = -1
 
 let rec do_all
     (env, pr, pr_celem, pr_cspace, pr_space, pr_arity, pr_barrier,
-     indent, unindent)
+     indent, unindent, eatspace)
     generating xxs before =
 
 (* Just to be able to copy paste the code from pretty_print_cocci.ml. *)
@@ -110,8 +113,7 @@ let print_around printer term = function
       print_anything bef; printer term; print_anything aft in
 
 let print_string_befaft fn fn1 x info =
-  let print ln col =
-    function Ast.Noindent s | Ast.Indent s -> print_string s ln col in
+  let print ln col s = print_string (get_string_info s) ln col in
   List.iter
     (function (s,ln,col) -> fn1(); print ln col s; force_newline())
     info.Ast.strbef;
@@ -121,11 +123,13 @@ let print_string_befaft fn fn1 x info =
     info.Ast.straft in
 let print_meta (r,x) = print_text x in
 
-let print_pos = function
-    Ast.MetaPos(name,_,_,_,_) ->
-      let name = Ast.unwrap_mcode name in
-      print_text "@"; print_meta name
-  | _ -> () in
+let print_pos l =
+  List.iter
+    (function
+       Ast.MetaPos(name,_,_,_,_) ->
+         let name = Ast.unwrap_mcode name in
+         print_text "@"; print_meta name)
+    l in
 
 (* --------------------------------------------------------------------- *)
 
@@ -145,11 +149,11 @@ let mcode fn (s,info,mc,pos) =
                  let str =
                    match str with
                      Ast.Noindent s -> unindent false; s
-                   | Ast.Indent s -> s in
+                   | Ast.Indent s -> s
+                   | Ast.Space s -> s in
                  print_string str line col; Some line
              | Some lb when line =|= lb ->
-                 let str = match str with Ast.Noindent s | Ast.Indent s -> s in
-                 print_string str line col; Some line
+                 print_string (get_string_info str) line col; Some line
              | _ ->
                  force_newline();
                  (* not super elegant to put side-effecting unindent in a let
@@ -157,7 +161,8 @@ let mcode fn (s,info,mc,pos) =
                  let str =
                    match str with
                      Ast.Noindent s -> unindent false; s
-                   | Ast.Indent s -> s in
+                   | Ast.Indent s -> s
+                   | Ast.Space s -> s in
                  print_string str line col; Some line)
          lb comments in
       let line_before = print_comments None info.Ast.strbef in
@@ -177,7 +182,9 @@ let mcode fn (s,info,mc,pos) =
       force_newline();
       print_text "- ";
       fn s line lcol; print_pos pos;
-      print_anything plus_stream
+      (match plus_stream with
+       Ast.NOREPLACEMENT -> ()
+      |        Ast.REPLACEMENT(plus_stream,ct) -> print_anything plus_stream)
   | (true, Ast.CONTEXT(_,plus_streams)) ->
       let fn s = force_newline(); fn s line lcol; print_pos pos in
       print_around fn s plus_streams
@@ -238,6 +245,11 @@ let nest_dots starter ender fn f d =
   mcode print_string ender
 in
 
+let print_disj_list fn l =
+  print_text "\n(\n";
+  print_between (function _ -> print_text "\n|\n") fn l;
+  print_text "\n)\n" in
+
 (* --------------------------------------------------------------------- *)
 (* Identifier *)
 
@@ -260,6 +272,10 @@ let rec ident i =
                               | _ -> raise Impossible
                            )
 
+    | Ast.DisjId(id_list) ->
+       if generating
+       then print_disj_list ident id_list
+       else raise CantBeInPlus
     | Ast.OptIdent(_) | Ast.UniqueIdent(_) ->
        raise CantBeInPlus
 
@@ -268,11 +284,6 @@ in
 (* --------------------------------------------------------------------- *)
 (* Expression *)
 
-let print_disj_list fn l =
-  print_text "\n(\n";
-  print_between (function _ -> print_text "\n|\n") fn l;
-  print_text "\n)\n" in
-
 let rec expression e =
   match Ast.unwrap e with
     Ast.Ident(id) -> ident id
@@ -315,6 +326,9 @@ let rec expression e =
       mcode print_string_box lp; fullType ty; close_box();
       mcode print_string rp
   | Ast.TypeExp(ty) -> fullType ty
+  | Ast.Constructor(lp,ty,rp,init) ->
+      mcode print_string_box lp; fullType ty; close_box();
+      mcode print_string rp; initialiser true init
 
   | Ast.MetaErr(name,_,_,_) ->
       failwith "metaErr not handled"
@@ -378,6 +392,7 @@ and arg_expression e =
 
 and  unaryOp = function
     Ast.GetRef -> print_string "&"
+  | Ast.GetRefLabel -> print_string "&&"
   | Ast.DeRef -> print_string "*"
   | Ast.UnPlus -> print_string "+"
   | Ast.UnMinus -> print_string "-"
@@ -399,7 +414,7 @@ and  binaryOp = function
   | Ast.Logical(lop) -> logicalOp lop
 
 and  arithOp = function
-    Ast.Plus -> print_string "+"
+   Ast.Plus -> print_string "+"
   | Ast.Minus -> print_string "-"
   | Ast.Mul -> print_string "*"
   | Ast.Div -> print_string "/"
@@ -451,7 +466,8 @@ and typeC ty =
     Ast.BaseType(ty,strings) ->
       print_between pr_space (mcode print_string) strings
   | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option_prespace typeC ty
-  | Ast.Pointer(ty,star) -> fullType ty; ft_space ty; mcode print_string star
+  | Ast.Pointer(ty,star) ->
+      fullType ty; ft_space ty; mcode print_string star; eatspace()
   | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
        (function _ -> ())
@@ -460,8 +476,14 @@ and typeC ty =
   | Ast.Array(ty,lb,size,rb) ->
       fullType ty; mcode print_string lb; print_option expression size;
       mcode print_string rb
-  | Ast.EnumName(kind,name) -> mcode print_string kind; pr_space();
-      ident name
+  | Ast.EnumName(kind,name) ->
+      mcode print_string kind;
+      print_option_prespace ident name
+  | Ast.EnumDef(ty,lb,ids,rb) ->
+      fullType ty; ft_space ty;
+      mcode print_string lb;
+      dots force_newline expression ids;
+      mcode print_string rb
   | Ast.StructUnionName(kind,name) ->
       mcode structUnion kind; print_option_prespace ident name
   | Ast.StructUnionDef(ty,lb,decls,rb) ->
@@ -480,11 +502,18 @@ and baseType = function
     Ast.VoidType -> print_string "void"
   | Ast.CharType -> print_string "char"
   | Ast.ShortType -> print_string "short"
+  | Ast.ShortIntType -> print_string "short int"
   | Ast.IntType -> print_string "int"
   | Ast.DoubleType -> print_string "double"
+  | Ast.LongDoubleType -> print_string "long double"
   | Ast.FloatType -> print_string "float"
   | Ast.LongType -> print_string "long"
+  | Ast.LongIntType -> print_string "long int"
   | Ast.LongLongType -> print_string "long long"
+  | Ast.LongLongIntType -> print_string "long long int"
+  | Ast.SizeType -> print_string "size_t "
+  | Ast.SSizeType -> print_string "ssize_t "
+  | Ast.PtrDiffType -> print_string "ptrdiff_t "
 
 and structUnion = function
     Ast.Struct -> print_string "struct"
@@ -573,9 +602,17 @@ and declaration d =
   | Ast.MetaField(name,_,_) ->
       handle_metavar name
        (function
-           Ast_c.MetaFieldVal d ->
-              pretty_print_c.Pretty_print_c.field d
+           Ast_c.MetaFieldVal f ->
+              pretty_print_c.Pretty_print_c.field f
+          | _ -> raise Impossible)
+
+  | Ast.MetaFieldList(name,_,_,_) ->
+      handle_metavar name
+       (function
+           Ast_c.MetaFieldListVal f ->
+             print_between force_newline pretty_print_c.Pretty_print_c.field f
           | _ -> raise Impossible)
+
   | Ast.Init(stg,ty,id,eq,ini,sem) ->
       print_option (mcode storage) stg;
       print_option (function _ -> pr_space()) stg;
@@ -611,17 +648,27 @@ and initialiser nlcomma i =
           Ast_c.MetaInitVal ini ->
             pretty_print_c.Pretty_print_c.init ini
         | _ -> raise Impossible)
+  | Ast.MetaInitList(name,_,_,_) ->
+      handle_metavar name  (function
+          Ast_c.MetaInitListVal ini ->
+           pretty_print_c.Pretty_print_c.init_list ini
+        | _ -> raise Impossible)
   | Ast.InitExpr(exp) -> expression exp
-  | Ast.InitList(_,lb,initlist,rb,[]) ->
+  | Ast.ArInitList(lb,initlist,rb) ->
+      (match Ast.undots initlist with
+       [] -> mcode print_string lb; mcode print_string rb
+      |        lst ->
+         mcode print_string lb; start_block();
+         initialiser_list nlcomma lst;
+         end_block(); mcode print_string rb)
+  | Ast.StrInitList(_,lb,[],rb,[]) ->
+      mcode print_string lb; mcode print_string rb
+  | Ast.StrInitList(_,lb,initlist,rb,[]) ->
       mcode print_string lb; start_block();
-      (* awkward, because the comma is separate from the initialiser *)
-      let rec loop = function
-         [] -> ()
-       | [x] -> initialiser false x
-       | x::xs -> initialiser nlcomma x; loop xs in
-      loop initlist;
+      initialiser_list nlcomma initlist;
       end_block(); mcode print_string rb
-  | Ast.InitList(_,lb,initlist,rb,_) -> failwith "unexpected whencode in plus"
+  | Ast.StrInitList(_,lb,initlist,rb,_) ->
+      failwith "unexpected whencode in plus"
   | Ast.InitGccExt(designators,eq,ini) ->
       List.iter designator designators; pr_space();
       mcode print_string eq; pr_space(); initialiser nlcomma ini
@@ -629,10 +676,27 @@ and initialiser nlcomma i =
       ident name; mcode print_string eq; initialiser nlcomma ini
   | Ast.IComma(comma) ->
       mcode print_string comma;
-      if nlcomma then force_newline()
+      if nlcomma then force_newline() else pr_space()
+  | Ast.Idots(dots,Some whencode) ->
+      if generating
+      then
+       (mcode print_string dots;
+        print_text "   when != ";
+        initialiser nlcomma whencode)
+      else raise CantBeInPlus
+  | Ast.Idots(dots,None) ->
+      if generating
+      then mcode print_string dots
+      else raise CantBeInPlus
   | Ast.OptIni(ini) | Ast.UniqueIni(ini) ->
       raise CantBeInPlus
 
+and initialiser_list nlcomma = function
+  (* awkward, because the comma is separate from the initialiser *)
+    [] -> ()
+  | [x] -> initialiser false x
+  | x::xs -> initialiser nlcomma x; initialiser_list nlcomma xs
+
 and designator = function
     Ast.DesignatorField(dot,id) -> mcode print_string dot; ident id
   | Ast.DesignatorIndex(lb,exp,rb) ->
@@ -657,7 +721,11 @@ and parameterTypeDef p =
               pretty_print_c.Pretty_print_c.param p
           | _ -> raise Impossible)
   | Ast.MetaParamList(name,_,_,_) ->
-      failwith "not handling MetaParamList"
+      handle_metavar name
+       (function
+           Ast_c.MetaParamListVal p ->
+              pretty_print_c.Pretty_print_c.paramlist p
+          | _ -> raise Impossible)
 
   | Ast.PComma(cm) -> mcode print_string cm
   | Ast.Pdots(dots) | Ast.Pcircles(dots) when generating ->
@@ -706,7 +774,7 @@ and rule_elem arity re =
       end_block(); pr_arity arity; mcode print_string brace
 
   | Ast.ExprStatement(exp,sem) ->
-      pr_arity arity; expression exp; mcode print_string sem
+      pr_arity arity; print_option expression exp; mcode print_string sem
 
   | Ast.IfHeader(iff,lp,exp,rp) ->
       pr_arity arity;
@@ -764,6 +832,8 @@ and rule_elem arity re =
   | Ast.TopInit(init) -> initialiser false init
   | Ast.Include(inc,s) ->
       mcode print_string inc; print_text " "; mcode inc_file s
+  | Ast.Undef(def,id) ->
+      mcode print_string def; pr_space(); ident id
   | Ast.DefineHeader(def,id,params) ->
       mcode print_string def; pr_space(); ident id;
       print_define_parameters params
@@ -852,7 +922,7 @@ let rec statement arity s =
   | Ast.Iterator(header,body,(_,_,_,aft)) ->
       rule_elem arity header;
       indent_if_needed body (function _ -> statement arity body);
-      mcode (fun _ _ _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
+      mcode (fun _ _ _ -> ()) ((),Ast.no_info,aft,[])
 
   | Ast.Switch(header,lb,decls,cases,rb) ->
       rule_elem arity header; pr_space(); rule_elem arity lb;
@@ -933,7 +1003,7 @@ and case_line arity c =
 let top_level t =
   match Ast.unwrap t with
     Ast.FILEINFO(old_file,new_file) -> raise CantBeInPlus
-  | Ast.DECL(stmt) -> statement "" stmt
+  | Ast.NONDECL(stmt) -> statement "" stmt
   | Ast.CODE(stmt_dots) -> dots force_newline (statement "") stmt_dots
   | Ast.ERRORWORDS(exps) -> raise CantBeInPlus
 in
@@ -978,8 +1048,19 @@ let rec pp_any = function
 
   | Ast.ConstVolTag(x) -> const_vol x unknown unknown; false
   | Ast.Pragma(xs) ->
-      let print = function Ast.Noindent s | Ast.Indent s -> print_text s in
-      print_between force_newline print xs; false
+      (match xs with (Ast.Space s)::_ -> pr_space() | _ -> ());
+      let rec loop = function
+         [] -> ()
+       | [Ast.Noindent s] -> unindent false; print_text s
+       | [Ast.Indent s] -> print_text s
+       | (Ast.Space s) :: (((Ast.Indent _ | Ast.Noindent _) :: _) as rest) ->
+           print_text s; force_newline(); loop rest
+       | (Ast.Space s) :: rest -> print_text s; pr_space(); loop rest
+       | Ast.Noindent s :: rest ->
+           unindent false; print_text s; force_newline(); loop rest
+       | Ast.Indent s :: rest ->
+           print_text s; force_newline(); loop rest in
+      loop xs; false
   | Ast.Token(x,None) -> print_text x; if_open_brace x
   | Ast.Token(x,Some info) ->
       mcode
@@ -989,7 +1070,7 @@ let rec pp_any = function
          | _ -> ());
          print_string x line lcol)
        (let nomcodekind = Ast.CONTEXT(Ast.DontCarePos,Ast.NOTHING) in
-       (x,info,nomcodekind,Ast.NoMetaPos));
+       (x,info,nomcodekind,[]));
       if_open_brace x
 
   | Ast.Code(x) -> let _ = top_level x in false
@@ -1007,6 +1088,8 @@ let rec pp_any = function
   | Ast.SgrepEndTag(x) -> failwith "unexpected end tag"
 in
 
+(*Printf.printf "start of the function\n";*)
+
   anything := (function x -> let _ = pp_any x in ());
 
   (* todo? imitate what is in pretty_print_cocci ? *)
@@ -1028,7 +1111,10 @@ in
        then
          let hd = List.hd xxs in
          match hd with
-            (Ast.StatementTag s::_) when isfn s ->
+           (Ast.Pragma l::_)
+             when List.for_all (function Ast.Space x -> true | _ -> false) l ->
+               ()
+          | (Ast.StatementTag s::_) when isfn s ->
              force_newline(); force_newline()
          | (Ast.Pragma _::_)
           | (Ast.Rule_elemTag _::_) | (Ast.StatementTag _::_)
@@ -1073,10 +1159,12 @@ in
                  (match Ast.unwrap x with
                    Ast.IComma _ -> false
                  | _ -> true)
-             | Ast.Token(t,_) when List.mem t [",";";";"(";")"] -> false
+             | Ast.Token(t,_) when List.mem t [",";";";"(";")";".";"->"] ->
+                 false
              | _ -> true in
            let space_needed_after = function
-               Ast.Token(t,_) when List.mem t ["("] -> (*never needed*) false
+               Ast.Token(t,_)
+               when List.mem t ["(";".";"->"] -> (*never needed*) false
              | Ast.Token(t,_) when List.mem t ["if";"for";"while";"do"] ->
                  (* space always needed *)
                  pr_space(); false
@@ -1103,11 +1191,11 @@ in
       newline_after()
 
 let rec pp_list_list_any (envs, pr, pr_celem, pr_cspace, pr_space, pr_arity,
-                         pr_barrier, indent, unindent)
+                         pr_barrier, indent, unindent, eatspace)
     generating xxs before =
   List.iter
     (function env ->
       do_all (env, pr, pr_celem, pr_cspace, pr_space, pr_arity, pr_barrier,
-             indent, unindent)
+             indent, unindent, eatspace)
        generating xxs before)
     envs