TruncateLoops extends StandardVisitor { public TruncateLoops(int limit, String prefix, TranslationUnit tu) { this.limit = limit; this.tu = tu; this.prefix = prefix; counter = 0; visit(tu); } TruncateLoops(int limit, String prefix, TranslationUnit tu); @Override void visitForStmt(ForStmt forStmt); @Override void visitWhileStmt(WhileStmt whileStmt); @Override void visitDoStmt(DoStmt doStmt);  }
@Test public void testTruncateLoops() throws Exception { final String program = "void main() {" + " int x = 0;" + " for (int i = 0; i < 100; i++)" + " while (x < i)" + " do {" + " x++;" + " for (int j = 0; j < 200; j++) {" + " ;" + " }" + " } while (x > 0);" + "}"; final String expected = "" + "void main() {\n" + " int x = 0;\n" + " {\n" + " int pre_looplimiter3 = 0;\n" + " for (int i = 0; i < 100; i++) {\n" + " if (pre_looplimiter3 >= 3) {\n" + " break;\n" + " }\n" + " pre_looplimiter3++;\n" + " int pre_looplimiter2 = 0;\n" + " while (x < i) {\n" + " if (pre_looplimiter2 >= 3) {\n" + " break;\n" + " }\n" + " pre_looplimiter2++;\n" + " int pre_looplimiter1 = 0;\n" + " do {\n" + " if (pre_looplimiter1 >= 3) {\n" + " break;\n" + " }\n" + " pre_looplimiter1++;\n" + " x++;\n" + " {\n" + " int pre_looplimiter0 = 0;\n" + " for (int j = 0; j < 200; j++) {\n" + " if (pre_looplimiter0 >= 3) {\n" + " break;\n" + " }\n" + " pre_looplimiter0++;\n" + " ;\n" + " }\n" + " }\n" + " } while (x > 0);\n" + " }\n" + " }\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); new TruncateLoops(3, "pre", tu); assertEquals(PrettyPrinterVisitor.prettyPrintAsString(ParseHelper.parse(expected)), PrettyPrinterVisitor.prettyPrintAsString(tu)); }