1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-02 22:05:44 +02:00

Don't call exitNode method in finally blocks since the method may throw.

This commit is contained in:
Sergey Prigogin 2013-03-18 20:39:06 -07:00
parent af6790ddc6
commit 741970beb4

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2012 Wind River Systems, Inc. and others. * Copyright (c) 2006, 2013 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -645,11 +645,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (!enterNode(node)) { if (!enterNode(node)) {
return PROCESS_SKIP; return PROCESS_SKIP;
} }
try { int result = formatDeclaration(node);
return formatDeclaration(node); exitNode(node);
} finally { return result;
exitNode(node);
}
} }
private int formatDeclaration(IASTDeclaration node) { private int formatDeclaration(IASTDeclaration node) {
@ -703,17 +701,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(IASTName node) { public int visit(IASTName node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { if (node instanceof ICPPASTQualifiedName) {
if (node instanceof ICPPASTQualifiedName) { visit((ICPPASTQualifiedName) node);
visit((ICPPASTQualifiedName) node); } else if (node instanceof ICPPASTTemplateId) {
} else if (node instanceof ICPPASTTemplateId) { visit((ICPPASTTemplateId) node);
visit((ICPPASTTemplateId) node); } else {
} else { formatRaw(node);
formatRaw(node);
}
} finally {
exitNode(node);
} }
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -734,19 +729,16 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { if (node instanceof IASTEqualsInitializer) {
if (node instanceof IASTEqualsInitializer) { visit((IASTEqualsInitializer) node);
visit((IASTEqualsInitializer) node); } else if (node instanceof IASTInitializerList) {
} else if (node instanceof IASTInitializerList) { visit((IASTInitializerList) node);
visit((IASTInitializerList) node); } else if (node instanceof ICASTDesignatedInitializer) {
} else if (node instanceof ICASTDesignatedInitializer) { visit((ICASTDesignatedInitializer) node);
visit((ICASTDesignatedInitializer) node); } else {
} else { formatRaw(node);
formatRaw(node);
}
} finally {
exitNode(node);
} }
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -756,24 +748,21 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(IASTParameterDeclaration node) { public int visit(IASTParameterDeclaration node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { // decl-specifier
// decl-specifier final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
final IASTDeclSpecifier declSpec= node.getDeclSpecifier(); if (declSpec != null) {
if (declSpec != null) { declSpec.accept(this);
declSpec.accept(this);
}
// declarator
final IASTDeclarator declarator= node.getDeclarator();
if (declarator != null) {
boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
if (needSpace) {
scribe.space();
}
declarator.accept(this);
}
} finally {
exitNode(node);
} }
// declarator
final IASTDeclarator declarator= node.getDeclarator();
if (declarator != null) {
boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
if (needSpace) {
scribe.space();
}
declarator.accept(this);
}
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -784,58 +773,55 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
public int visit(IASTDeclarator node) { public int visit(IASTDeclarator node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { // Common to all declarators
// Common to all declarators final IASTPointerOperator[] pointerOperators= node.getPointerOperators();
final IASTPointerOperator[] pointerOperators= node.getPointerOperators(); formatPointers(pointerOperators);
formatPointers(pointerOperators); if (node instanceof IASTStandardFunctionDeclarator) {
if (node instanceof IASTStandardFunctionDeclarator) { if (preferences.insert_new_line_before_identifier_in_function_declaration) {
if (preferences.insert_new_line_before_identifier_in_function_declaration) { scribe.startNewLine();
scribe.startNewLine(); } else {
} else { // Preserve newline if not explicitly requested
// Preserve newline if not explicitly requested if (scribe.printCommentPreservingNewLines()) {
if (scribe.printCommentPreservingNewLines()) { scribe.space();
scribe.space();
}
} }
} }
IASTName name= node.getName();
IASTDeclarator nestedDecl= node.getNestedDeclarator();
if (name != null && name.getSimpleID().length != 0 || nestedDecl != null) {
if (node.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR &&
isFirstDeclarator(node)) {
// Preserve non-space between pointer operator and name or nested declarator.
if (pointerOperators.length == 0 || scribe.printComment()) {
scribe.space();
}
}
if (name != null)
name.accept(this);
}
if (nestedDecl != null) {
scribe.printNextToken(Token.tLPAREN, false);
nestedDecl.accept(this);
scribe.printNextToken(Token.tRPAREN, false);
}
if (node instanceof ICPPASTFunctionDeclarator) {
return visit((ICPPASTFunctionDeclarator) node);
} else if (node instanceof IASTStandardFunctionDeclarator) {
visit((IASTStandardFunctionDeclarator) node);
} else if (node instanceof ICASTKnRFunctionDeclarator) {
visit((ICASTKnRFunctionDeclarator) node);
} else if (node instanceof IASTFieldDeclarator) {
visit((IASTFieldDeclarator) node);
} else if (node instanceof IASTArrayDeclarator) {
visit((IASTArrayDeclarator) node);
}
IASTInitializer initializer= node.getInitializer();
if (initializer != null) {
initializer.accept(this);
}
} finally {
exitNode(node);
} }
IASTName name= node.getName();
IASTDeclarator nestedDecl= node.getNestedDeclarator();
if (name != null && name.getSimpleID().length != 0 || nestedDecl != null) {
if (node.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR &&
isFirstDeclarator(node)) {
// Preserve non-space between pointer operator and name or nested declarator.
if (pointerOperators.length == 0 || scribe.printComment()) {
scribe.space();
}
}
if (name != null)
name.accept(this);
}
if (nestedDecl != null) {
scribe.printNextToken(Token.tLPAREN, false);
nestedDecl.accept(this);
scribe.printNextToken(Token.tRPAREN, false);
}
if (node instanceof ICPPASTFunctionDeclarator) {
return visit((ICPPASTFunctionDeclarator) node);
} else if (node instanceof IASTStandardFunctionDeclarator) {
visit((IASTStandardFunctionDeclarator) node);
} else if (node instanceof ICASTKnRFunctionDeclarator) {
visit((ICASTKnRFunctionDeclarator) node);
} else if (node instanceof IASTFieldDeclarator) {
visit((IASTFieldDeclarator) node);
} else if (node instanceof IASTArrayDeclarator) {
visit((IASTArrayDeclarator) node);
}
IASTInitializer initializer= node.getInitializer();
if (initializer != null) {
initializer.accept(this);
}
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -860,25 +846,22 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(IASTDeclSpecifier node) { public int visit(IASTDeclSpecifier node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { if (node instanceof ICPPASTCompositeTypeSpecifier) {
if (node instanceof ICPPASTCompositeTypeSpecifier) { visit((ICPPASTCompositeTypeSpecifier) node);
visit((ICPPASTCompositeTypeSpecifier) node); } else if (node instanceof ICASTCompositeTypeSpecifier) {
} else if (node instanceof ICASTCompositeTypeSpecifier) { visit((ICASTCompositeTypeSpecifier) node);
visit((ICASTCompositeTypeSpecifier) node); } else if (node instanceof IASTElaboratedTypeSpecifier) {
} else if (node instanceof IASTElaboratedTypeSpecifier) { visit((IASTElaboratedTypeSpecifier) node);
visit((IASTElaboratedTypeSpecifier) node); } else if (node instanceof IASTEnumerationSpecifier) {
} else if (node instanceof IASTEnumerationSpecifier) { visit((IASTEnumerationSpecifier) node);
visit((IASTEnumerationSpecifier) node); } else if (node instanceof IASTSimpleDeclSpecifier) {
} else if (node instanceof IASTSimpleDeclSpecifier) { visit((IASTSimpleDeclSpecifier) node);
visit((IASTSimpleDeclSpecifier) node); } else if (node instanceof IASTNamedTypeSpecifier) {
} else if (node instanceof IASTNamedTypeSpecifier) { visit((IASTNamedTypeSpecifier) node);
visit((IASTNamedTypeSpecifier) node); } else {
} else { formatRaw(node);
formatRaw(node);
}
} finally {
exitNode(node);
} }
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -888,45 +871,42 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(IASTExpression node) { public int visit(IASTExpression node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { if (node instanceof IASTArraySubscriptExpression) {
if (node instanceof IASTArraySubscriptExpression) { visit((IASTArraySubscriptExpression) node);
visit((IASTArraySubscriptExpression) node); } else if (node instanceof IASTConditionalExpression) {
} else if (node instanceof IASTConditionalExpression) { visit((IASTConditionalExpression) node);
visit((IASTConditionalExpression) node); } else if (node instanceof IASTFunctionCallExpression) {
} else if (node instanceof IASTFunctionCallExpression) { visit((IASTFunctionCallExpression) node);
visit((IASTFunctionCallExpression) node); } else if (node instanceof IASTExpressionList) {
} else if (node instanceof IASTExpressionList) { visit((IASTExpressionList) node);
visit((IASTExpressionList) node); } else if (node instanceof IASTTypeIdExpression) {
} else if (node instanceof IASTTypeIdExpression) { visit((IASTTypeIdExpression) node);
visit((IASTTypeIdExpression) node); } else if (node instanceof IASTBinaryExpression) {
} else if (node instanceof IASTBinaryExpression) { visit((IASTBinaryExpression) node);
visit((IASTBinaryExpression) node); } else if (node instanceof IASTLiteralExpression) {
} else if (node instanceof IASTLiteralExpression) { visit((IASTLiteralExpression) node);
visit((IASTLiteralExpression) node); } else if (node instanceof IASTIdExpression) {
} else if (node instanceof IASTIdExpression) { visit((IASTIdExpression) node);
visit((IASTIdExpression) node); } else if (node instanceof IASTCastExpression) {
} else if (node instanceof IASTCastExpression) { visit((IASTCastExpression) node);
visit((IASTCastExpression) node); } else if (node instanceof IASTUnaryExpression) {
} else if (node instanceof IASTUnaryExpression) { visit((IASTUnaryExpression) node);
visit((IASTUnaryExpression) node); } else if (node instanceof IASTFieldReference) {
} else if (node instanceof IASTFieldReference) { visit((IASTFieldReference) node);
visit((IASTFieldReference) node); } else if (node instanceof IASTTypeIdInitializerExpression) {
} else if (node instanceof IASTTypeIdInitializerExpression) { visit((IASTTypeIdInitializerExpression) node);
visit((IASTTypeIdInitializerExpression) node); } else if (node instanceof ICPPASTNewExpression) {
} else if (node instanceof ICPPASTNewExpression) { visit((ICPPASTNewExpression) node);
visit((ICPPASTNewExpression) node); } else if (node instanceof ICPPASTDeleteExpression) {
} else if (node instanceof ICPPASTDeleteExpression) { visit((ICPPASTDeleteExpression) node);
visit((ICPPASTDeleteExpression) node); } else if (node instanceof ICPPASTSimpleTypeConstructorExpression) {
} else if (node instanceof ICPPASTSimpleTypeConstructorExpression) { visit((ICPPASTSimpleTypeConstructorExpression) node);
visit((ICPPASTSimpleTypeConstructorExpression) node); } else if (node instanceof IASTProblemExpression) {
} else if (node instanceof IASTProblemExpression) { visit((IASTProblemExpression) node);
visit((IASTProblemExpression) node); } else {
} else { formatRaw(node);
formatRaw(node);
}
} finally {
exitNode(node);
} }
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -982,6 +962,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} else { } else {
formatRaw(node); formatRaw(node);
} }
exitNode(node);
} catch (ASTProblemException e) { } catch (ASTProblemException e) {
if (node instanceof IASTProblemStatement) { if (node instanceof IASTProblemStatement) {
throw e; throw e;
@ -991,7 +972,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.unIndent(); scribe.unIndent();
} }
} }
} finally {
exitNode(node); exitNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
@ -1003,27 +983,24 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(IASTTypeId node) { public int visit(IASTTypeId node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { if (node instanceof IASTProblemHolder) {
if (node instanceof IASTProblemHolder) { throw new ASTProblemException(((IASTProblemHolder) node).getProblem());
throw new ASTProblemException(((IASTProblemHolder) node).getProblem());
}
// decl-specifier
final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
if (declSpec != null) {
declSpec.accept(this);
}
// declarator
final IASTDeclarator declarator= node.getAbstractDeclarator();
if (declarator != null) {
boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
if (needSpace) {
scribe.space();
}
declarator.accept(this);
}
} finally {
exitNode(node);
} }
// decl-specifier
final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
if (declSpec != null) {
declSpec.accept(this);
}
// declarator
final IASTDeclarator declarator= node.getAbstractDeclarator();
if (declarator != null) {
boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
if (needSpace) {
scribe.space();
}
declarator.accept(this);
}
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1033,22 +1010,19 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(IASTEnumerator enumerator) { public int visit(IASTEnumerator enumerator) {
if (!enterNode(enumerator)) { return PROCESS_SKIP; } if (!enterNode(enumerator)) { return PROCESS_SKIP; }
try { // name
// name enumerator.getName().accept(this);
enumerator.getName().accept(this);
// optional value assignment // optional value assignment
final IASTExpression value= enumerator.getValue(); final IASTExpression value= enumerator.getValue();
if (value != null) { if (value != null) {
scribe.printNextToken(Token.tASSIGN, preferences.insert_space_before_assignment_operator); scribe.printNextToken(Token.tASSIGN, preferences.insert_space_before_assignment_operator);
if (preferences.insert_space_after_assignment_operator) { if (preferences.insert_space_after_assignment_operator) {
scribe.space(); scribe.space();
}
value.accept(this);
} }
} finally { value.accept(this);
exitNode(enumerator);
} }
exitNode(enumerator);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1058,29 +1032,26 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(ICPPASTBaseSpecifier specifier) { public int visit(ICPPASTBaseSpecifier specifier) {
if (!enterNode(specifier)) { return PROCESS_SKIP; } if (!enterNode(specifier)) { return PROCESS_SKIP; }
try { boolean needSpace= false;
boolean needSpace= false; loop: while (true) {
loop: while (true) { int token= peekNextToken();
int token= peekNextToken(); switch (token) {
switch (token) { case Token.t_public:
case Token.t_public: case Token.t_protected:
case Token.t_protected: case Token.t_private:
case Token.t_private: case Token.t_virtual:
case Token.t_virtual: scribe.printNextToken(token, needSpace);
scribe.printNextToken(token, needSpace); needSpace= true;
needSpace= true; break;
break; default:
default: break loop;
break loop;
}
} }
if (needSpace) {
scribe.space();
}
specifier.getName().accept(this);
} finally {
exitNode(specifier);
} }
if (needSpace) {
scribe.space();
}
specifier.getName().accept(this);
exitNode(specifier);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1090,29 +1061,26 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override @Override
public int visit(ICPPASTNamespaceDefinition node) { public int visit(ICPPASTNamespaceDefinition node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { final int line= scribe.line;
final int line= scribe.line; // namespace <name>
// namespace <name> scribe.printNextToken(Token.t_namespace, false);
scribe.printNextToken(Token.t_namespace, false); scribe.space();
scribe.space(); node.getName().accept(this);
node.getName().accept(this);
// member declarations // member declarations
IASTDeclaration[] memberDecls= node.getDeclarations(); IASTDeclaration[] memberDecls= node.getDeclarations();
formatLeftCurlyBrace(line, preferences.brace_position_for_namespace_declaration); formatLeftCurlyBrace(line, preferences.brace_position_for_namespace_declaration);
formatOpeningBrace(preferences.brace_position_for_namespace_declaration, preferences.insert_space_before_opening_brace_in_namespace_declaration); formatOpeningBrace(preferences.brace_position_for_namespace_declaration, preferences.insert_space_before_opening_brace_in_namespace_declaration);
if (preferences.indent_body_declarations_compare_to_namespace_header) { if (preferences.indent_body_declarations_compare_to_namespace_header) {
scribe.indent(); scribe.indent();
}
scribe.startNewLine();
formatDeclarations(memberDecls, scribe.indentationLevel);
if (preferences.indent_body_declarations_compare_to_namespace_header) {
scribe.unIndent();
}
formatClosingBrace(preferences.brace_position_for_namespace_declaration);
} finally {
exitNode(node);
} }
scribe.startNewLine();
formatDeclarations(memberDecls, scribe.indentationLevel);
if (preferences.indent_body_declarations_compare_to_namespace_header) {
scribe.unIndent();
}
formatClosingBrace(preferences.brace_position_for_namespace_declaration);
exitNode(node);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1222,9 +1190,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} else { } else {
visit((IASTParameterDeclaration) node); visit((IASTParameterDeclaration) node);
} }
exitNode(node);
} catch (ASTProblemException e) { } catch (ASTProblemException e) {
skipNode(node); skipNode(node);
} finally {
exitNode(node); exitNode(node);
} }
return PROCESS_SKIP; return PROCESS_SKIP;
@ -1292,12 +1260,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private int visit(ICPPASTConstructorInitializer node) { private int visit(ICPPASTConstructorInitializer node) {
if (!enterNode(node)) { return PROCESS_SKIP; } if (!enterNode(node)) { return PROCESS_SKIP; }
try { // Format like a function call
// Format like a function call formatFunctionCallArguments(node.getArguments());
formatFunctionCallArguments(node.getArguments()); exitNode(node);
} finally {
exitNode(node);
}
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -1399,17 +1364,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
// Body // Body
if (bodyStmt instanceof IASTCompoundStatement) { if (bodyStmt instanceof IASTCompoundStatement) {
if (enterNode(bodyStmt)) { if (enterNode(bodyStmt)) {
try { if (getCurrentPosition() <= nodeOffset(bodyStmt)) {
if (getCurrentPosition() <= nodeOffset(bodyStmt)) { formatLeftCurlyBrace(line, preferences.brace_position_for_method_declaration);
formatLeftCurlyBrace(line, preferences.brace_position_for_method_declaration);
}
formatBlock((IASTCompoundStatement) bodyStmt,
preferences.brace_position_for_method_declaration,
preferences.insert_space_before_opening_brace_in_method_declaration,
preferences.indent_statements_compare_to_body);
} finally {
exitNode(bodyStmt);
} }
formatBlock((IASTCompoundStatement) bodyStmt,
preferences.brace_position_for_method_declaration,
preferences.insert_space_before_opening_brace_in_method_declaration,
preferences.indent_statements_compare_to_body);
exitNode(bodyStmt);
} }
} else if (bodyStmt != null) { } else if (bodyStmt != null) {
bodyStmt.accept(this); bodyStmt.accept(this);
@ -1934,23 +1896,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.unIndent(); scribe.unIndent();
} }
if (enterNode(declaration)) { if (enterNode(declaration)) {
try { scribe.startNewLine();
scribe.startNewLine(); visit((ICPPASTVisibilityLabel) declaration);
visit((ICPPASTVisibilityLabel) declaration); scribe.startNewLine();
scribe.startNewLine(); exitNode(declaration);
} finally {
exitNode(declaration);
}
} }
} else { } else {
if (enterNode(declaration)) { if (enterNode(declaration)) {
try { if (getCurrentPosition() <= nodeOffset(declaration))
if (getCurrentPosition() <= nodeOffset(declaration)) scribe.startNewLine();
scribe.startNewLine(); formatDeclaration(declaration);
formatDeclaration(declaration); exitNode(declaration);
} finally {
exitNode(declaration);
}
} else { } else {
skipNode(declaration); skipNode(declaration);
} }
@ -3262,17 +3218,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) { if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
if (enterNode(body)) { if (enterNode(body)) {
try { if (getCurrentPosition() <= nodeOffset(body)) {
if (getCurrentPosition() <= nodeOffset(body)) { formatLeftCurlyBrace(line, preferences.brace_position_for_block);
formatLeftCurlyBrace(line, preferences.brace_position_for_block);
}
formatBlock((IASTCompoundStatement) body,
preferences.brace_position_for_block,
preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block);
} finally {
exitNode(body);
} }
formatBlock((IASTCompoundStatement) body,
preferences.brace_position_for_block,
preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block);
exitNode(body);
} }
} else { } else {
formatAction(line, body, preferences.brace_position_for_block); formatAction(line, body, preferences.brace_position_for_block);
@ -3618,132 +3571,129 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
statements= Collections.singletonList(bodyStmt); statements= Collections.singletonList(bodyStmt);
} }
if (!enterNode(bodyStmt)) { return PROCESS_SKIP; } if (!enterNode(bodyStmt)) { return PROCESS_SKIP; }
try { final int statementsLength = statements.size();
final int statementsLength = statements.size(); if (statementsLength != 0) {
if (statementsLength != 0) { boolean wasACase = false;
boolean wasACase = false; boolean wasAStatement = false;
boolean wasAStatement = false; for (int i = 0; i < statementsLength; i++) {
for (int i = 0; i < statementsLength; i++) { final IASTStatement statement = statements.get(i);
final IASTStatement statement = statements.get(i); if (doNodeLocationsOverlap(controllerExpression, statement)) {
if (doNodeLocationsOverlap(controllerExpression, statement)) { statement.accept(this);
statement.accept(this); continue;
continue; }
if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) {
if (wasACase) {
scribe.startNewLine();
} }
if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) { if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) {
if (wasACase) { scribe.unIndent();
scribe.startNewLine(); }
statement.accept(this);
scribe.printTrailingComment();
wasACase = true;
wasAStatement = false;
if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
} else if (statement instanceof IASTBreakStatement) {
if (preferences.indent_breaks_compare_to_cases) {
if (!preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
} }
} else {
if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) { if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) {
scribe.unIndent(); scribe.unIndent();
} }
statement.accept(this); }
scribe.printTrailingComment(); if (wasACase) {
wasACase = true;
wasAStatement = false;
if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
} else if (statement instanceof IASTBreakStatement) {
if (preferences.indent_breaks_compare_to_cases) {
if (!preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
} else {
if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) {
scribe.unIndent();
}
}
if (wasACase) {
scribe.startNewLine();
}
statement.accept(this);
if (preferences.indent_breaks_compare_to_cases) {
if (!preferences.indent_switchstatements_compare_to_cases) {
scribe.unIndent();
}
} else if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
wasACase = false;
wasAStatement = true;
} else if (statement instanceof IASTCompoundStatement && !startsWithMacroExpansion(statement)) {
String bracePosition;
if (wasACase) {
if (preferences.indent_switchstatements_compare_to_cases) {
scribe.unIndent();
}
bracePosition = preferences.brace_position_for_block_in_case;
try {
enterNode(statement);
formatBlock((IASTCompoundStatement) statement, bracePosition,
preferences.insert_space_after_colon_in_case,
preferences.indent_statements_compare_to_block);
} catch (ASTProblemException e) {
if (i < statementsLength - 1) {
final IASTStatement nextStatement= statements.get(i + 1);
skipToNode(nextStatement);
}
} finally {
exitNode(statement);
}
if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
} else {
bracePosition = preferences.brace_position_for_block;
try {
enterNode(statement);
formatBlock((IASTCompoundStatement) statement, bracePosition,
preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block);
} catch (ASTProblemException e) {
if (i < statementsLength - 1) {
final IASTStatement nextStatement= statements.get(i + 1);
skipToNode(nextStatement);
}
} finally {
exitNode(statement);
}
}
wasAStatement = true;
wasACase = false;
} else {
scribe.startNewLine(); scribe.startNewLine();
}
statement.accept(this);
if (preferences.indent_breaks_compare_to_cases) {
if (!preferences.indent_switchstatements_compare_to_cases) {
scribe.unIndent();
}
} else if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
wasACase = false;
wasAStatement = true;
} else if (statement instanceof IASTCompoundStatement && !startsWithMacroExpansion(statement)) {
String bracePosition;
if (wasACase) {
if (preferences.indent_switchstatements_compare_to_cases) {
scribe.unIndent();
}
bracePosition = preferences.brace_position_for_block_in_case;
try { try {
statement.accept(this); enterNode(statement);
formatBlock((IASTCompoundStatement) statement, bracePosition,
preferences.insert_space_after_colon_in_case,
preferences.indent_statements_compare_to_block);
exitNode(statement);
} catch (ASTProblemException e) { } catch (ASTProblemException e) {
if (i < statementsLength - 1) { if (i < statementsLength - 1) {
final IASTStatement nextStatement= statements.get(i + 1); final IASTStatement nextStatement= statements.get(i + 1);
skipToNode(nextStatement); skipToNode(nextStatement);
} }
exitNode(statement);
}
if (preferences.indent_switchstatements_compare_to_cases) {
scribe.indent();
}
} else {
bracePosition = preferences.brace_position_for_block;
try {
enterNode(statement);
formatBlock((IASTCompoundStatement) statement, bracePosition,
preferences.insert_space_before_opening_brace_in_block,
preferences.indent_statements_compare_to_block);
exitNode(statement);
} catch (ASTProblemException e) {
if (i < statementsLength - 1) {
final IASTStatement nextStatement= statements.get(i + 1);
skipToNode(nextStatement);
}
exitNode(statement);
} }
wasAStatement = true;
wasACase = false;
} }
if (!wasACase) { wasAStatement = true;
scribe.startNewLine(); wasACase = false;
} else {
scribe.startNewLine();
try {
statement.accept(this);
} catch (ASTProblemException e) {
if (i < statementsLength - 1) {
final IASTStatement nextStatement= statements.get(i + 1);
skipToNode(nextStatement);
}
} }
scribe.printComment(); wasAStatement = true;
wasACase = false;
} }
if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) { if (!wasACase) {
scribe.unIndent(); scribe.startNewLine();
} }
scribe.printComment();
} }
if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) {
if (!startsWithMacroExpansion(bodyStmt)) { scribe.unIndent();
if (preferences.indent_switchstatements_compare_to_switch) {
scribe.unIndent();
}
if (scribe.numberOfIndentations < braceIndent) {
scribe.indent();
}
scribe.startNewLine();
formatClosingBrace(brace_position);
} }
} finally {
exitNode(bodyStmt);
} }
if (!startsWithMacroExpansion(bodyStmt)) {
if (preferences.indent_switchstatements_compare_to_switch) {
scribe.unIndent();
}
if (scribe.numberOfIndentations < braceIndent) {
scribe.indent();
}
scribe.startNewLine();
formatClosingBrace(brace_position);
}
exitNode(bodyStmt);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
@ -3816,14 +3766,16 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return false; return false;
IASTNodeLocation[] locations= node.getNodeLocations(); IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) { if (locations.length == 0) {
return true;
} else if (!fInsideMacroArguments && locations[0] instanceof IASTMacroExpansionLocation) { } else if (!fInsideMacroArguments && locations[0] instanceof IASTMacroExpansionLocation) {
IASTMacroExpansionLocation location = (IASTMacroExpansionLocation) locations[0]; IASTMacroExpansionLocation location = (IASTMacroExpansionLocation) locations[0];
if (locations.length <= 2 && node instanceof IASTStatement) { if (locations.length <= 2 && node instanceof IASTStatement) {
IASTPreprocessorMacroExpansion macroExpansion = location.getExpansion(); IASTPreprocessorMacroExpansion macroExpansion = location.getExpansion();
IASTFileLocation macroLocation = macroExpansion.getFileLocation(); IASTFileLocation macroLocation = macroExpansion.getFileLocation();
if (macroLocation.getNodeOffset() >= currentPosition && int macroOffset = macroLocation.getNodeOffset();
!scribe.shouldSkip(macroLocation.getNodeOffset()) && if (macroOffset >= currentPosition &&
(nodeEndOffset == macroLocation.getNodeOffset() + macroLocation.getNodeLength() || !scribe.shouldSkip(macroOffset) &&
(nodeEndOffset == macroOffset + macroLocation.getNodeLength() ||
locations.length == 2 && isSemicolonLocation(locations[1])) && locations.length == 2 && isSemicolonLocation(locations[1])) &&
isFunctionStyleMacroExpansion(macroExpansion)) { isFunctionStyleMacroExpansion(macroExpansion)) {
if (locations.length == 2 && isSemicolonLocation(locations[1])) { if (locations.length == 2 && isSemicolonLocation(locations[1])) {
@ -3844,7 +3796,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
continueNode(node.getParent()); continueNode(node.getParent());
return false; return false;
} }
} else { } else if (nodeLocation != null) {
scribe.restartAtOffset(nodeLocation.getNodeOffset()); scribe.restartAtOffset(nodeLocation.getNodeOffset());
} }
return true; return true;
@ -3995,13 +3947,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (stmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(stmt)) { if (stmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(stmt)) {
formatLeftCurlyBrace(line, brace_position); formatLeftCurlyBrace(line, brace_position);
if (enterNode(stmt)) { if (enterNode(stmt)) {
try { formatBlock((IASTCompoundStatement) stmt, brace_position,
formatBlock((IASTCompoundStatement) stmt, brace_position, preferences.insert_space_before_opening_brace_in_block,
preferences.insert_space_before_opening_brace_in_block, preferences.indent_statements_compare_to_block);
preferences.indent_statements_compare_to_block); exitNode(stmt);
} finally {
exitNode(stmt);
}
} }
} else if (stmt instanceof IASTNullStatement) { } else if (stmt instanceof IASTNullStatement) {
scribe.indent(); scribe.indent();