From d2c442c37335fb4ac8e64473f8d95008dbdce8a5 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 7 Jan 2008 16:06:22 +0000 Subject: [PATCH] Fix for 214354: [code formatter] whitespace removed when declaring a variable and initializing it with a macro --- .../formatter/CodeFormatterVisitor.java | 43 +++++++++++++++---- .../resources/formatter/bugs/After.cpp | 4 +- .../cdt/ui/tests/text/CodeFormatterTest.java | 13 ++++++ 3 files changed, 49 insertions(+), 11 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 a60e5a07ceb..e18546c634d 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 @@ -41,6 +41,7 @@ import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpressionList; import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement; import org.eclipse.cdt.core.dom.ast.IASTFieldDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFieldReference; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTForStatement; import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression; @@ -402,13 +403,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor { * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTInitializer) */ public int visit(IASTInitializer node) { - IASTNodeLocation[] locations= node.getNodeLocations(); - if (locations.length == 0) { - return PROCESS_SKIP; - } else if (locations[0] instanceof IASTMacroExpansion) { - formatNode(node); - return PROCESS_SKIP; - } +// IASTNodeLocation[] locations= node.getNodeLocations(); +// if (locations.length == 0) { +// return PROCESS_SKIP; +// } else if (locations[0] instanceof IASTMacroExpansion) { +// formatNode(node); +// return PROCESS_SKIP; +// } + if (node instanceof ICPPASTConstructorInitializer) { visit((ICPPASTConstructorInitializer)node); return PROCESS_SKIP; @@ -420,6 +422,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.space(); } } + if (node instanceof IASTInitializerExpression) { visit((IASTInitializerExpression)node); } else if (node instanceof IASTInitializerList) { @@ -523,6 +526,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { * @see org.eclipse.cdt.core.dom.ast.ASTVisitor#visit(org.eclipse.cdt.core.dom.ast.IASTExpression) */ public int visit(IASTExpression node) { + scribe.printComment(); IASTNodeLocation[] locations= node.getNodeLocations(); if (locations.length == 0) { return PROCESS_SKIP; @@ -549,6 +553,8 @@ public class CodeFormatterVisitor extends CPPASTVisitor { visit((IASTCastExpression)node); } else if (node instanceof IASTUnaryExpression) { visit((IASTUnaryExpression)node); + } else if (node instanceof IASTFieldReference) { + visit((IASTFieldReference)node); } else if (node instanceof IASTProblemExpression) { visit((IASTProblemExpression)node); } else { @@ -1630,12 +1636,18 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final int operator= node.getOperator(); switch (operator) { case IASTUnaryExpression.op_bracketedPrimary: - scribe.printNextToken(Token.tLPAREN, scribe.printComment()); + scribe.printNextToken(Token.tLPAREN, scribe.printComment() /* insert_space_before_opening_paren_in_parenthesized_expression */); + if (scribe.printComment() /* insert_space_after_opening_paren_in_parenthesized_expression */) { + scribe.space(); + } operand.accept(this); if (peekNextToken() != Token.tRPAREN) { scribe.skipToToken(Token.tRPAREN); } - scribe.printNextToken(Token.tRPAREN, scribe.printComment()); + scribe.printNextToken(Token.tRPAREN, scribe.printComment() /* insert_space_before_closing_paren_in_parenthesized_expression */); + if (scribe.printComment() /* insert_space_after_closing_paren_in_parenthesized_expression */) { + scribe.space(); + } break; case IASTUnaryExpression.op_prefixIncr: scribe.printNextToken(Token.tINCR, scribe.printComment()); @@ -1761,6 +1773,19 @@ public class CodeFormatterVisitor extends CPPASTVisitor { return PROCESS_SKIP; } + private int visit(IASTFieldReference node) { + IASTExpression expr= node.getFieldOwner(); + if (expr != null) { + expr.accept(this); + } + final IASTName fieldName= node.getFieldName(); + if (fieldName != null) { + skipToNode(fieldName); + fieldName.accept(this); + } + return PROCESS_SKIP; + } + private int visit(IASTContinueStatement node) { scribe.printNextToken(Token.t_continue); scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp index 0c4b4981b95..0ab88a7a74e 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp @@ -43,11 +43,11 @@ void bug183220() { rtc_s2000_src_pending, rtc_s2000_cr_sync_pending, rtc_hdw_cr_sync_next, rtc_hdw_current_clock; int rtc_s2000_clock_source_state, RTC_CLOCK_PLL; - if (( (rtc_hdw_cr_sync_next != rtc_hdw_cr_sync ) + if (( ( rtc_hdw_cr_sync_next != rtc_hdw_cr_sync ) || rtc_hdw_cr_resync_enable )&& !rtc_s2000_src_pending && !rtc_s2000_cr_sync_pending) { if (!identify_hdw_fvr_master() || !rtc_hdw_current_clock->external - || !rtc_hdw_cr_sync_next ||( (rtc_hdw_current_clock->external + || !rtc_hdw_cr_sync_next ||( ( rtc_hdw_current_clock->external && rtc_hdw_cr_sync_next && rtc_s2000_clock_source_state != RTC_CLOCK_PLL ) )) { } 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 62ec04ce2ff..1d5eecde1a2 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 @@ -339,4 +339,17 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //void f() { + //#define I 0 + // int i = I; + //} + + //void f() { + //#define I 0 + // int i = I; + //} + public void testMacroAsInitializer_Bug214354() throws Exception { + assertFormatterResult(); + } + }