| 1 |
(* |
| 2 |
* This file is part of Coccinelle, lincensed under the terms of the GPL v2. |
| 3 |
* See copyright.txt in the Coccinelle source code for more information. |
| 4 |
* The Coccinelle source code can be obtained at http://coccinelle.lip6.fr |
| 5 |
*) |
| 6 |
|
| 7 |
(* ------------------------------------------------------------------------- *) |
| 8 |
|
| 9 |
(* Driver module for the whole program. |
| 10 |
* |
| 11 |
* The high-level process is something like: |
| 12 |
* 1. Parse the cocci file using the main parser |
| 13 |
* 2. Parse the user-provided input for the spgenerated file |
| 14 |
* 3. Generate context rules (ie. rules with *'s and positions) |
| 15 |
* 4. Generate script rules (ie. rules for org and report modes) |
| 16 |
* 5. Print the preface (ie. metadata and virtuals) |
| 17 |
* 6. Print the original file with transformations in dependencies, names, etc. |
| 18 |
* 7. Print the generated context and script rules |
| 19 |
*) |
| 20 |
|
| 21 |
(* ------------------------------------------------------------------------- *) |
| 22 |
|
| 23 |
type options = |
| 24 |
{ |
| 25 |
file : string; |
| 26 |
config : string; |
| 27 |
output : string; |
| 28 |
interactive : bool; |
| 29 |
default : bool; |
| 30 |
hide : bool; |
| 31 |
year : int; |
| 32 |
} |
| 33 |
|
| 34 |
(* if no options are set, it defaults to interactive mode *) |
| 35 |
let make_options |
| 36 |
?(config = "") |
| 37 |
?(output = "") |
| 38 |
?(interactive = false) |
| 39 |
?(default = false) |
| 40 |
?(hide = false) |
| 41 |
?(year = Common.this_year()) |
| 42 |
file = |
| 43 |
{ file; config; output; interactive; default; hide; year } |
| 44 |
|
| 45 |
|
| 46 |
(* ------------------------------------------------------------------------- *) |
| 47 |
(* ENTRY POINT *) |
| 48 |
|
| 49 |
let run { file; config; output; interactive; default; hide; year; } = |
| 50 |
|
| 51 |
(* ------------- CHECK ------------- *) |
| 52 |
|
| 53 |
if not(Sys.file_exists file) then |
| 54 |
failwith ("The file \""^ file ^"\" doesn't exist!"); |
| 55 |
|
| 56 |
if config <> "" && not(Sys.file_exists config) then |
| 57 |
failwith ("The config file \"" ^ config ^ "\" doesn't exist!"); |
| 58 |
|
| 59 |
if file = output then |
| 60 |
failwith ("Input file cannot be the same as output file!"); |
| 61 |
|
| 62 |
|
| 63 |
(* ------------- SETTINGS ------------- *) |
| 64 |
|
| 65 |
(* default config name. Ie. <file_name>.config *) |
| 66 |
let (dir, base, _) = Common.dbe_of_filename file in |
| 67 |
let name = Common.filename_of_dbe (dir, base, "config") in |
| 68 |
|
| 69 |
(* if no config specified, but the default config exists, use it *) |
| 70 |
let config = |
| 71 |
if config = "" && Sys.file_exists name |
| 72 |
then name |
| 73 |
else config in |
| 74 |
|
| 75 |
(* if no config specified and no default config, run in interactive mode *) |
| 76 |
let interactive = (config = "") || interactive in |
| 77 |
|
| 78 |
|
| 79 |
(* ------------- PARSE ------------- *) |
| 80 |
|
| 81 |
Flag_parsing_cocci.generating_mode := true; |
| 82 |
let (_, rules, virtuals, _) = Parse_cocci.parse file in |
| 83 |
Flag_parsing_cocci.generating_mode := false; (* cleanup! for tests, etc. *) |
| 84 |
|
| 85 |
(* if the rule is a star rule, the sgrep_mode2 flag is set after parsing *) |
| 86 |
let context_mode = !Flag.sgrep_mode2 in |
| 87 |
|
| 88 |
(* get the */+/- rules and check that their names don't create conflicts *) |
| 89 |
let rules_disj_maps = Detect_patch.filter_patch_rules rules in |
| 90 |
let rule_names = |
| 91 |
List.map (fun (x,_) -> Ast0_cocci.get_rule_name x) rules_disj_maps in |
| 92 |
let _ = List.iter (Globals.check_rule ~strict:false) rule_names in |
| 93 |
|
| 94 |
|
| 95 |
(* ------------- GLOBALS ------------- *) |
| 96 |
|
| 97 |
(* these are settings that are usually the same regardless of rule *) |
| 98 |
|
| 99 |
let (_(*author*), _(*license*), rule_name, pos_name, error_msg, char_limit) = |
| 100 |
Spgen_config.parse_global ~config_name:"" in |
| 101 |
let _ = Globals.init ~rule_name ~pos_name ~error_msg ~char_limit in |
| 102 |
let virtuals = Globals.key_virtuals ~context_mode virtuals in |
| 103 |
|
| 104 |
|
| 105 |
(* ------------- LOCALS ------------- *) |
| 106 |
|
| 107 |
let user_input = |
| 108 |
if default then |
| 109 |
Spgen_config.parse_default |
| 110 |
else if interactive then |
| 111 |
Spgen_interactive.interact ~rule_names ~config_name:name |
| 112 |
else |
| 113 |
Spgen_config.parse_local ~rule_names ~config_name:config in |
| 114 |
|
| 115 |
let preface = User_input.get_preface ~year user_input in |
| 116 |
|
| 117 |
|
| 118 |
(* ------------- GENERATE ------------- *) |
| 119 |
|
| 120 |
(* drules is the ordered list of patch rules, tupled with their disj maps. *) |
| 121 |
let generate drules = |
| 122 |
let rec generate' rules fn = |
| 123 |
match rules with |
| 124 |
| (rule, disj_map) :: rs -> |
| 125 |
|
| 126 |
(* extract the corresponding user input to current rule *) |
| 127 |
let old_name = Ast0_cocci.get_rule_name rule in |
| 128 |
let user_rule = User_input.get_rule ~rule_name:old_name user_input in |
| 129 |
let new_name = User_input.Rule.get_name user_rule in |
| 130 |
|
| 131 |
(* generate context and script rules *) |
| 132 |
let nrule = (rule, new_name) in |
| 133 |
let (ctxt, meta_pos) = |
| 134 |
Context_rule.generate ~context_mode ~new_name ~disj_map ~rule_names ~rule in |
| 135 |
let script = |
| 136 |
Script_rule.generate ~meta_pos ~user_rule in |
| 137 |
|
| 138 |
let add (rs, cs, ss) = fn (nrule::rs, ctxt::cs, script::ss) in |
| 139 |
generate' rs add |
| 140 |
|
| 141 |
| [] -> fn ([],[],[]) in |
| 142 |
generate' drules (fun x -> x) in |
| 143 |
|
| 144 |
let (namedrules, contexts, scripts) = generate rules_disj_maps in |
| 145 |
|
| 146 |
|
| 147 |
(* ------------- PRINT ------------- *) |
| 148 |
|
| 149 |
if not(hide) then begin |
| 150 |
let out = if output = "" then stdout else (open_out output) in |
| 151 |
let split() = output_string out |
| 152 |
("// --------------------------------------" ^ |
| 153 |
"--------------------------------------\n\n") in |
| 154 |
try |
| 155 |
|
| 156 |
File_transform.print |
| 157 |
~context_mode ~file_name:file ~preface ~virtuals |
| 158 |
~ordered_rules:namedrules out; |
| 159 |
split(); |
| 160 |
List.iter (Context_rule.print out) contexts; |
| 161 |
split(); |
| 162 |
List.iter (Script_rule.print_org out) scripts; |
| 163 |
split(); |
| 164 |
List.iter (Script_rule.print_report out) scripts; |
| 165 |
flush out; |
| 166 |
close_out out |
| 167 |
|
| 168 |
with Failure msg -> |
| 169 |
|
| 170 |
flush out; |
| 171 |
close_out out; |
| 172 |
if output <> "" && Sys.file_exists output then Sys.remove output; |
| 173 |
failwith msg |
| 174 |
|
| 175 |
end |