From 1a6378291ac16484addec833f55a536bc85dbe6c Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Mon, 10 May 2021 22:15:12 +0100 Subject: [PATCH] Emit 'else if' without nesting. No functional change intended. (#2944) Instead of: if (a) { x; } else { if (b) { y; } else { if (c) { z; } else { w; } } } Emit: if (a) { x; } else if (b) { y; } else if (c) { z; } else { w; } --- src/V3EmitC.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/V3EmitC.cpp b/src/V3EmitC.cpp index a8f4de8ea..3a8b4b89b 100644 --- a/src/V3EmitC.cpp +++ b/src/V3EmitC.cpp @@ -812,11 +812,19 @@ public: if (!nodep->branchPred().unknown()) puts(")"); puts(") {\n"); iterateAndNextNull(nodep->ifsp()); - if (nodep->elsesp()) { - puts("} else {\n"); - iterateAndNextNull(nodep->elsesp()); + puts("}"); + if (!nodep->elsesp()) { + puts("\n"); + } else { + if (VN_IS(nodep->elsesp(), NodeIf) && !nodep->elsesp()->nextp()) { + puts(" else "); + iterateAndNextNull(nodep->elsesp()); + } else { + puts(" else {\n"); + iterateAndNextNull(nodep->elsesp()); + puts("}\n"); + } } - puts("}\n"); } virtual void visit(AstExprStmt* nodep) override { // GCC allows compound statements in expressions, but this is not standard.