[Cocci] unreadable files

2014-08-05 Thread Akos PASZTORY
Currently if coccinelle meets a file that it cannot open (e.g. it's an
dangling symlink), it exits with:
Fatal error: exception Sys_error(/tmp/aa/what.c: No such file or directory)
even if run in recursive mode (i.e. passing a directory as argument).

Could you please add a command line option to just skip those files
and keep running?  (like make -k)
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] unreadable files

2014-08-05 Thread Julia Lawall
On Tue, 5 Aug 2014, Akos PASZTORY wrote:

 Currently if coccinelle meets a file that it cannot open (e.g. it's an
 dangling symlink), it exits with:
 Fatal error: exception Sys_error(/tmp/aa/what.c: No such file or directory)
 even if run in recursive mode (i.e. passing a directory as argument).

 Could you please add a command line option to just skip those files
 and keep running?  (like make -k)

Do you really want a command line argument for that?  I think it could
just be the default behavior?  Already, Coccinelle doesn't process code
that it can't parse, so in general there is no guarantee that all code in
a file or directory is processed.  It could give a warning on standard
error if it encounters this problem.

julia
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] unreadable files

2014-08-05 Thread Julia Lawall
Could you try the following patch?

julia

diff --git a/commons/common.ml b/commons/common.ml
index 9914c44..a8ee414 100644
--- a/commons/common.ml
+++ b/commons/common.ml
@@ -3505,6 +3505,19 @@ let (with_open_infile: filename - ((in_channel) - 'a) 
- 'a) = fun file f -
 res)
 (fun e - close_in chan)

+let (safe_with_open_infile: 'a - filename - ((in_channel) - 'a) - 'a) =
+  fun default file f -
+  let chan = try Some (open_in file) with _ - None in
+  match chan with
+Some chan -
+  unwind_protect (fun () -
+   let res = f chan in
+   close_in chan;
+   res)
+   (fun e - close_in chan)
+  | None -
+  pr2 (Failed to open ^file);
+  default

 let (with_open_outfile_append: filename - (((string - unit) * out_channel) 
- 'a) - 'a) =
  fun file f -
diff --git a/commons/common.mli b/commons/common.mli
index f20d66f..2cef883 100644
--- a/commons/common.mli
+++ b/commons/common.mli
@@ -1128,6 +1128,8 @@ val with_open_outfile :
   filename - ((string - unit) * out_channel - 'a) - 'a
 val with_open_infile :
   filename - (in_channel - 'a) - 'a
+val safe_with_open_infile :
+  'a - filename - (in_channel - 'a) - 'a
 val with_open_outfile_append :
   filename - ((string - unit) * out_channel - 'a) - 'a

diff --git a/parsing_c/parse_c.ml b/parsing_c/parse_c.ml
index aabd57c..e322f3a 100644
--- a/parsing_c/parse_c.ml
+++ b/parsing_c/parse_c.ml
@@ -242,7 +242,7 @@ let print_commentized xs =
 let tokens2 file =
  let table = Common.full_charpos_to_pos_large file in

- Common.with_open_infile file (fun chan -
+ Common.safe_with_open_infile [] file (fun chan -
   let lexbuf = Lexing.from_channel chan in
   try
 let rec tokens_aux acc =
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] unreadable files

2014-08-05 Thread Julia Lawall
Hmm, that patch ay not be enough...

julia

On Tue, 5 Aug 2014, Akos PASZTORY wrote:

 Currently if coccinelle meets a file that it cannot open (e.g. it's an
 dangling symlink), it exits with:
 Fatal error: exception Sys_error(/tmp/aa/what.c: No such file or directory)
 even if run in recursive mode (i.e. passing a directory as argument).

 Could you please add a command line option to just skip those files
 and keep running?  (like make -k)
 ___
 Cocci mailing list
 Cocci@systeme.lip6.fr
 https://systeme.lip6.fr/mailman/listinfo/cocci

___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] unreadable files

2014-08-05 Thread Julia Lawall
A better patch is below.

julia

diff --git a/cocci.ml b/cocci.ml
index e67f5ba..5fc6ed0 100644
--- a/cocci.ml
+++ b/cocci.ml
@@ -516,7 +516,13 @@ let worth_trying2 cfiles (tokens,_,query,_) =
   res

 let worth_trying a b  =
-  Common.profile_code worth_trying (fun () - worth_trying2 a b)
+  Common.profile_code worth_trying (fun () -
+try worth_trying2 a b
+with Flag.UnreadableFile file -
+  begin
+   pr2 (Skipping unreadable file:  ^ file);
+   false
+  end)

 let check_macro_in_sp_and_adjust = function
 None - ()
@@ -1134,14 +1140,23 @@ let fixpath s =

 let rec prepare_h seen env (hpath : string) choose_includes parse_strings
 : file_info list =
-  if not (Common.lfile_exists hpath)
-  then
-begin
-  pr2_once (TYPE: header  ^ hpath ^  not found);
-  []
-end
-  else
-begin
+  let h_cs =
+if not (Common.lfile_exists hpath)
+then
+  begin
+   pr2_once (TYPE: header  ^ hpath ^  not found);
+None
+  end
+else
+  try Some (cprogram_of_file_cached parse_strings hpath)
+  with Flag.UnreadableFile file -
+   begin
+ pr2_once (TYPE: header  ^ hpath ^  not readable);
+ None
+   end in
+  match h_cs with
+None - []
+  | Some h_cs -
   let h_cs = cprogram_of_file_cached parse_strings hpath in
   let local_includes =
if choose_includes =*= Flag_cocci.I_REALLY_ALL_INCLUDES
@@ -1171,11 +1186,20 @@ let rec prepare_h seen env (hpath : string) 
choose_includes parse_strings
fpath = hpath;
fkind = Header;
   }]
-end

 let prepare_c files choose_includes parse_strings : file_info list =
-  let cprograms = List.map (cprogram_of_file_cached parse_strings) files in
-  let includes = includes_to_parse (zip files cprograms) choose_includes in
+  let files_and_cprograms =
+List.rev
+  (List.fold_left
+(function prev -
+  function file -
+try (file,cprogram_of_file_cached parse_strings file) :: prev
+with Flag.UnreadableFile file -
+  pr2_once (C file  ^ file ^  not readable);
+  prev)
+[] files) in
+  let (files,cprograms) = List.split files_and_cprograms in
+  let includes = includes_to_parse files_and_cprograms choose_includes in
   let seen = ref includes in

   (* todo?: may not be good to first have all the headers and then all the c *)
diff --git a/globals/flag.ml b/globals/flag.ml
index e77c599..d04ea86 100644
--- a/globals/flag.ml
+++ b/globals/flag.ml
@@ -44,3 +44,5 @@ let ibm = ref false

 (* was in main *)
 let include_headers = ref false
+
+exception UnreadableFile of string
diff --git a/parsing_c/parse_c.ml b/parsing_c/parse_c.ml
index aabd57c..972579b 100644
--- a/parsing_c/parse_c.ml
+++ b/parsing_c/parse_c.ml
@@ -888,7 +888,8 @@ let with_program2_unit f program2 =
 let parse_print_error_heuristic2 saved_typedefs saved_macros parse_strings
 file =

-  let filelines = Common.cat_array file in
+  let filelines =
+try Common.cat_array file with _ - raise (Flag.UnreadableFile file) in
   let stat = Parsing_stat.default_stat file in

   (* -- *)
diff --git a/parsing_cocci/cocci_grep.ml b/parsing_cocci/cocci_grep.ml
index 1b9e087..2e0ef00 100644
--- a/parsing_cocci/cocci_grep.ml
+++ b/parsing_cocci/cocci_grep.ml
@@ -6,7 +6,7 @@ let interpret_clause re l =
   with Not_found - false

 let interpret (big_regexp,regexps) file =
-  let i = open_in file in
+  let i = try open_in file with _ - raise (Flag.UnreadableFile file) in
   let simple = match regexps with [_] - true | _ - false in
   let rec loop big_regexp simple regexps =
 let l = input_line i in
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] [PATCH] Coccinelle: Script to replace if and BUG with BUG_ON

2014-08-05 Thread Michal Marek
On 2014-07-20 23:36, Himangi Saraogi wrote:
 This script detects cases where BUG() follows an if condition on an
 expression and replaces the if condition and BUG() with a BUG_ON having
 the conditional expression of the if statement as argument.
 
 Signed-off-by: Himangi Saraogi himangi...@gmail.com
 Acked-by: Julia Lawall julia.law...@lip6.fr

Is this a result of some internal review, or where does Julia's Acked-by
come from? I'm just wondering, because I haven't seen any previous
version of this patch.

Thanks,
Michal
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


[Cocci] Goto Directionality?

2014-08-05 Thread Mitchell Cuddie
Hi Julia,

I am attempting to create a patch to identify the direction of a goto
statement.(i.e. label before/after goto)

but the patch matches both?

In my eyes a should match but b should not

Cheers,

Mitchell


@r @
identifier i;
position p;
@@
* i@p:
...
* goto i;

diff = 
--- test.c
+++ /tmp/cocci-output-22798-0e39f1-test.c
@@ -7,15 +7,11 @@ main()
int b = 0xF0F0F0F0;
void *a = b;
size_t c = (size_t)a;
-a:
printf(%PRIxPTR\n, (uintptr_t)a);
-goto b;
printf(%zu\n, c);  // prints as unsigned decimal
printf(%zx\n, c);  // prints as hex
printf(%zd\n, c);  // prints as signed decimal
-b:
printf(%%\n);
-goto a;
printf(%% %s, cat\n);
 
return 0;
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] Goto Directionality?

2014-08-05 Thread Julia Lawall


On Tue, 5 Aug 2014, Mitchell Cuddie wrote:

 Hi Julia,
 
 I am attempting to create a patch to identify the direction of a goto
 statement.(i.e. label before/after goto)
 
 but the patch matches both?
 
 In my eyes a should match but b should not
 
 Cheers,
 
 Mitchell
 
 
 @r @
 identifier i;
 position p;
 @@
 * i@p:
 ...
 * goto i;

... follows the possible execution paths in the program.  It has no notion 
of up or down.  b and goto b are matched because of the goto a, which 
makes a loop around the b code.

Perhaps just taking into account the line number of the label and the goto 
is good enough?  There could be problems for the case where there is a 
goto from one if branch to another, but that seems like quite unpleasant 
code.

julia

 diff = 
 --- test.c
 +++ /tmp/cocci-output-22798-0e39f1-test.c
 @@ -7,15 +7,11 @@ main()
 int b = 0xF0F0F0F0;
 void *a = b;
 size_t c = (size_t)a;
 -a:
 printf(%PRIxPTR\n, (uintptr_t)a);
 -goto b;
 printf(%zu\n, c);  // prints as unsigned decimal
 printf(%zx\n, c);  // prints as hex
 printf(%zd\n, c);  // prints as signed decimal
 -b:
 printf(%%\n);
 -goto a;
 printf(%% %s, cat\n);
  
 return 0;
 ___
 Cocci mailing list
 Cocci@systeme.lip6.fr
 https://systeme.lip6.fr/mailman/listinfo/cocci
 
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci