Skip to main content
summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorHannes Vogt2019-03-28 03:07:51 -0400
committerNathan Ridge2019-03-30 17:10:48 -0400
commit3a95ee0655073c10bb719c09fb069ce56094ef12 (patch)
tree3f0ed9b93284c55560d7c063e8c38d4829086e2f /core
parent92272c64659b19bbc11c27cf1a1857b4f1f68e0e (diff)
downloadorg.eclipse.cdt-3a95ee0655073c10bb719c09fb069ce56094ef12.tar.gz
org.eclipse.cdt-3a95ee0655073c10bb719c09fb069ce56094ef12.tar.xz
org.eclipse.cdt-3a95ee0655073c10bb719c09fb069ce56094ef12.zip
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>
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java27
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java2
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java9
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalUnaryTypeID.java5
4 files changed, 41 insertions, 2 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
index 43a8fd83eb..37d4cb0e64 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
@@ -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);
+ }
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java
index e3afebd2e6..d18585b19d 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/AggregateInitialization.java
@@ -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;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java
index 4a735d9447..fd240c4c9d 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java
@@ -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);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalUnaryTypeID.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalUnaryTypeID.java
index bf8bb5ca29..e08dce34c8 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalUnaryTypeID.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalUnaryTypeID.java
@@ -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:

Back to the top