From f159bcdadc181677777d75f96fd222a66abd4cd2 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 14 May 2018 20:33:52 +1000 Subject: [PATCH] Remove parenthesesPolicy, part 1 of N --- cmd/wuffs-c/internal/cgen/statement.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index d9e0256c..fdce6de9 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -234,11 +234,12 @@ func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error { } } - b.writes("if (") - if err := g.writeExpr(b, n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil { + condition := buffer(nil) + if err := g.writeExpr(&condition, n.Condition(), replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { return err } - b.writes(") {\n") + // Calling trimParens avoids clang's -Wparentheses-equality warning. + b.printf("if (%s) {\n", trimParens(condition)) for _, o := range n.BodyIfTrue() { if err := g.writeStatement(b, o, depth); err != nil { return err @@ -423,11 +424,12 @@ func (g *gen) writeStatementWhile(b *buffer, n *a.While, depth uint32) error { } b.printf("label_%d_continue:;\n", jt) } - b.writes("while (") - if err := g.writeExpr(b, n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil { + condition := buffer(nil) + if err := g.writeExpr(&condition, n.Condition(), replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { return err } - b.writes(") {\n") + // Calling trimParens avoids clang's -Wparentheses-equality warning. + b.printf("while (%s) {\n", trimParens(condition)) for _, o := range n.Body() { if err := g.writeStatement(b, o, depth); err != nil { return err @@ -904,6 +906,13 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia return nil } +func trimParens(b []byte) []byte { + if len(b) > 1 && b[0] == '(' && b[len(b)-1] == ')' { + return b[1 : len(b)-1] + } + return b +} + func isInSrc(tm *t.Map, n *a.Expr, methodName t.ID, nArgs int) bool { callSuspendible := methodName != t.IDSinceMark && methodName != t.IDSetMark &&