permit multiline comments and strings in macros
[bpt/coccinelle.git] / parsing_c / flag_parsing_c.ml
CommitLineData
34e49164 1(*****************************************************************************)
708f4980 2(* convenient globals. *)
34e49164 3(*****************************************************************************)
ae4735db 4let path = ref
485bce71
C
5 (try (Sys.getenv "YACFE_HOME")
6 with Not_found-> "/home/pad/c-yacfe"
7 )
708f4980
C
8
9(*****************************************************************************)
10(* macros *)
11(*****************************************************************************)
12
ae4735db
C
13let macro_dir = "config/macros/"
14let mk_macro_path ~cocci_path file =
15 Filename.concat cocci_path (macro_dir ^ file)
708f4980
C
16
17
18(* to pass to parse_c.init_defs *)
19let std_h = ref (mk_macro_path ~cocci_path:!path "standard.h")
20let common_h = ref (mk_macro_path ~cocci_path:!path "common_macros.h")
485bce71 21
34e49164 22
ae4735db 23let cmdline_flags_macrofile () =
34e49164 24 [
17ba0788 25 "--macro-file-builtins", Arg.Set_string std_h,
34e49164 26 " <file> (default=" ^ !std_h ^ ")";
485bce71
C
27 ]
28
29
30(*****************************************************************************)
31(* used only by cpp_ast_c, not by the parser *)
32(*****************************************************************************)
33let cpp_i_opts = ref []
34let cpp_d_opts = ref []
35
36let cmdline_flags_cpp () = [
37 "-D", Arg.String (fun s -> Common.push2 s cpp_d_opts),
38 " <x=y>";
39 "-I", Arg.String (fun s -> Common.push2 s cpp_i_opts),
40 " <dir>"
34e49164
C
41 ]
42
91eba41f
C
43(*****************************************************************************)
44(* types *)
45(*****************************************************************************)
46let std_envir = ref (Filename.concat !path "config/envos/environment_splint.h")
47
ae4735db 48let cmdline_flags_envfile () =
91eba41f 49 [
17ba0788 50 "--env-file", Arg.Set_string std_envir,
91eba41f
C
51 " <file> (default=" ^ !std_envir ^ ")";
52 ]
53
708f4980
C
54(*****************************************************************************)
55(* show *)
56(*****************************************************************************)
57
58let show_parsing_error = ref true
91eba41f 59
34e49164
C
60(*****************************************************************************)
61(* verbose *)
62(*****************************************************************************)
63
64let verbose_lexing = ref true
65let verbose_parsing = ref true
66let verbose_type = ref true
708f4980 67let verbose_cfg = ref true
91eba41f 68let verbose_annotater = ref true
708f4980
C
69let verbose_unparsing = ref true
70let verbose_visit = ref true
71let verbose_cpp_ast = ref true
34e49164
C
72
73let filter_msg = ref false
485bce71
C
74let filter_msg_define_error = ref false
75
34e49164 76let filter_define_error = ref false
485bce71
C
77
78let filter_passed_level = ref 0
34e49164
C
79
80let pretty_print_type_info = ref false
0708f913 81let pretty_print_comment_info = ref false
708f4980 82let pretty_print_typedef_value = ref false
34e49164
C
83
84(* cocci specific *)
85let show_flow_labels = ref true
86
87
ae4735db 88let cmdline_flags_verbose () =
34e49164 89 [
17ba0788
C
90 "--no-verbose-parsing", Arg.Clear verbose_parsing , " ";
91 "--no-verbose-lexing", Arg.Clear verbose_lexing , " ";
92 "--no-verbose-annotater", Arg.Clear verbose_annotater , " ";
91eba41f 93
17ba0788
C
94 "--no-parse-error-msg", Arg.Clear verbose_parsing, " ";
95 "--no-type-error-msg", Arg.Clear verbose_type, " ";
ae4735db
C
96
97
17ba0788 98 "--filter-msg", Arg.Set filter_msg ,
34e49164 99 " filter some cpp message when the macro is a \"known\" cpp construct";
17ba0788 100 "--filter-msg-define-error",Arg.Set filter_msg_define_error,
485bce71
C
101 " filter the error msg";
102
17ba0788 103 "--filter-define-error",Arg.Set filter_define_error,
485bce71 104 " filter the error, which will not be added in the stat";
17ba0788 105 "--filter-passed-level",Arg.Set_int filter_passed_level," ";
34e49164
C
106 ]
107
108
109(*****************************************************************************)
110(* debugging *)
111(*****************************************************************************)
112
113let debug_lexer = ref false
114let debug_etdt = ref false
115let debug_typedef = ref false
116let debug_cpp = ref false
117
708f4980
C
118let debug_cpp_ast = ref false
119
34e49164
C
120let debug_unparsing = ref false
121
122let debug_cfg = ref false
123
124(* "debug C parsing/unparsing", "" *)
ae4735db 125let cmdline_flags_debugging () =
34e49164 126 [
17ba0788
C
127 "--debug-cpp", Arg.Set debug_cpp, " ";
128 "--debug-lexer", Arg.Set debug_lexer , " ";
129 "--debug-etdt", Arg.Set debug_etdt , " ";
130 "--debug-typedef", Arg.Set debug_typedef, " ";
34e49164 131
17ba0788
C
132 "--debug-cfg", Arg.Set debug_cfg , " ";
133 "--debug-unparsing", Arg.Set debug_unparsing, " ";
34e49164
C
134 ]
135
91eba41f
C
136(*****************************************************************************)
137(* checks *)
138(*****************************************************************************)
139
140let check_annotater = ref true
ae4735db 141let cmdline_flags_checks () =
91eba41f 142 [
17ba0788
C
143 "--disable-check-annotater", Arg.Clear check_annotater, " ";
144 "--enable-check-annotater", Arg.Set check_annotater, " ";
91eba41f
C
145 ]
146
34e49164
C
147(*****************************************************************************)
148(* change algo *)
149(*****************************************************************************)
150
34e49164
C
151(* cocci specific *)
152let label_strategy_2 = ref false
153
154let cmdline_flags_algos () =
155 [
17ba0788 156 "--l1", Arg.Clear label_strategy_2, " ";
485bce71
C
157 ]
158
159(*****************************************************************************)
160(* Disable parsing feature (for CC09 and also to see if useful) *)
161(*****************************************************************************)
162
163let cpp_directive_passing = ref false
ae4735db 164let ifdef_directive_passing = ref false
485bce71 165
708f4980 166let disable_multi_pass = ref false
485bce71
C
167let disable_add_typedef = ref false
168
169let if0_passing = ref true
170let add_typedef_root = ref true
171
7f339edd
C
172(* defined and undefined constants *)
173let add c s = c := (Str.split (Str.regexp ",") s) @ !c
174let defined = ref ([] : string list)
175let undefined = ref ([] : string list)
176
485bce71
C
177let cmdline_flags_parsing_algos () = [
178
17ba0788 179 "--directive-passing", Arg.Set cpp_directive_passing,
485bce71 180 " pass most cpp directives, especially when inside function";
17ba0788 181 "--ifdef-passing", Arg.Set ifdef_directive_passing,
485bce71
C
182 " pass ifdef directives ";
183
17ba0788 184 "--noif0-passing", Arg.Clear if0_passing,
34e49164 185 " ";
17ba0788
C
186 "--noadd-typedef-root", Arg.Clear add_typedef_root, " ";
187 "--noadd-typedef", Arg.Set disable_add_typedef, " ";
34e49164 188
17ba0788 189 "--disable-multi-pass", Arg.Set disable_multi_pass, " ";
485bce71 190]
34e49164
C
191
192(*****************************************************************************)
193(* other *)
194(*****************************************************************************)
195
196(* for compare_c *)
197let diff_lines = ref (None : string option) (* number of lines of context *)
198
199(* for parse_c *)
200let use_cache = ref false
5427db06 201let cache_prefix = ref (None : string option)
f3c4ece6 202let cache_limit = ref (None : int option)
34e49164 203
ae4735db 204let cmdline_flags_other () =
34e49164 205 [
ae4735db 206 "-U", Arg.Int (fun n -> diff_lines := Some (Common.i_to_s n)),
34e49164 207 " set number of diff context lines";
ae4735db 208
17ba0788 209 "--use-cache", Arg.Set use_cache,
34e49164
C
210 " use .ast_raw pre-parsed cached C file";
211 ]
212
34e49164 213(*****************************************************************************)
708f4980
C
214(* for lexing of integer constants *)
215(*****************************************************************************)
216
217let int_thresholds =
218 ref (None :
219 (int (*int_sz*) * int (*long_sz*) *
220 Big_int.big_int (*uint threshold*) *
221 Big_int.big_int (*long threshold*) *
222 Big_int.big_int (*ulong threshold*)) option)
223
224let set_int_bits n =
225 match !int_thresholds with
226 None ->
227 (*assume long is 2*int; this can be corrected by a subsequent long_bits*)
228 let uint_threshold = Big_int.power_int_positive_int 2 (n-1) in
229 let long_threshold = Big_int.power_int_positive_int 2 n in
230 let ulong_threshold = Big_int.power_int_positive_int 2 ((2*n)-1) in
231 int_thresholds :=
232 Some (n,2*n,uint_threshold,long_threshold,ulong_threshold)
233 | Some(int_sz,long_sz,uint_threshold,long_threshold,ulong_threshold) ->
234 let uint_threshold = Big_int.power_int_positive_int 2 (n-1) in
235 let long_threshold = Big_int.power_int_positive_int 2 n in
236 int_thresholds :=
237 Some (n,long_sz,uint_threshold,long_threshold,ulong_threshold)
238
239let set_long_bits n =
240 match !int_thresholds with
241 None ->
242 (*assume int is 1/2*int; this can be corrected by a subsequent int_bits*)
243 set_int_bits (n/2)
244 | Some(int_sz,long_sz,uint_threshold,long_threshold,ulong_threshold) ->
245 let ulong_threshold = Big_int.power_int_positive_int 2 (n-1) in
246 int_thresholds :=
247 Some (int_sz,n,uint_threshold,long_threshold,ulong_threshold)
248
249(*****************************************************************************)
250(* unparsing strategy *)
251(*****************************************************************************)
252
253type spacing = LINUX | SMPL
254let spacing = ref LINUX
255
256let set_linux_spacing _ = spacing := LINUX (*follow the conventions of Linux*)
257let set_smpl_spacing _ = spacing := SMPL (*use spacing from the SP*)
258
5636bb2c
C
259let max_width = 78
260
708f4980 261(*****************************************************************************)
978fd7e5
C
262
263(* drop back edges made by proper loop constructs -
264 unsafe but more efficient *)
265let no_loops = ref false
951c7801 266let no_gotos = ref false
7f004419
C
267
268let keep_comments = ref false (* unparsing *)