From 66df5ff4280266ad8ca9e507570beb8b58873f04 Mon Sep 17 00:00:00 2001 From: Hansruedi Patzen Date: Tue, 29 May 2018 16:23:35 +0200 Subject: [PATCH] Bug 535278: Formatter error formatting CF statements with attributes Fix and tests. Change-Id: I1928d5fe70c02cbc9c147bb305720ad75b4913fc Signed-off-by: Hansruedi Patzen Signed-off-by: Thomas Corbat --- .../formatter/CodeFormatterVisitor.java | 14 ++++ .../cdt/ui/tests/text/CodeFormatterTest.java | 79 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index d98787ad350..16fef87327f 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -56,6 +56,7 @@ import org.eclipse.cdt.core.dom.ast.IASTForStatement; import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression; import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; +import org.eclipse.cdt.core.dom.ast.IASTGotoStatement; import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTIfStatement; import org.eclipse.cdt.core.dom.ast.IASTInitializer; @@ -984,6 +985,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, visit((IASTCaseStatement) node); } else if (node instanceof IASTDefaultStatement) { visit((IASTDefaultStatement) node); + } else if (node instanceof IASTGotoStatement) { + visit((IASTGotoStatement) node); } else if (node instanceof IASTLabelStatement) { visit((IASTLabelStatement) node); } else if (node instanceof IASTProblemStatement) { @@ -2241,6 +2244,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } private int visit(IASTBreakStatement node) { + formatLeadingAttributes(node); scribe.printNextToken(Token.t_break); scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); scribe.printTrailingComment(); @@ -3137,6 +3141,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } private int visit(IASTContinueStatement node) { + formatLeadingAttributes(node); scribe.printNextToken(Token.t_continue); scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); scribe.printTrailingComment(); @@ -3583,6 +3588,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } private int visit(IASTReturnStatement node) { + formatLeadingAttributes(node); scribe.printNextToken(Token.t_return); final IASTExpression expression = node.getReturnValue(); if (expression != null) { @@ -3602,6 +3608,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private int visit(IASTLabelStatement node) { // TLETODO [formatter] label indentation + formatLeadingAttributes(node); node.getName().accept(this); scribe.printNextToken(Token.tCOLON, preferences.insert_space_before_colon_in_labeled_statement); if (preferences.insert_space_after_colon_in_labeled_statement) { @@ -3633,6 +3640,13 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, return PROCESS_SKIP; } + + private int visit(IASTGotoStatement node) { + formatLeadingAttributes(node); + formatRaw(node); + return PROCESS_SKIP; + } + private void beginSwitchClause() { scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_switch); if (preferences.insert_space_after_opening_paren_in_switch) { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index d8c3db1c665..5ace07573a3 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -3446,4 +3446,83 @@ public class CodeFormatterTest extends BaseUITestCase { public void testAttributedSwitchCompoundStatement_Bug535263_2() throws Exception { assertFormatterResult(); } + + //void f() { + // for (;;) { + // [[foo]]continue; + // } + //} + + //void f() { + // for (;;) { + // [[foo]] continue; + // } + //} + public void testAttributedContinueStatement_Bug535278_1() throws Exception { + assertFormatterResult(); + } + + //void f() { + // for (;;) { + // [[foo]]break; + // } + //} + + //void f() { + // for (;;) { + // [[foo]] break; + // } + //} + public void testAttributedBreakStatement_Bug535278_2() throws Exception { + assertFormatterResult(); + } + + //void f() { + // for (;;) { + // [[foo]]return; + // } + //} + + //void f() { + // for (;;) { + // [[foo]] return; + // } + //} + public void testAttributedReturnStatement_Bug535278_3() throws Exception { + assertFormatterResult(); + } + + //void f() { + // for (;;) { + // [[foo]]goto label; + // } + // label: ; + //} + + //void f() { + // for (;;) { + // [[foo]] goto label; + // } + // label: ; + //} + public void testAttributedGotoStatement_Bug535278_4() throws Exception { + assertFormatterResult(); + } + + //void f() { + // for (;;) { + // goto label; + // } + // [[bar]]label: ; + //} + + //void f() { + // for (;;) { + // goto label; + // } + // [[bar]] label: ; + //} + public void testAttributedGotoLabel_Bug535278_5() throws Exception { + assertFormatterResult(); + } }