Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/lib/libc_preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ addToLibrary({

// Returns a tokenized array of the given string expression, i.e. "FOO > BAR && BAZ" -> ["FOO", ">", "BAR", "&&", "BAZ"]
// Optionally keeps whitespace as tokens to be able to reconstruct the original input string.
/**
* @param {string} exprString
* @param {(number|boolean)=} keepWhitespace Optional, can be omitted. Defaults to false.
*/
function tokenize(exprString, keepWhitespace) {
var out = [], len = exprString.length;
for (var i = 0; i <= len; ++i) {
Expand Down Expand Up @@ -109,8 +113,12 @@ addToLibrary({
}

// Expands preprocessing macros on substring str[lineStart...lineEnd]
function expandMacros(str, lineStart, lineEnd) {
if (lineEnd === undefined) lineEnd = str.length;
/**
* @param {string} str
* @param {number} lineStart
* @param {number=} lineEnd Optional, may be omitted.
*/
function expandMacros(str, lineStart, lineEnd=str.length) {
var len = str.length;
var out = '';
for (var i = lineStart; i < lineEnd; ++i) {
Expand Down Expand Up @@ -168,7 +176,7 @@ addToLibrary({

if (operatorAndPriority == 13 /* parens '(' */) {
// Find the closing parens position
var j = find_closing_parens_index(tokens, i);
j = find_closing_parens_index(tokens, i);
if (j) {
tokens.splice(i, j+1-i, buildExprTree(tokens.slice(i+1, j)));
return tokens;
Expand Down Expand Up @@ -224,7 +232,7 @@ addToLibrary({
if (i < 0) i = len;

// Find the first non-whitespace character on the line.
for (var j = lineStart; j < i && isWhitespace(code, j); ++j);
for (var j = lineStart; j < i && isWhitespace(code, j);) ++j;

// Is this a non-preprocessor directive line?
var thisLineIsInActivePreprocessingBlock = stack[stack.length-1];
Expand Down
8 changes: 6 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10686,8 +10686,12 @@ def test_function_exports_are_small(self, args, opt, closure):

# Tests the library_c_preprocessor.js functionality.
@crossplatform
def test_c_preprocessor(self):
self.do_runf('test_c_preprocessor.c', cflags=['--js-library', path_from_root('src/lib/libc_preprocessor.js')])
@parameterized({
'': ([],),
'closure': (['--closure=1'],),
})
def test_c_preprocessor(self, args):
self.do_runf('test_c_preprocessor.c', cflags=['--js-library', path_from_root('src/lib/libc_preprocessor.js')] + args)

# Test that legacy settings that have been fixed to a specific value and their value can no longer be changed,
def test_legacy_settings_forbidden_to_change(self):
Expand Down