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>
This commit is contained in:
Hansruedi Patzen 2018-05-29 11:15:28 +02:00 committed by Thomas Corbat
parent a9988957f6
commit 8eefa560ac
3 changed files with 83 additions and 1 deletions

View file

@ -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;
}
});
}
}

View file

@ -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) {

View file

@ -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();
}
}