1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Correct types for numeric literals, bug 225534.

This commit is contained in:
Markus Schorn 2008-05-21 16:24:30 +00:00
parent 8cc0de06be
commit 0982e99b7c
6 changed files with 90 additions and 27 deletions

View file

@ -5527,13 +5527,13 @@ public class AST2CPPTests extends AST2BaseTest {
}
// long x= 10L;
public void _testLongLiteral_225534() throws Exception {
public void testLongLiteral_225534() throws Exception {
IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.CPP);
IASTDeclarator decltor= ((IASTSimpleDeclaration)tu.getDeclarations()[0]).getDeclarators()[0];
IASTInitializerExpression init= (IASTInitializerExpression) decltor.getInitializer();
ICPPASTLiteralExpression exp= (ICPPASTLiteralExpression) init.getExpression();
ICPPBasicType type= (ICPPBasicType) CPPVisitor.getExpressionType(exp);
assertEquals(ICPPBasicType.IS_LONG, type.getType());
assertTrue(type.isLong());
}
// void foo/*_a*/(int x) {}
@ -5567,13 +5567,13 @@ public class AST2CPPTests extends AST2BaseTest {
// foo/*f2*/(010UL);
// foo/*f3*/(0x010UL);
//
// foo/*g1*/(100000000000000000L);
// foo/*g2*/(0100000000000L);
// foo/*g3*/(0x01000000000L);
// foo/*g1*/(100000000000000000LL);
// foo/*g2*/(0100000000000LL);
// foo/*g3*/(0x01000000000LL);
//
// foo/*h1*/(100000000000000000UL);
// foo/*h2*/(0100000000000UL);
// foo/*h3*/(0x01000000000UL);
// foo/*h1*/(100000000000000000ULL);
// foo/*h2*/(0100000000000ULL);
// foo/*h3*/(0x01000000000ULL);
//
// foo/*i1*/(11.1F);
// foo/*i2*/(11E1F);
@ -5584,7 +5584,7 @@ public class AST2CPPTests extends AST2BaseTest {
// foo/*k1*/(11.1L);
// foo/*k2*/(11.1E1L);
// }
public void _testLiteralsViaOverloads_225534() throws Exception {
public void testLiteralsViaOverloads_225534() throws Exception {
BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), true);
char[] cs= {'a','b','e','f','g','h','i','j','k'};
for(char c : cs) {

View file

@ -1104,7 +1104,7 @@ public class IndexBugsTests extends BaseTestCase {
IIndexBinding[] bindings = fIndex.findBindings("func_209049".toCharArray(),
IndexFilter.ALL, NPM);
IFunctionType ft = ((IFunction) bindings[0]).getType();
assertEquals("void (long long)", ASTTypeUtil.getType(ft));
assertEquals("void (long long int)", ASTTypeUtil.getType(ft));
} finally {
fIndex.releaseReadLock();
}

View file

@ -7,6 +7,7 @@
*
* Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.model;
@ -61,6 +62,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypenameExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.parser.Keywords;
@ -371,15 +373,15 @@ public class ASTStringUtil {
private static StringBuilder appendArrayQualifiersString(StringBuilder buffer, IASTArrayDeclarator declarator) {
final IASTArrayModifier[] modifiers= declarator.getArrayModifiers();
for (int i = 0; i < modifiers.length; i++) {
final int count= modifiers.length;
for (int i = 0; i < count; i++) {
buffer.append(Keywords.cpLBRACKET).append(Keywords.cpRBRACKET);
}
return buffer;
}
private static StringBuilder appendPointerOperatorsString(StringBuilder buffer, IASTPointerOperator[] pointerOperators) {
for (int i= 0; i < pointerOperators.length; i++) {
final IASTPointerOperator pointerOperator= pointerOperators[i];
for (final IASTPointerOperator pointerOperator : pointerOperators) {
if (pointerOperator instanceof IASTPointer) {
final IASTPointer pointer= (IASTPointer)pointerOperator;
if (pointer instanceof ICPPASTPointerToMember) {
@ -587,6 +589,11 @@ public class ASTStringUtil {
buffer.append(Keywords._BOOL).append(' ');
break;
}
} else if (simpleDeclSpec instanceof IGPPASTSimpleDeclSpecifier) {
final IGPPASTSimpleDeclSpecifier gppSimpleDeclSpec= (IGPPASTSimpleDeclSpecifier)simpleDeclSpec;
if (gppSimpleDeclSpec.isLongLong()) {
buffer.append(Keywords.LONG_LONG).append(' ');
}
}
switch (simpleDeclSpec.getType()) {
case IASTSimpleDeclSpecifier.t_void:

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2007 IBM Corporation and others.
* Copyright (c) 2005, 2008 IBM Corporation 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
@ -270,7 +270,7 @@ public class GCCBuiltinSymbolProvider implements IBuiltinBindingsProvider {
cpp_long_int = new CPPBasicType(IBasicType.t_int, ICPPBasicType.IS_LONG);
cpp_long_double = new CPPBasicType(IBasicType.t_double, ICPPBasicType.IS_LONG);
cpp_long_double_complex = new GPPBasicType(IBasicType.t_double, ICPPBasicType.IS_LONG | ICPPBasicType.IS_COMPLEX, null);
cpp_long_long_int = new CPPBasicType(IBasicType.t_int, ICPPBasicType.IS_LONG_LONG);
cpp_long_long_int = new GPPBasicType(IBasicType.t_int, ICPPBasicType.IS_LONG_LONG, null);
cpp_signed_long_int = new CPPBasicType(IBasicType.t_int, ICPPBasicType.IS_LONG | ICPPBasicType.IS_SIGNED);
cpp_unsigned_int = new CPPBasicType(IBasicType.t_int, ICPPBasicType.IS_UNSIGNED);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2007 IBM Corporation and others.
* Copyright (c) 2004, 2008 IBM Corporation 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
@ -9,9 +9,6 @@
* IBM Corporation - initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
/*
* Created on Dec 10, 2004
*/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.IBasicType;
@ -28,11 +25,11 @@ public class GPPBasicType extends CPPBasicType implements IGPPBasicType {
public GPPBasicType( int type, int bits, IType typeOf ){
super( type, bits );
this.typeOf = typeOf;
if( type == IBasicType.t_unspecified ){
if( this.type == IBasicType.t_unspecified ){
if((qualifierBits & ( IS_COMPLEX | IS_IMAGINARY )) != 0 )
type = IBasicType.t_float;
this.type = IBasicType.t_float;
else if( (qualifierBits & IS_LONG_LONG) != 0 )
type = IBasicType.t_int;
this.type = IBasicType.t_int;
}
}

View file

@ -1808,7 +1808,8 @@ public class CPPVisitor {
IType type = createType(id.getDeclSpecifier());
return createType(type, id.getAbstractDeclarator());
} else if (expression instanceof ICPPASTLiteralExpression) {
switch(((ICPPASTLiteralExpression) expression).getKind()) {
ICPPASTLiteralExpression lit= (ICPPASTLiteralExpression) expression;
switch(lit.getKind()) {
case ICPPASTLiteralExpression.lk_this : {
IScope scope = getContainingScope(expression);
return getThisType(scope);
@ -1819,9 +1820,9 @@ public class CPPVisitor {
case IASTLiteralExpression.lk_char_constant:
return new CPPBasicType(IBasicType.t_char, 0, expression);
case IASTLiteralExpression.lk_float_constant:
return new CPPBasicType(IBasicType.t_float, 0, expression);
return classifyTypeOfFloatLiteral(lit);
case IASTLiteralExpression.lk_integer_constant:
return new CPPBasicType(IBasicType.t_int, 0, expression);
return classifyTypeOfIntLiteral(lit);
case IASTLiteralExpression.lk_string_literal:
IType type = new CPPBasicType(IBasicType.t_char, 0, expression);
type = new CPPQualifierType(type, true, false);
@ -2093,6 +2094,64 @@ public class CPPVisitor {
return null;
}
private static IType classifyTypeOfFloatLiteral(final IASTLiteralExpression expr) {
final String lit= expr.toString();
final int len= lit.length();
int kind= IBasicType.t_double;
int flags= 0;
if (len > 0) {
switch(lit.charAt(len-1)) {
case 'f': case 'F':
kind= IBasicType.t_float;
break;
case 'l': case 'L':
flags |= ICPPBasicType.IS_LONG;
break;
}
}
return new CPPBasicType(kind, flags, expr);
}
private static IType classifyTypeOfIntLiteral(IASTLiteralExpression expression) {
int makelong= 0;
boolean unsigned= false;
final String lit= expression.toString();
for (int i=lit.length()-1; i >=0; i--) {
final char c= lit.charAt(i);
if (!(c > 'f' && c <= 'z') && !(c > 'F' && c <= 'Z')) {
break;
}
switch (lit.charAt(i)) {
case 'u':
case 'U':
unsigned = true;
break;
case 'l':
case 'L':
makelong++;
break;
}
}
int flags= 0;
if (unsigned) {
flags |= ICPPBasicType.IS_UNSIGNED;
}
if (makelong > 1) {
flags |= ICPPBasicType.IS_LONG_LONG;
GPPBasicType result = new GPPBasicType(IBasicType.t_int, flags, null);
result.setValue(expression);
return result;
}
if (makelong == 1) {
flags |= ICPPBasicType.IS_LONG;
}
return new CPPBasicType(IBasicType.t_int, flags, expression);
}
public static IASTDeclarator getMostNestedDeclarator(IASTDeclarator dtor) {
if (dtor == null) return null;
IASTDeclarator nested = null;