From 8eefa560ac9db0118d8a7aa8f44884199ace87ce Mon Sep 17 00:00:00 2001 From: Hansruedi Patzen Date: Tue, 29 May 2018 11:15:28 +0200 Subject: [PATCH] Bug 535263: Switch statement attributes lost on rewrite Fixed with the patch for 533552, only the CodeFormatter needed fixing. Change-Id: I258617d01b091764ad9776921e773e208002c989 Signed-off-by: Hansruedi Patzen Signed-off-by: Thomas Corbat --- .../rewrite/changegenerator/ReplaceTests.java | 53 +++++++++++++++++++ .../formatter/CodeFormatterVisitor.java | 5 +- .../cdt/ui/tests/text/CodeFormatterTest.java | 26 +++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ReplaceTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ReplaceTests.java index c92f2e66053..5a6514c17b3 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ReplaceTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ReplaceTests.java @@ -1166,4 +1166,57 @@ public class ReplaceTests extends ChangeGeneratorTest { public void testCopyReplaceAttribute_Bug535265_1() throws Exception { compareCopyResult(new CopyReplaceVisitor(this, IASTSwitchStatement.class::isInstance)); } + + //void f() { + // [[foo]] switch (true) { + // } + //} + + //void f() { + // [[foo]][[bar]] switch (true) { + // } + //} + public void testCopyReplaceAttributeOnSwitchStatement_Bug535263_1() throws Exception { + compareResult(new ASTVisitor() { + { + shouldVisitStatements = true; + } + + @Override + public int visit(IASTStatement statement) { + if (statement instanceof IASTSwitchStatement) { + addAttributeListModification(statement, "bar"); + return PROCESS_ABORT; + } + return PROCESS_CONTINUE; + } + }); + } + + //void f() { + // [[foo]] switch (true) [[bar]] { + // } + //} + + //void f() { + // [[foo]] switch (true) [[bar]][[foobar]] { + // } + //} + public void testCopyReplaceAttributeOnSwitchCompoundStatement_Bug535263_2() throws Exception { + compareResult(new ASTVisitor() { + { + shouldVisitStatements = true; + } + + @Override + public int visit(IASTStatement statement) { + if (statement instanceof IASTSwitchStatement) { + IASTSwitchStatement switchStatement = (IASTSwitchStatement) statement; + addAttributeListModification(switchStatement.getBody(), "foobar"); + return PROCESS_ABORT; + } + return PROCESS_CONTINUE; + } + }); + } } 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 a38417f8b11..d98787ad350 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 @@ -3642,6 +3642,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private int visit(IASTSwitchStatement node) { final int headerIndent= scribe.numberOfIndentations; + formatLeadingAttributes(node); // 'switch' keyword if (!startsWithMacroExpansion(node)) { scribe.printNextToken(Token.t_switch); @@ -3676,7 +3677,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, int braceIndent = -1; IASTStatement bodyStmt= node.getBody(); if (!startsWithMacroExpansion(bodyStmt)) { - formatOpeningBrace(brace_position, preferences.insert_space_before_opening_brace_in_switch); + boolean insertSpaceBeforeOpeningBrace = preferences.insert_space_before_opening_brace_in_switch; + formatAttributes(bodyStmt, insertSpaceBeforeOpeningBrace, false); + formatOpeningBrace(brace_position, insertSpaceBeforeOpeningBrace); scribe.startNewLine(); braceIndent= scribe.numberOfIndentations; if (braceIndent > headerIndent) { 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 405575896c0..d8c3db1c665 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 @@ -3420,4 +3420,30 @@ public class CodeFormatterTest extends BaseUITestCase { public void testIndendtionSizeofParampack_535331() throws Exception { assertFormatterResult(); } + + //void f() { + // [[foo]]switch (true) { + // } + //} + + //void f() { + // [[foo]] switch (true) { + // } + //} + public void testAttributedSwitchStatement_Bug535263_1() throws Exception { + assertFormatterResult(); + } + + //void f() { + // [[foo]] switch (true) [[bar]] { + // } + //} + + //void f() { + // [[foo]] switch (true) [[bar]] { + // } + //} + public void testAttributedSwitchCompoundStatement_Bug535263_2() throws Exception { + assertFormatterResult(); + } }