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