[PATCH] D134928: [Sema] Don't treat a non-null template argument as if it were null.

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 30 12:26:52 PDT 2022


efriedma updated this revision to Diff 464363.
efriedma added a comment.

Add release note.  Add test coverage for C++17, since it uses a different codepath.  (The C++17 codepath appears to work correctly without any changes.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134928/new/

https://reviews.llvm.org/D134928

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaTemplate.cpp
  clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp
  clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1.cpp


Index: clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1.cpp
===================================================================
--- clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1.cpp
+++ clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1.cpp
@@ -176,7 +176,7 @@
 //   -- a pointer to member expressed as described in 5.3.1.
 
 namespace bad_args {
-  template <int* N> struct X0 { }; // precxx17-note 2{{template parameter is declared here}}
+  template <int* N> struct X0 { }; // precxx17-note 4{{template parameter is declared here}}
   int i = 42;
   X0<&i + 2> x0a; // precxx17-error{{non-type template argument does not refer to any declaration}} \
                      cxx17-error {{non-type template argument is not a constant expression}} \
@@ -194,6 +194,13 @@
   // cxx17-error at -5 {{non-type template argument is not a constant expression}}
   // expected-note at -6 {{read of non-constexpr variable 'iptr' is not allowed in a constant expression}}
 #endif
+  X0<(int*)1> x0c;
+  // precxx17-error at -1 {{does not refer to any declaration}}
+  // cxx17-error at -2 {{non-type template argument is not a constant expression}}
+  // cxx17-note at -3 {{reinterpret_cast}}
+  X0<__builtin_constant_p(0) ? (int*)1 : (int*)1> x0d;
+  // precxx17-error at -1 {{does not refer to any declaration}}
+  // cxx17-error at -2 {{non-type template argument refers to subobject '(int *)1'}}
 }
 #endif // CPP11ONLY
 
Index: clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp
===================================================================
--- clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp
+++ clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1-11.cpp
@@ -4,7 +4,7 @@
   typedef decltype(nullptr) nullptr_t;
 }
 
-template<int *ip> struct IP {  // expected-note 5 {{template parameter is declared here}}
+template<int *ip> struct IP {  // expected-note 6 {{template parameter is declared here}}
   IP<ip> *ip2;
 };
 
@@ -28,6 +28,7 @@
 // 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 *'}}
 IP<&tl> ip7; // expected-error{{non-type template argument of type 'int *' is not a constant expression}}
+IP<(int*)1> ip8; // expected-error {{non-type template argument does not refer to any declaration}}
 
 IR<tl> ir1; // expected-error{{non-type template argument refers to thread-local object}}
 
Index: clang/lib/Sema/SemaTemplate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplate.cpp
+++ clang/lib/Sema/SemaTemplate.cpp
@@ -6495,7 +6495,7 @@
   //   - a constant expression that evaluates to a null pointer value (4.10); or
   //   - a constant expression that evaluates to a null member pointer value
   //     (4.11); or
-  if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) ||
+  if ((EvalResult.Val.isLValue() && EvalResult.Val.isNullPointer()) ||
       (EvalResult.Val.isMemberPointer() &&
        !EvalResult.Val.getMemberPointerDecl())) {
     // If our expression has an appropriate type, we've succeeded.
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -163,6 +163,9 @@
 - The template arguments of a variable template being accessed as a
   member will now be represented in the AST.
 - Fix incorrect handling of inline builtins with asm labels.
+- Reject non-type template arguments formed by casting a non-zero integer
+  to a pointer in pre-C++17 modes, instead of treating them as null
+  pointers.
 
 
 Improvements to Clang's diagnostics


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134928.464363.patch
Type: text/x-patch
Size: 3732 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220930/cd739c46/attachment.bin>


More information about the cfe-commits mailing list