mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-10 09:45:39 +02:00
Bug 545756 - Fix initialization of constant expressions
- propagate associatedNumericalValue in sizeof() - set associatedNumericalValue from LiteralExpression Change-Id: Ibf1fdb70e7bbbb889113f337a0b12532c5040300 Signed-off-by: Hannes Vogt <hannes@havogt.de>
This commit is contained in:
parent
92272c6465
commit
3a95ee0655
4 changed files with 41 additions and 2 deletions
|
@ -13009,4 +13009,31 @@ public class AST2CPPTests extends AST2CPPTestBase {
|
|||
public void testBraceElisionForAggregateInit6_typedef_543038() throws Exception {
|
||||
parseAndCheckBindings();
|
||||
}
|
||||
|
||||
// struct type{
|
||||
// int a;
|
||||
// };
|
||||
// type b{sizeof(type)};
|
||||
public void testAggregateInitNoNarrowingConversionInConstContext_545756() throws Exception {
|
||||
parseAndCheckImplicitNameBindings();
|
||||
}
|
||||
|
||||
// struct type{
|
||||
// int a;
|
||||
// };
|
||||
// const unsigned long v = 1;
|
||||
// type b{v};
|
||||
public void testAggregateInitNoNarrowingConversionInConstContext2_545756() throws Exception {
|
||||
parseAndCheckImplicitNameBindings();
|
||||
}
|
||||
|
||||
// struct type{
|
||||
// int a;
|
||||
// };
|
||||
// unsigned long v = 1;
|
||||
// type b{v};
|
||||
public void testAggregateInitNarrowingConversion_545756() throws Exception {
|
||||
BindingAssertionHelper bh = getAssertionHelper();
|
||||
bh.assertImplicitName("b{v};", 1, IProblemBinding.class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ class AggregateInitialization {
|
|||
if (!cost.converts()) {
|
||||
return cost;
|
||||
}
|
||||
// If the initializer-clause is an expression and a narrowing conversion is
|
||||
// [dcl.init.aggr] If the initializer-clause is an expression and a narrowing conversion is
|
||||
// required to convert the expression, the program is ill-formed.
|
||||
if (!(initializer instanceof EvalInitList) && cost.isNarrowingConversion()) {
|
||||
return Cost.NO_CONVERSION;
|
||||
|
|
|
@ -67,6 +67,7 @@ import org.eclipse.cdt.core.dom.ast.IASTInitializer;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTInitializerList;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
|
@ -2148,6 +2149,14 @@ public class CPPVisitor extends ASTQueries {
|
|||
IASTNode initClause = declarator.getInitializer();
|
||||
if (initClause instanceof IASTEqualsInitializer) {
|
||||
initClause = ((IASTEqualsInitializer) initClause).getInitializerClause();
|
||||
if (initClause instanceof IASTLiteralExpression && SemanticUtil.isConst(type)) {
|
||||
IType t = SemanticUtil.getNestedType(type, TDEF | ALLCVQ);
|
||||
if (t instanceof CPPBasicType) {
|
||||
IValue v = SemanticUtil.getValueOfInitializer(declarator.getInitializer(), t);
|
||||
if (v.numberValue() != null)
|
||||
((CPPBasicType) t).setAssociatedNumericalValue(v.numberValue().longValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (initClause instanceof IASTInitializerList) {
|
||||
IType t = SemanticUtil.getNestedType(type, TDEF);
|
||||
|
|
|
@ -160,7 +160,10 @@ public class EvalUnaryTypeID extends CPPDependentEvaluation {
|
|||
case op_sizeof:
|
||||
case op_sizeofParameterPack:
|
||||
case op_alignof:
|
||||
return CPPVisitor.get_SIZE_T();
|
||||
CPPBasicType t = (CPPBasicType) CPPVisitor.get_SIZE_T().clone();
|
||||
if (getValue().numberValue() != null)
|
||||
t.setAssociatedNumericalValue(getValue().numberValue().longValue());
|
||||
return t;
|
||||
case op_typeid:
|
||||
return CPPVisitor.get_type_info();
|
||||
case op_has_nothrow_copy:
|
||||
|
|
Loading…
Add table
Reference in a new issue