Debian Codegraph @

tools/spgen/source/globals.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 Ast = Ast_cocci
8
9 (* ------------------------------------------------------------------------- *)
10
11 (* Global variables and hardcoded standard stuff goes here.
12 * Also some general purpose functions for strings and sanity checks.
13 *)
14
15 (* ------------------------------------------------------------------------- *)
16 (* GLOBAL VARIABLES AND GETTERS *)
17 (* read-only after initialisation *)
18
19 (* Default names for generated positions *)
20 let pos_name = ref "j"
21 let get_pos_name() = !pos_name
22
23 (* Default rule names for unnamed rules. Accessed through generate_rule. *)
24 let rule_name = ref "rule"
25 let rule_counter = ref 0
26
27 (* Default error message for org and report mode *)
28 let error_message = ref "found a match here ..."
29 let get_default_message() = !error_message
30
31 let get_context_name ~context_mode str =
32 if context_mode then str else str ^ "_context"
33 let get_disj_name str = str ^ "_disj"
34 let get_org_name str = str ^ "_org"
35 let get_report_name str = str ^ "_report"
36
37 (* Page width limit for generated script (not always upheld ...) *)
38 let char_limit = ref !Flag_parsing_c.max_width
39
40 let init ~rule_name:r ~pos_name:p ~error_msg:e ~char_limit:cl =
41 rule_counter := 0;
42 pos_name := p;
43 rule_name := r;
44 error_message := e;
45 char_limit := cl
46
47
48 (* ------------------------------------------------------------------------- *)
49 (* HARDCODED (PHOOEY!) *)
50
51 (* list of things you can't call your rules, because it will mess with spgen *)
52 let keywords =
53 ["patch"; "context"; "org"; "report"; "description"; "limitations";
54 "keywords"; "comments"; "options"; "confidence"; "authors"; "url";
55 "d";"k";"c";"m";"o";"l";"a";"u"]
56
57 (* default virtual rule names *)
58 let get_virtuals ~context_mode =
59 if context_mode
60 then ["context"; "org"; "report"]
61 else ["patch"; "context"; "org"; "report"]
62
63 (* check if virtual rule names are valid and return the standard ones *)
64 let key_virtuals ~context_mode v =
65 let keyvirtuals = get_virtuals ~context_mode in
66 let check x =
67 if List.mem x keyvirtuals
68 then failwith
69 "Error: patch, context, org, and report are reserved virtual rules." in
70 List.iter check v;
71 keyvirtuals
72
73 (* adds the hardcoded 'default' patch rule dependency. *)
74 let add_patch_dependency deps =
75 let patch_dep =
76 Ast.AndDep(
77 Ast.Dep "patch",
78 Ast.AndDep(
79 Ast.AntiDep "context",
80 Ast.AndDep(Ast.AntiDep "org", Ast.AntiDep "report"))) in
81 match deps with
82 Ast.NoDep -> Ast.ExistsDep patch_dep
83 | Ast.FailDep -> Ast.FailDep
84 | Ast.ExistsDep d -> Ast.ExistsDep(Ast.AndDep(d, patch_dep))
85 | Ast.ForallDep d -> Ast.ForallDep(Ast.AndDep(d, patch_dep))
86
87 (* adds the hardcoded 'default' context rule dependency. *)
88 let add_context_dependency ~context_mode deps =
89 let context_dep = (* context || org || report *)
90 Ast.OrDep(Ast.Dep "context", Ast.OrDep(Ast.Dep "org", Ast.Dep "report")) in
91 let context_dep =
92 if context_mode
93 then context_dep
94 else Ast.AndDep(Ast.AntiDep "patch", context_dep) in
95 match deps with
96 Ast.NoDep -> Ast.ExistsDep context_dep
97 | Ast.FailDep -> Ast.FailDep
98 | Ast.ExistsDep d -> Ast.ExistsDep(Ast.AndDep(d, context_dep))
99 | Ast.ForallDep d -> Ast.ForallDep(Ast.AndDep(d, context_dep))
100
101 (* ------------------------------------------------------------------------- *)
102 (* SANITY CHECKS AND RULE HELPERS *)
103
104 let starts_with_digit x = Str.string_match (Str.regexp "^[0-9]") x 0
105
106 (* check if a rulename is valid *)
107 let check_rule ~strict x =
108
109 if x = ""
110 then failwith
111 "Error: Rulename cannot be empty!";
112
113 if strict && String.contains x ' '
114 then failwith
115 ("Error: Rulenames cannot contain spaces: \"" ^x^ "\".");
116
117 if starts_with_digit x
118 then failwith
119 ("Error: Rules that start with digits are not allowed: \"" ^x^ "\".");
120
121 let gen_rule = !rule_name in
122 let regexp = Str.regexp ((Str.quote gen_rule) ^ "[0-9]+$") in
123 if Str.string_match regexp x 0
124 then failwith
125 ("Error: The default generated rule name is \""^ gen_rule ^"<number>\".\n"^
126 "The name \"" ^ x ^ "\" is invalid, since it may overlap with a " ^
127 "generated rule name.");
128
129 if List.mem x keywords
130 then failwith
131 ("Error: A rule can't be called \""^ x ^"\"! That's a keyword in spgen ...")
132
133 (* for rules with no name; get the line they are starting on *)
134 let extract_line str =
135 if Str.string_match (Str.regexp "^\\(rule starting on line \\)") str 0
136 then
137 let i = Str.match_end() in
138 let num = String.sub str i ((String.length str) - i) in
139 int_of_string num
140 else failwith ("Was not a nameless rule: " ^ str)
141
142 (* Only generates rulename if input name is invalid as rulename *)
143 let generate_rule nm =
144 try
145 check_rule ~strict:true nm;
146 nm
147 with Failure _ ->
148 let new_name = !rule_name ^ (string_of_int !rule_counter) in
149 rule_counter := !rule_counter + 1;
150 new_name
151
152
153 (* ------------------------------------------------------------------------- *)
154 (* NIFTY STRING FUNCTIONS *)
155
156 (* split a string into a list of strings with at most limit characters each,
157 * delimitering by space.
158 *)
159 let split limit s =
160
161 (* returns list of (start index, len) of each sequence of words (string) that
162 * contains at most limit chars. In reverse order.
163 *)
164 let get_rev_indices str lim =
165 let len = String.length s in
166 let rec get_rev acc start =
167 if (len - start <= lim)
168 then (start, len) :: acc
169 else
170 let space_index =
171 try String.rindex_from str (start + lim) ' '
172 with Not_found -> start + lim in
173 get_rev ((start, space_index) :: acc) (space_index + 1) in
174 get_rev [] 0 in
175
176 let rev_indices = get_rev_indices s (limit-1) in
177
178 let get_line (start, endd) = String.sub s start (endd - start) in
179 List.rev_map get_line rev_indices
180
181 (* split string into strings of at most char_limit length
182 * append prefix to each new string
183 *)
184 let pre_split ?(prefix = "") s =
185 let limit = (!char_limit - String.length prefix) in
186 let splitted = split limit s in
187 String.concat "\n" (List.map (fun x -> prefix ^ x) splitted)
188
189 (* same as pre_split, but with a string option *)
190 let pre_split_opt ?(prefix = "") = function
191 | Some s -> pre_split ~prefix s
192 | None -> ""
193
194 (* Concatenate a list of strings, but first add newlines so that the
195 concatenation will not result in a line of more than 80 characters. *)
196 let concat_limit_width lst =
197 String.concat ""
198 (List.rev
199 (snd
200 (List.fold_left
201 (fun (sz,prev) cur ->
202 let len = String.length cur in
203 let newsz = sz + len in
204 if newsz > !char_limit
205 then (len,("\n"^cur)::prev)
206 else (newsz,cur::prev))
207 (0,[]) lst)))