Skip to content

Commit 4df23b8

Browse files
committed
poor man's global dead code analyzer for ocaml
1 parent 10af602 commit 4df23b8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

.vscode/tasks.json

+16
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@
5050
}
5151
]
5252
}
53+
},
54+
{
55+
"label": "check unused",
56+
"command" : "node",
57+
"options": {
58+
"cwd": "${workspaceRoot}/lib"
59+
},
60+
"args": [
61+
"../scripts/checkUnused.js"
62+
],
63+
"problemMatcher" :{
64+
"base": "$ocamlc",
65+
"fileLocation" :
66+
"autoDetect",
67+
"owner": "globalUnused"
68+
}
5369
}
5470
]
5571
}

scripts/checkUnused.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
//@ts-check
3+
var cp = require("child_process");
4+
var path = require("path");
5+
var fs = require("fs");
6+
var opt = path.join(__dirname, "..", "native", "4.06.1", "bin", "ocamlopt.opt");
7+
8+
// TOOD: share with ninja.js
9+
// check compiler code base
10+
var sourceDirs = [
11+
"stubs",
12+
"ext",
13+
"common",
14+
"js_parser",
15+
"syntax",
16+
"depends",
17+
"core",
18+
"super_errors",
19+
"outcome_printer",
20+
"bsb",
21+
"bsb_helper",
22+
"ounit",
23+
"ounit_tests",
24+
"main"
25+
];
26+
var fileMap = new Map();
27+
for (let dir of sourceDirs){
28+
let ydir = path.join('jscomp',dir)
29+
let xdir = path.join(__dirname,'..',ydir)
30+
for(let file of fs.readdirSync(xdir,'utf8')){
31+
let p = path.parse(file)
32+
if(p.ext === ".ml" || p.ext === ".mli"){
33+
fileMap[p.base] = ydir
34+
}
35+
}
36+
}
37+
var output = cp.spawnSync(
38+
`${opt} -c -opaque -linscan -I 4.06.1 -w a+32 4.06.1/whole_compiler.mli 4.06.1/whole_compiler.ml`,
39+
{ cwd: path.join(__dirname, "..", "lib"), encoding: "utf8", shell: true }
40+
);
41+
42+
// debugger
43+
44+
var result = output.stderr.replace(/File "(.*)"/g,(file,p1)=>{
45+
let query = fileMap[p1]
46+
if(!query){
47+
return `Unkonwn file`
48+
}
49+
return `File "${query}/${p1}"`
50+
})
51+
52+
console.log(result)

0 commit comments

Comments
 (0)