1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

Moved getOperandsOfMultiExpression method to CPPVisitor.

This commit is contained in:
Sergey Prigogin 2012-03-07 20:09:39 -08:00
parent 8153aff8ff
commit e799de15a0
3 changed files with 50 additions and 47 deletions

View file

@ -36,7 +36,7 @@ public class AST2UtilTests extends AST2BaseTest {
}
public void testSimpleSignature() throws Exception {
StringBuffer buff = new StringBuffer();
StringBuilder buff = new StringBuilder();
buff.append("int l, m, n=0;\n"); //$NON-NLS-1$
buff.append("int j = l ? m : n;\n"); //$NON-NLS-1$
buff.append("int i = l^m;\n"); //$NON-NLS-1$
@ -64,7 +64,7 @@ public class AST2UtilTests extends AST2BaseTest {
}
public void testSimpleParameter() throws Exception {
StringBuffer buff = new StringBuffer();
StringBuilder buff = new StringBuilder();
buff.append("int a(int x);\n"); //$NON-NLS-1$
buff.append("int * b(char y, int x);\n"); //$NON-NLS-1$
buff.append("void c(int * z, float **b);\n"); //$NON-NLS-1$
@ -112,7 +112,7 @@ public class AST2UtilTests extends AST2BaseTest {
}
public void testSimpleCParameterSignature() throws Exception {
StringBuffer buff = new StringBuffer();
StringBuilder buff = new StringBuilder();
buff.append("int a(int x);\n"); //$NON-NLS-1$
buff.append("int * b(char y, int x);\n"); //$NON-NLS-1$
buff.append("void c(int * z, float **b);\n"); //$NON-NLS-1$
@ -128,7 +128,7 @@ public class AST2UtilTests extends AST2BaseTest {
}
public void testSimpleTypeId() throws Exception {
StringBuffer buff = new StringBuffer();
StringBuilder buff = new StringBuilder();
buff.append("int x = sizeof( int );\n"); //$NON-NLS-1$
buff.append("union Squaw { int x; double u; };\n"); //$NON-NLS-1$
buff.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
@ -152,7 +152,7 @@ public class AST2UtilTests extends AST2BaseTest {
}
public void testKnRC() throws Exception {
StringBuffer buff = new StringBuffer();
StringBuilder buff = new StringBuilder();
buff.append("int foo(x, y) char x; int y; {}\n"); //$NON-NLS-1$
buff.append("int foo2(char x, int y) {}\n"); //$NON-NLS-1$

View file

@ -19,6 +19,13 @@ import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUti
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getNestedType;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getUltimateTypeUptoPointers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.cdt.core.dom.ast.ASTGenericVisitor;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
@ -192,13 +199,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.index.IIndexScope;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Collection of methods to extract information from a C++ translation unit.
*/
@ -2524,4 +2524,31 @@ public class CPPVisitor extends ASTQueries {
} while ((node = node.getParent()) != null);
return null;
}
/**
* Traverses a chain of nested homogeneous left-to-right-associative binary expressions and
* returns a list of their operands in left-to-right order. For example, for the expression
* a + b * c + d, it will return a list containing expressions: a, b * c, and d.
*
* @param binaryExpression the top-level binary expression
* @return a list of expression operands from left to right
*/
public static IASTExpression[] getOperandsOfMultiExpression(IASTBinaryExpression binaryExpression) {
int operator = binaryExpression.getOperator();
IASTExpression[] operands = new IASTExpression[2];
IASTExpression node;
int len = 0;
do {
operands = ArrayUtil.appendAt(operands, len++, binaryExpression.getOperand2());
node = binaryExpression.getOperand1();
if (!(node instanceof IASTBinaryExpression)) {
break;
}
binaryExpression = (IASTBinaryExpression) node;
} while (binaryExpression.getOperator() == operator);
operands = ArrayUtil.appendAt(operands, len++, node);
operands = ArrayUtil.trim(operands, len);
ArrayUtil.reverse(operands);
return operands;
}
}

View file

@ -42,6 +42,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
@ -90,20 +91,20 @@ import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.c.ICASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression;
@ -138,11 +139,11 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.formatter.align.Alignment;
import org.eclipse.cdt.internal.formatter.align.AlignmentException;
import org.eclipse.cdt.internal.formatter.scanner.Scanner;
@ -903,8 +904,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
visit((IASTUnaryExpression) node);
} else if (node instanceof IASTFieldReference) {
visit((IASTFieldReference) node);
} else if (node instanceof ICASTTypeIdInitializerExpression) {
visit((ICASTTypeIdInitializerExpression) node);
} else if (node instanceof IASTTypeIdInitializerExpression) {
visit((IASTTypeIdInitializerExpression) node);
} else if (node instanceof ICPPASTNewExpression) {
visit((ICPPASTNewExpression) node);
} else if (node instanceof ICPPASTDeleteExpression) {
@ -2644,7 +2645,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
// To improve speed of the algorithm we flatten homogeneous nested binary expressions
// to reduce overall depth of the expression tree.
List<IASTExpression> operands = getOperandsOfMultiExpression(node);
IASTExpression[] operands = CPPVisitor.getOperandsOfMultiExpression(node);
Runnable tailFormatter = endsWithMacroExpansion(node) ? null : scribe.takeTailFormatter();
@ -2652,15 +2653,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
Alignment.BINARY_EXPRESSION,
preferences.alignment_for_binary_expression,
Alignment.R_OUTERMOST,
operands.size(),
operands.length,
scribe.scanner.getCurrentPosition());
scribe.enterAlignment(alignment);
boolean ok = false;
do {
try {
for (int i = 0; i < operands.size(); i++) {
final IASTExpression operand = operands.get(i);
for (int i = 0; i < operands.length; i++) {
final IASTExpression operand = operands[i];
// In case of macros we may have already passed the operator position.
if (i > 0 && scribe.scanner.getCurrentPosition() < operand.getFileLocation().getNodeOffset()) {
scribe.alignFragment(alignment, i);
@ -2702,31 +2703,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return PROCESS_SKIP;
}
/**
* Traverses a chain of nested homogeneous left-to-right-associative binary expressions and
* returns a list of their operands in left-to-right order. For example, for the expression
* a + b * c + d, it will return a list containing expressions: a, b * c, and d.
*
* @param binaryExpression the top-level binary expression
* @return a list of expression operands from left to right
*/
private List<IASTExpression> getOperandsOfMultiExpression(IASTBinaryExpression binaryExpression) {
int operator = binaryExpression.getOperator();
List<IASTExpression> operands = new ArrayList<IASTExpression>(2);
IASTExpression node;
do {
operands.add(binaryExpression.getOperand2());
node = binaryExpression.getOperand1();
if (!(node instanceof IASTBinaryExpression)) {
break;
}
binaryExpression = (IASTBinaryExpression) node;
} while (binaryExpression.getOperator() == operator);
operands.add(node);
Collections.reverse(operands);
return operands;
}
private int formatAssignment(IASTBinaryExpression node) {
Runnable tailFormatter = scribe.takeTailFormatter();
final IASTExpression op1= node.getOperand1();
@ -2968,7 +2944,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return PROCESS_SKIP;
}
private int visit(ICASTTypeIdInitializerExpression node) {
private int visit(IASTTypeIdInitializerExpression node) {
scribe.printComment();
final int line= scribe.line;