| 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 Ast = Ast_cocci |
| 8 |
module MV = Meta_variable |
| 9 |
|
| 10 |
(* ------------------------------------------------------------------------- *) |
| 11 |
|
| 12 |
(* Generates rule headers with dependencies, isos, etc. in correct order. *) |
| 13 |
|
| 14 |
(* ------------------------------------------------------------------------- *) |
| 15 |
(* TYPES AND HELPERS *) |
| 16 |
|
| 17 |
type t = |
| 18 |
{ |
| 19 |
first_line : string; (* @rule_name ...@ *) |
| 20 |
meta_vars : MV.t list; |
| 21 |
meta_pos : MV.t list; |
| 22 |
last_line : string; (* @@ *) |
| 23 |
} |
| 24 |
|
| 25 |
let comma_sep = String.concat ", " |
| 26 |
|
| 27 |
(* Stringify dependencies. Adds parentheses where necessary for precedence. *) |
| 28 |
let tostring_dep = function |
| 29 |
| Ast.NoDep -> "" |
| 30 |
| deps -> |
| 31 |
let rec dep in_and n k = |
| 32 |
match n with |
| 33 |
| Ast.Dep s -> k s |
| 34 |
| Ast.AntiDep s -> k ("!" ^ s) |
| 35 |
| Ast.EverDep s -> k ("ever " ^ s) |
| 36 |
| Ast.NeverDep s -> k ("never " ^ s) |
| 37 |
| Ast.AndDep(s1, s2) -> |
| 38 |
dep true s1 (fun l -> |
| 39 |
dep true s2 (fun r -> k (l ^ " && " ^ r))) |
| 40 |
| Ast.OrDep(s1, s2) -> |
| 41 |
dep false s1 (fun l -> |
| 42 |
dep false s2 (fun r -> |
| 43 |
let s = l ^ " || " ^ r in |
| 44 |
if in_and then k ("(" ^ s ^ ")") else k s)) |
| 45 |
| Ast.FileIn s -> k ("file in \"" ^ s ^ "\"") |
| 46 |
| Ast.NotFileIn s -> k ("!(file in \"" ^ s ^ "\")") in |
| 47 |
let topdep = function |
| 48 |
| Ast.NoDep -> "no_dep" |
| 49 |
| Ast.FailDep -> "fail_dep" |
| 50 |
| Ast.ExistsDep d -> dep false d (fun x -> x) |
| 51 |
| Ast.ForallDep d -> "forall " ^ (dep false d (fun x -> x)) in |
| 52 |
" depends on " ^ (topdep deps) |
| 53 |
|
| 54 |
(* gather data into a string rule header... may or may not be slightly buggy.*) |
| 55 |
let rule_declaration ~rule_name ~isos ~drop_isos ~deps ~exists = |
| 56 |
let _ = assert (not (String.contains rule_name ' ')) in |
| 57 |
let extends = "" in (* where is this information ?? *) |
| 58 |
let expression = "" in (* where is this information ?? *) |
| 59 |
let deps = tostring_dep deps in |
| 60 |
let isos = match isos with |
| 61 |
| [] -> "" |
| 62 |
| x -> " using " ^ (comma_sep (List.map (fun x -> "\""^x^"\"") x)) in |
| 63 |
let drop_isos = match drop_isos with |
| 64 |
| [] -> "" |
| 65 |
| x -> " disable " ^ (comma_sep x) in |
| 66 |
let exists = match exists with |
| 67 |
| Ast.Exists -> " exists" | Ast.Forall -> " forall" |
| 68 |
| Ast.Undetermined -> "" in |
| 69 |
Globals.concat_limit_width |
| 70 |
["@"; rule_name; extends; deps; isos; drop_isos; exists; expression; "@"] |
| 71 |
|
| 72 |
(* ------------------------------------------------------------------------- *) |
| 73 |
(* ENTRY POINT *) |
| 74 |
|
| 75 |
(* generate rule header *) |
| 76 |
let generate |
| 77 |
~rule_name ~isos ~drop_isos ~deps ~exists ~meta_vars ~meta_pos = |
| 78 |
let first_line = |
| 79 |
rule_declaration ~rule_name ~isos ~drop_isos ~deps ~exists in |
| 80 |
let last_line = "@@\n\n" in |
| 81 |
{ |
| 82 |
first_line = first_line ^ "\n"; |
| 83 |
meta_vars; meta_pos; last_line; |
| 84 |
} |
| 85 |
|
| 86 |
(* print a rule header *) |
| 87 |
let print out {first_line = f; meta_vars = mv; meta_pos = mp; last_line = l;} = |
| 88 |
output_string out f; |
| 89 |
MV.print_list out ~do_group:true mv; |
| 90 |
MV.print_list out ~do_group:true mp; |
| 91 |
output_string out l |
| 92 |
|
| 93 |
(* prints only the first line of the rule header, ie. the declaration *) |
| 94 |
let print_declaration out {first_line = f; _} = output_string out f |