From 586ccf790bf9f12aebbce7057115857455fe42f7 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Wed, 28 Nov 2018 22:41:32 -0500 Subject: Bug 541670 - constexpr implies toplevel const Change-Id: I318a9293d12eeecb7f49bfba1acb849109fab666 --- .../org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java | 5 +++++ .../eclipse/cdt/core/parser/tests/ast2/SemanticTestBase.java | 10 +++++++++- .../cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java | 4 +++- 3 files changed, 17 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 3ec4cf786d3..d1ee11ec350 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 @@ -11627,6 +11627,11 @@ public class AST2CPPTests extends AST2CPPTestBase { assertSameType(waldo2.getType(), CommonCPPTypes.constInt); } + // constexpr int* waldo; + public void testConstexprPointerVariable_541670() throws Exception { + getAssertionHelper().assertVariableType("waldo", CommonCPPTypes.constPointerToInt); + } + // constexpr int waldo1(); // constexpr int (*waldo2())(int); // struct S { constexpr int waldo3(); }; diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/SemanticTestBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/SemanticTestBase.java index 3cd2404c478..aff2182fed0 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/SemanticTestBase.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/SemanticTestBase.java @@ -83,6 +83,7 @@ public class SemanticTestBase extends BaseTestCase { public static IType constChar = constOf(char_); public static IType constInt = constOf(int_); public static IType pointerToInt = pointerTo(int_); + public static IType constPointerToInt = constPointerTo(int_); public static IType pointerToConstChar = pointerTo(constChar); public static IType pointerToConstInt = pointerTo(constInt); public static IType referenceToInt = referenceTo(int_); @@ -90,11 +91,18 @@ public class SemanticTestBase extends BaseTestCase { public static IType rvalueReferenceToInt = rvalueReferenceTo(int_); public static IType rvalueReferenceToConstInt = rvalueReferenceTo(constInt); + // Not quite the same as constOf(pointerTo(type)) because of the + // idiosyncratic way we represent cosnt pointers using a flag + // on the CPPPointerType rather than using CPPQualifierType. + private static IType constPointerTo(IType type) { + return new CPPPointerType(type, true, false, false); + } + private static IType pointerTo(IType type) { return new CPPPointerType(type); } - private static IType constOf(IType type) { + public static IType constOf(IType type) { return new CPPQualifierType(type, true, false); } 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 d39a95274a1..7e3f47306c7 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 @@ -2140,9 +2140,11 @@ public class CPPVisitor extends ASTQueries { } IType type = createType(declSpec); - type = makeConstIfConstexpr(type, declSpec, declarator); type = createType(type, declarator, flags); + // constexpr implies toplevel-const + type = makeConstIfConstexpr(type, declSpec, declarator); + // C++ specification 8.3.4.3 and 8.5.1.4 IASTNode initClause = declarator.getInitializer(); if (initClause instanceof IASTEqualsInitializer) { -- cgit v1.2.3