]> granicus.if.org Git - clang/commitdiff
In Microsoft mode, allow template function explicit specialization at class scope.
authorFrancois Pichet <pichet2000@gmail.com>
Sat, 14 May 2011 17:46:46 +0000 (17:46 +0000)
committerFrancois Pichet <pichet2000@gmail.com>
Sat, 14 May 2011 17:46:46 +0000 (17:46 +0000)
Necessary to parse MFC and MSVC standard lib code.

Example:
struct X {
  template<class T> void f(T) { }
  template<> void f(int) { }
}

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
lib/Sema/SemaTemplate.cpp
test/SemaCXX/MicrosoftExtensions.cpp

index ea1ef56010a26d2a287d931391472c84dfd21856..ea82cf80edb218d4b55ba4fca6f8f11033e68497 100644 (file)
@@ -1775,6 +1775,9 @@ def err_template_spec_decl_function_scope : Error<
   "explicit specialization of %0 in function scope">;
 def err_template_spec_decl_class_scope : Error<
   "explicit specialization of %0 in class scope">;
+def war_template_spec_decl_class_scope : ExtWarn<
+  "Allowing explicit specialization of %0 in class scope is a Microsoft "
+  "extension">, InGroup<Microsoft>;
 def err_template_spec_decl_friend : Error<
   "cannot declare an explicit specialization in a friend">;
 def err_template_spec_decl_out_of_scope_global : Error<
index 58b807fb2aed635771fdad0e23a8192acb7a0164..25d23584b8f29ce4bda5df5418e46a55cd0b347c 100644 (file)
@@ -1685,10 +1685,14 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
     if (OldMethod && NewMethod) {
       // Preserve triviality.
       NewMethod->setTrivial(OldMethod->isTrivial());
-
+   
+      // MSVC allows explicit template specialization at class scope.
+      bool IsMSExplicitSpecialization = getLangOptions().Microsoft &&
+                                 NewMethod->isFunctionTemplateSpecialization();
       bool isFriend = NewMethod->getFriendObjectKind();
 
-      if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord()) {
+      if (!isFriend && NewMethod->getLexicalDeclContext()->isRecord() &&
+          !IsMSExplicitSpecialization) {
         //    -- Member function declarations with the same name and the
         //       same parameter types cannot be overloaded if any of them
         //       is a static member function declaration.
index c31ed017377ccd43dcf4c66f826c73ea389ac220..9b14f63301334d6a472d9b8dcb6bc354cb5efada 100644 (file)
@@ -4401,9 +4401,12 @@ static bool CheckTemplateSpecializationScope(Sema &S,
   }
 
   if (S.CurContext->isRecord() && !IsPartialSpecialization) {
-    S.Diag(Loc, diag::err_template_spec_decl_class_scope)
-      << Specialized;
-    return true;
+    if (S.getLangOptions().Microsoft)
+      S.Diag(Loc, diag::war_template_spec_decl_class_scope) << Specialized;
+    else {    
+      S.Diag(Loc, diag::err_template_spec_decl_class_scope) << Specialized;
+      return true;
+    }
   }
 
   // C++ [temp.class.spec]p6:
index 88e39226708c98cb7944bac6cc5fde5b4d1a1ec0..e1c64b1ac8f68119ef5f871dd68798f1bf4d6ce2 100644 (file)
@@ -197,3 +197,10 @@ void pointer_to_integral_type_conv(char* ptr) {
    ch = (char)ptr;
    sh = (short)ptr;
 } 
+
+
+struct X1 {
+  template<typename T> void f(T);
+
+  template<> void f(int) { } // expected-warning{{Allowing explicit specialization of 'f' in class scope is a Microsoft extension}}
+};