From 35a973d9a5e4f9564ce6d6e7cc3a3e91f98cbee5 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 5 Dec 2012 11:47:53 -0800 Subject: [PATCH] Formatter support for defaulted and deleted functions and for rvalue references. --- .../formatter/CodeFormatterVisitor.java | 41 +++++++++++++++---- .../cdt/ui/tests/text/CodeFormatterTest.java | 22 ++++++++++ 2 files changed, 54 insertions(+), 9 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 821dc500396..3a9d9d64d8e 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 @@ -1326,12 +1326,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, Runnable tailFormatter = null; IASTStatement bodyStmt= node.getBody(); if (DefaultCodeFormatterConstants.END_OF_LINE.equals(preferences.brace_position_for_method_declaration) && - !hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock)) { - if (bodyStmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(bodyStmt)) { - tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, nodeOffset(bodyStmt), - preferences.insert_space_before_opening_brace_in_method_declaration, false); - scribe.setTailFormatter(tailFormatter); - } + !hasMemberInitializers(node) && !(node instanceof ICPPASTFunctionWithTryBlock) && + bodyStmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(bodyStmt)) { + tailFormatter = new TrailingTokenFormatter(Token.tLBRACE, nodeOffset(bodyStmt), + preferences.insert_space_before_opening_brace_in_method_declaration, false); + scribe.setTailFormatter(tailFormatter); } declarator.accept(this); @@ -1342,8 +1341,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } if (node instanceof ICPPASTFunctionDefinition) { + ICPPASTFunctionDefinition cppFunctionDefinition = (ICPPASTFunctionDefinition) node; final ICPPASTConstructorChainInitializer[] constructorChain= - ((ICPPASTFunctionDefinition) node).getMemberInitializers(); + cppFunctionDefinition.getMemberInitializers(); if (constructorChain != null && constructorChain.length > 0) { if (preferences.insert_new_line_before_colon_in_constructor_initializer_list) { scribe.printTrailingComment(); @@ -1363,6 +1363,25 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, formatList(Arrays.asList(constructorChain), options, false, false, null); scribe.unIndentForContinuation(); } + + if (cppFunctionDefinition.isDefaulted() || cppFunctionDefinition.isDeleted()) { + int token = peekNextToken(); + if (token == Token.tASSIGN) { + if (preferences.insert_space_before_assignment_operator) + scribe.space(); + scribe.printNextToken(token); + if (preferences.insert_space_after_assignment_operator) + scribe.space(); + } + token = peekNextToken(); + if (token == Token.t_default || token == Token.t_delete) { + scribe.printNextToken(token); + } + if (bodyStmt == null) { + tailFormatter = new TrailingSemicolonFormatter(node); + scribe.setTailFormatter(tailFormatter); + } + } } if (tailFormatter != null) { @@ -1385,7 +1404,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, exitNode(bodyStmt); } } - } else { + } else if (bodyStmt != null) { bodyStmt.accept(this); } scribe.printTrailingComment(); @@ -1563,7 +1582,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.space(); } if (pointer instanceof ICPPASTReferenceOperator) { - scribe.printNextToken(Token.tAMPER, false); + if (((ICPPASTReferenceOperator) pointer).isRValueReference()) { + scribe.printNextToken(Token.tAND, false); + } else { + scribe.printNextToken(Token.tAMPER, false); + } } else if (pointer instanceof ICPPASTPointerToMember) { final ICPPASTPointerToMember ptrToMember= (ICPPASTPointerToMember) pointer; final IASTName name= ptrToMember.getName(); 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 8b832972a30..f2ef0f47940 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 @@ -1140,6 +1140,28 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //struct moveonly { + //moveonly()=default; + //moveonly(const moveonly&)=delete; + //moveonly(moveonly&&)=default; + //moveonly& operator=(const moveonly&)=delete; + //moveonly& operator=(moveonly&&)=default; + //~moveonly()=default; + //}; + + //struct moveonly { + // moveonly() = default; + // moveonly(const moveonly&) = delete; + // moveonly(moveonly&&) = default; + // moveonly& operator=(const moveonly&) = delete; + // moveonly& operator=(moveonly&&) = default; + // ~moveonly() = default; + //}; + public void testFunctionDefinitionWithoutBody() throws Exception { + fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE); + assertFormatterResult(); + } + //int f1(int a, int b, int c, int d, int e, int f, int g); //int f2(int a, int b, int c, int d, int e, int f, int g); //