Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHansruedi Patzen2018-05-29 09:15:28 +0000
committerThomas Corbat2018-06-15 08:36:26 +0000
commit8eefa560ac9db0118d8a7aa8f44884199ace87ce (patch)
tree106dae3224e60711341ed100559e0a3e8ba157c7
parenta9988957f653cf75d24b12180658f266d6dd9f16 (diff)
downloadorg.eclipse.cdt-8eefa560ac9db0118d8a7aa8f44884199ace87ce.tar.gz
org.eclipse.cdt-8eefa560ac9db0118d8a7aa8f44884199ace87ce.tar.xz
org.eclipse.cdt-8eefa560ac9db0118d8a7aa8f44884199ace87ce.zip
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 <hansruedi.patzen@hsr.ch> Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ReplaceTests.java53
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java5
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java26
3 files changed, 83 insertions, 1 deletions
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();
+ }
}

Back to the top