Debian Codegraph @

tools/spgen/source/user_input.ml

raw
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 MV = Meta_variable
8 module RuleMap = Map.Make (String)
9
10 (* ------------------------------------------------------------------------- *)
11
12 (* Encapsulates all local input for the rule to be generated:
13 * - Description of the script.
14 * - Limitations of the script.
15 * - Keywords for the script.
16 * - Confidence level of the script (Low, Moderate, High).
17 * - Additional comments for the script.
18 * - Coccinelle options with which to call the script.
19 * - Authors of the script (copyright).
20 * Not yet globally configurable; see spgen_config.ml.
21 * - Error messages for org and report mode for each rule.
22 *)
23
24 (* ------------------------------------------------------------------------- *)
25 (* HELPERS FOR USER INPUT *)
26
27 let opt s = if s = "" then None else Some s
28 let rev_opt p = function Some s -> p ^ s ^ "\n" | None -> ""
29
30 (* Count number of format variables, effectively number of unescaped %'s. *)
31 let count_format_vars s =
32
33 (* ignore escaped percent signs, which in python is %%. *)
34 let s = Str.global_replace (Str.regexp_string "%%") "" s in
35 let rec count_pct acc i s =
36 if i < 0 then acc else
37 if s.[i] = '%' then count_pct (acc+1) (i-1) s else
38 count_pct acc (i-1) s in
39 count_pct 0 ((String.length s)-1) s
40
41 (* fails if the number of metavariables != the number of % in format str *)
42 let check_format_string (msg, mvs) =
43 let pcts = count_format_vars msg in
44 let mvcount = List.length mvs in
45 if pcts <> mvcount
46 then failwith
47 ("Config error: ill-formed format string.\n"
48 ^ "Number of format variables in the message \"" ^ msg ^ "\": "
49 ^ (string_of_int pcts) ^ "\n"
50 ^ "Number of declared format variables: " ^ (string_of_int mvcount))
51
52 (* reconstructs a format string from the message and metavars (as strings) *)
53 let make_format_string msg =
54 let msg = "\"" ^ msg ^ "\"" in
55 function
56 | [] -> msg
57 | x ->
58 let x = List.map MV.tostring_mv x in
59 msg ^ " % " ^ "(" ^ (String.concat "," x) ^ ")"
60
61 (* turn user-specified metavariable strings into metavariables.
62 * important that they are created with no rulename if within the same rule,
63 * since this is used to generate the right inheritance later on.
64 * They are initialised with type = "" since we don't need it.
65 *)
66 let make_metavars =
67 let mv a =
68 let split_name = Str.bounded_split (Str.regexp "\\.") a 2 in
69 match split_name with
70 | [meta_name] -> MV.make ~typ:"" meta_name
71 | [inherit_rule; meta_name] -> MV.make ~typ:"" ~inherit_rule meta_name
72 | _ -> failwith "bounded split" in
73 List.map mv
74
75
76 (* ------------------------------------------------------------------------- *)
77 (* CONFIDENCE TYPE *)
78
79 (* confidence in the accuracy of the script *)
80
81 module Confidence = struct
82 type t = Low | Moderate | High
83
84 exception Not_confidence of string
85
86 let to_string =
87 function Low -> "Low" | Moderate -> "Moderate" | High -> "High"
88
89 let from_string s =
90 match Stdcompat.String.lowercase_ascii s with
91 | "low" | "l" -> Low
92 | "moderate" | "m" -> Moderate
93 | "high" | "h" -> High
94 | s -> raise (Not_confidence s)
95 end
96
97
98 (* ------------------------------------------------------------------------- *)
99 (* USER RULE TYPE *)
100
101 (* user-specified data to put in spgenerated script.
102 * type is (rulename, (org message, org metavars), (rep message, rep metavars))
103 * rulename can be original rulename or a new user-specified one.
104 *)
105
106 module Rule = struct
107 type t = string * (string * MV.t list) * (string * MV.t list)
108
109 (* constructor. If rulename is nameless ie. "rule starting on line ...",
110 * generate a new one.
111 *)
112 let make ~rule_name ~org ~report =
113 let ((om,ov),(rm,rv)) = (org, report) in
114 let _ = assert (rule_name <> "" && not(om = "" && rm = "")) in
115 let rule_name =
116 if String.contains rule_name ' '
117 then Globals.generate_rule rule_name
118 else rule_name in
119 let ov, rv = make_metavars ov, make_metavars rv in
120 (rule_name, (om,ov), (rm,rv))
121
122 let get_name (n,_,_) = n
123
124 let get_org (_,o,_) = o
125
126 let get_report (_,_,r) = r
127 end
128
129
130 (* ------------------------------------------------------------------------- *)
131 (* USER INPUT TYPE *)
132
133 (* user_input type that covers all the data specified by the user
134 * rules are a map, mapping original rulename to
135 * (new rulename, (org message, org metavars), (rep message, rep metavars)
136 *)
137 type t =
138 {
139 description : string;
140 limitations : string list;
141 keywords : string option;
142 confidence : Confidence.t;
143 comments : string option;
144 options : string option;
145 authors : string list;
146 url : string option;
147 rules : Rule.t RuleMap.t;
148 }
149
150 (* CONSTRUCTOR, description and confidence levels are required. *)
151 let make ~description ~confidence =
152 if description = ""
153 then failwith "Error: Description is required."
154 else
155 {
156 description; limitations = []; keywords = None; confidence;
157 comments = None; options = None; authors = []; url = None;
158 rules = RuleMap.empty
159 }
160
161 (* SETTERS *)
162 let add_limit limit t = { t with limitations = limit :: t.limitations }
163 let set_limits limits t = { t with limitations = limits }
164 let set_keys keys t = { t with keywords = opt keys }
165 let set_conf conf t = { t with confidence = conf }
166 let set_comments cmnt t = { t with comments = opt cmnt }
167 let set_options optn t = { t with options = opt optn }
168 let set_url url t = { t with url = opt url }
169 let add_author auth t = { t with authors = auth :: t.authors }
170 let set_authors auths t = { t with authors = auths }
171
172 (* check that rulename is valid + is not already added. Returns unit. *)
173 let check_name nm t =
174 let already key value =
175 let new_name = Rule.get_name value in
176 (key = new_name && key = nm) || (* nm is same as already added rule *)
177 (new_name = nm) in (* nm is same as other rule with new nm *)
178 if RuleMap.exists already t.rules then
179 failwith ("Error: already another rule named \"" ^ nm ^"\"!")
180 else
181 Globals.check_rule ~strict:true nm
182
183 (* add rule to rulemap in t.
184 * for nameless rules: check legality of user-declared name.
185 *)
186 let add_rule ~rule_name rule t =
187 let newnm = Rule.get_name rule in
188 let _ = if rule_name <> newnm then check_name newnm t in
189 { t with rules = RuleMap.add rule_name rule t.rules }
190
191 (* GETTERS *)
192 (* format the preface information and turn it into one big string *)
193 let get_preface ~year
194 {
195 description=d; limitations=l; keywords=k; confidence=c;
196 comments=m; options=o; authors=a; url=u; _
197 } =
198
199 let author_format =
200 let year_string = string_of_int year in
201 Globals.pre_split ~prefix:("// Copyright: (C) "^year_string^" ") in
202 let desc = Globals.pre_split ~prefix:"/// " d in
203 let limits =
204 String.concat "\n" (List.map (Globals.pre_split ~prefix:"//# ") l) in
205 let keys = Globals.pre_split_opt ~prefix:"// Keywords: " k in
206 let confidence = "// Confidence: " ^ (Confidence.to_string c) in
207 let comments = Globals.pre_split_opt ~prefix:"// Comments: " m in
208 let options = Globals.pre_split_opt ~prefix:"// Options: " o in
209 let authors = String.concat "\n" (List.map author_format a) in
210 let url = Globals.pre_split_opt ~prefix:"// URL: " u in
211 let preface = [desc; limits; "///"; confidence; authors; url; comments;
212 options; keys] in
213 String.concat "\n" (List.filter ((<>) "") preface)
214
215 (* get the user-input rulename and org and report messages for rule_name.
216 * if the original rule could not be found in the userinput, use default msg
217 *)
218 let get_rule ~rule_name {rules = r; _} =
219 try
220 RuleMap.find rule_name r
221 with Not_found ->
222 let rulenm = Globals.generate_rule rule_name in
223 let default = Globals.get_default_message() in
224 (rulenm, (default, []), (default, []))
225
226
227 (* ------------------------------------------------------------------------- *)
228 (* UNPARSE USER INPUT INTO CONFIG *)
229
230 (* turns a rule into the config script that generated it *)
231 let unparse_rule rnm (newnm,(orgmsg,orgmvs),(repmsg,repmvs)) =
232 let orgmsg = make_format_string orgmsg orgmvs in
233 let repmsg = make_format_string repmsg repmvs in
234 let rnm =
235 if rnm <> newnm then
236 let l = Globals.extract_line rnm in (string_of_int l) ^ ":" ^ newnm
237 else
238 rnm in
239 rnm ^ " =\n" ^ " org:" ^ orgmsg ^ "\n report:" ^ repmsg ^ "\n"
240
241 (* turn a user input collection into its corresponding config script *)
242 let unparse
243 {
244 description; limitations; keywords; confidence; comments; options; rules;
245 authors; url
246 } =
247
248 let a = "// Generated config\n" in
249 let b = "description = " ^ description ^ "\n" in
250 let c = if limitations = [] then "" else
251 "limitations = " ^ (String.concat "|" limitations) ^ "\n" in
252 let d = rev_opt "keywords = " keywords in
253 let e = "confidence = " ^ (Confidence.to_string confidence) ^ "\n" in
254 let f = rev_opt "comments = " comments in
255 let g = rev_opt "options = " options in
256 let h = if authors = [] then "" else
257 "authors = " ^ (String.concat "|" authors) ^ "\n" in
258 let i = rev_opt "url = " url in
259 let j = RuleMap.fold
260 (fun rnm msg acc -> (unparse_rule rnm msg) :: acc) rules [] in
261 let preface = String.concat "" [a;b;c;d;e;f;g;h;i] in
262 let rules = String.concat "" j in
263 preface ^ rules