permit multiline comments and strings in macros
[bpt/coccinelle.git] / parsing_c / parse_c.ml
index 251e50d..e580944 100644 (file)
@@ -46,7 +46,7 @@ let mk_info_item2 filename toks =
         | Ast_c.OriginTok _ ->
             Buffer.add_string buf (TH.str_of_tok tok)
         | Ast_c.AbstractLineTok _ ->
-            raise Impossible
+            raise (Impossible 79)
         | _ -> ()
       );
       Buffer.contents buf
@@ -123,17 +123,25 @@ let commentized xs = xs +> Common.tail_map_filter (function
             ||
             (s =~ "__.*")
         | 3 ->
-            List.mem cppkind [Token_c.CppAttr;Token_c.CppPassingNormal;Token_c.CppDirective]
+           (match cppkind with
+             Token_c.CppAttr | Token_c.CppPassingNormal
+            | Token_c.CppDirective | Token_c.CppIfDirective _ -> true
+           | _ -> false)
             ||
             (s =~ "__.*")
         | 4 ->
-            List.mem cppkind [Token_c.CppAttr;Token_c.CppPassingNormal;Token_c.CppMacro]
+            List.mem cppkind
+             [Token_c.CppAttr;Token_c.CppPassingNormal;Token_c.CppMacro]
             ||
             (s =~ "__.*")
 
 
         | 5 ->
-            List.mem cppkind [Token_c.CppAttr;Token_c.CppPassingNormal;Token_c.CppDirective;Token_c.CppMacro]
+           (match cppkind with
+             Token_c.CppAttr | Token_c.CppPassingNormal
+            | Token_c.CppDirective | Token_c.CppIfDirective _
+            | Token_c.CppMacro -> true
+           | _ -> false)
             ||
             (s =~ "__.*")
 
@@ -350,7 +358,7 @@ let parse_gen parsefunc s =
         !cur_tok
     )
   in
-  let lexbuf_fake = Lexing.from_function (fun buf n -> raise Impossible) in
+  let lexbuf_fake = Lexing.from_function (fun buf n -> raise (Impossible 80)) in
   let result = parsefunc lexer_function lexbuf_fake in
   result
 
@@ -610,7 +618,6 @@ let rec lexer_function ~pass tr = fun lexbuf ->
 
 let max_pass = 4
 
-
 let get_one_elem ~pass tr (file, filelines) =
 
   if not (LP.is_enabled_typedef()) && !Flag_parsing_c.debug_typedef
@@ -627,7 +634,7 @@ let get_one_elem ~pass tr (file, filelines) =
 
   tr.passed <- [];
 
-  let lexbuf_fake = Lexing.from_function (fun buf n -> raise Impossible) in
+  let lexbuf_fake = Lexing.from_function (fun buf n -> raise (Impossible 81)) in
 
   (try
       (* -------------------------------------------------- *)
@@ -834,6 +841,7 @@ let parse_print_error_heuristic2 saved_typedefs saved_macros file =
   (* -------------------------------------------------- *)
   (* call lexer and get all the tokens *)
   (* -------------------------------------------------- *)
+
   LP.lexer_reset_typedef saved_typedefs;
   Parsing_hacks.ifdef_paren_cnt := 0;
 
@@ -997,7 +1005,7 @@ let parse_print_error_heuristic2 saved_typedefs saved_macros file =
                   pr2 ("parse error \n = " ^ error_msg_tok cur)
               | Semantic_c.Semantic (s, i) ->
                   pr2 ("semantic error " ^s^ "\n ="^ error_msg_tok cur)
-              | e -> raise Impossible
+              | e -> raise (Impossible 82)
               );
               (* bugfix: *)
               if (checkpoint_file =$= checkpoint2_file) &&
@@ -1061,7 +1069,7 @@ let parse_cache file =
   if not !Flag_parsing_c.use_cache
   then parse_print_error_heuristic None None file
   else
-  let _ = pr2 "TOFIX" in
+  let _ = pr2_once "TOFIX: use_cache is not sensitive to changes in the considered macros, include files, etc" in
   let need_no_changed_files =
     (* should use Sys.argv.(0), would be safer. *)
 
@@ -1074,16 +1082,35 @@ let parse_cache file =
       *)
       !Config.std_h;
       *)
-    ]
-  in
+    ] in
   let need_no_changed_variables =
     (* could add some of the flags of flag_parsing_c.ml *)
-    []
-  in
-  Common.cache_computation_robust
-    file ".ast_raw"
+    [] in
+  Common.cache_computation_robust_in_dir
+    !Flag_parsing_c.cache_prefix file ".ast_raw"
     (need_no_changed_files, need_no_changed_variables) ".depend_raw"
-    (fun () -> parse_print_error_heuristic None None file)
+    (fun () ->
+      (* check whether to clear the cache *)
+      (match (!Flag_parsing_c.cache_limit,!Flag_parsing_c.cache_prefix) with
+       (None,_) | (_,None) -> ()
+      |        (Some limit,Some prefix) ->
+         let count =
+           Common.cmd_to_list
+             (Printf.sprintf "test -e %s && find %s -name \"*_raw\" | wc -l"
+                prefix prefix) in
+         match count with
+           [c] ->
+             if int_of_string c >= limit
+             then
+               let _ =
+                 Sys.command
+                   (Printf.sprintf
+                      "find %s -name \"*_raw\" -exec /bin/rm {} \\;"
+                      prefix) in
+               ()
+         | _ -> ());
+      (* recompute *)
+      parse_print_error_heuristic None None file)