forked from facebookarchive/pfff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_db.ml
331 lines (274 loc) · 10.4 KB
/
main_db.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(*
* Please imagine a long and boring gnu-style copyright notice
* appearing just here.
*)
open Common
(*****************************************************************************)
(* Purpose *)
(*****************************************************************************)
(*
* pfff_db computes a "light" code database; something lighter than a
* full berkeley database but still more powerful than just a TAGS file.
* Such a light_database can be leveraged by the codemap visualizer
* to convey visual semantic information that can help navigate a
* big codebase with huge APIs.
* update: could maybe be merged with the graph code database.
*
* todo: use this file for provide an interface to the layers generation
* functions?
*
* We try to leverage multiple code artifacts:
* - the source code
* - documentation such as API reference or PLEAC cookbooks
* - SEMI the test coverage (the static part)
* - TODO some static analysis (deadcode, SEMI bad smell, type inference, etc)
* - TODO some dynamic analysis (tainting, test coverage)
* - TODO version history
*
* See also docs/manual/vision.txt
*)
(*****************************************************************************)
(* Flags *)
(*****************************************************************************)
(* In addition to flags that can be tweaked via -xxx options (cf the
* full list of options in the "the options" section below), this
* program also depends on external files ?
*)
let verbose = ref false
let db_file = ref (None: string option)
let pleac_dir = ref "/tmp/pleac"
(* action mode *)
let action = ref ""
let lang = ref "web"
let with_php_db = ref ""
(*****************************************************************************)
(* Some debugging functions *)
(*****************************************************************************)
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
(*****************************************************************************)
(* Language specific *)
(*****************************************************************************)
let rec light_db_of_files_or_dirs lang xs =
let verbose = !verbose in
let db =
match lang with
| "ml" ->
Database_light_ml.compute_database ~verbose xs
| "php" ->
(match xs with
| [_x] ->
let db =
(try
Database_php.check_is_database_dir !with_php_db;
Database_php_storage.open_db !with_php_db
with _ ->
let root = Common2.common_prefix_of_files_or_dirs xs in
let php_files =
Lib_parsing_php.find_php_files_of_dir_or_files xs
+> List.map Common2.relative_to_absolute
in
Database_php_build.create_db
~db_support:Database_php.Mem
~annotate_variables_program:
(Some Check_variables_php.check_and_annotate_program)
~files:(Some php_files)
(Database_php.Project (root, None))
);
in
Common.finalize (fun () ->
Database_light_php.database_code_from_php_database
~verbose db
) (fun () -> Database_php.close_db db)
| _ ->
failwith "for PHP we expect one dir"
)
| "js" ->
Database_light_js.compute_database ~verbose xs
| "cpp" ->
Parse_cpp.init_defs !Flag_parsing_cpp.macros_h;
Database_light_cpp.compute_database ~verbose xs
| "web" ->
let db1 = light_db_of_files_or_dirs "js" xs in
let db2 = light_db_of_files_or_dirs "php" xs in
Database_code.merge_databases db1 db2
| _ -> failwith ("language not supported: " ^ lang)
in
db
(*****************************************************************************)
(* Main action *)
(*****************************************************************************)
let main_action xs =
let xs = xs +> List.map Common2.relative_to_absolute in
let db = light_db_of_files_or_dirs !lang xs in
let file =
match !db_file, xs with
| None, [dir] ->
Filename.concat dir Database_code.default_db_name
| Some s, _ ->
s
| _ ->
failwith "please use -o"
in
let file = Common2.relative_to_absolute file in
let res = Common2.y_or_no (spf "writing data in %s" file) in
if not res
then failwith "ok I stop";
Database_code.save_database db file;
()
(*****************************************************************************)
(* Extra actions *)
(*****************************************************************************)
(*---------------------------------------------------------------------------*)
(* Pleac *)
(*---------------------------------------------------------------------------*)
(* Why put pleac code here ? Because pleac.ml helps generate code from
* pleac.data files which transforms all this doc into regular code
* that can be indexed as any other code and shown via the visualizer.
* You can then benefit for free from the navigation/visualization of
* pfff_visual and in turn improves the pfff_visual experience by
* being the source for a good "goto-example-or-test" repository of code.
* We just leverage another "code-artifact".
*
* ex: ./pfff_db_light -lang ml -gen_pleac
* ~/software-src/pleac/
* ex: ./pfff_db_light -lang php -output_dir ~/www/flib/pleac/
* -gen_pleac ~/software-src/pleac/
*)
let gen_pleac pleac_src =
let skeleton_file = Filename.concat pleac_src "skeleton.sgml" in
(* The section names are in the skeleton file *)
let skeleton =
Pleac.parse_skeleton_file skeleton_file in
let pleac_data_file, ext_file,
hook_start_section2, hook_line_body, hook_end_section2 =
match !lang with
| "ml" ->
"ocaml", "ml",
(* TODO if at some point we use a real parser for ML,
* this will not work ...
*)
(fun s -> spf "let pleac_%s () = " s),
(* ugly: for now my ocaml tagger does not index functions
* when they are not at the toplevel so add this extra
* space
*)
(fun s -> " " ^ s),
(fun s -> "")
| "php" ->
"php", "php",
(* TODO if at some point we use a real parser for ML,
* this will not work ...
*)
(fun s -> spf "<?php\nfunction pleac_%s() {" s),
(fun s -> s),
(fun s -> "}\n?>\n")
| _ -> failwith (spf "language %s is not yet supported" !lang)
in
let pleac_data_file =
Filename.concat pleac_src (spf "pleac_%s.data" pleac_data_file)
in
let code_sections =
Pleac.parse_data_file pleac_data_file in
let comment_style =
Pleac.detect_comment_style pleac_data_file in
Pleac.gen_source_files skeleton
code_sections comment_style
~output_dir:!pleac_dir
~gen_mode:Pleac.OneDirPerSection
~ext_file
~hook_start_section2
~hook_line_body
~hook_end_section2
;
()
(*---------------------------------------------------------------------------*)
(* the command line flags *)
(*---------------------------------------------------------------------------*)
let extra_actions () = [
"-gen_pleac", " <pleac_src>; works with -lang",
Common.mk_action_1_arg (gen_pleac);
]
(*****************************************************************************)
(* The options *)
(*****************************************************************************)
let all_actions () =
extra_actions() ++
Test_program_lang.actions () ++
[]
let options () =
[
"-verbose", Arg.Set verbose,
" ";
"-lang", Arg.Set_string lang,
(spf " <str> choose language (default = %s)" !lang);
"-o", Arg.String (fun s -> db_file := Some s),
(spf " <file> output file (default = %s)" Database_code.default_db_name);
"-with_php_db", Arg.Set_string with_php_db,
(" <metapath>");
"-output_dir", Arg.Set_string pleac_dir,
(spf " <dir> output file (default = %s)" !pleac_dir);
] ++
Flag_parsing_cpp.cmdline_flags_macrofile() ++
Common.options_of_actions action (all_actions()) ++
Common2.cmdline_flags_devel () ++
Common2.cmdline_flags_other () ++
[
"-version", Arg.Unit (fun () ->
pr2 (spf "pfff (console) version: %s" Config_pfff.version);
exit 0;
),
" guess what";
(* this can not be factorized in Common *)
"-date", Arg.Unit (fun () ->
pr2 "version: $Date: 2008/10/26 00:44:57 $";
raise (Common.UnixExit 0)
),
" guess what";
] ++
[]
(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let main () =
Gc.set {(Gc.get ()) with Gc.stack_limit = 1000 * 1024 * 1024};
(* Common_extra.set_link();
let argv = Features.Distribution.mpi_adjust_argv Sys.argv in
*)
(* ugly *)
Database_php_storage.set_link ();
let usage_msg =
"Usage: " ^ Common2.basename Sys.argv.(0) ^
" [options] <file or dir> " ^ "\n" ^ "Options are:"
in
(* does side effect on many global flags *)
let args = Common.parse_options (options()) usage_msg Sys.argv in
(* must be done after Arg.parse, because Common.profile is set by it *)
Common.profile_code "Main total" (fun () ->
(match args with
(* --------------------------------------------------------- *)
(* actions, useful to debug subpart *)
(* --------------------------------------------------------- *)
| xs when List.mem !action (Common.action_list (all_actions())) ->
Common.do_action !action xs (all_actions())
| _ when not (Common.null_string !action) ->
failwith ("unrecognized action or wrong params: " ^ !action)
(* --------------------------------------------------------- *)
(* main entry *)
(* --------------------------------------------------------- *)
| x::xs ->
main_action (x::xs)
(* --------------------------------------------------------- *)
(* empty entry *)
(* --------------------------------------------------------- *)
| [] ->
Common.usage usage_msg (options());
failwith "too few arguments"
)
)
(*****************************************************************************)
let _ =
Common.main_boilerplate (fun () ->
main ();
)