Skip to content

Commit 6071f78

Browse files
authored
Improve error/warning reporting during JS library processing (#19029)
Keep track of which file is bring processed not just during pre-processing but during the entire processing of a given library file.
1 parent fda8dd1 commit 6071f78

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/modules.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ global.LibraryManager = {
215215
},
216216
});
217217
}
218+
currentFile = filename;
218219
try {
219220
processed = processMacros(preprocess(filename));
220221
vm.runInThisContext(processed, { filename: filename.replace(/\.\w+$/, '.preprocessed$&') });
@@ -232,6 +233,7 @@ global.LibraryManager = {
232233
}
233234
throw e;
234235
} finally {
236+
currentFile = null;
235237
if (origLibrary) {
236238
this.library = origLibrary;
237239
}

src/parseTools.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
global.FOUR_GB = 4 * 1024 * 1024 * 1024;
1212
const FLOAT_TYPES = new Set(['float', 'double']);
1313

14-
let currentlyParsedFilename = '';
15-
1614
// Does simple 'macro' substitution, using Django-like syntax,
1715
// {{{ code }}} will be replaced with |eval(code)|.
1816
// NOTE: Be careful with that ret check. If ret is |0|, |ret ? ret.toString() : ''| would result in ''!
@@ -57,7 +55,8 @@ function preprocess(filename) {
5755
const showStack = [];
5856
const showCurrentLine = () => showStack.every((x) => x == SHOW);
5957

60-
currentlyParsedFilename = filename;
58+
const oldFilename = currentFile;
59+
currentFile = filename;
6160
const fileExt = filename.split('.').pop().toLowerCase();
6261
const isHtml = (fileExt === 'html' || fileExt === 'htm') ? true : false;
6362
let inStyle = false;
@@ -155,7 +154,7 @@ function preprocess(filename) {
155154
no matching #endif found (${showStack.length$}' unmatched preprocessing directives on stack)`);
156155
return ret;
157156
} finally {
158-
currentlyParsedFilename = null;
157+
currentFile = oldFilename;
159158
}
160159
}
161160

@@ -565,7 +564,7 @@ function makeDynCall(sig, funcPtr) {
565564

566565

567566
if (funcPtr === undefined) {
568-
warn(`${currentlyParsedFilename}: \
567+
warn(`
569568
Legacy use of {{{ makeDynCall("${sig}") }}}(funcPtr, arg1, arg2, ...). \
570569
Starting from Emscripten 2.0.2 (Aug 31st 2020), syntax for makeDynCall has changed. \
571570
New syntax is {{{ makeDynCall("${sig}", "funcPtr") }}}(arg1, arg2, ...). \

src/utility.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ function dump(item) {
4040
}
4141

4242
global.warnings = false;
43+
global.currentFile = null;
44+
45+
function errorPrefix() {
46+
if (currentFile) {
47+
return currentFile + ': '
48+
} else {
49+
return '';
50+
}
51+
}
4352

4453
function warn(a, msg) {
4554
global.warnings = true;
@@ -48,7 +57,7 @@ function warn(a, msg) {
4857
a = false;
4958
}
5059
if (!a) {
51-
printErr('warning: ' + msg);
60+
printErr(`warning: ${errorPrefix()}${msg}`);
5261
}
5362
}
5463

@@ -69,7 +78,7 @@ global.abortExecution = false;
6978

7079
function error(msg) {
7180
abortExecution = true;
72-
printErr('error: ' + msg);
81+
printErr(`error: ${errorPrefix()}${msg}`);
7382
}
7483

7584
function range(size) {

test/test_other.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,7 +3740,7 @@ def test_js_lib_no_override(self):
37403740

37413741
self.emcc_args += ['--js-library', 'duplicated_func_1.js', '--js-library', 'duplicated_func_2.js']
37423742
err = self.expect_fail([EMCC, 'duplicated_func.c'] + self.get_emcc_args())
3743-
self.assertContained('error: Symbol re-definition in JavaScript library: duplicatedFunc. Do not use noOverride if this is intended', err)
3743+
self.assertContained('duplicated_func_2.js: Symbol re-definition in JavaScript library: duplicatedFunc. Do not use noOverride if this is intended', err)
37443744

37453745
def test_override_stub(self):
37463746
self.do_run_from_file(test_file('other/test_override_stub.c'), test_file('other/test_override_stub.out'))
@@ -3767,7 +3767,7 @@ def test_js_lib_missing_sig(self):
37673767

37683768
self.emcc_args += ['--js-library', 'some_func.js']
37693769
err = self.expect_fail([EMCC, 'some_func.c'] + self.get_emcc_args())
3770-
self.assertContained('error: __sig is missing for function: someFunc. Do not use checkSig if this is intended', err)
3770+
self.assertContained('some_func.js: __sig is missing for function: someFunc. Do not use checkSig if this is intended', err)
37713771

37723772
def test_js_lib_quoted_key(self):
37733773
create_file('lib.js', r'''
@@ -11592,7 +11592,7 @@ def test_jslib_ifdef(self):
1159211592
#endif
1159311593
''')
1159411594
proc = self.run_process([EMCC, test_file('hello_world.c'), '--js-library=lib.js'], stderr=PIPE)
11595-
self.assertContained('warning: use of #ifdef in js library. Use #if instead.', proc.stderr)
11595+
self.assertContained('lib.js: use of #ifdef in js library. Use #if instead.', proc.stderr)
1159611596

1159711597
def test_jslib_mangling(self):
1159811598
create_file('lib.js', '''

0 commit comments

Comments
 (0)