Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Leherbauer2011-10-10 12:20:26 +0000
committerAnton Leherbauer2011-10-10 12:29:05 +0000
commite7f50acc252c66de101203cf9b901c57ff52e736 (patch)
treeab5c1baef934dea2bdde5a9793d15d3df0e004ec
parentc617df3475fcbaed5c3ff44ba9e7b43ff370ecf1 (diff)
downloadorg.eclipse.cdt-e7f50acc252c66de101203cf9b901c57ff52e736.tar.gz
org.eclipse.cdt-e7f50acc252c66de101203cf9b901c57ff52e736.tar.xz
org.eclipse.cdt-e7f50acc252c66de101203cf9b901c57ff52e736.zip
Bug 359658 - code formatter does not work as expected when complicate macro precedes formatted code
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java35
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java19
2 files changed, 40 insertions, 14 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 b4626fc404e..b6808425482 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
@@ -3033,22 +3033,24 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final IASTStatement action = node.getBody();
formatAction(line, action, preferences.brace_position_for_block);
- if (peekNextToken() == Token.t_while) {
- if (preferences.insert_new_line_before_while_in_do_statement) {
- scribe.startNewLine();
- }
- scribe.printNextToken(Token.t_while, preferences.insert_space_after_closing_brace_in_block);
- scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_while);
-
- if (preferences.insert_space_after_opening_paren_in_while) {
- scribe.space();
+ if (scribe.scanner.getCurrentPosition() < getNodeEndLocation(node)) {
+ if (peekNextToken() == Token.t_while) {
+ if (preferences.insert_new_line_before_while_in_do_statement) {
+ scribe.startNewLine();
+ }
+ scribe.printNextToken(Token.t_while, preferences.insert_space_after_closing_brace_in_block);
+ scribe.printNextToken(Token.tLPAREN, preferences.insert_space_before_opening_paren_in_while);
+
+ if (preferences.insert_space_after_opening_paren_in_while) {
+ scribe.space();
+ }
+
+ node.getCondition().accept(this);
+
+ scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_while);
}
-
- node.getCondition().accept(this);
-
- scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_while);
+ scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
}
- scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
scribe.printTrailingComment();
return PROCESS_SKIP;
}
@@ -4021,6 +4023,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return node1.getFileLocation().getNodeOffset() == node2.getFileLocation().getNodeOffset();
}
+ private static int getNodeEndLocation(IASTNode node) {
+ IASTFileLocation loc = node.getFileLocation();
+ return loc.getNodeOffset() + loc.getNodeLength();
+ }
+
private void formatBlock(IASTCompoundStatement block, String block_brace_position,
boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) {
formatBlockOpening(block, block_brace_position, insertSpaceBeforeOpeningBrace);
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 ef9692b2095..a38fe09518a 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
@@ -2758,4 +2758,23 @@ public class CodeFormatterTest extends BaseUITestCase {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_MIN_DISTANCE_BETWEEN_CODE_AND_LINE_COMMENT, "2");
assertFormatterResult();
}
+
+ //#define TESTING(m) ;do{}while(0)
+ //void f() {
+ // TESTING(1);
+ // if(Test(a) != 1) {
+ // status = ERROR;
+ // }
+ //}
+
+ //#define TESTING(m) ;do{}while(0)
+ //void f() {
+ // TESTING(1);
+ // if (Test(a) != 1) {
+ // status = ERROR;
+ // }
+ //}
+ public void testDoWhileInMacro_Bug359658() throws Exception {
+ assertFormatterResult();
+ }
}

Back to the top