Skip to content

Commit

Permalink
Emit 'else if' without nesting. No functional change intended. (#2944)
Browse files Browse the repository at this point in the history
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;
}
  • Loading branch information
gezalore committed May 10, 2021
1 parent 267c6f6 commit 1a63782
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/V3EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,19 @@ class EmitCStmts VL_NOT_FINAL : public EmitCBaseVisitor {
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.
Expand Down

0 comments on commit 1a63782

Please sign in to comment.