From da70901535e58502f19364cc923f8cd0ce55e623 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Thu, 7 Aug 2008 08:45:59 +0000 Subject: [PATCH] Bug 243056 - [Formatter] changes syntax by removing a space --- .../formatter/CodeFormatterVisitor.java | 53 ++++++------------- .../cdt/ui/tests/text/CodeFormatterTest.java | 7 +++ 2 files changed, 24 insertions(+), 36 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 f6537ecb302..6189ee61a10 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 @@ -91,7 +91,6 @@ import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator; import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier; import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer; -import org.eclipse.cdt.core.dom.ast.c.ICASTPointer; import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression; import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression; @@ -461,7 +460,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { if (preferences.insert_new_line_before_identifier_in_function_declaration) { scribe.startNewLine(); } else { - // preserve newline if not explicitely requested + // preserve newline if not explicitly requested if (scribe.preserveNewLine()) { scribe.space(); } @@ -993,13 +992,12 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } declarator.accept(this); - // tletodo if (node instanceof ICPPASTFunctionWithTryBlock) { scribe.startNewLine(); scribe.printNextToken(Token.t_try, false); scribe.printTrailingComment(); } - + if (node instanceof ICPPASTFunctionDefinition) { final ICPPASTConstructorChainInitializer[] constructorChain= ((ICPPASTFunctionDefinition) node).getMemberInitializers(); if (constructorChain != null && constructorChain.length > 0) { @@ -1053,7 +1051,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private int visit(ICPPASTFunctionDeclarator node) { visit((IASTStandardFunctionDeclarator)node); - skipConstVolatile(); + skipConstVolatileRestrict(); final IASTTypeId[] exceptionSpecification= node.getExceptionSpecification(); if (exceptionSpecification != null) { @@ -1061,30 +1059,8 @@ public class CodeFormatterVisitor extends CPPASTVisitor { formatExceptionSpecification(exceptionSpecification); } } - -// tletodo -// if (node instanceof ICPPASTFunctionTryBlockDeclarator) { -// scribe.startNewLine(); -// scribe.printNextToken(Token.t_try, false); -// scribe.printTrailingComment(); -// // for catch handlers @see #visit(IASTFunctionDefinition) -// } -// -// final ICPPASTConstructorChainInitializer[] constructorChain= node.getConstructorChain(); -// if (constructorChain != null && constructorChain.length > 0) { -// // TLETODO [formatter] need special constructor chain alignment -// scribe.printNextToken(Token.tCOLON, true); -// scribe.printTrailingComment(); -// scribe.startNewLine(); -// scribe.indent(); -// final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT); -// formatList(Arrays.asList(constructorChain), align, false, false); -// scribe.unIndent(); -// } else { - // skip the rest (=0) - skipNode(node); -// } - + // skip the rest (=0) + skipNode(node); return PROCESS_SKIP; } @@ -1133,12 +1109,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } } - private void skipConstVolatile() { + private boolean skipConstVolatileRestrict() { + boolean skipped= false; int token= peekNextToken(); - while (token == Token.t_const || token == Token.t_volatile) { + while (token == Token.t_const || token == Token.t_volatile || token == Token.t_restrict) { scribe.printNextToken(token, true); token= peekNextToken(); + skipped= true; } + return skipped; } private int visit(IASTStandardFunctionDeclarator node) { @@ -1166,9 +1145,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } if (pointer instanceof ICPPASTReferenceOperator) { scribe.printNextToken(Token.tAMPER, false); - } else if (pointer instanceof ICASTPointer) { - scribe.printNextToken(Token.tSTAR, false); - skipConstVolatile(); } else if (pointer instanceof ICPPASTPointerToMember) { final ICPPASTPointerToMember ptrToMember= (ICPPASTPointerToMember)pointer; final IASTName name= ptrToMember.getName(); @@ -1176,9 +1152,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor { name.accept(this); } scribe.printNextToken(Token.tSTAR, false); - skipConstVolatile(); + if (skipConstVolatileRestrict()) { + scribe.space(); + } } else { - formatRaw(pointer); + scribe.printNextToken(Token.tSTAR, false); + if (skipConstVolatileRestrict()) { + scribe.space(); + } } } } 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 f8f453463e7..857a635d6bb 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 @@ -846,4 +846,11 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //char *b, * const a; + + //char *b, * const a; + public void testPreserveSpaceBetweenPointerModifierAndIdentifier_Bug243056() throws Exception { + assertFormatterResult(); + } + }