[cfe-commits] r103482 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaTemplate.cpp test/CXX/temp/temp.spec/temp.explicit/p2.cpp test/CXX/temp/temp.spec/temp.explicit/p5.cpp test/SemaTemplate/temp_explicit.cpp

Douglas Gregor dgregor at apple.com
Tue May 11 10:39:34 PDT 2010


Author: dgregor
Date: Tue May 11 12:39:34 2010
New Revision: 103482

URL: http://llvm.org/viewvc/llvm-project?rev=103482&view=rev
Log:
The C++98/03 standard is disturbingly silent about out-of-scope
explicit instantiations of template. C++0x clarifies the intent
(they're ill-formed in some cases; see [temp.explicit] for
details). However, one could squint at the C++98/03 standard and
conclude they are permitted, so reduce the error to a warning
(controlled by -Wc++0x-compat) in C++98/03 mode.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p2.cpp
    cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p5.cpp
    cfe/trunk/test/SemaTemplate/temp_explicit.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=103482&r1=103481&r2=103482&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue May 11 12:39:34 2010
@@ -26,6 +26,7 @@
 def : DiagGroup<"cast-align">;
 def : DiagGroup<"cast-qual">;
 def : DiagGroup<"char-align">;
+def : DiagGroup<"c++0x-compat">;
 def Comment : DiagGroup<"comment">;
 def : DiagGroup<"ctor-dtor-privacy">;
 def : DiagGroup<"declaration-after-statement">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=103482&r1=103481&r2=103482&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue May 11 12:39:34 2010
@@ -1504,6 +1504,12 @@
   "explicit instantiation of %0 not in a namespace enclosing %1">;
 def err_explicit_instantiation_must_be_global : Error<
   "explicit instantiation of %0 must occur at global scope">;
+def warn_explicit_instantiation_out_of_scope_0x : Warning<
+  "explicit instantiation of %0 not in a namespace enclosing %1">, 
+  InGroup<DiagGroup<"-Wc++0x-compat"> >;
+def warn_explicit_instantiation_must_be_global_0x : Warning<
+  "explicit instantiation of %0 must occur at global scope">, 
+  InGroup<DiagGroup<"-Wc++0x-compat"> >;
   
 def err_explicit_instantiation_requires_name : Error<
   "explicit instantiation declaration requires a name">;
@@ -1532,6 +1538,9 @@
   "qualifier in explicit instantiation of '%0%1' requires a template-id">;
 def err_explicit_instantiation_unqualified_wrong_namespace : Error<
   "explicit instantiation of %q0 must occur in %1">;
+def warn_explicit_instantiation_unqualified_wrong_namespace_0x : Warning<
+  "explicit instantiation of %q0 must occur in %1">, 
+  InGroup<DiagGroup<"c++0x-compat"> >;
 def err_explicit_instantiation_undefined_member : Error<
   "explicit instantiation of undefined %select{member class|member function|"
   "static data member}0 %1 of class template %2">;

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=103482&r1=103481&r2=103482&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Tue May 11 12:39:34 2010
@@ -4544,10 +4544,16 @@
   if (S.getLangOptions().CPlusPlus0x && 
       !CurContext->Encloses(ExpectedContext)) {
     if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext))
-      S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope)
+      S.Diag(InstLoc, 
+             S.getLangOptions().CPlusPlus0x? 
+                 diag::err_explicit_instantiation_out_of_scope
+               : diag::warn_explicit_instantiation_out_of_scope_0x)
         << D << NS;
     else
-      S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global)
+      S.Diag(InstLoc, 
+             S.getLangOptions().CPlusPlus0x?
+                 diag::err_explicit_instantiation_must_be_global
+               : diag::warn_explicit_instantiation_out_of_scope_0x)
         << D;
     S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
     return;
@@ -4564,7 +4570,10 @@
   if (CurContext->Equals(ExpectedContext))
     return;
   
-  S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace)
+  S.Diag(InstLoc, 
+         S.getLangOptions().CPlusPlus0x?
+             diag::err_explicit_instantiation_unqualified_wrong_namespace
+           : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
     << D << ExpectedContext;
   S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
 }

Modified: cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p2.cpp?rev=103482&r1=103481&r2=103482&view=diff
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p2.cpp (original)
+++ cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p2.cpp Tue May 11 12:39:34 2010
@@ -39,5 +39,5 @@
 }
 using namespace N;
 
-template struct X1<int>; // expected-error{{must occur in}}
-template void f1(int); // expected-error{{must occur in}}
+template struct X1<int>; // expected-warning{{must occur in}}
+template void f1(int); // expected-warning{{must occur in}}

Modified: cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p5.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p5.cpp?rev=103482&r1=103481&r2=103482&view=diff
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p5.cpp (original)
+++ cfe/trunk/test/CXX/temp/temp.spec/temp.explicit/p5.cpp Tue May 11 12:39:34 2010
@@ -11,7 +11,7 @@
 // FIXME: This example from the standard is wrong; note posted to CWG reflector
 // on 10/27/2009
 using N::Y; 
-template class Y<int>; // expected-error{{must occur in}}
+template class Y<int>; // expected-warning{{must occur in}}
 
 template class N::Y<char*>; 
 template void N::Y<double>::mf();

Modified: cfe/trunk/test/SemaTemplate/temp_explicit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/temp_explicit.cpp?rev=103482&r1=103481&r2=103482&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/temp_explicit.cpp (original)
+++ cfe/trunk/test/SemaTemplate/temp_explicit.cpp Tue May 11 12:39:34 2010
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++0x-compat %s
 //
 // Tests explicit instantiation of templates.
 template<typename T, typename U = T> class X0 { };
@@ -125,3 +125,27 @@
 template <>
 struct Foo<int>::Bar<void>
 {};
+
+namespace N1 {
+
+  template<typename T> struct X7 { }; // expected-note{{here}}
+
+  namespace Inner {
+    template<typename T> struct X8 { };
+  }
+
+  template struct X7<int>;
+  template struct Inner::X8<int>;
+}
+
+template<typename T> struct X9 { }; // expected-note{{here}}
+
+template struct ::N1::Inner::X8<float>;
+
+namespace N2 {
+  using namespace N1;
+
+  template struct X7<double>; // expected-warning{{must occur in namespace}}
+
+  template struct X9<float>; // expected-warning{{must occur in the global}}
+}





More information about the cfe-commits mailing list