]> granicus.if.org Git - clang/commitdiff
PR19415: Converting 'constexpr' to 'const' in a non-static data member can fail
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 14 Apr 2014 21:00:40 +0000 (21:00 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 14 Apr 2014 21:00:40 +0000 (21:00 +0000)
if the member is already 'const'. Don't assert in that case.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206205 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclCXX.cpp
test/FixIt/fixit-cxx0x.cpp

index fbc3fd7eb8169c0297a3b657b32adbfaa0c1b911..65705f5a559d2213b98e50e9cd06e91e7d650c6f 100644 (file)
@@ -1960,14 +1960,19 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
         Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member);
     SourceLocation ConstexprLoc = DS.getConstexprSpecLoc();
     if (InitStyle == ICIS_NoInit) {
-      B << 0 << 0 << FixItHint::CreateReplacement(ConstexprLoc, "const");
-      D.getMutableDeclSpec().ClearConstexprSpec();
-      const char *PrevSpec;
-      unsigned DiagID;
-      bool Failed = D.getMutableDeclSpec().SetTypeQual(DeclSpec::TQ_const, ConstexprLoc,
-                                         PrevSpec, DiagID, getLangOpts());
-      (void)Failed;
-      assert(!Failed && "Making a constexpr member const shouldn't fail");
+      B << 0 << 0;
+      if (D.getDeclSpec().getTypeQualifiers() & DeclSpec::TQ_const)
+        B << FixItHint::CreateRemoval(ConstexprLoc);
+      else {
+        B << FixItHint::CreateReplacement(ConstexprLoc, "const");
+        D.getMutableDeclSpec().ClearConstexprSpec();
+        const char *PrevSpec;
+        unsigned DiagID;
+        bool Failed = D.getMutableDeclSpec().SetTypeQual(
+            DeclSpec::TQ_const, ConstexprLoc, PrevSpec, DiagID, getLangOpts());
+        (void)Failed;
+        assert(!Failed && "Making a constexpr member const shouldn't fail");
+      }
     } else {
       B << 1;
       const char *PrevSpec;
index bfd8c3dcfea6fa40e4639c0674bd6c9b13112a06..5fe7ca4ea9cb6f8d8b4db171aa4b797f5f62fa9f 100644 (file)
@@ -125,7 +125,8 @@ namespace NonStaticConstexpr {
   struct foo {
     constexpr int i; // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}}
     constexpr int j = 7; // expected-error {{non-static data member cannot be constexpr; did you intend to make it static?}}
-    foo() : i(3) {
+    constexpr const int k; // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}}
+    foo() : i(3), k(4) {
     }
     static int get_j() {
       return j;