Skip to content

Commit

Permalink
Add line filename and line number information to acorn-optimizer errors
Browse files Browse the repository at this point in the history
This helps a lot when trying to debug emscripten-core#20818.
  • Loading branch information
sbc100 committed Dec 4, 2023
1 parent 66db982 commit 1dd30e1
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions tools/acorn-optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ function assert(condition, text) {
}
}

function assertAt(condition, offset, message) {
if (!condition) {
const loc = acorn.getLineInfo(input, offset);
throw new Error(`${infile}:${loc.line}: ${message} (use EMCC_DEBUG_SAVE=1 to preserve temporary inputs)`);
}
}

function warnOnce(msg) {
if (!warnOnce.msgs) warnOnce.msgs = {};
if (msg in warnOnce.msgs) return;
Expand Down Expand Up @@ -190,7 +197,7 @@ function restoreForVars(node) {
let restored = 0;
function fix(init) {
if (init && init.type === 'EmptyStatement') {
assert(init.oldDeclarations);
assertAt(init.oldDeclarations, init.start);
init.type = 'VariableDeclaration';
init.declarations = init.oldDeclarations;
restored++;
Expand Down Expand Up @@ -427,12 +434,12 @@ function runJSDCE(ast, aggressive) {
});
} else if (id.type === 'ArrayPattern') {
id.elements.forEach((node) => {
assert(node.type === 'Identifier');
assertAt(node.type === 'Identifier', node.start, "expected Indentifier but found " + node.type);
const name = node.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
});
} else {
assert(id.type === 'Identifier');
assertAt(id.type === 'Identifier', id.start);
const name = id.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
}
Expand Down Expand Up @@ -726,7 +733,7 @@ function emitDCEGraph(ast) {
// use the left hand identifier.
value = value.left;
}
assert(value.type === 'Identifier');
assertAt(value.type === 'Identifier', value.start);
imports.push(value.name); // the name doesn't matter, only the value which is that actual thing we are importing
});
foundWasmImportsAssign = true;
Expand Down Expand Up @@ -786,7 +793,7 @@ function emitDCEGraph(ast) {
// var x = Module['x'] = 1234;
// this form occurs when global addresses are exported from the
// module. It doesn't constitute a usage.
assert(typeof value.right.value === 'number');
assertAt(typeof value.right.value === 'number', value.start);
emptyOut(node);
}
}
Expand Down Expand Up @@ -1720,7 +1727,7 @@ function minifyLocals(ast) {
// locals are just numbers, not functions; functions are all declared
// in the outer scope. If a local is called, that is a bug.
if (node.callee.type === 'Identifier') {
assert(!isLocalName(node.callee.name), 'cannot call a local');
assertAt(!isLocalName(node.callee.name), node.callee.start, 'cannot call a local');
}
},
});
Expand Down

0 comments on commit 1dd30e1

Please sign in to comment.