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 08f8b002805..f7f0294d75a 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 @@ -129,6 +129,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDesignator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldReference; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; @@ -340,12 +341,16 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, token = peekNextToken(); needSpace = true; } - if (token == Token.t_throw || token == Token.tIDENTIFIER) { + if (token == Token.t_noexcept || token == Token.t_throw || token == Token.tIDENTIFIER) { if (node instanceof ICPPASTFunctionDeclarator) { final IASTTypeId[] exceptionSpecification = ((ICPPASTFunctionDeclarator) node) .getExceptionSpecification(); if (exceptionSpecification != null && token == Token.t_throw) formatExceptionSpecification(exceptionSpecification); + final ICPPASTExpression noexceptExpression = ((ICPPASTFunctionDeclarator) node) + .getNoexceptExpression(); + if (noexceptExpression != null && token == Token.t_noexcept) + formatExceptionSpecification(noexceptExpression); } if (peekNextToken() == Token.tIDENTIFIER) { Alignment alignment = scribe.createAlignment(Alignment.TRAILING_TEXT, Alignment.M_COMPACT_SPLIT, 1, @@ -1672,6 +1677,38 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } } + private void formatExceptionSpecification(final ICPPASTExpression noexceptSpecification) { + if (noexceptSpecification != null && noexceptSpecification != ICPPASTFunctionDeclarator.NOEXCEPT_DEFAULT) { + Alignment alignment = scribe.createAlignment(Alignment.EXCEPTION_SPECIFICATION, + preferences.alignment_for_throws_clause_in_method_declaration, 1, getCurrentPosition()); + + scribe.enterAlignment(alignment); + boolean ok = false; + do { + try { + scribe.alignFragment(alignment, 0); + scribe.printNextToken(Token.t_noexcept, true); + scribe.printNextToken(Token.tLPAREN, + preferences.insert_space_before_opening_paren_in_exception_specification); + if (preferences.insert_space_after_opening_paren_in_exception_specification) { + scribe.space(); + } + noexceptSpecification.accept(this); + if (peekNextToken() == Token.tRPAREN) { + scribe.printNextToken(Token.tRPAREN, + preferences.insert_space_before_closing_paren_in_exception_specification); + } + ok = true; + } catch (AlignmentException e) { + scribe.redoAlignment(e); + } + } while (!ok); + scribe.exitAlignment(alignment, true); + } else { + scribe.printNextToken(Token.t_noexcept, true); + } + } + private boolean skipConstVolatileRestrict(boolean spaceBefore) { return skipTokenWhile(token -> token == Token.t_const || token == Token.t_volatile || token == Token.t_restrict, spaceBefore); @@ -3106,6 +3143,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, operand.accept(this); } break; + case IASTUnaryExpression.op_noexcept: + scribe.printNextToken(Token.t_noexcept, scribe.printComment()); + if (operand != null) { + if (peekNextToken() != Token.tLPAREN) { + scribe.space(); + } + operand.accept(this); + } + break; case IASTUnaryExpression.op_typeid: scribe.printNextToken(Token.t_typeid, scribe.printComment()); if (peekNextToken() != Token.tLPAREN) { diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java index f04a4cdb5ff..fa140edfb41 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/SimpleScanner.java @@ -947,6 +947,7 @@ public class SimpleScanner { fgKeywords.put("while", Integer.valueOf(Token.t_while)); //$NON-NLS-1$ fgKeywords.put("xor", Integer.valueOf(Token.t_xor)); //$NON-NLS-1$ fgKeywords.put("xor_eq", Integer.valueOf(Token.t_xor_eq)); //$NON-NLS-1$ + fgKeywords.put("noexcept", Integer.valueOf(Token.t_noexcept)); //$NON-NLS-1$ // additional java keywords fgKeywords.put("abstract", Integer.valueOf(Token.t_abstract)); //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/Token.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/Token.java index e77cf85166c..d40fa160663 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/Token.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/scanner/Token.java @@ -510,4 +510,5 @@ public class Token { static public final int t_transient = 215; static public final int t_native = 216; static public final int t_constexpr = 5400; + static public final int t_noexcept = 5401; } 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 c72f0b79fa0..b850037659f 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 @@ -4790,4 +4790,28 @@ public class CodeFormatterTest extends BaseUITestCase { CCorePlugin.INSERT); assertFormatterResult(); } + + //class Foo { + //public: + // void bar() noexcept + // { + // } + // void baz() noexcept(false){ + // } + // void biz() noexcept(noexcept(false)){ + // } + //}; + + //class Foo { + //public: + // void bar() noexcept { + // } + // void baz() noexcept (false) { + // } + // void biz() noexcept (noexcept(false)) { + // } + //}; + public void testNoexcept_Bug562723() throws Exception { + assertFormatterResult(); + } }