Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2011-09-12 23:11:54 +0000
committerSergey Prigogin2011-09-12 23:28:15 +0000
commit7065e4648d6f0ac0621540841207da1f1136ce7f (patch)
treef7c5d62828621eb1ca2ca26db73536e03b650016
parent6d7a8182a01e264b6726f48e9bf29e9bd774d753 (diff)
downloadorg.eclipse.cdt-7065e4648d6f0ac0621540841207da1f1136ce7f.tar.gz
org.eclipse.cdt-7065e4648d6f0ac0621540841207da1f1136ce7f.tar.xz
org.eclipse.cdt-7065e4648d6f0ac0621540841207da1f1136ce7f.zip
Bug 357423 - Code formatter gets confused by a macro.
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java70
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java31
2 files changed, 76 insertions, 25 deletions
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 e93c0e53547..a5299967ae1 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
@@ -3460,29 +3460,43 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private int visit(IASTSwitchStatement node) {
final int headerIndent= scribe.numberOfIndentations;
- scribe.printNextToken(Token.t_switch);
- scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_switch);
-
- if (preferences.insert_space_after_opening_paren_in_switch) {
- scribe.space();
+ /*
+ * 'switch' keyword
+ */
+ if (!startsWithMacroExpansion(node)) {
+ scribe.printNextToken(Token.t_switch);
}
-
- node.getControllerExpression().accept(this);
- scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
/*
- * switch body
+ * Controller expression
*/
- String switch_brace = preferences.brace_position_for_switch;
- formatOpeningBrace(switch_brace, preferences.insert_space_before_opening_brace_in_switch);
- scribe.startNewLine();
- final int braceIndent= scribe.numberOfIndentations;
- if (braceIndent > headerIndent) {
- scribe.unIndent();
+ 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();
+ }
}
- if (preferences.indent_switchstatements_compare_to_switch) {
- scribe.indent();
+ controllerExpression.accept(this);
+ if (!enclosedInMacroExpansion(controllerExpression)) {
+ scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_switch);
}
+ /*
+ * switch body
+ */
+ 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();
+ braceIndent= scribe.numberOfIndentations;
+ if (braceIndent > headerIndent) {
+ scribe.unIndent();
+ }
+ if (preferences.indent_switchstatements_compare_to_switch) {
+ scribe.indent();
+ }
+ }
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,15 +3616,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
}
- if (preferences.indent_switchstatements_compare_to_switch) {
- scribe.unIndent();
- }
- if (scribe.numberOfIndentations < braceIndent) {
- scribe.indent();
+ if (!startsWithMacroExpansion(bodyStmt)) {
+ if (preferences.indent_switchstatements_compare_to_switch) {
+ scribe.unIndent();
+ }
+ if (scribe.numberOfIndentations < braceIndent) {
+ scribe.indent();
+ }
+ scribe.startNewLine();
+
+ formatClosingBrace(brace_position);
}
- scribe.startNewLine();
-
- formatClosingBrace(switch_brace);
} finally {
endOfNode(bodyStmt);
}
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 12f57841fc3..98bae6602eb 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
@@ -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

Back to the top