mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-01 21:35:40 +02:00
Type of subscript expression, bug 291595.
This commit is contained in:
parent
bbbe311296
commit
081526f656
4 changed files with 75 additions and 33 deletions
|
@ -6408,6 +6408,52 @@ public class AST2Tests extends AST2BaseTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// /*
|
||||||
|
// * Check that the type returned by CASTArraySubscriptExpression
|
||||||
|
// * handles typedefs correctly.
|
||||||
|
// *
|
||||||
|
// */
|
||||||
|
// struct s {
|
||||||
|
// int a;
|
||||||
|
// };
|
||||||
|
// typedef struct s* ptr;
|
||||||
|
// typedef struct s array[10];
|
||||||
|
// typedef array newArray;
|
||||||
|
// ptr var1;
|
||||||
|
// struct s* var2;
|
||||||
|
// array var3;
|
||||||
|
// struct s var4[10];
|
||||||
|
// newArray var5;
|
||||||
|
//
|
||||||
|
// void foo() {
|
||||||
|
// /* The type of the arraysubscript expression should be struct s
|
||||||
|
// * each of the following statements
|
||||||
|
// */
|
||||||
|
// var1[1].a = 1;
|
||||||
|
// var2[1].a = 1;
|
||||||
|
// var3[1].a = 1;
|
||||||
|
// var4[1].a = 1;
|
||||||
|
// var5[1].a = 1;
|
||||||
|
// }
|
||||||
|
public void testArraySubscriptExpressionGetExpressionType() throws Exception {
|
||||||
|
for(ParserLanguage lang : ParserLanguage.values()) {
|
||||||
|
IASTTranslationUnit tu = parseAndCheckBindings(getAboveComment(), lang);
|
||||||
|
assertTrue(tu.isFrozen());
|
||||||
|
for (IASTDeclaration d : tu.getDeclarations()) {
|
||||||
|
if (d instanceof IASTFunctionDefinition) {
|
||||||
|
for (IASTStatement s : ((IASTCompoundStatement)((IASTFunctionDefinition)d).getBody()).getStatements()) {
|
||||||
|
IASTExpression op1 = ((IASTBinaryExpression)((IASTExpressionStatement)s).getExpression()).getOperand1();
|
||||||
|
IASTExpression owner = ((IASTFieldReference)op1).getFieldOwner();
|
||||||
|
IType t = owner.getExpressionType();
|
||||||
|
assertTrue( t instanceof ICompositeType );
|
||||||
|
assertEquals( "s",((ICompositeType)t).getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// extern int a[];
|
// extern int a[];
|
||||||
// int a[1];
|
// int a[1];
|
||||||
public void testIncompleteArrays_269926() throws Exception {
|
public void testIncompleteArrays_269926() throws Exception {
|
||||||
|
|
|
@ -113,6 +113,7 @@ public class CASTArraySubscriptExpression extends ASTNode implements
|
||||||
|
|
||||||
public IType getExpressionType() {
|
public IType getExpressionType() {
|
||||||
IType t = getArrayExpression().getExpressionType();
|
IType t = getArrayExpression().getExpressionType();
|
||||||
|
t = CVisitor.unwrapTypedefs(t);
|
||||||
if (t instanceof IPointerType)
|
if (t instanceof IPointerType)
|
||||||
return ((IPointerType)t).getType();
|
return ((IPointerType)t).getType();
|
||||||
else if (t instanceof IArrayType)
|
else if (t instanceof IArrayType)
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
package org.eclipse.cdt.internal.core.dom.parser.c;
|
package org.eclipse.cdt.internal.core.dom.parser.c;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
|
@ -129,39 +128,35 @@ public class CASTBinaryExpression extends ASTNode implements
|
||||||
|
|
||||||
public IType getExpressionType() {
|
public IType getExpressionType() {
|
||||||
int op = getOperator();
|
int op = getOperator();
|
||||||
try {
|
switch(op) {
|
||||||
switch(op) {
|
case op_lessEqual:
|
||||||
case op_lessEqual:
|
case op_lessThan:
|
||||||
case op_lessThan:
|
case op_greaterEqual:
|
||||||
case op_greaterEqual:
|
case op_greaterThan:
|
||||||
case op_greaterThan:
|
case op_logicalAnd:
|
||||||
case op_logicalAnd:
|
case op_logicalOr:
|
||||||
case op_logicalOr:
|
case op_equals:
|
||||||
case op_equals:
|
case op_notequals:
|
||||||
case op_notequals:
|
CBasicType basicType = new CBasicType(IBasicType.t_int, 0);
|
||||||
CBasicType basicType = new CBasicType(IBasicType.t_int, 0);
|
basicType.setValue(this);
|
||||||
basicType.setValue(this);
|
return basicType;
|
||||||
return basicType;
|
case IASTBinaryExpression.op_plus:
|
||||||
case IASTBinaryExpression.op_plus:
|
IType t2 = getOperand2().getExpressionType();
|
||||||
IType t2 = getOperand2().getExpressionType();
|
if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) {
|
||||||
if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) {
|
return t2;
|
||||||
return t2;
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
|
|
||||||
case IASTBinaryExpression.op_minus:
|
case IASTBinaryExpression.op_minus:
|
||||||
t2= getOperand2().getExpressionType();
|
t2= getOperand2().getExpressionType();
|
||||||
if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) {
|
if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) {
|
||||||
IType t1 = getOperand1().getExpressionType();
|
IType t1 = getOperand1().getExpressionType();
|
||||||
if (CVisitor.unwrapTypedefs(t1) instanceof IPointerType) {
|
if (CVisitor.unwrapTypedefs(t1) instanceof IPointerType) {
|
||||||
return CVisitor.getPtrDiffType(this);
|
return CVisitor.getPtrDiffType(this);
|
||||||
}
|
|
||||||
return t1;
|
|
||||||
}
|
}
|
||||||
break;
|
return t1;
|
||||||
}
|
}
|
||||||
} catch (DOMException e) {
|
break;
|
||||||
return e.getProblem();
|
|
||||||
}
|
}
|
||||||
return getOperand1().getExpressionType();
|
return getOperand1().getExpressionType();
|
||||||
}
|
}
|
||||||
|
|
|
@ -668,7 +668,7 @@ public class CVisitor extends ASTQueries {
|
||||||
return new CBasicType(IBasicType.t_int, CBasicType.IS_LONG | CBasicType.IS_UNSIGNED);
|
return new CBasicType(IBasicType.t_int, CBasicType.IS_LONG | CBasicType.IS_UNSIGNED);
|
||||||
}
|
}
|
||||||
|
|
||||||
static IType unwrapTypedefs(IType type) throws DOMException {
|
static IType unwrapTypedefs(IType type) {
|
||||||
while (type instanceof ITypedef) {
|
while (type instanceof ITypedef) {
|
||||||
type= ((ITypedef) type).getType();
|
type= ((ITypedef) type).getType();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue