~ubuntu-branches/ubuntu/trusty/coccinelle/trusty

« back to all changes in this revision

Viewing changes to parsing_c/unparse_c.ml

  • Committer: Package Import Robot
  • Author(s): Євгеній Мещеряков
  • Date: 2012-08-19 20:40:52 UTC
  • mfrom: (7.2.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120819204052-8cujknwy6cn8a6h6
Tags: 1.0.0~rc15.deb-1
* New upstream RC 
  - Do not build-depend on libsexplib-camlp4-dev and libextlib-ocaml-dev
    anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
561
561
 
562
562
(* don't touch ifdefs, done after *)
563
563
let set_minus_comment_or_plus adj = function
564
 
    T2(Parser_c.TCommentCpp (Token_c.CppIfDirective _, _),_,_)
565
 
  | Cocci2 _ | C2 _ | Comma _ | Indent_cocci2 | Unindent_cocci2 _
 
564
    Cocci2 _ | C2 _ | Comma _ | Indent_cocci2 | Unindent_cocci2 _
566
565
  | EatSpace2 as x -> x
567
566
  | x -> set_minus_comment adj x
568
567
 
748
747
    | rest -> adjust_before_brace rest in
749
748
 
750
749
  let xs = List.rev (from_newline (List.rev xs)) in
 
750
 
 
751
  let cleanup_ifdefs toks =
 
752
  (* TODO: these functions are horrid, but using tokens caused circularity *)
 
753
    let is_ifdef = function
 
754
        T2((Parser_c.TCommentCpp
 
755
              (Token_c.CppIfDirective Token_c.IfDef, _)),m,idx) -> true
 
756
      | T2((Parser_c.TCommentCpp
 
757
              (Token_c.CppIfDirective Token_c.IfDef0, _)),m,idx) -> true
 
758
      | t -> false in
 
759
    let is_else = function
 
760
        T2((Parser_c.TCommentCpp
 
761
              (Token_c.CppIfDirective Token_c.Else, _)),m,idx) -> true
 
762
      | _ -> false in
 
763
    let is_endif = function
 
764
        T2((Parser_c.TCommentCpp
 
765
              (Token_c.CppIfDirective Token_c.Endif, _)),m,idx) -> true
 
766
      | _ -> false in
 
767
    let add t = function
 
768
        l::rest -> (t::l)::rest
 
769
      | _ -> failwith "not possible" in
 
770
    let rec parse_ifdef acc_keywords acc_code stack = function
 
771
        [] -> (None,acc_keywords,acc_code)
 
772
      | t::rest when is_else t ->
 
773
          (match stack with
 
774
            [] -> parse_ifdef (t::acc_keywords) ([]::acc_code) stack rest
 
775
          | _ -> parse_ifdef acc_keywords (add t acc_code) stack rest)
 
776
      | t::rest when is_endif t ->
 
777
          (match stack with
 
778
            [] -> ((Some (t,rest)),acc_keywords,acc_code)
 
779
          | _::stack -> parse_ifdef acc_keywords (add t acc_code) stack rest)
 
780
      | t::rest when is_ifdef t ->
 
781
          parse_ifdef acc_keywords (add t acc_code) (()::stack) rest
 
782
      | t::rest -> parse_ifdef acc_keywords (add t acc_code) stack rest in
 
783
    let unminus = function
 
784
        T2 (t,Min adj,idx) -> T2 (t,Ctx,idx)
 
785
      | x -> x in
 
786
    let is_minus = function
 
787
        T2 (t,Min adj,idx) -> true
 
788
      | x -> false in
 
789
    let rec loop = function
 
790
        [] -> []
 
791
      | t::rest when is_ifdef t ->
 
792
          let (ender,acc_keywords,acc_code) =
 
793
            parse_ifdef [t] [[]] [] rest in
 
794
          let acc_code = List.map loop acc_code in
 
795
          let merge = (* args reversed *)
 
796
            List.fold_left2
 
797
              (fun prev kwd code -> kwd :: (List.rev code) @ prev)
 
798
              [] in
 
799
          (match ender with
 
800
            None -> merge (List.map unminus acc_keywords) acc_code
 
801
          | Some(endif,rest) ->
 
802
              let rest = loop rest in
 
803
              if List.for_all is_minus (endif::acc_keywords)
 
804
              then (merge acc_keywords acc_code) @ (endif :: rest)
 
805
              else
 
806
                (merge (List.map unminus acc_keywords) acc_code) @
 
807
                ((unminus endif) :: rest))
 
808
      | x::xs -> x :: loop xs in
 
809
    loop toks in
 
810
      
 
811
  let xs = cleanup_ifdefs xs in
751
812
  let xs = drop_minus xs in
752
813
  xs
753
814
 
754
 
let cleanup_ifdefs toks =
755
 
  (* TODO: these functions are horrid, but using tokens caused circularity *)
756
 
  let is_ifdef0 = function
757
 
      T2((Parser_c.TCommentCpp
758
 
            (Token_c.CppIfDirective Token_c.IfDef0, _)),m,idx) -> true
759
 
    | _ -> false in
760
 
  let is_ifdef = function
761
 
      T2((Parser_c.TCommentCpp
762
 
            (Token_c.CppIfDirective Token_c.IfDef, _)),m,idx) -> true
763
 
    | t -> is_ifdef0 t in
764
 
  let is_endif = function
765
 
      T2((Parser_c.TCommentCpp
766
 
            (Token_c.CppIfDirective Token_c.Endif, _)),m,idx) -> true
767
 
    | _ -> false in
768
 
  (* only real comments *)
769
 
let is_whitespace_or_comment if0 = function
770
 
  | (T2 (t,_b,_i)) ->
771
 
      (match t with
772
 
      | Parser_c.TComment _ -> false (* previous pass chooses comments *)
773
 
      | Parser_c.TCommentSpace _ -> true  (* only whitespace *)
774
 
      | Parser_c.TCommentNewline _ (* newline plus whitespace *)
775
 
      | Parser_c.TCommentCpp (Token_c.CppIfDirective Token_c.Else, _) -> true
776
 
      | Parser_c.TCommentCpp (Token_c.CppPassingNormal, _) -> if0
777
 
      | t -> false
778
 
      )
779
 
  | _ -> false in
780
 
  let rec loop prev if0s = function
781
 
      [] -> prev
782
 
    | t::rest when is_ifdef0 t -> loop (t::prev) (true::if0s) rest
783
 
    | t::rest when is_ifdef t -> loop (t::prev) (false::if0s) rest
784
 
    | t::rest when is_endif t ->
785
 
        (match if0s with
786
 
          [] -> loop (t::prev) if0s rest
787
 
        | tgt::if0s ->
788
 
            let (spaces,prest) =
789
 
              Common.span (is_whitespace_or_comment tgt) prev in
790
 
            (match prest with
791
 
              t1::prest when is_ifdef t1 -> loop prest if0s rest
792
 
            | _ -> loop (t::prev) [] rest)) (* failed, so drop if0 info *)
793
 
    | x::xs -> loop (x::prev) if0s xs in
794
 
  List.rev (loop [] [] toks)
795
 
 
796
815
(* things that should not be followed by space - boundary between SmPL
797
816
   code and C code *)
798
817
let adjust_eat_space toks =
1345
1364
              let toks = paren_to_space toks in
1346
1365
              let toks = drop_end_comma toks in
1347
1366
              let toks = remove_minus_and_between_and_expanded_and_fake toks in
1348
 
              let toks = cleanup_ifdefs toks in
1349
1367
              (* assert Origin + Cocci + C and no minus *)
1350
1368
              let toks = add_space toks in
1351
1369
              let toks = add_newlines toks tu in