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:
parent
6d7a8182a0
commit
7065e4648d
2 changed files with 77 additions and 26 deletions
|
@ -3460,29 +3460,43 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
|
|
||||||
private int visit(IASTSwitchStatement node) {
|
private int visit(IASTSwitchStatement node) {
|
||||||
final int headerIndent= scribe.numberOfIndentations;
|
final int headerIndent= scribe.numberOfIndentations;
|
||||||
scribe.printNextToken(Token.t_switch);
|
/*
|
||||||
scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_switch);
|
* 'switch' keyword
|
||||||
|
*/
|
||||||
if (preferences.insert_space_after_opening_paren_in_switch) {
|
if (!startsWithMacroExpansion(node)) {
|
||||||
scribe.space();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
controllerExpression.accept(this);
|
||||||
|
if (!enclosedInMacroExpansion(controllerExpression)) {
|
||||||
|
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
|
||||||
}
|
}
|
||||||
|
|
||||||
node.getControllerExpression().accept(this);
|
|
||||||
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
|
|
||||||
/*
|
/*
|
||||||
* switch body
|
* switch body
|
||||||
*/
|
*/
|
||||||
String switch_brace = preferences.brace_position_for_switch;
|
String brace_position = preferences.brace_position_for_switch;
|
||||||
formatOpeningBrace(switch_brace, preferences.insert_space_before_opening_brace_in_switch);
|
int braceIndent = -1;
|
||||||
scribe.startNewLine();
|
|
||||||
final int braceIndent= scribe.numberOfIndentations;
|
|
||||||
if (braceIndent > headerIndent) {
|
|
||||||
scribe.unIndent();
|
|
||||||
}
|
|
||||||
if (preferences.indent_switchstatements_compare_to_switch) {
|
|
||||||
scribe.indent();
|
|
||||||
}
|
|
||||||
IASTStatement bodyStmt= node.getBody();
|
IASTStatement bodyStmt= node.getBody();
|
||||||
|
if (!startsWithMacroExpansion(bodyStmt)) {
|
||||||
|
formatOpeningBrace(brace_position, preferences.insert_space_before_opening_brace_in_switch);
|
||||||
|
scribe.startNewLine();
|
||||||
|
braceIndent= scribe.numberOfIndentations;
|
||||||
|
if (braceIndent > headerIndent) {
|
||||||
|
scribe.unIndent();
|
||||||
|
}
|
||||||
|
if (preferences.indent_switchstatements_compare_to_switch) {
|
||||||
|
scribe.indent();
|
||||||
|
}
|
||||||
|
}
|
||||||
final List<IASTStatement> statements;
|
final List<IASTStatement> statements;
|
||||||
if (bodyStmt instanceof IASTCompoundStatement) {
|
if (bodyStmt instanceof IASTCompoundStatement) {
|
||||||
statements= Arrays.asList(((IASTCompoundStatement) bodyStmt).getStatements());
|
statements= Arrays.asList(((IASTCompoundStatement) bodyStmt).getStatements());
|
||||||
|
@ -3497,6 +3511,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
boolean wasAStatement = false;
|
boolean wasAStatement = false;
|
||||||
for (int i = 0; i < statementsLength; i++) {
|
for (int i = 0; i < statementsLength; i++) {
|
||||||
final IASTStatement statement = statements.get(i);
|
final IASTStatement statement = statements.get(i);
|
||||||
|
if (doNodeLocationsOverlap(controllerExpression, statement)) {
|
||||||
|
statement.accept(this);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) {
|
if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) {
|
||||||
if (wasACase) {
|
if (wasACase) {
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
|
@ -3598,15 +3616,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preferences.indent_switchstatements_compare_to_switch) {
|
if (!startsWithMacroExpansion(bodyStmt)) {
|
||||||
scribe.unIndent();
|
if (preferences.indent_switchstatements_compare_to_switch) {
|
||||||
|
scribe.unIndent();
|
||||||
|
}
|
||||||
|
if (scribe.numberOfIndentations < braceIndent) {
|
||||||
|
scribe.indent();
|
||||||
|
}
|
||||||
|
scribe.startNewLine();
|
||||||
|
|
||||||
|
formatClosingBrace(brace_position);
|
||||||
}
|
}
|
||||||
if (scribe.numberOfIndentations < braceIndent) {
|
|
||||||
scribe.indent();
|
|
||||||
}
|
|
||||||
scribe.startNewLine();
|
|
||||||
|
|
||||||
formatClosingBrace(switch_brace);
|
|
||||||
} finally {
|
} finally {
|
||||||
endOfNode(bodyStmt);
|
endOfNode(bodyStmt);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2369,6 +2369,37 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
assertFormatterResult();
|
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() {
|
//int main() {
|
||||||
// std::vector<std::vector<int>> test;
|
// std::vector<std::vector<int>> test;
|
||||||
// // some comment
|
// // some comment
|
||||||
|
|
Loading…
Add table
Reference in a new issue