forked from rescript-lang/reanalyze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReanalyze.re
309 lines (294 loc) · 8.89 KB
/
Reanalyze.re
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
open Common;
type analysisType =
| All
| Dce
| Exception
| Noalloc
| Termination;
let loadCmtFile = (~analysis, cmtFilePath) => {
let cmt_infos = Cmt_format.read_cmt(cmtFilePath);
let excludePath = sourceFile =>
Cli.excludePaths^
|> List.exists(prefix_ => {
let prefix =
Filename.is_relative(sourceFile)
? prefix_ : Filename.concat(Sys.getcwd(), prefix_);
String.length(prefix) <= String.length(sourceFile)
&& (
try(String.sub(sourceFile, 0, String.length(prefix)) == prefix) {
| Invalid_argument(_) => false
}
);
});
switch (cmt_infos.cmt_annots |> FindSourceFile.cmt) {
| Some(sourceFile) when !excludePath(sourceFile) =>
if (Cli.debug^) {
Log_.item(
"Scanning %s Source:%s@.",
Cli.ci^ && !Filename.is_relative(cmtFilePath)
? Filename.basename(cmtFilePath) : cmtFilePath,
Cli.ci^ && !Filename.is_relative(sourceFile)
? sourceFile |> Filename.basename : sourceFile,
);
};
FileReferences.addFile(sourceFile);
currentSrc := sourceFile;
currentModule := Paths.getModuleName(sourceFile);
currentModuleName :=
currentModule^
|> Name.create(~isInterface=Filename.check_suffix(currentSrc^, "i"));
switch (analysis) {
| All =>
cmt_infos |> DeadCode.processCmt(~cmtFilePath);
cmt_infos |> Exception.processCmt;
cmt_infos |> Arnold.processCmt;
| Dce => cmt_infos |> DeadCode.processCmt(~cmtFilePath)
| Exception => cmt_infos |> Exception.processCmt
| Noalloc => cmt_infos |> Noalloc.processCmt
| Termination => cmt_infos |> Arnold.processCmt
};
| _ => ()
};
};
let runAnalysis = (~analysis, ~cmtRoot, ~ppf) => {
Log_.Color.setup();
let (+++) = Filename.concat;
switch (cmtRoot) {
| Some(root) =>
let rec walkSubDirs = dir => {
let absDir = dir == "" ? root : root +++ dir;
let skipDir = {
let base = Filename.basename(dir);
base == "node_modules" || base == "_esy";
};
if (!skipDir && Sys.file_exists(absDir)) {
if (Sys.is_directory(absDir)) {
absDir |> Sys.readdir |> Array.iter(d => walkSubDirs(dir +++ d));
} else if (Filename.check_suffix(absDir, ".cmt")
|| Filename.check_suffix(absDir, ".cmti")) {
absDir |> loadCmtFile(~analysis);
};
};
};
walkSubDirs("");
| None =>
Paths.setProjectRoot();
let lib_bs = Suppress.projectRoot^ +++ "lib" +++ "bs";
let sourceDirs =
Paths.readSourceDirs(~configSources=None) |> List.sort(String.compare);
sourceDirs
|> List.iter(sourceDir => {
let libBsSourceDir = Filename.concat(lib_bs, sourceDir);
let files =
switch (Sys.readdir(libBsSourceDir) |> Array.to_list) {
| files => files
| exception (Sys_error(_)) => []
};
let cmtFiles =
files
|> List.filter(x =>
Filename.check_suffix(x, ".cmt")
|| Filename.check_suffix(x, ".cmti")
);
cmtFiles
|> List.sort(String.compare)
|> List.iter(cmtFile => {
let cmtFilePath = Filename.concat(libBsSourceDir, cmtFile);
cmtFilePath |> loadCmtFile(~analysis);
});
});
};
let dce = () => {
DeadException.forceDelayedItems();
DeadOptionalArgs.forceDelayedItems();
DeadCommon.reportDead(~checkOptionalArg=DeadOptionalArgs.check, ppf);
DeadCommon.WriteDeadAnnotations.write();
};
switch (analysis) {
| All =>
dce();
Exception.reportResults(~ppf);
Arnold.reportResults(~ppf);
| Dce => dce()
| Exception => Exception.reportResults(~ppf)
| Noalloc => Noalloc.reportResults(~ppf)
| Termination => Arnold.reportResults(~ppf)
};
Log_.Stats.report();
Log_.Stats.clear();
};
type cliCommand =
| All(option(string))
| Exception(option(string))
| DCE(option(string))
| Noalloc
| NoOp
| Termination(option(string));
[@raises exit]
let cli = () => {
let cliCommand = ref(NoOp);
let usage = "reanalyze version " ++ Version.version;
[@raises exit]
let versionAndExit = () => {
print_endline(usage);
exit(0);
};
[@raises exit]
let rec printUsageAndExit = () => {
Arg.usage(speclist, usage);
exit(0);
}
[@raises exit]
and setCliCommand = command => {
if (cliCommand^ != NoOp) {
printUsageAndExit();
};
cliCommand := command;
}
[@raises exit]
and setAll = cmtRoot => {
All(cmtRoot) |> setCliCommand;
}
[@raises exit]
and setDCE = cmtRoot => {
DCE(cmtRoot) |> setCliCommand;
}
and setDebug = () => {
Cli.debug := true;
}
[@raises exit]
and setException = cmtRoot => {
Exception(cmtRoot) |> setCliCommand;
}
and setExperimental = () => {
Common.Cli.experimental := true;
}
[@raises exit]
and setNoalloc = () => {
Noalloc |> setCliCommand;
}
and setSuppress = s => {
let names = s |> String.split_on_char(',');
Suppress.suppress := names @ Suppress.suppress^;
}
and setUnsuppress = s => {
let names = s |> String.split_on_char(',');
Suppress.unsuppress := names @ Suppress.unsuppress^;
}
and setWrite = () => {
Common.Cli.write := true;
}
[@raises exit]
and setTermination = cmtRoot => {
Termination(cmtRoot) |> setCliCommand;
}
and setLiveNames = s => {
let names = s |> String.split_on_char(',');
Common.Cli.liveNames := names @ Common.Cli.liveNames.contents;
}
and setExcludePaths = s => {
let paths = s |> String.split_on_char(',');
Common.Cli.excludePaths := paths @ Common.Cli.excludePaths.contents;
}
and setLivePaths = s => {
let paths = s |> String.split_on_char(',');
Common.Cli.livePaths := paths @ Common.Cli.livePaths.contents;
}
and speclist = [
("-all", Arg.Unit(() => setAll(None)), "Run all the analyses."),
(
"-all-cmt",
Arg.String(s => setAll(Some(s))),
"root_path Run all the analyses for all the .cmt files under the root path",
),
("-ci", Arg.Unit(() => Cli.ci := true), "Internal flag for use in CI"),
("-dce", Arg.Unit(() => setDCE(None)), "Eperimental DCE"),
("-debug", Arg.Unit(setDebug), "Print debug information"),
(
"-dce-cmt",
Arg.String(s => setDCE(Some(s))),
"root_path Experimental DCE for all the .cmt files under the root path",
),
(
"-exception",
Arg.Unit(() => setException(None)),
"Experimental exception analysis",
),
(
"-exception-cmt",
Arg.String(s => setException(Some(s))),
"root_path Experimental exception analysis for all the .cmt files under the root path",
),
(
"-exclude-paths",
Arg.String(s => setExcludePaths(s)),
"comma-separated-path-prefixes Exclude from analysis files whose path has a prefix in the list",
),
(
"-experimental",
Arg.Unit(setExperimental),
"Turn on experimental analyses (this option is currently unused)",
),
(
"-live-names",
Arg.String(s => setLiveNames(s)),
"comma-separated-names Consider all values with the give names as live",
),
(
"-live-paths",
Arg.String(s => setLivePaths(s)),
"comma-separated-path-prefixes Consider all values whose path has a prefix in the list as live",
),
("-noalloc", Arg.Unit(setNoalloc), ""),
(
"-suppress",
Arg.String(setSuppress),
"comma-separated-path-prefixes Don't report on files whose path has a prefix in the list",
),
(
"-termination",
Arg.Unit(() => setTermination(None)),
"Experimental termination analysis",
),
(
"-termination-cmt",
Arg.String(s => setTermination(Some(s))),
"root_path Experimental termination analysis for all the .cmt files under the root path",
),
(
"-unsuppress",
Arg.String(setUnsuppress),
"comma-separated-path-prefixes Report on files whose path a prefix in the list, overriding -suppress (no-op if -suppress is not specified)",
),
(
"-version",
Arg.Unit(versionAndExit),
"Show version information and exit",
),
(
"--version",
Arg.Unit(versionAndExit),
"Show version information and exit",
),
(
"-write",
Arg.Unit(setWrite),
"Write @dead annotations directly in the source files",
),
];
let ppf = Format.std_formatter;
[@raises exit]
let executeCliCommand = cliCommand =>
switch (cliCommand) {
| NoOp => printUsageAndExit()
| All(cmtRoot) => runAnalysis(~analysis=All, ~cmtRoot, ~ppf)
| DCE(cmtRoot) => runAnalysis(~analysis=Dce, ~cmtRoot, ~ppf)
| Exception(cmtRoot) => runAnalysis(~analysis=Exception, ~cmtRoot, ~ppf)
| Noalloc => runAnalysis(~analysis=Noalloc, ~cmtRoot=None, ~ppf)
| Termination(cmtRoot) =>
runAnalysis(~analysis=Termination, ~cmtRoot, ~ppf)
};
Arg.parse(speclist, print_endline, usage);
executeCliCommand(cliCommand^);
};
cli();