coccinelle release 1.0.0-rc2
[bpt/coccinelle.git] / parsing_cocci / pretty_print_cocci.ml
index bf92cfb..74f792a 100644 (file)
@@ -1,3 +1,27 @@
+(*
+ * Copyright 2010, INRIA, University of Copenhagen
+ * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
+ * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
+ * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
+ * This file is part of Coccinelle.
+ *
+ * Coccinelle is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, according to version 2 of the License.
+ *
+ * Coccinelle is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Coccinelle.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The authors reserve the right to distribute this or future versions of
+ * Coccinelle under other licenses.
+ *)
+
+
 open Format
 module Ast = Ast_cocci
 
@@ -58,18 +82,21 @@ let print_around printer term = function
       print_anything "<<< " bef; printer term; print_anything ">>> " aft
 
 let print_string_befaft fn x info =
-  let print = function Ast.Noindent s | Ast.Indent s -> print_string s in
+  let print = function
+      Ast.Noindent s | Ast.Indent s | Ast.Space s -> print_string s in
   List.iter (function (s,_,_) -> print s; force_newline()) info.Ast.strbef;
   fn x;
   List.iter (function (s,_,_) -> force_newline(); print s) info.Ast.straft
 
 let print_meta (r,x) = print_string r; print_string ":"; print_string x
 
-let print_pos = function
-    Ast.MetaPos(name,_,_,_,_) ->
-      let name = Ast.unwrap_mcode name in
-      print_string "@"; print_meta name
-  | _ -> ()
+let print_pos l =
+  List.iter
+    (function
+       Ast.MetaPos(name,_,_,_,_) ->
+         let name = Ast.unwrap_mcode name in
+         print_string "@"; print_meta name)
+    l
 
 let mcode fn = function
     (x, _, Ast.MINUS(_,_,adj,plus_stream), pos) ->
@@ -77,7 +104,10 @@ let mcode fn = function
       then print_string (if !Flag.sgrep_mode2 then "*" else "-");
       fn x; print_pos pos;
       if !print_plus_flag
-      then print_anything ">>> " plus_stream
+      then
+       (match plus_stream with
+         Ast.NOREPLACEMENT -> ()
+       | Ast.REPLACEMENT(plus_stream,_) -> print_anything ">>> " plus_stream)
   | (x, _, Ast.CONTEXT(_,plus_streams), pos) ->
       if !print_plus_flag
       then
@@ -91,7 +121,9 @@ let mcode fn = function
 let print_mcodekind = function
     Ast.MINUS(_,_,_,plus_stream) ->
       print_string "MINUS";
-      print_anything ">>> " plus_stream
+      (match plus_stream with
+       Ast.NOREPLACEMENT -> ()
+      | Ast.REPLACEMENT(plus_stream,_) -> print_anything ">>> " plus_stream)
   | Ast.CONTEXT(_,plus_streams) ->
       print_around (function _ -> print_string "CONTEXT") () plus_streams
   | Ast.PLUS _ -> print_string "PLUS"
@@ -116,6 +148,23 @@ let nest_dots starter ender fn f d =
   end_block();
   mcode print_string ender
 
+(* --------------------------------------------------------------------- *)
+(* Disjunctions *)
+
+let print_disj_list fn l =
+  if !print_newlines_disj
+  then (force_newline(); print_string "("; force_newline())
+  else print_string "(";
+  print_between
+    (function _ ->
+      if !print_newlines_disj
+      then (force_newline(); print_string "|"; force_newline())
+      else print_string " | ")
+    fn l;
+  if !print_newlines_disj
+  then (force_newline(); print_string ")"; force_newline())
+  else print_string ")"
+
 (* --------------------------------------------------------------------- *)
 
 let print_type keep info = function
@@ -154,12 +203,13 @@ and regconstraint = function
 
 let rec ident i =
   match Ast.unwrap i with
-      Ast.Id(name) -> mcode print_string name
-    | Ast.MetaId(name,_,keep,inherited) -> mcode print_meta name
-    | Ast.MetaFunc(name,_,_,_) -> mcode print_meta name
-    | Ast.MetaLocalFunc(name,_,_,_) -> mcode print_meta name
-    | Ast.OptIdent(id) -> print_string "?"; ident id
-    | Ast.UniqueIdent(id) -> print_string "!"; ident id
+    Ast.Id(name) -> mcode print_string name
+  | Ast.MetaId(name,_,keep,inherited) -> mcode print_meta name
+  | Ast.MetaFunc(name,_,_,_) -> mcode print_meta name
+  | Ast.MetaLocalFunc(name,_,_,_) -> mcode print_meta name
+  | Ast.DisjId(id_list) -> print_disj_list ident id_list
+  | Ast.OptIdent(id) -> print_string "?"; ident id
+  | Ast.UniqueIdent(id) -> print_string "!"; ident id
 
 and print_unitary = function
     Type_cocci.Unitary -> print_string "unitary"
@@ -169,20 +219,6 @@ and print_unitary = function
 (* --------------------------------------------------------------------- *)
 (* Expression *)
 
-let print_disj_list fn l =
-  if !print_newlines_disj
-  then (force_newline(); print_string "("; force_newline())
-  else print_string "(";
-  print_between
-    (function _ ->
-      if !print_newlines_disj
-      then (force_newline(); print_string "|"; force_newline())
-      else print_string " | ")
-    fn l;
-  if !print_newlines_disj
-  then (force_newline(); print_string ")"; force_newline())
-  else print_string ")"
-
 let rec expression e =
   match Ast.unwrap e with
     Ast.Ident(id) -> ident id
@@ -252,6 +288,7 @@ let rec 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 "-"
@@ -348,8 +385,13 @@ 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; print_string " ";
-      ident name
+  | Ast.EnumName(kind,name) ->
+      mcode print_string kind;
+      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;
+      mcode print_string rb
   | Ast.StructUnionName(kind,name) ->
       mcode structUnion kind;
       print_option (function x -> ident x; print_string " ") name
@@ -370,6 +412,9 @@ and baseType = function
   | Ast.FloatType -> print_string "float "
   | Ast.LongType -> print_string "long "
   | Ast.LongLongType -> print_string "long long "
+  | 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 "
@@ -418,7 +463,9 @@ and print_named_type ty id =
 
 and declaration d =
   match Ast.unwrap d with
-    Ast.MetaDecl(name,_,_) | Ast.MetaField(name,_,_) -> mcode print_meta name
+    Ast.MetaDecl(name,_,_) | Ast.MetaField(name,_,_)
+  | Ast.MetaFieldList(name,_,_,_) ->
+      mcode print_meta name
   | Ast.Init(stg,ty,id,eq,ini,sem) ->
       print_option (mcode storage) stg; print_named_type ty id;
       print_string " "; mcode print_string eq;
@@ -448,8 +495,14 @@ and initialiser i =
   match Ast.unwrap i with
     Ast.MetaInit(name,_,_) ->
       mcode print_meta name; print_string " "
+  | Ast.MetaInitList(name,_,_,_) ->
+      mcode print_meta name; print_string " "
   | Ast.InitExpr(exp) -> expression exp
-  | Ast.InitList(allminus,lb,initlist,rb,whencode) ->
+  | Ast.ArInitList(lb,initlist,rb) ->
+      mcode print_string lb; open_box 0;
+      dots force_newline initialiser initlist; close_box();
+      mcode print_string rb
+  | Ast.StrInitList(allminus,lb,initlist,rb,whencode) ->
       mcode print_string lb; open_box 0;
       if not (whencode = [])
       then
@@ -465,6 +518,9 @@ and initialiser i =
   | Ast.InitGccName(name,eq,ini) ->
       ident name; mcode print_string eq; initialiser ini
   | Ast.IComma(comma) -> mcode print_string comma; force_newline()
+  | Ast.Idots(dots,Some whencode) ->
+      mcode print_string dots; print_string "   when != "; initialiser whencode
+  | Ast.Idots(dots,None) -> mcode print_string dots
   | Ast.OptIni(ini) -> print_string "?"; initialiser ini
   | Ast.UniqueIni(ini) -> print_string "!"; initialiser ini
 
@@ -500,13 +556,13 @@ and parameter_list l = dots (function _ -> ()) parameterTypeDef l
 let rec rule_elem arity re =
   match Ast.unwrap re with
     Ast.FunHeader(bef,allminus,fninfo,name,lp,params,rp) ->
-      mcode (function _ -> ()) ((),Ast.no_info,bef,Ast.NoMetaPos);
+      mcode (function _ -> ()) ((),Ast.no_info,bef,[]);
       print_string arity; List.iter print_fninfo fninfo;
       ident name; mcode print_string_box lp;
       parameter_list params; close_box(); mcode print_string rp;
       print_string " "
   | Ast.Decl(bef,allminus,decl) ->
-      mcode (function _ -> ()) ((),Ast.no_info,bef,Ast.NoMetaPos);
+      mcode (function _ -> ()) ((),Ast.no_info,bef,[]);
       print_string arity;
       declaration decl
   | Ast.SeqStart(brace) ->
@@ -516,7 +572,7 @@ let rec rule_elem arity re =
       if !print_newlines_disj then end_block();
       print_string arity; mcode print_string brace
   | Ast.ExprStatement(exp,sem) ->
-      print_string arity; expression exp; mcode print_string sem
+      print_string arity; print_option expression exp; mcode print_string sem
   | Ast.IfHeader(iff,lp,exp,rp) ->
       print_string arity;
       mcode print_string iff; print_string " "; mcode print_string_box lp;
@@ -574,6 +630,8 @@ let rec rule_elem arity re =
   | Ast.TopInit(init) -> initialiser init
   | Ast.Include(inc,s) ->
       mcode print_string inc; print_string " "; mcode inc_file s
+  | Ast.Undef(def,id) ->
+      mcode print_string def; print_string " "; ident id
   | Ast.DefineHeader(def,id,params) ->
       mcode print_string def; print_string " "; ident id;
       print_define_parameters params
@@ -616,23 +674,23 @@ and statement arity s =
       rule_elem arity rbrace
   | Ast.IfThen(header,branch,(_,_,_,aft)) ->
       rule_elem arity header; statement arity branch;
-      mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
+      mcode (function _ -> ()) ((),Ast.no_info,aft,[])
   | Ast.IfThenElse(header,branch1,els,branch2,(_,_,_,aft)) ->
       rule_elem arity header; statement arity branch1; print_string " ";
       rule_elem arity els; statement arity branch2;
-      mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
+      mcode (function _ -> ()) ((),Ast.no_info,aft,[])
   | Ast.While(header,body,(_,_,_,aft)) ->
       rule_elem arity header; statement arity body;
-      mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
+      mcode (function _ -> ()) ((),Ast.no_info,aft,[])
   | Ast.Do(header,body,tail) ->
       rule_elem arity header; statement arity body;
       rule_elem arity tail
   | Ast.For(header,body,(_,_,_,aft)) ->
       rule_elem arity header; statement arity body;
-      mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
+      mcode (function _ -> ()) ((),Ast.no_info,aft,[])
   | Ast.Iterator(header,body,(_,_,_,aft)) ->
       rule_elem arity header; statement arity body;
-      mcode (function _ -> ()) ((),Ast.no_info,aft,Ast.NoMetaPos)
+      mcode (function _ -> ()) ((),Ast.no_info,aft,[])
   | Ast.Switch(header,lb,decls,cases,rb) ->
       rule_elem arity header; rule_elem arity lb;
       dots force_newline (statement arity) decls;
@@ -772,7 +830,8 @@ let _ =
     | Ast.Token(x,Some info) -> print_string_befaft print_string x info
     | Ast.Token(x,None) -> print_string x
     | Ast.Pragma(xs) ->
-       let print = function Ast.Noindent s | Ast.Indent s -> print_string s in
+       let print = function
+           Ast.Noindent s | Ast.Indent s | Ast.Space s -> print_string s in
        print_between force_newline print xs
     | Ast.Code(x) -> let _ = top_level x in ()
     | Ast.ExprDotsTag(x) -> dots (function _ -> ()) expression x