[cfe-commits] r154424 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaTemplate.cpp test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp

Douglas Gregor dgregor at apple.com
Tue Apr 10 12:03:31 PDT 2012


Author: dgregor
Date: Tue Apr 10 14:03:30 2012
New Revision: 154424

URL: http://llvm.org/viewvc/llvm-project?rev=154424&view=rev
Log:
Improve diagnostics in C++11 when a non-type template argument for a
non-type template parameter of pointer type is not a constant expression.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=154424&r1=154423&r2=154424&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Apr 10 14:03:30 2012
@@ -2344,6 +2344,8 @@
 def err_template_arg_not_ice : Error<
   "non-type template argument of type %0 is not an integral constant "
   "expression">;
+def err_template_arg_not_address_constant : Error<
+  "non-type template argument of type %0 is not a constant expression">;
 def err_template_arg_untyped_null_constant : Error<
   "null non-type template argument must be cast to template parameter type %0">;
 def err_template_arg_wrongtype_null_constant : Error<
@@ -2367,8 +2369,6 @@
 def err_template_arg_ref_bind_ignores_quals : Error<
   "reference binding of non-type template parameter of type %0 to template "
   "argument of type %1 ignores qualifiers">;
-def err_template_arg_not_address_constant : Error<
-  "non-type template argument of type %0 is not a constant expression">;
 def err_template_arg_not_decl_ref : Error<
   "non-type template argument does not refer to any declaration">;
 def err_template_arg_not_object_or_func_form : Error<

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=154424&r1=154423&r2=154424&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Tue Apr 10 14:03:30 2012
@@ -3479,10 +3479,35 @@
     return NPV_NotNullPointer;
   
   // Determine whether we have a constant expression.
+  ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg);
+  if (ArgRV.isInvalid())
+    return NPV_Error;
+  Arg = ArgRV.take();
+  
   Expr::EvalResult EvalResult;
+  llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
+  EvalResult.Diag = &Notes;
   if (!Arg->EvaluateAsRValue(EvalResult, S.Context) ||
-      EvalResult.HasSideEffects)
-    return NPV_NotNullPointer;
+      EvalResult.HasSideEffects) {
+    SourceLocation DiagLoc = Arg->getExprLoc();
+    
+    // If our only note is the usual "invalid subexpression" note, just point
+    // the caret at its location rather than producing an essentially
+    // redundant note.
+    if (Notes.size() == 1 && Notes[0].second.getDiagID() ==
+        diag::note_invalid_subexpr_in_const_expr) {
+      DiagLoc = Notes[0].first;
+      Notes.clear();
+    }
+    
+    S.Diag(DiagLoc, diag::err_template_arg_not_address_constant)
+      << Arg->getType() << Arg->getSourceRange();
+    for (unsigned I = 0, N = Notes.size(); I != N; ++I)
+      S.Diag(Notes[I].first, Notes[I].second);
+    
+    S.Diag(Param->getLocation(), diag::note_template_param_here);
+    return NPV_Error;
+  }
   
   // C++11 [temp.arg.nontype]p1:
   //   - an address constant expression of type std::nullptr_t

Modified: cfe/trunk/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp?rev=154424&r1=154423&r2=154424&view=diff
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp (original)
+++ cfe/trunk/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp Tue Apr 10 14:03:30 2012
@@ -12,7 +12,7 @@
 
 constexpr std::nullptr_t np = nullptr;
 
-std::nullptr_t nonconst_np;
+std::nullptr_t nonconst_np; // expected-note{{declared here}}
 
 IP<0> ip0; // expected-error{{null non-type template argument must be cast to template parameter type 'int *'}}
 IP<(0)> ip1; // expected-error{{null non-type template argument must be cast to template parameter type 'int *'}}
@@ -20,7 +20,8 @@
 IP<get_nullptr()> ip3;
 IP<(int*)0> ip4;
 IP<np> ip5;
-IP<nonconst_np> ip5; // expected-error{{non-type template argument for template parameter of pointer type 'int *' must have its address taken}}
+IP<nonconst_np> ip5; // expected-error{{non-type template argument of type 'std::nullptr_t' (aka 'nullptr_t') is not a constant expression}} \
+// expected-note{{read of non-constexpr variable 'nonconst_np' is not allowed in a constant expression}}
 IP<(float*)0> ip6; // expected-error{{null non-type template argument of type 'float *' does not match template parameter of type 'int *'}}
 
 struct X { };





More information about the cfe-commits mailing list