Skip to content

Commit 6e9ff6b

Browse files
committed
Add Noop, a better variant of Null for internal usages
1 parent 37421cc commit 6e9ff6b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

py/dml/codegen.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def fail(self, site):
210210
class IgnoreFailure(Failure):
211211
'''Ignore exceptions'''
212212
def fail(self, site):
213-
return mkNull(site)
213+
return mkNoop(site)
214214

215215
class ExitHandler(ABC):
216216
current = None
@@ -1916,9 +1916,10 @@ def codegen_statements(trees, *args):
19161916
return stmts
19171917

19181918
def codegen_statement(tree, *args):
1919-
rbrace_site = tree.args[1] if tree.kind == 'compound' else None
1920-
return mkCompound(tree.site, codegen_statements([tree], *args),
1921-
rbrace_site)
1919+
stmts = codegen_statements([tree], *args)
1920+
if len(stmts) == 1 and not stmts[0].is_declaration:
1921+
return stmts[0]
1922+
return mkCompound(tree.site, stmts)
19221923

19231924
@statement_dispatcher
19241925
def stmt_compound(stmt, location, scope):
@@ -2190,7 +2191,7 @@ def stmt_saved(stmt, location, scope):
21902191

21912192
@statement_dispatcher
21922193
def stmt_null(stmt, location, scope):
2193-
return []
2194+
return [mkNull(stmt.site)]
21942195

21952196
@statement_dispatcher
21962197
def stmt_if(stmt, location, scope):
@@ -3207,7 +3208,7 @@ def stmt_while(stmt, location, scope):
32073208
[cond, statement] = stmt.args
32083209
cond = as_bool(codegen_expression(cond, location, scope))
32093210
if stmt.site.dml_version() == (1, 2) and cond.constant and not cond.value:
3210-
return [mkNull(stmt.site)]
3211+
return [mkNoop(stmt.site)]
32113212
else:
32123213
with CLoopContext():
32133214
res = mkWhile(stmt.site, cond,
@@ -3345,7 +3346,7 @@ def mkcall_method(site, func, indices):
33453346

33463347
def common_inline(site, method, indices, inargs, outargs):
33473348
if not verify_args(site, method.inp, method.outp, inargs, outargs):
3348-
return mkNull(site)
3349+
return mkNoop(site)
33493350

33503351
if dml.globals.debuggable:
33513352
if method.fully_typed and (
@@ -3999,7 +4000,7 @@ def codegen_call_traitmethod(site, expr, inargs, outargs):
39994000
if not isinstance(expr, TraitMethodRef):
40004001
raise ICE(site, "cannot call %r: not a trait method" % (expr,))
40014002
if not verify_args(site, expr.inp, expr.outp, inargs, outargs):
4002-
return mkNull(site)
4003+
return mkNoop(site)
40034004
def mkcall(args):
40044005
rettype = c_rettype(expr.outp, expr.throws)
40054006
# implicitly convert endian int arguments to integers
@@ -4011,7 +4012,7 @@ def mkcall(args):
40114012
def codegen_call(site, meth_node, indices, inargs, outargs):
40124013
'''Generate a call using a direct reference to the method node'''
40134014
if not verify_args(site, meth_node.inp, meth_node.outp, inargs, outargs):
4014-
return mkNull(site)
4015+
return mkNoop(site)
40154016
require_fully_typed(site, meth_node)
40164017
func = method_instance(meth_node)
40174018

py/dml/ctree.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
'param_bool_fixup',
4141

4242
'mkCompound',
43-
'mkNull', 'Null',
43+
'mkNull',
44+
'mkNoop',
4445
'mkLabel',
4546
'mkUnrolledLoop',
4647
'mkGoto',
@@ -432,6 +433,9 @@ def mkCompound(site, statements, rbrace_site=None):
432433
return Compound(site, collapsed, rbrace_site)
433434

434435
class Null(Statement):
436+
'''Should only be used to represent stand-alone ; present in DML code. For
437+
all other purposes (artificially created empty statements) Noop should
438+
be used instead.'''
435439
is_empty = True
436440
def toc_stmt(self):
437441
self.linemark()
@@ -441,6 +445,19 @@ def toc(self):
441445

442446
mkNull = Null
443447

448+
class Noop(Statement):
449+
'''An empty statement represented by a pair of braces in generated C. This
450+
avoids certain GCC and Coverity warnings that could manifest if ';' were
451+
used instead.'''
452+
is_empty = True
453+
def toc_stmt(self):
454+
self.linemark()
455+
out('{}\n')
456+
def toc(self):
457+
pass
458+
459+
mkNoop = Noop
460+
444461
class Label(Statement):
445462
def __init__(self, site, label, unused=False):
446463
Statement.__init__(self, site)
@@ -664,7 +681,7 @@ def toc_stmt(self):
664681

665682
def mkExpressionStatement(site, expr):
666683
if isinstance(expr, Constant):
667-
return mkNull(site)
684+
return mkNoop(site)
668685
return ExpressionStatement(site, expr)
669686

670687
def toc_constsafe_pointer_assignment(site, source, target, typ):
@@ -840,7 +857,7 @@ def mkIf(site, cond, truebranch, falsebranch = None, else_site=None):
840857
elif falsebranch:
841858
return falsebranch
842859
else:
843-
return mkNull(site)
860+
return mkNoop(site)
844861
return If(site, cond, truebranch, falsebranch, else_site)
845862

846863
class While(Statement):

0 commit comments

Comments
 (0)