1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Bug 562723 - Added support for noexcept in the formatter

Change-Id: I021934657842868c196320f4e126217ab799c07c
This commit is contained in:
Marco Stornelli 2020-05-02 15:52:52 +02:00
parent b45df9e9d5
commit 0124c964b5
4 changed files with 73 additions and 1 deletions

View file

@ -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) {

View file

@ -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$

View file

@ -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;
}

View file

@ -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();
}
}