1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 357423 - Code formatter gets confused by a macro.

This commit is contained in:
Sergey Prigogin 2011-09-12 16:11:54 -07:00
parent 6d7a8182a0
commit 7065e4648d
2 changed files with 77 additions and 26 deletions

View file

@ -3460,29 +3460,43 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private int visit(IASTSwitchStatement node) {
final int headerIndent= scribe.numberOfIndentations;
/*
* 'switch' keyword
*/
if (!startsWithMacroExpansion(node)) {
scribe.printNextToken(Token.t_switch);
}
/*
* Controller expression
*/
IASTExpression controllerExpression = node.getControllerExpression();
if (!enclosedInMacroExpansion(controllerExpression)) {
scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_switch);
if (preferences.insert_space_after_opening_paren_in_switch) {
scribe.space();
}
node.getControllerExpression().accept(this);
}
controllerExpression.accept(this);
if (!enclosedInMacroExpansion(controllerExpression)) {
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
}
/*
* switch body
*/
String switch_brace = preferences.brace_position_for_switch;
formatOpeningBrace(switch_brace, preferences.insert_space_before_opening_brace_in_switch);
String brace_position = preferences.brace_position_for_switch;
int braceIndent = -1;
IASTStatement bodyStmt= node.getBody();
if (!startsWithMacroExpansion(bodyStmt)) {
formatOpeningBrace(brace_position, preferences.insert_space_before_opening_brace_in_switch);
scribe.startNewLine();
final int braceIndent= scribe.numberOfIndentations;
braceIndent= scribe.numberOfIndentations;
if (braceIndent > headerIndent) {
scribe.unIndent();
}
if (preferences.indent_switchstatements_compare_to_switch) {
scribe.indent();
}
IASTStatement bodyStmt= node.getBody();
}
final List<IASTStatement> statements;
if (bodyStmt instanceof IASTCompoundStatement) {
statements= Arrays.asList(((IASTCompoundStatement) bodyStmt).getStatements());
@ -3497,6 +3511,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
boolean wasAStatement = false;
for (int i = 0; i < statementsLength; i++) {
final IASTStatement statement = statements.get(i);
if (doNodeLocationsOverlap(controllerExpression, statement)) {
statement.accept(this);
continue;
}
if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) {
if (wasACase) {
scribe.startNewLine();
@ -3598,6 +3616,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
}
if (!startsWithMacroExpansion(bodyStmt)) {
if (preferences.indent_switchstatements_compare_to_switch) {
scribe.unIndent();
}
@ -3606,7 +3625,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
scribe.startNewLine();
formatClosingBrace(switch_brace);
formatClosingBrace(brace_position);
}
} finally {
endOfNode(bodyStmt);
}

View file

@ -2369,6 +2369,37 @@ public class CodeFormatterTest extends BaseUITestCase {
assertFormatterResult();
}
//struct Stream {
//Stream& operator <<(const char*);
//};
//Stream GetStream();
//
//#define MY_MACRO switch (0) case 0: default: GetStream()
//
//void test() {
//MY_MACRO << "Loooooooooooooooooooong string literal" << " another literal.";
//MY_MACRO << "Looooooooooooooooooooong string literal" << " another literal.";
//}
//struct Stream {
// Stream& operator <<(const char*);
//};
//Stream GetStream();
//
//#define MY_MACRO switch (0) case 0: default: GetStream()
//
//void test() {
// MY_MACRO << "Loooooooooooooooooooong string literal" << " another literal.";
// MY_MACRO << "Looooooooooooooooooooong string literal"
// << " another literal.";
//}
public void testOverloadedLeftShiftChain_5() throws Exception {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_OVERLOADED_LEFT_SHIFT_CHAIN,
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
assertFormatterResult();
}
//int main() {
// std::vector<std::vector<int>> test;
// // some comment