[PATCH] [MSVC2012] Allow 'mutable' references (fix for http://llvm.org/PR22444)

Alexey Bataev a.bataev at hotmail.com
Tue Feb 3 20:47:32 PST 2015


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D7370

Files:
  cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
  cfe/trunk/lib/Sema/SemaDecl.cpp
  cfe/trunk/test/SemaCXX/ms_mutable_reference_member.cpp

Index: cfe/trunk/lib/Sema/SemaDecl.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp
+++ cfe/trunk/lib/Sema/SemaDecl.cpp
@@ -12348,17 +12348,20 @@
   if (!InvalidDecl && Mutable) {
     unsigned DiagID = 0;
     if (T->isReferenceType())
-      DiagID = diag::err_mutable_reference;
+      DiagID = getLangOpts().MSVCCompat ? diag::ext_mutable_reference
+                                        : diag::err_mutable_reference;
     else if (T.isConstQualified())
       DiagID = diag::err_mutable_const;
 
     if (DiagID) {
       SourceLocation ErrLoc = Loc;
       if (D && D->getDeclSpec().getStorageClassSpecLoc().isValid())
         ErrLoc = D->getDeclSpec().getStorageClassSpecLoc();
       Diag(ErrLoc, DiagID);
-      Mutable = false;
-      InvalidDecl = true;
+      if (DiagID != diag::ext_mutable_reference) {
+        Mutable = false;
+        InvalidDecl = true;
+      }
     }
   }
 
Index: cfe/trunk/test/SemaCXX/ms_mutable_reference_member.cpp
===================================================================
--- cfe/trunk/test/SemaCXX/ms_mutable_reference_member.cpp
+++ cfe/trunk/test/SemaCXX/ms_mutable_reference_member.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -fms-compatibility
+
+struct S {
+  mutable int &a; // expected-warning {{'mutable' on a reference type is a Microsoft extension}}
+  S(int &b) : a(b) {}
+};
+
+int main() {
+  int a = 0;
+  const S s(a);
+  s.a = 10;
+  return s.a + a;
+}
Index: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1241,6 +1241,9 @@
   "storage class specified for a member declaration">;
 def err_mutable_function : Error<"'mutable' cannot be applied to functions">;
 def err_mutable_reference : Error<"'mutable' cannot be applied to references">;
+def ext_mutable_reference : ExtWarn<
+  "'mutable' on a reference type is a Microsoft extension">,
+  InGroup<Microsoft>;
 def err_mutable_const : Error<"'mutable' and 'const' cannot be mixed">;
 def err_mutable_nonmember : Error<
   "'mutable' can only be applied to member variables">;

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7370.19300.patch
Type: text/x-patch
Size: 2277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150204/1989b7d2/attachment.bin>


More information about the cfe-commits mailing list