forked from rescript-lang/reanalyze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReanalyze.re
227 lines (211 loc) · 6.29 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
open DeadCommon;
let version = Version.version;
type analysisType =
| Dce
| Exception
| Termination;
let loadCmtFile = (~analysis, cmtFilePath) => {
if (debug^) {
Log_.item("Scanning %s@.", cmtFilePath);
};
let cmt_infos = Cmt_format.read_cmt(cmtFilePath);
switch (cmt_infos.cmt_annots |> FindSourceFile.cmt) {
| None => ()
| Some(sourceFile) =>
FileHash.addFile(fileReferences, sourceFile);
currentSrc := sourceFile;
currentModule := Paths.getModuleName(sourceFile);
currentModuleName :=
currentModule^
|> Name.create(~isInterface=Filename.check_suffix(currentSrc^, "i"));
switch (analysis) {
| Dce => cmt_infos |> DeadCode.processCmt(~cmtFilePath)
| Exception => cmt_infos |> Exception.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 = Paths.projectRoot^ +++ "lib" +++ "bs";
let sourceDirs = Paths.readSourceDirs(~configSources=None);
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.iter(cmtFile => {
let cmtFilePath = Filename.concat(libBsSourceDir, cmtFile);
cmtFilePath |> loadCmtFile(~analysis);
});
});
};
switch (analysis) {
| Dce =>
reportDead(ppf);
WriteDeadAnnotations.write();
| Exception => Exception.reportResults(~ppf)
| Termination => Arnold.reportResults(~ppf)
};
};
type cliCommand =
| Exception(option(string))
| DCE(option(string))
| NoOp
| Termination(option(string));
let cli = () => {
let cliCommand = ref(NoOp);
let usage = "reanalyze version " ++ version;
let versionAndExit = () => {
print_endline(usage);
exit(0);
};
let rec printUsageAndExit = () => {
Arg.usage(speclist, usage);
exit(0);
}
and setCliCommand = command => {
if (cliCommand^ != NoOp) {
printUsageAndExit();
};
cliCommand := command;
}
and setDCE = cmtRoot => {
DCE(cmtRoot) |> setCliCommand;
}
and setException = cmtRoot => {
Exception(cmtRoot) |> setCliCommand;
}
and setDebug = () => {
DeadCommon.debug := true;
}
and setBlacklist = s => {
let names = s |> String.split_on_char(',');
Blacklist.blacklist := names @ Blacklist.blacklist.contents;
}
and setWhitelist = s => {
let names = s |> String.split_on_char(',');
Blacklist.whitelist := names @ Blacklist.whitelist.contents;
}
and setWrite = () => {
DeadCommon.write := true;
}
and setTermination = cmtRoot => {
Termination(cmtRoot) |> setCliCommand;
}
and setLiveNames = s => {
let names = s |> String.split_on_char(',');
DeadCommon.liveNames := names @ DeadCommon.liveNames.contents;
}
and setLivePaths = s => {
let paths = s |> String.split_on_char(',');
DeadCommon.livePaths := paths @ DeadCommon.livePaths.contents;
}
and speclist = [
(
"-blacklist",
Arg.String(setBlacklist),
"comma-separated-path-prefixes Don't report on files whose path has a prefix in the list",
),
("-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 eexception analysis",
),
(
"-exception-cmt",
Arg.String(s => setException(Some(s))),
"root_path Experimental exception analysis for all the .cmt files under the root path",
),
(
"-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",
),
(
"-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",
),
(
"-version",
Arg.Unit(versionAndExit),
"Show version information and exit",
),
(
"--version",
Arg.Unit(versionAndExit),
"Show version information and exit",
),
(
"-whitelist",
Arg.String(setWhitelist),
"comma-separated-path-prefixes Report on files whose path a prefix in the list, overriding blacklist (no-op if a blacklist is not specified)",
),
(
"-write",
Arg.Unit(setWrite),
"Write @dead annotations directly in the source files",
),
];
let ppf = Format.std_formatter;
let executeCliCommand = cliCommand =>
switch (cliCommand) {
| NoOp => printUsageAndExit()
| DCE(cmtRoot) => runAnalysis(~analysis=Dce, ~cmtRoot, ~ppf)
| Exception(cmtRoot) => runAnalysis(~analysis=Exception, ~cmtRoot, ~ppf)
| Termination(cmtRoot) =>
runAnalysis(~analysis=Termination, ~cmtRoot, ~ppf)
};
Arg.parse(speclist, print_endline, usage);
executeCliCommand(cliCommand^);
};
cli();