Skip to content

Commit

Permalink
lib: add internal check macros
Browse files Browse the repository at this point in the history
PR-URL: #18852
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
devsnek committed Mar 5, 2018
1 parent 0c25cdf commit 3ed363c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
7 changes: 7 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ intl_optgroup.add_option('--download-path',

parser.add_option_group(intl_optgroup)

parser.add_option('--debug-lib',
action='store_true',
dest='node_debug_lib',
help='build lib with DCHECK macros')

http2_optgroup.add_option('--debug-http2',
action='store_true',
dest='debug_http2',
Expand Down Expand Up @@ -935,6 +940,8 @@ def configure_node(o):
if options.enable_static:
o['variables']['node_target_type'] = 'static_library'

o['variables']['node_debug_lib'] = b(options.node_debug_lib)

if options.debug_http2:
o['variables']['debug_http2'] = 1
else:
Expand Down
15 changes: 15 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ rules:
node-core/no-let-in-for-declaration: error
node-core/lowercase-name-for-primitive: error
node-core/non-ascii-character: error
globals:
CHECK: false
CHECK_EQ: false
CHECK_GE: false
CHECK_GT: false
CHECK_LE: false
CHECK_LT: false
CHECK_NE: false
DCHECK: false
DCHECK_EQ: false
DCHECK_GE: false
DCHECK_GT: false
DCHECK_LE: false
DCHECK_LT: false
DCHECK_NE: false
7 changes: 7 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@
'inputs': [
'<@(library_files)',
'./config.gypi',
'tools/check_macros.py'
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/node_javascript.cc',
Expand All @@ -724,6 +725,12 @@
}],
[ 'node_use_perfctr=="false"', {
'inputs': [ 'src/noperfctr_macros.py' ]
}],
[ 'node_debug_lib=="false"', {
'inputs': [ 'tools/nodcheck_macros.py' ]
}],
[ 'node_debug_lib=="true"', {
'inputs': [ 'tools/dcheck_macros.py' ]
}]
],
'action': [
Expand Down
7 changes: 7 additions & 0 deletions tools/check_macros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro CHECK(x) = do { if (!(x)) (process._rawDebug("CHECK: x == true"), process.abort()) } while (0);
macro CHECK_EQ(a, b) = CHECK((a) === (b));
macro CHECK_GE(a, b) = CHECK((a) >= (b));
macro CHECK_GT(a, b) = CHECK((a) > (b));
macro CHECK_LE(a, b) = CHECK((a) <= (b));
macro CHECK_LT(a, b) = CHECK((a) < (b));
macro CHECK_NE(a, b) = CHECK((a) !== (b));
7 changes: 7 additions & 0 deletions tools/dcheck_macros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro DCHECK(x) = do { if (!(x)) (process._rawDebug("DCHECK: x == true"), process.abort()) } while (0);
macro DCHECK_EQ(a, b) = DCHECK((a) === (b));
macro DCHECK_GE(a, b) = DCHECK((a) >= (b));
macro DCHECK_GT(a, b) = DCHECK((a) > (b));
macro DCHECK_LE(a, b) = DCHECK((a) <= (b));
macro DCHECK_LT(a, b) = DCHECK((a) < (b));
macro DCHECK_NE(a, b) = DCHECK((a) !== (b));
28 changes: 19 additions & 9 deletions tools/js2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,27 @@ def ExpandConstants(lines, constants):


def ExpandMacros(lines, macros):
def expander(s):
return ExpandMacros(s, macros)
for name, macro in macros.items():
start = lines.find(name + '(', 0)
while start != -1:
name_pattern = re.compile("\\b%s\\(" % name)
pattern_match = name_pattern.search(lines, 0)
while pattern_match is not None:
# Scan over the arguments
assert lines[start + len(name)] == '('
height = 1
end = start + len(name) + 1
start = pattern_match.start()
end = pattern_match.end()
assert lines[end - 1] == '('
last_match = end
arg_index = 0
mapping = { }
arg_index = [0] # Wrap state into array, to work around Python "scoping"
mapping = {}
def add_arg(str):
# Remember to expand recursively in the arguments
replacement = ExpandMacros(str.strip(), macros)
mapping[macro.args[arg_index]] = replacement
if arg_index[0] >= len(macro.args):
return
replacement = expander(str.strip())
mapping[macro.args[arg_index[0]]] = replacement
arg_index[0] += 1
while end < len(lines) and height > 0:
# We don't count commas at higher nesting levels.
if lines[end] == ',' and height == 1:
Expand All @@ -100,10 +107,13 @@ def add_arg(str):
end = end + 1
# Remember to add the last match.
add_arg(lines[last_match:end-1])
if arg_index[0] < len(macro.args) -1:
lineno = lines.count(os.linesep, 0, start) + 1
raise Exception('line %s: Too few arguments for macro "%s"' % (lineno, name))
result = macro.expand(mapping)
# Replace the occurrence of the macro with the expansion
lines = lines[:start] + result + lines[end:]
start = lines.find(name + '(', start)
pattern_match = name_pattern.search(lines, start + len(result))
return lines


Expand Down
7 changes: 7 additions & 0 deletions tools/nodcheck_macros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro DCHECK(x) = void(x);
macro DCHECK_EQ(a, b) = void(a, b);
macro DCHECK_GE(a, b) = void(a, b);
macro DCHECK_GT(a, b) = void(a, b);
macro DCHECK_LE(a, b) = void(a, b);
macro DCHECK_LT(a, b) = void(a, b);
macro DCHECK_NE(a, b) = void(a, b);

0 comments on commit 3ed363c

Please sign in to comment.