Skip to content

Commit

Permalink
Improve errors on mismatched #endif/#else in JS library files. NFC (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Aug 8, 2024
1 parent c26eb85 commit 08f5c0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,19 @@ export function preprocess(filename) {
}
}
} else if (first === '#else') {
assert(showStack.length > 0);
if (showStack.length == 0) {
error(`${filename}:${i + 1}: #else without matching #if`);
}
const curr = showStack.pop();
if (curr == IGNORE) {
showStack.push(SHOW);
} else {
showStack.push(IGNORE);
}
} else if (first === '#endif') {
assert(showStack.length > 0);
if (showStack.length == 0) {
error(`${filename}:${i + 1}: #endif without matching #if`);
}
showStack.pop();
} else if (first === '#warning') {
if (showCurrentLine()) {
Expand Down
16 changes: 16 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -4522,6 +4522,22 @@ def test_js_lib_using_asm_lib(self):
''')
self.do_runf('src.c', 'c calling: 14\n', emcc_args=['--js-library', 'lib.js'])

def test_js_lib_errors(self):
create_file('lib.js', '''\
// This is a library file
#endif // line 2
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library', 'lib.js'])
self.assertContained('lib.js:2: #endif without matching #if', err)

create_file('lib.js', '''\
// This is a library file

#else // line 3
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library', 'lib.js'])
self.assertContained('lib.js:3: #else without matching #if', err)

def test_js_internal_deps(self):
create_file('lib.js', r'''
addToLibrary({
Expand Down

0 comments on commit 08f5c0f

Please sign in to comment.