1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-04 23:55:26 +02:00

Bug 362464: Sizeof computation for plain C.

This commit is contained in:
Markus Schorn 2011-11-04 07:58:02 +01:00
parent 92955735be
commit 5e359e4722
7 changed files with 46 additions and 8 deletions

View file

@ -100,12 +100,19 @@ public class AST2BaseTest extends BaseTestCase {
protected static boolean sValidateCopy; protected static boolean sValidateCopy;
private static final ScannerInfo GNU_SCANNER_INFO = new ScannerInfo(getGnuMap()); private static final ScannerInfo GNU_SCANNER_INFO = new ScannerInfo(getGnuMap());
private static final ScannerInfo SCANNER_INFO = new ScannerInfo(); private static final ScannerInfo SCANNER_INFO = new ScannerInfo(getStdMap());
private static Map<String, String> getGnuMap() { private static Map<String, String> getGnuMap() {
Map<String, String> map= new HashMap<String, String>(); Map<String, String> map= new HashMap<String, String>();
map.put("__GNUC__", "4"); map.put("__GNUC__", "4");
map.put("__GNUC_MINOR__", "5"); map.put("__GNUC_MINOR__", "5");
map.put("__SIZEOF_INT__", "4");
return map;
}
private static Map<String, String> getStdMap() {
Map<String, String> map= new HashMap<String, String>();
map.put("__SIZEOF_INT__", "4");
return map; return map;
} }

View file

@ -7342,4 +7342,17 @@ public class AST2Tests extends AST2BaseTest {
es= getStatement(a, 2); es= getStatement(a, 2);
assertEquals("unsigned long int", ASTTypeUtil.getType(es.getExpression().getExpressionType())); assertEquals("unsigned long int", ASTTypeUtil.getType(es.getExpression().getExpressionType()));
} }
// typedef int T[sizeof(int)];
public void testSizeofExpression_Bug362464() throws Exception {
String code= getAboveComment();
for (ParserLanguage l : ParserLanguage.values()) {
IASTTranslationUnit tu= parseAndCheckBindings(code, l);
IASTSimpleDeclaration sdecl= getDeclaration(tu, 0);
ITypedef tdef= (ITypedef) sdecl.getDeclarators()[0].getName().resolveBinding();
IArrayType at= (IArrayType) tdef.getType();
IValue v= at.getSize();
assertTrue(v.numericalValue() == 4);
}
}
} }

View file

@ -31,9 +31,11 @@ import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding; import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.INodeFactory; import org.eclipse.cdt.core.dom.ast.INodeFactory;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileSet; import org.eclipse.cdt.core.index.IIndexFileSet;
@ -293,7 +295,7 @@ public abstract class ASTTranslationUnit extends ASTNode implements IASTTranslat
return fLocationResolver.flattenLocations(nodeLocations); return fLocationResolver.flattenLocations(nodeLocations);
} }
public final IDependencyTree getDependencyTree() { public final IDependencyTree getDependencyTree() {
if (fLocationResolver == null) if (fLocationResolver == null)
return null; return null;
return fLocationResolver.getDependencyTree(); return fLocationResolver.getDependencyTree();
@ -414,7 +416,12 @@ public abstract class ASTTranslationUnit extends ASTNode implements IASTTranslat
* Must be called by the parser, before the ast is passed to the clients. * Must be called by the parser, before the ast is passed to the clients.
*/ */
public abstract void resolveAmbiguities(); public abstract void resolveAmbiguities();
/**
* Can be called to create a type for a type-id.
*/
abstract protected IType createType(IASTTypeId typeid);
protected void copyAbstractTU(ASTTranslationUnit copy, CopyStyle style) { protected void copyAbstractTU(ASTTranslationUnit copy, CopyStyle style) {
copy.setIndex(fIndex); copy.setIndex(fIndex);
copy.fIsHeader = fIsHeader; copy.fIsHeader = fIsHeader;

View file

@ -26,7 +26,6 @@ import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue; import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
@ -111,7 +110,7 @@ public class SizeofCalculator {
public SizeAndAlignment sizeAndAlignment(IType type) { public SizeAndAlignment sizeAndAlignment(IType type) {
type = SemanticUtil.getNestedType(type, SemanticUtil.CVTYPE | SemanticUtil.TDEF); type = SemanticUtil.getNestedType(type, SemanticUtil.CVTYPE | SemanticUtil.TDEF);
if (type instanceof ICPPBasicType) { if (type instanceof IBasicType) {
return sizeAndAlignment((IBasicType) type); return sizeAndAlignment((IBasicType) type);
} }
if (type instanceof IPointerType || type instanceof ICPPReferenceType) { if (type instanceof IPointerType || type instanceof ICPPReferenceType) {

View file

@ -37,7 +37,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator.SizeAndAlignment; import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator.SizeAndAlignment;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.parser.scanner.ExpressionEvaluator; import org.eclipse.cdt.internal.core.parser.scanner.ExpressionEvaluator;
import org.eclipse.cdt.internal.core.parser.scanner.ExpressionEvaluator.EvalException; import org.eclipse.cdt.internal.core.parser.scanner.ExpressionEvaluator.EvalException;
import org.eclipse.cdt.internal.core.pdom.db.TypeMarshalBuffer; import org.eclipse.cdt.internal.core.pdom.db.TypeMarshalBuffer;
@ -468,8 +467,9 @@ public class Value implements IValue {
IASTTypeIdExpression typeIdEx = (IASTTypeIdExpression) e; IASTTypeIdExpression typeIdEx = (IASTTypeIdExpression) e;
switch (typeIdEx.getOperator()) { switch (typeIdEx.getOperator()) {
case IASTTypeIdExpression.op_sizeof: case IASTTypeIdExpression.op_sizeof:
IType type = CPPVisitor.createType(typeIdEx.getTypeId()); final IType type;
ASTTranslationUnit ast = (ASTTranslationUnit) typeIdEx.getTranslationUnit(); ASTTranslationUnit ast = (ASTTranslationUnit) typeIdEx.getTranslationUnit();
type = ast.createType(typeIdEx.getTypeId());
SizeofCalculator calculator = ast.getSizeofCalculator(); SizeofCalculator calculator = ast.getSizeofCalculator();
SizeAndAlignment info = calculator.sizeAndAlignment(type); SizeAndAlignment info = calculator.sizeAndAlignment(type);
if (info == null) if (info == null)

View file

@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.EScopeKind; import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IMacroBinding; import org.eclipse.cdt.core.dom.ast.IMacroBinding;
@ -114,4 +115,9 @@ public class CASTTranslationUnit extends ASTTranslationUnit implements IASTAmbig
public IType mapToASTType(ICompositeType type) { public IType mapToASTType(ICompositeType type) {
return fStructMapper.mapToAST(type); return fStructMapper.mapToAST(type);
} }
@Override
protected IType createType(IASTTypeId typeid) {
return CVisitor.createType(typeid.getAbstractDeclarator());
}
} }

View file

@ -13,6 +13,7 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IBasicType; import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind; import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
@ -58,7 +59,7 @@ public class CPPASTTranslationUnit extends ASTTranslationUnit implements ICPPAST
return copy; return copy;
} }
public CPPNamespaceScope getScope() { public CPPNamespaceScope getScope() {
if (fScope == null) { if (fScope == null) {
fScope = new CPPNamespaceScope(this); fScope = new CPPNamespaceScope(this);
addBuiltinOperators(fScope); addBuiltinOperators(fScope);
@ -180,4 +181,9 @@ public class CPPASTTranslationUnit extends ASTTranslationUnit implements ICPPAST
public void resolveAmbiguities() { public void resolveAmbiguities() {
accept(new CPPASTAmbiguityResolver()); accept(new CPPASTAmbiguityResolver());
} }
@Override
protected IType createType(IASTTypeId typeid) {
return CPPVisitor.createType(typeid);
}
} }