Coccinelle release 0.2.5-rc8
[bpt/coccinelle.git] / parsing_c / unparse_c.ml
index 82eda1a..8472aba 100644 (file)
@@ -725,6 +725,11 @@ let is_ident_like s = s ==~ Common.regexp_alpha
 let rec drop_space_at_endline = function
     [] -> []
   | [x] -> [x]
+  | (C2 " ") ::
+    ((((T2(Parser_c.TCommentSpace _,Ctx,_i)) |
+    (T2(Parser_c.TCommentNewline _,Ctx,_i))) :: _) as rest) ->
+      (* when unparse_cocci doesn't know whether space is needed *)
+      drop_space_at_endline rest
   | ((T2(Parser_c.TCommentSpace _,Ctx,_i)) as a)::rest ->
       let (outer_spaces,rest) = Common.span is_space rest in
       let minus_or_comment_or_space_nocpp = function
@@ -765,7 +770,13 @@ let rec add_space xs =
           something should be done to add newlines too, rather than
           printing them explicitly in unparse_cocci. *)
        x::C2 (String.make (lcoly-rcolx) ' ')::add_space (y::xs)
-  | x::y::xs ->
+  | ((T2(_,Ctx,_)) as x)::((Cocci2 _) as y)::xs -> (* add space on boundary *)
+      let sx = str_of_token2 x in
+      let sy = str_of_token2 y in
+      if is_ident_like sx && (is_ident_like sy or List.mem sy ["="])
+      then x::C2 " "::(add_space (y::xs))
+      else x::(add_space (y::xs))
+  | x::y::xs -> (* not boundary, not sure if it is possible *)
       let sx = str_of_token2 x in
       let sy = str_of_token2 y in
       if is_ident_like sx && is_ident_like sy
@@ -923,6 +934,15 @@ let rec adjust_indentation xs =
     | x::xs -> find_first_tab started xs in
   find_first_tab false xs;
 
+  let rec balanced ct = function
+      [] -> ct >= 0
+    | ((T2(tok,_,_)) as x)::xs ->
+       (match str_of_token2 x with
+         "(" -> balanced (ct+1) xs
+       | ")" -> balanced (ct-1) xs
+       | _ -> balanced ct xs)
+    | x::xs -> balanced ct xs in
+
   let rec aux started xs =
     match xs with
     | [] ->  []
@@ -932,7 +952,8 @@ let rec adjust_indentation xs =
       when started && str_of_token2 x =$= ")" ->
        (* to be done for if, etc, but not for a function header *)
        x::(C2 " ")::a::(aux started xs)
-    | ((T2 (Parser_c.TCommentNewline s, _, _)) as x)::xs ->
+    | ((T2 (Parser_c.TCommentNewline s, _, _)) as x)::xs
+      when balanced 0 (fst(Common.span (function x -> not(is_newline x)) xs)) ->
        let old_tabbing = !_current_tabbing in
         str_of_token2 x +> new_tabbing +> (fun s -> _current_tabbing := s);
        (* only trust the indentation after the first { *)
@@ -1124,7 +1145,6 @@ let pp_program2 xs outfile  =
       match ppmethod with
       | PPnormal ->
           (* now work on tokens *)
-
           (* phase1: just get all the tokens, all the information *)
           assert(toks_e +> List.for_all (fun t ->
            TH.is_origin t or TH.is_expanded t
@@ -1137,7 +1157,6 @@ let pp_program2 xs outfile  =
           (* assert Origin;ExpandedTok; + Cocci + C (was AbstractLineTok)
            * and no tag information, just NOTHING. *)
 
-
          let toks =
            if !Flag.sgrep_mode2
            then