Skip to content

Commit d3d1ee7

Browse files
devsnektargos
authored andcommitted
lib: add internal check macros
PR-URL: #18852 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 7b850a7 commit d3d1ee7

7 files changed

+69
-9
lines changed

configure

+7
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ intl_optgroup.add_option('--download-path',
439439

440440
parser.add_option_group(intl_optgroup)
441441

442+
parser.add_option('--debug-lib',
443+
action='store_true',
444+
dest='node_debug_lib',
445+
help='build lib with DCHECK macros')
446+
442447
http2_optgroup.add_option('--debug-http2',
443448
action='store_true',
444449
dest='debug_http2',
@@ -949,6 +954,8 @@ def configure_node(o):
949954
if options.enable_static:
950955
o['variables']['node_target_type'] = 'static_library'
951956

957+
o['variables']['node_debug_lib'] = b(options.node_debug_lib)
958+
952959
if options.debug_http2:
953960
o['variables']['debug_http2'] = 1
954961
else:

lib/.eslintrc.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@ rules:
55
no-let-in-for-declaration: error
66
lowercase-name-for-primitive: error
77
non-ascii-character: error
8+
globals:
9+
CHECK: false
10+
CHECK_EQ: false
11+
CHECK_GE: false
12+
CHECK_GT: false
13+
CHECK_LE: false
14+
CHECK_LT: false
15+
CHECK_NE: false
16+
DCHECK: false
17+
DCHECK_EQ: false
18+
DCHECK_GE: false
19+
DCHECK_GT: false
20+
DCHECK_LE: false
21+
DCHECK_LT: false
22+
DCHECK_NE: false

node.gyp

+7
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@
702702
'inputs': [
703703
'<@(library_files)',
704704
'./config.gypi',
705+
'tools/check_macros.py'
705706
],
706707
'outputs': [
707708
'<(SHARED_INTERMEDIATE_DIR)/node_javascript.cc',
@@ -715,6 +716,12 @@
715716
}],
716717
[ 'node_use_perfctr=="false"', {
717718
'inputs': [ 'src/noperfctr_macros.py' ]
719+
}],
720+
[ 'node_debug_lib=="false"', {
721+
'inputs': [ 'tools/nodcheck_macros.py' ]
722+
}],
723+
[ 'node_debug_lib=="true"', {
724+
'inputs': [ 'tools/dcheck_macros.py' ]
718725
}]
719726
],
720727
'action': [

tools/check_macros.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro CHECK(x) = do { if (!(x)) (process._rawDebug("CHECK: x == true"), process.abort()) } while (0);
2+
macro CHECK_EQ(a, b) = CHECK((a) === (b));
3+
macro CHECK_GE(a, b) = CHECK((a) >= (b));
4+
macro CHECK_GT(a, b) = CHECK((a) > (b));
5+
macro CHECK_LE(a, b) = CHECK((a) <= (b));
6+
macro CHECK_LT(a, b) = CHECK((a) < (b));
7+
macro CHECK_NE(a, b) = CHECK((a) !== (b));

tools/dcheck_macros.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro DCHECK(x) = do { if (!(x)) (process._rawDebug("DCHECK: x == true"), process.abort()) } while (0);
2+
macro DCHECK_EQ(a, b) = DCHECK((a) === (b));
3+
macro DCHECK_GE(a, b) = DCHECK((a) >= (b));
4+
macro DCHECK_GT(a, b) = DCHECK((a) > (b));
5+
macro DCHECK_LE(a, b) = DCHECK((a) <= (b));
6+
macro DCHECK_LT(a, b) = DCHECK((a) < (b));
7+
macro DCHECK_NE(a, b) = DCHECK((a) !== (b));

tools/js2c.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,27 @@ def ExpandConstants(lines, constants):
7474

7575

7676
def ExpandMacros(lines, macros):
77+
def expander(s):
78+
return ExpandMacros(s, macros)
7779
for name, macro in macros.items():
78-
start = lines.find(name + '(', 0)
79-
while start != -1:
80+
name_pattern = re.compile("\\b%s\\(" % name)
81+
pattern_match = name_pattern.search(lines, 0)
82+
while pattern_match is not None:
8083
# Scan over the arguments
81-
assert lines[start + len(name)] == '('
8284
height = 1
83-
end = start + len(name) + 1
85+
start = pattern_match.start()
86+
end = pattern_match.end()
87+
assert lines[end - 1] == '('
8488
last_match = end
85-
arg_index = 0
86-
mapping = { }
89+
arg_index = [0] # Wrap state into array, to work around Python "scoping"
90+
mapping = {}
8791
def add_arg(str):
8892
# Remember to expand recursively in the arguments
89-
replacement = ExpandMacros(str.strip(), macros)
90-
mapping[macro.args[arg_index]] = replacement
93+
if arg_index[0] >= len(macro.args):
94+
return
95+
replacement = expander(str.strip())
96+
mapping[macro.args[arg_index[0]]] = replacement
97+
arg_index[0] += 1
9198
while end < len(lines) and height > 0:
9299
# We don't count commas at higher nesting levels.
93100
if lines[end] == ',' and height == 1:
@@ -100,10 +107,13 @@ def add_arg(str):
100107
end = end + 1
101108
# Remember to add the last match.
102109
add_arg(lines[last_match:end-1])
110+
if arg_index[0] < len(macro.args) -1:
111+
lineno = lines.count(os.linesep, 0, start) + 1
112+
raise Exception('line %s: Too few arguments for macro "%s"' % (lineno, name))
103113
result = macro.expand(mapping)
104114
# Replace the occurrence of the macro with the expansion
105115
lines = lines[:start] + result + lines[end:]
106-
start = lines.find(name + '(', start)
116+
pattern_match = name_pattern.search(lines, start + len(result))
107117
return lines
108118

109119

tools/nodcheck_macros.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro DCHECK(x) = void(x);
2+
macro DCHECK_EQ(a, b) = void(a, b);
3+
macro DCHECK_GE(a, b) = void(a, b);
4+
macro DCHECK_GT(a, b) = void(a, b);
5+
macro DCHECK_LE(a, b) = void(a, b);
6+
macro DCHECK_LT(a, b) = void(a, b);
7+
macro DCHECK_NE(a, b) = void(a, b);

0 commit comments

Comments
 (0)