summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2013-03-18 23:39:06 -0400
committerSergey Prigogin2013-03-18 23:39:53 -0400
commit741970beb4b68e075593800078ffc46ec366f5f2 (patch)
treeb4ee35695264a3bb2a9c217082df4c08d6bf93fc
parentaf6790ddc6499148727df070489451e243ab6ddc (diff)
downloadorg.eclipse.cdt-741970beb4b68e075593800078ffc46ec366f5f2.zip
org.eclipse.cdt-741970beb4b68e075593800078ffc46ec366f5f2.tar.gz
org.eclipse.cdt-741970beb4b68e075593800078ffc46ec366f5f2.tar.xz
Don't call exitNode method in finally blocks since the method may throw.
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java693
1 files changed, 321 insertions, 372 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 f95cdeb..14321ad 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
@@ -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
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -645,11 +645,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (!enterNode(node)) {
return PROCESS_SKIP;
}
- try {
- return formatDeclaration(node);
- } finally {
- exitNode(node);
- }
+ int result = formatDeclaration(node);
+ exitNode(node);
+ return result;
}
private int formatDeclaration(IASTDeclaration node) {
@@ -703,17 +701,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(IASTName node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- if (node instanceof ICPPASTQualifiedName) {
- visit((ICPPASTQualifiedName) node);
- } else if (node instanceof ICPPASTTemplateId) {
- visit((ICPPASTTemplateId) node);
- } else {
- formatRaw(node);
- }
- } finally {
- exitNode(node);
+ if (node instanceof ICPPASTQualifiedName) {
+ visit((ICPPASTQualifiedName) node);
+ } else if (node instanceof ICPPASTTemplateId) {
+ visit((ICPPASTTemplateId) node);
+ } else {
+ formatRaw(node);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -734,19 +729,16 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- if (node instanceof IASTEqualsInitializer) {
- visit((IASTEqualsInitializer) node);
- } else if (node instanceof IASTInitializerList) {
- visit((IASTInitializerList) node);
- } else if (node instanceof ICASTDesignatedInitializer) {
- visit((ICASTDesignatedInitializer) node);
- } else {
- formatRaw(node);
- }
- } finally {
- exitNode(node);
+ if (node instanceof IASTEqualsInitializer) {
+ visit((IASTEqualsInitializer) node);
+ } else if (node instanceof IASTInitializerList) {
+ visit((IASTInitializerList) node);
+ } else if (node instanceof ICASTDesignatedInitializer) {
+ visit((ICASTDesignatedInitializer) node);
+ } else {
+ formatRaw(node);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -756,24 +748,21 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(IASTParameterDeclaration node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- // decl-specifier
- final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
- if (declSpec != null) {
- 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);
+ // decl-specifier
+ final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
+ if (declSpec != null) {
+ declSpec.accept(this);
+ }
+ // declarator
+ final IASTDeclarator declarator= node.getDeclarator();
+ if (declarator != null) {
+ boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
+ if (needSpace) {
+ scribe.space();
}
- } finally {
- exitNode(node);
+ declarator.accept(this);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -784,58 +773,55 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
public int visit(IASTDeclarator node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- // Common to all declarators
- final IASTPointerOperator[] pointerOperators= node.getPointerOperators();
- formatPointers(pointerOperators);
- if (node instanceof IASTStandardFunctionDeclarator) {
- if (preferences.insert_new_line_before_identifier_in_function_declaration) {
- scribe.startNewLine();
- } else {
- // Preserve newline if not explicitly requested
- if (scribe.printCommentPreservingNewLines()) {
- scribe.space();
- }
+ // Common to all declarators
+ final IASTPointerOperator[] pointerOperators= node.getPointerOperators();
+ formatPointers(pointerOperators);
+ if (node instanceof IASTStandardFunctionDeclarator) {
+ if (preferences.insert_new_line_before_identifier_in_function_declaration) {
+ scribe.startNewLine();
+ } else {
+ // Preserve newline if not explicitly requested
+ if (scribe.printCommentPreservingNewLines()) {
+ 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();
- }
+ }
+ 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 (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);
- }
+ 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);
+ IASTInitializer initializer= node.getInitializer();
+ if (initializer != null) {
+ initializer.accept(this);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -860,25 +846,22 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(IASTDeclSpecifier node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- if (node instanceof ICPPASTCompositeTypeSpecifier) {
- visit((ICPPASTCompositeTypeSpecifier) node);
- } else if (node instanceof ICASTCompositeTypeSpecifier) {
- visit((ICASTCompositeTypeSpecifier) node);
- } else if (node instanceof IASTElaboratedTypeSpecifier) {
- visit((IASTElaboratedTypeSpecifier) node);
- } else if (node instanceof IASTEnumerationSpecifier) {
- visit((IASTEnumerationSpecifier) node);
- } else if (node instanceof IASTSimpleDeclSpecifier) {
- visit((IASTSimpleDeclSpecifier) node);
- } else if (node instanceof IASTNamedTypeSpecifier) {
- visit((IASTNamedTypeSpecifier) node);
- } else {
- formatRaw(node);
- }
- } finally {
- exitNode(node);
+ if (node instanceof ICPPASTCompositeTypeSpecifier) {
+ visit((ICPPASTCompositeTypeSpecifier) node);
+ } else if (node instanceof ICASTCompositeTypeSpecifier) {
+ visit((ICASTCompositeTypeSpecifier) node);
+ } else if (node instanceof IASTElaboratedTypeSpecifier) {
+ visit((IASTElaboratedTypeSpecifier) node);
+ } else if (node instanceof IASTEnumerationSpecifier) {
+ visit((IASTEnumerationSpecifier) node);
+ } else if (node instanceof IASTSimpleDeclSpecifier) {
+ visit((IASTSimpleDeclSpecifier) node);
+ } else if (node instanceof IASTNamedTypeSpecifier) {
+ visit((IASTNamedTypeSpecifier) node);
+ } else {
+ formatRaw(node);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -888,45 +871,42 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(IASTExpression node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- if (node instanceof IASTArraySubscriptExpression) {
- visit((IASTArraySubscriptExpression) node);
- } else if (node instanceof IASTConditionalExpression) {
- visit((IASTConditionalExpression) node);
- } else if (node instanceof IASTFunctionCallExpression) {
- visit((IASTFunctionCallExpression) node);
- } else if (node instanceof IASTExpressionList) {
- visit((IASTExpressionList) node);
- } else if (node instanceof IASTTypeIdExpression) {
- visit((IASTTypeIdExpression) node);
- } else if (node instanceof IASTBinaryExpression) {
- visit((IASTBinaryExpression) node);
- } else if (node instanceof IASTLiteralExpression) {
- visit((IASTLiteralExpression) node);
- } else if (node instanceof IASTIdExpression) {
- visit((IASTIdExpression) node);
- } else if (node instanceof IASTCastExpression) {
- visit((IASTCastExpression) node);
- } else if (node instanceof IASTUnaryExpression) {
- visit((IASTUnaryExpression) node);
- } else if (node instanceof IASTFieldReference) {
- visit((IASTFieldReference) node);
- } else if (node instanceof IASTTypeIdInitializerExpression) {
- visit((IASTTypeIdInitializerExpression) node);
- } else if (node instanceof ICPPASTNewExpression) {
- visit((ICPPASTNewExpression) node);
- } else if (node instanceof ICPPASTDeleteExpression) {
- visit((ICPPASTDeleteExpression) node);
- } else if (node instanceof ICPPASTSimpleTypeConstructorExpression) {
- visit((ICPPASTSimpleTypeConstructorExpression) node);
- } else if (node instanceof IASTProblemExpression) {
- visit((IASTProblemExpression) node);
- } else {
- formatRaw(node);
- }
- } finally {
- exitNode(node);
+ if (node instanceof IASTArraySubscriptExpression) {
+ visit((IASTArraySubscriptExpression) node);
+ } else if (node instanceof IASTConditionalExpression) {
+ visit((IASTConditionalExpression) node);
+ } else if (node instanceof IASTFunctionCallExpression) {
+ visit((IASTFunctionCallExpression) node);
+ } else if (node instanceof IASTExpressionList) {
+ visit((IASTExpressionList) node);
+ } else if (node instanceof IASTTypeIdExpression) {
+ visit((IASTTypeIdExpression) node);
+ } else if (node instanceof IASTBinaryExpression) {
+ visit((IASTBinaryExpression) node);
+ } else if (node instanceof IASTLiteralExpression) {
+ visit((IASTLiteralExpression) node);
+ } else if (node instanceof IASTIdExpression) {
+ visit((IASTIdExpression) node);
+ } else if (node instanceof IASTCastExpression) {
+ visit((IASTCastExpression) node);
+ } else if (node instanceof IASTUnaryExpression) {
+ visit((IASTUnaryExpression) node);
+ } else if (node instanceof IASTFieldReference) {
+ visit((IASTFieldReference) node);
+ } else if (node instanceof IASTTypeIdInitializerExpression) {
+ visit((IASTTypeIdInitializerExpression) node);
+ } else if (node instanceof ICPPASTNewExpression) {
+ visit((ICPPASTNewExpression) node);
+ } else if (node instanceof ICPPASTDeleteExpression) {
+ visit((ICPPASTDeleteExpression) node);
+ } else if (node instanceof ICPPASTSimpleTypeConstructorExpression) {
+ visit((ICPPASTSimpleTypeConstructorExpression) node);
+ } else if (node instanceof IASTProblemExpression) {
+ visit((IASTProblemExpression) node);
+ } else {
+ formatRaw(node);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -982,6 +962,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} else {
formatRaw(node);
}
+ exitNode(node);
} catch (ASTProblemException e) {
if (node instanceof IASTProblemStatement) {
throw e;
@@ -991,7 +972,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.unIndent();
}
}
- } finally {
exitNode(node);
}
return PROCESS_SKIP;
@@ -1003,27 +983,24 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(IASTTypeId node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- if (node instanceof IASTProblemHolder) {
- 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);
+ if (node instanceof IASTProblemHolder) {
+ 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();
}
- } finally {
- exitNode(node);
+ declarator.accept(this);
}
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -1033,22 +1010,19 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(IASTEnumerator enumerator) {
if (!enterNode(enumerator)) { return PROCESS_SKIP; }
- try {
- // name
- enumerator.getName().accept(this);
-
- // optional value assignment
- final IASTExpression value= enumerator.getValue();
- if (value != null) {
- scribe.printNextToken(Token.tASSIGN, preferences.insert_space_before_assignment_operator);
- if (preferences.insert_space_after_assignment_operator) {
- scribe.space();
- }
- value.accept(this);
+ // name
+ enumerator.getName().accept(this);
+
+ // optional value assignment
+ final IASTExpression value= enumerator.getValue();
+ if (value != null) {
+ scribe.printNextToken(Token.tASSIGN, preferences.insert_space_before_assignment_operator);
+ if (preferences.insert_space_after_assignment_operator) {
+ scribe.space();
}
- } finally {
- exitNode(enumerator);
+ value.accept(this);
}
+ exitNode(enumerator);
return PROCESS_SKIP;
}
@@ -1058,29 +1032,26 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(ICPPASTBaseSpecifier specifier) {
if (!enterNode(specifier)) { return PROCESS_SKIP; }
- try {
- boolean needSpace= false;
- loop: while (true) {
- int token= peekNextToken();
- switch (token) {
- case Token.t_public:
- case Token.t_protected:
- case Token.t_private:
- case Token.t_virtual:
- scribe.printNextToken(token, needSpace);
- needSpace= true;
- break;
- default:
- break loop;
- }
- }
- if (needSpace) {
- scribe.space();
+ boolean needSpace= false;
+ loop: while (true) {
+ int token= peekNextToken();
+ switch (token) {
+ case Token.t_public:
+ case Token.t_protected:
+ case Token.t_private:
+ case Token.t_virtual:
+ scribe.printNextToken(token, needSpace);
+ needSpace= true;
+ break;
+ default:
+ break loop;
}
- specifier.getName().accept(this);
- } finally {
- exitNode(specifier);
}
+ if (needSpace) {
+ scribe.space();
+ }
+ specifier.getName().accept(this);
+ exitNode(specifier);
return PROCESS_SKIP;
}
@@ -1090,29 +1061,26 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
@Override
public int visit(ICPPASTNamespaceDefinition node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- final int line= scribe.line;
- // namespace <name>
- scribe.printNextToken(Token.t_namespace, false);
- scribe.space();
- node.getName().accept(this);
+ final int line= scribe.line;
+ // namespace <name>
+ scribe.printNextToken(Token.t_namespace, false);
+ scribe.space();
+ node.getName().accept(this);
- // member declarations
- IASTDeclaration[] memberDecls= node.getDeclarations();
- formatLeftCurlyBrace(line, preferences.brace_position_for_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) {
- 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);
+ // member declarations
+ IASTDeclaration[] memberDecls= node.getDeclarations();
+ formatLeftCurlyBrace(line, preferences.brace_position_for_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) {
+ 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);
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -1222,9 +1190,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} else {
visit((IASTParameterDeclaration) node);
}
+ exitNode(node);
} catch (ASTProblemException e) {
skipNode(node);
- } finally {
exitNode(node);
}
return PROCESS_SKIP;
@@ -1292,12 +1260,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private int visit(ICPPASTConstructorInitializer node) {
if (!enterNode(node)) { return PROCESS_SKIP; }
- try {
- // Format like a function call
- formatFunctionCallArguments(node.getArguments());
- } finally {
- exitNode(node);
- }
+ // Format like a function call
+ formatFunctionCallArguments(node.getArguments());
+ exitNode(node);
return PROCESS_SKIP;
}
@@ -1399,17 +1364,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
// Body
if (bodyStmt instanceof IASTCompoundStatement) {
if (enterNode(bodyStmt)) {
- try {
- if (getCurrentPosition() <= nodeOffset(bodyStmt)) {
- 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);
+ if (getCurrentPosition() <= nodeOffset(bodyStmt)) {
+ 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);
+ exitNode(bodyStmt);
}
} else if (bodyStmt != null) {
bodyStmt.accept(this);
@@ -1934,23 +1896,17 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.unIndent();
}
if (enterNode(declaration)) {
- try {
- scribe.startNewLine();
- visit((ICPPASTVisibilityLabel) declaration);
- scribe.startNewLine();
- } finally {
- exitNode(declaration);
- }
+ scribe.startNewLine();
+ visit((ICPPASTVisibilityLabel) declaration);
+ scribe.startNewLine();
+ exitNode(declaration);
}
} else {
if (enterNode(declaration)) {
- try {
- if (getCurrentPosition() <= nodeOffset(declaration))
- scribe.startNewLine();
- formatDeclaration(declaration);
- } finally {
- exitNode(declaration);
- }
+ if (getCurrentPosition() <= nodeOffset(declaration))
+ scribe.startNewLine();
+ formatDeclaration(declaration);
+ exitNode(declaration);
} else {
skipNode(declaration);
}
@@ -3262,17 +3218,14 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (body instanceof IASTCompoundStatement && !startsWithMacroExpansion(body)) {
if (enterNode(body)) {
- try {
- if (getCurrentPosition() <= nodeOffset(body)) {
- 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);
+ if (getCurrentPosition() <= nodeOffset(body)) {
+ 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);
+ exitNode(body);
}
} else {
formatAction(line, body, preferences.brace_position_for_block);
@@ -3618,132 +3571,129 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
statements= Collections.singletonList(bodyStmt);
}
if (!enterNode(bodyStmt)) { return PROCESS_SKIP; }
- try {
- final int statementsLength = statements.size();
- if (statementsLength != 0) {
- boolean wasACase = false;
- boolean wasAStatement = false;
- for (int i = 0; i < statementsLength; i++) {
- final IASTStatement statement = statements.get(i);
- if (doNodeLocationsOverlap(controllerExpression, statement)) {
- statement.accept(this);
- continue;
+ final int statementsLength = statements.size();
+ if (statementsLength != 0) {
+ boolean wasACase = false;
+ boolean wasAStatement = false;
+ for (int i = 0; i < statementsLength; i++) {
+ final IASTStatement statement = statements.get(i);
+ if (doNodeLocationsOverlap(controllerExpression, statement)) {
+ statement.accept(this);
+ continue;
+ }
+ if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) {
+ if (wasACase) {
+ scribe.startNewLine();
+ }
+ if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) {
+ scribe.unIndent();
+ }
+ statement.accept(this);
+ scribe.printTrailingComment();
+ wasACase = true;
+ wasAStatement = false;
+ if (preferences.indent_switchstatements_compare_to_cases) {
+ scribe.indent();
}
- if (statement instanceof IASTCaseStatement || statement instanceof IASTDefaultStatement) {
- if (wasACase) {
- scribe.startNewLine();
+ } 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();
}
- statement.accept(this);
- scribe.printTrailingComment();
- wasACase = true;
- wasAStatement = false;
+ }
+ 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.indent();
+ scribe.unIndent();
}
- } 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();
+ 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);
+ exitNode(statement);
+ } catch (ASTProblemException e) {
+ if (i < statementsLength - 1) {
+ final IASTStatement nextStatement= statements.get(i + 1);
+ skipToNode(nextStatement);
}
+ exitNode(statement);
}
- 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) {
+ 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();
+ bracePosition = preferences.brace_position_for_block;
try {
- statement.accept(this);
+ 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) {
- scribe.startNewLine();
+ wasAStatement = true;
+ 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) {
- scribe.unIndent();
+ if (!wasACase) {
+ scribe.startNewLine();
}
+ scribe.printComment();
}
+ if ((wasACase || wasAStatement) && preferences.indent_switchstatements_compare_to_cases) {
+ scribe.unIndent();
+ }
+ }
- if (!startsWithMacroExpansion(bodyStmt)) {
- if (preferences.indent_switchstatements_compare_to_switch) {
- scribe.unIndent();
- }
- if (scribe.numberOfIndentations < braceIndent) {
- scribe.indent();
- }
- scribe.startNewLine();
-
- formatClosingBrace(brace_position);
+ if (!startsWithMacroExpansion(bodyStmt)) {
+ if (preferences.indent_switchstatements_compare_to_switch) {
+ scribe.unIndent();
}
- } finally {
- exitNode(bodyStmt);
+ if (scribe.numberOfIndentations < braceIndent) {
+ scribe.indent();
+ }
+ scribe.startNewLine();
+
+ formatClosingBrace(brace_position);
}
+ exitNode(bodyStmt);
return PROCESS_SKIP;
}
@@ -3816,14 +3766,16 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return false;
IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) {
+ return true;
} else if (!fInsideMacroArguments && locations[0] instanceof IASTMacroExpansionLocation) {
IASTMacroExpansionLocation location = (IASTMacroExpansionLocation) locations[0];
if (locations.length <= 2 && node instanceof IASTStatement) {
IASTPreprocessorMacroExpansion macroExpansion = location.getExpansion();
IASTFileLocation macroLocation = macroExpansion.getFileLocation();
- if (macroLocation.getNodeOffset() >= currentPosition &&
- !scribe.shouldSkip(macroLocation.getNodeOffset()) &&
- (nodeEndOffset == macroLocation.getNodeOffset() + macroLocation.getNodeLength() ||
+ int macroOffset = macroLocation.getNodeOffset();
+ if (macroOffset >= currentPosition &&
+ !scribe.shouldSkip(macroOffset) &&
+ (nodeEndOffset == macroOffset + macroLocation.getNodeLength() ||
locations.length == 2 && isSemicolonLocation(locations[1])) &&
isFunctionStyleMacroExpansion(macroExpansion)) {
if (locations.length == 2 && isSemicolonLocation(locations[1])) {
@@ -3844,7 +3796,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
continueNode(node.getParent());
return false;
}
- } else {
+ } else if (nodeLocation != null) {
scribe.restartAtOffset(nodeLocation.getNodeOffset());
}
return true;
@@ -3995,13 +3947,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (stmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(stmt)) {
formatLeftCurlyBrace(line, brace_position);
if (enterNode(stmt)) {
- try {
- formatBlock((IASTCompoundStatement) stmt, brace_position,
- preferences.insert_space_before_opening_brace_in_block,
- preferences.indent_statements_compare_to_block);
- } finally {
- exitNode(stmt);
- }
+ formatBlock((IASTCompoundStatement) stmt, brace_position,
+ preferences.insert_space_before_opening_brace_in_block,
+ preferences.indent_statements_compare_to_block);
+ exitNode(stmt);
}
} else if (stmt instanceof IASTNullStatement) {
scribe.indent();