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
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
| 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
| [] -> []
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 { *)
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
(* assert Origin;ExpandedTok; + Cocci + C (was AbstractLineTok)
* and no tag information, just NOTHING. *)
-
let toks =
if !Flag.sgrep_mode2
then