| 1 |
(* |
| 2 |
* This file is part of Coccinelle, licensed 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 |
module Ast0 = Ast0_cocci |
| 8 |
|
| 9 |
(* ------------------------------------------------------------------------- *) |
| 10 |
|
| 11 |
(* Transforms the original Coccinelle script and prints it. |
| 12 |
* Prints preface and added virtual rules. |
| 13 |
* |
| 14 |
* Naming conventions: outch = out_channel, inch = in_channel. |
| 15 |
* |
| 16 |
* TODO: There are a number of edge cases that are not handled well in this |
| 17 |
* module due to using pure string-matching without context. |
| 18 |
* Example: @'s inside comments within rule declarations. |
| 19 |
* For most _reasonable_ SmPL scripts, this shouldn't be a problem though. |
| 20 |
*) |
| 21 |
|
| 22 |
(* ------------------------------------------------------------------------- *) |
| 23 |
(* GENERAL PURPOSE FUNCTIONS *) |
| 24 |
|
| 25 |
let line_number = ref 0 |
| 26 |
|
| 27 |
let get_line inch = line_number := !line_number + 1; input_line inch |
| 28 |
|
| 29 |
let nothing _ = () |
| 30 |
|
| 31 |
let print = output_string |
| 32 |
|
| 33 |
let print_newline outch = output_string outch "\n" |
| 34 |
|
| 35 |
let print_nl outch x = print outch x; print_newline outch |
| 36 |
|
| 37 |
let print_virtuals outch virtuals = |
| 38 |
print_newline outch; |
| 39 |
List.iter (fun x -> print_nl outch ("virtual " ^ x)) virtuals; |
| 40 |
print_newline outch |
| 41 |
|
| 42 |
exception Eof_error of string |
| 43 |
|
| 44 |
let fail_eof name = |
| 45 |
let errmsg = "Error: Reached end of file before rule "^name^" was found." in |
| 46 |
raise (Eof_error errmsg) |
| 47 |
|
| 48 |
|
| 49 |
(* ------------------------------------------------------------------------- *) |
| 50 |
(* REGEXES AND STRING MATCH FUNCTIONS *) |
| 51 |
|
| 52 |
(* returns true if str matches the regular expression in regexp *) |
| 53 |
let regex_match regex str = Str.string_match (Str.regexp regex) str 0 |
| 54 |
|
| 55 |
(* regex for any number of same-line whitespace *) |
| 56 |
let sp_re = "[ \t]*" |
| 57 |
|
| 58 |
(* regex for at least one space or tab *) |
| 59 |
let spp_re = "[ \t]+" |
| 60 |
|
| 61 |
(* regex for any number of /**/ comments *) |
| 62 |
let cmnt_re = "\\(" ^ sp_re ^ "/\\*.*\\*/" ^ sp_re ^ "\\)*" |
| 63 |
|
| 64 |
(* regex for any number of /**/ comments with arbitrary whitespace *) |
| 65 |
let spcmnt_re = sp_re ^ cmnt_re ^ sp_re |
| 66 |
|
| 67 |
(* we have to handle many cases since it is technically possible to have |
| 68 |
* comments and large amounts of whitespace in rule header declarations. |
| 69 |
* If someone actually writes a script like this, they should be punished. |
| 70 |
*) |
| 71 |
let escape = Str.global_replace (Str.regexp "\\$") "\\\\$" |
| 72 |
|
| 73 |
let match_full rule_name = |
| 74 |
regex_match ("^@"^spcmnt_re^(escape rule_name)^"\\(@\\|"^spp_re^".*@\\)") |
| 75 |
|
| 76 |
let match_part rule_name = |
| 77 |
regex_match ("^@"^spcmnt_re^(escape rule_name)^"\\("^spp_re^".*\\)?$") |
| 78 |
|
| 79 |
let match_end = regex_match ".*@" |
| 80 |
|
| 81 |
let match_nameless_rule = regex_match "\\(^\\(@@\\)\\|^@.*@$\\)" |
| 82 |
|
| 83 |
let match_rule_start = regex_match ("^@") |
| 84 |
|
| 85 |
let match_rule_start_arob = regex_match ("^@"^spcmnt_re^"$") |
| 86 |
|
| 87 |
let match_rule_end = regex_match (spcmnt_re^"@@") |
| 88 |
|
| 89 |
let match_non_empty = regex_match (spcmnt_re^"[^ \t]") |
| 90 |
|
| 91 |
|
| 92 |
(* ------------------------------------------------------------------------- *) |
| 93 |
(* IN_CHANNEL TRAVERSAL *) |
| 94 |
|
| 95 |
let rec find_match ~do_this ~until inch = |
| 96 |
let line = get_line inch in |
| 97 |
if until line then (line, inch) |
| 98 |
else begin |
| 99 |
do_this line; |
| 100 |
find_match ~do_this ~until inch |
| 101 |
end |
| 102 |
|
| 103 |
let find_line ~do_this ~until_line inch = |
| 104 |
find_match ~do_this ~until:(fun _ -> until_line = !line_number) inch |
| 105 |
|
| 106 |
(* upon a call to regex string matching, print what follows after the match *) |
| 107 |
let print_rest outch line = |
| 108 |
let i = Str.match_end() in |
| 109 |
let length = String.length line in |
| 110 |
let rest = String.sub line i (length - i) in |
| 111 |
if i <> length then print_nl outch rest |
| 112 |
|
| 113 |
(* prints the contents of the opened channel until finishes *) |
| 114 |
let rec print_to_end outch inch = |
| 115 |
(try |
| 116 |
print_nl outch (get_line inch) |
| 117 |
with |
| 118 |
End_of_file -> (print_newline outch; raise End_of_file)); |
| 119 |
print_to_end outch inch |
| 120 |
|
| 121 |
(* goes through the file, printing it as it goes, until finding the rule |
| 122 |
* declaration of name, without printing the rule declaration. |
| 123 |
* returns the line where the rule dec ends, and the in_channel at that stage. |
| 124 |
*) |
| 125 |
let skip_rule_dec name outch inch = |
| 126 |
|
| 127 |
let rec traverse outch inch = |
| 128 |
let line = get_line inch in |
| 129 |
if match_full name line then (* "@rulename@" *) |
| 130 |
(line, inch) |
| 131 |
else if match_part name line then (* "@rulename" *) |
| 132 |
find_match ~do_this:nothing ~until:match_end inch |
| 133 |
else if match_rule_start_arob line then (* "@", next line maybe rulename *) |
| 134 |
let (line,inch) = |
| 135 |
find_match ~do_this:nothing ~until:match_non_empty inch in |
| 136 |
if regex_match (sp_re^name) line then |
| 137 |
find_match ~do_this:nothing ~until:match_end inch |
| 138 |
else begin |
| 139 |
print_nl outch ("@"^line); |
| 140 |
traverse outch inch |
| 141 |
end |
| 142 |
else begin (* line does not contain rule dec *) |
| 143 |
print_nl outch line; |
| 144 |
traverse outch inch |
| 145 |
end in |
| 146 |
|
| 147 |
traverse outch inch |
| 148 |
|
| 149 |
|
| 150 |
(* ------------------------------------------------------------------------- *) |
| 151 |
(* PATCH SPECIFIC *) |
| 152 |
|
| 153 |
(* outputs the rule declaration with standard patch dependencies. |
| 154 |
* rule_name is the new name which overrules the one in the Ast0 rule. |
| 155 |
*) |
| 156 |
let print_patch_decl outch rule_name = function |
| 157 |
| Ast0.InitialScriptRule (nm,_,_,_,_,_) |
| 158 |
| Ast0.FinalScriptRule (nm,_,_,_,_,_) |
| 159 |
| Ast0.ScriptRule (nm,_,_,_,_,_,_) -> |
| 160 |
failwith ("Error: The rule " ^ nm ^ " is a script rule ...!") |
| 161 |
| Ast0.CocciRule ((_,_,(isos,drop_isos,deps,_,exists)),_,_,_) -> |
| 162 |
let deps = Globals.add_patch_dependency deps in |
| 163 |
let patch_header = Rule_header.generate |
| 164 |
~isos ~drop_isos ~deps ~rule_name ~exists ~meta_vars:[] ~meta_pos:[] in |
| 165 |
Rule_header.print_declaration outch patch_header |
| 166 |
|
| 167 |
(* prints the file until the declaration of the rule, which is then substituted |
| 168 |
* with whatever handler does. |
| 169 |
*) |
| 170 |
let print_named_rule ~rule ~handler ~outch ~inch = |
| 171 |
let name = Ast0.get_rule_name rule in |
| 172 |
let (line,inch) = skip_rule_dec name outch inch in |
| 173 |
handler line inch |
| 174 |
|
| 175 |
(* prints the file until the rule declaration (rule_name must follow the format |
| 176 |
* "rule starting on line <num>"), which is substituted with whatever handler |
| 177 |
* does. |
| 178 |
*) |
| 179 |
let print_nameless_rule ~rule ~handler ~outch ~inch = |
| 180 |
let rule_name = Ast0.get_rule_name rule in |
| 181 |
let rule_line = Globals.extract_line rule_name in |
| 182 |
let _ = assert (rule_line > !line_number) in |
| 183 |
let (line, inch) = |
| 184 |
find_line ~do_this:(print_nl outch) ~until_line:rule_line inch in |
| 185 |
|
| 186 |
if match_nameless_rule line then |
| 187 |
handler line inch |
| 188 |
else if String.contains line '@' then |
| 189 |
let (line, inch) = find_match ~do_this:nothing ~until:match_end inch in |
| 190 |
handler line inch |
| 191 |
else |
| 192 |
failwith ("Error: Did not find a " ^rule_name^ ", instead found: " ^line) |
| 193 |
|
| 194 |
(* Finds the declaration of the input rule ("@rulename ...@") and substitutes |
| 195 |
* it with a patch dependent version ("@rulename depends on patch ...@"). |
| 196 |
*) |
| 197 |
let print_rule_patch outch inch (rule, new_name) = |
| 198 |
|
| 199 |
(* prints out patch header. If there was anything after the rule declaration, |
| 200 |
* print that too. returns the in_channel at the point after the printing. |
| 201 |
*) |
| 202 |
let handler line inch = |
| 203 |
print_patch_decl outch new_name rule; |
| 204 |
print_rest outch line; |
| 205 |
inch in |
| 206 |
|
| 207 |
let old_name = Ast0.get_rule_name rule in |
| 208 |
|
| 209 |
try |
| 210 |
if old_name = new_name then |
| 211 |
print_named_rule ~rule ~handler ~outch ~inch |
| 212 |
else |
| 213 |
print_nameless_rule ~rule ~handler ~outch ~inch |
| 214 |
with |
| 215 |
| End_of_file -> fail_eof old_name |
| 216 |
| e -> raise e (* propagate exception upwards *) |
| 217 |
|
| 218 |
|
| 219 |
let print_patch outch inch rules = |
| 220 |
let inch = List.fold_left (print_rule_patch outch) inch rules in |
| 221 |
print_to_end outch inch |
| 222 |
|
| 223 |
|
| 224 |
(* ------------------------------------------------------------------------- *) |
| 225 |
(* CONTEXT SPECIFIC *) |
| 226 |
|
| 227 |
(* in context mode, we do not want to keep the original rules, since our |
| 228 |
* generated versions contain the same information + added metapositions. *) |
| 229 |
|
| 230 |
(* find the start of the next rule. *) |
| 231 |
let next outch inch = |
| 232 |
try |
| 233 |
let r = find_match ~do_this:nothing ~until:match_rule_start inch in Some r |
| 234 |
with |
| 235 |
End_of_file -> None (* there were no more rules *) |
| 236 |
|
| 237 |
(* find the rule with that name and skip it entirely. *) |
| 238 |
let skip_named_rule ~rule ~last_line ~outch ~inch = |
| 239 |
let name = Ast0.get_rule_name rule in |
| 240 |
let (_,inch) = |
| 241 |
if match_part name last_line |
| 242 |
then (last_line, inch) |
| 243 |
else begin |
| 244 |
if String.contains last_line '@' then failwith |
| 245 |
("Transform error: Can't currently handle this case. Don't " ^ |
| 246 |
"put newlines in your rule declarations!!!"); |
| 247 |
print_nl outch last_line; |
| 248 |
skip_rule_dec name outch inch |
| 249 |
end in |
| 250 |
let (_,inch) = find_match ~do_this:nothing ~until:match_rule_end inch in |
| 251 |
next outch inch |
| 252 |
|
| 253 |
(* find the rule that starts on that line and skip it entirely *) |
| 254 |
let skip_nameless_rule ~rule ~outch ~inch = |
| 255 |
let name = Ast0.get_rule_name rule in |
| 256 |
let rule_line = Globals.extract_line name in |
| 257 |
let _ = assert (rule_line >= !line_number) in |
| 258 |
let inch = |
| 259 |
if rule_line = !line_number then |
| 260 |
inch |
| 261 |
else |
| 262 |
snd (find_line ~do_this:(print_nl outch) ~until_line:rule_line inch) in |
| 263 |
|
| 264 |
(* at this point, line is the line that contains the rule header. so we need |
| 265 |
* the rule header end @@ and then the start of the next rule. |
| 266 |
*) |
| 267 |
let (_,inch) = find_match ~do_this:nothing ~until:match_rule_end inch in |
| 268 |
next outch inch |
| 269 |
|
| 270 |
(* print a context rule (that is, don't print it, but find it and skip it!) |
| 271 |
* last_res is (the last line contents, the in_channel) from the previous call. |
| 272 |
* |
| 273 |
* returns Some (last line contents, in_channel) if there was another rule |
| 274 |
* after the input rule, otherwise None. |
| 275 |
*) |
| 276 |
let print_rule_context outch last_res (rule, new_name) = |
| 277 |
|
| 278 |
let old_name = Ast0.get_rule_name rule in |
| 279 |
|
| 280 |
try |
| 281 |
match last_res with |
| 282 |
| None -> raise End_of_file |
| 283 |
| Some (last_line, inch) -> |
| 284 |
if old_name = new_name then |
| 285 |
skip_named_rule ~rule ~last_line ~outch ~inch |
| 286 |
else |
| 287 |
skip_nameless_rule ~rule ~outch ~inch |
| 288 |
with |
| 289 |
| End_of_file -> fail_eof old_name |
| 290 |
| e -> raise e (* propagate exception upwards *) |
| 291 |
|
| 292 |
let print_context outch inch rules = |
| 293 |
let res = List.fold_left (print_rule_context outch) (Some ("",inch)) rules in |
| 294 |
match res with |
| 295 |
| Some (_,i) -> print_to_end outch i |
| 296 |
| None -> raise End_of_file |
| 297 |
|
| 298 |
(* ------------------------------------------------------------------------- *) |
| 299 |
(* ENTRY POINT *) |
| 300 |
|
| 301 |
(* reads the file and prints it with transformations. |
| 302 |
* assumes rules are sorted in order of when they occur in the script. |
| 303 |
*) |
| 304 |
let print ~context_mode ~file_name ~preface ~virtuals ~ordered_rules outch = |
| 305 |
let _ = line_number := 0 in |
| 306 |
let _ = print_nl outch preface in |
| 307 |
let _ = print_virtuals outch virtuals in |
| 308 |
let inch = open_in file_name in |
| 309 |
try |
| 310 |
if context_mode then |
| 311 |
print_context outch inch ordered_rules |
| 312 |
else |
| 313 |
print_patch outch inch ordered_rules |
| 314 |
with |
| 315 |
| End_of_file -> flush outch; close_in inch (* ended safely *) |
| 316 |
| Eof_error msg -> flush outch; close_in inch; failwith msg |
| 317 |
| e -> close_in_noerr inch; raise e |