[PATCH] Fix UseNullptr fails to replace explicit cast to nullptr assigned to const pointers

Ariel Bernal ariel.j.bernal at intel.com
Thu Apr 4 13:40:48 PDT 2013


Hi revane, tareqsiraj,

UseNullptr previously matched the implicit cast to const pointer and the explicit cast within that has an implicit cast to nullptr as a descendant. 
This patch skips implicit casts expressions that have an implicit cast to nullptr within, so we only match the explicit cast.

Added test cases.

http://llvm-reviews.chandlerc.com/D627

Files:
  cpp11-migrate/UseNullptr/NullptrMatchers.cpp
  test/cpp11-migrate/UseNullptr/basic.cpp

Index: cpp11-migrate/UseNullptr/NullptrMatchers.cpp
===================================================================
--- cpp11-migrate/UseNullptr/NullptrMatchers.cpp
+++ cpp11-migrate/UseNullptr/NullptrMatchers.cpp
@@ -59,6 +59,7 @@
 
   return castExpr(
            unless(hasAncestor(explicitCastExpr())),
+           unless(implicitCastExpr(hasDescendant(ImplicitCastToNull))),
            anyOf(
              hasDescendant(ImplicitCastToNull),
              ImplicitCastToNull
Index: test/cpp11-migrate/UseNullptr/basic.cpp
===================================================================
--- test/cpp11-migrate/UseNullptr/basic.cpp
+++ test/cpp11-migrate/UseNullptr/basic.cpp
@@ -251,3 +251,26 @@
   k = (int*)0;
   // CHECK: k = (int*)nullptr;
 }
+
+// Test assignments to const pointers
+void test_const_pointers() {
+  const int *const_p1 = 0;
+  // CHECK: const int *const_p1 = nullptr;
+  const int *const_p2 = NULL;
+  // CHECK: const int *const_p2 = nullptr;
+  const int *const_p3 = (int)0;
+  // CHECK: const int *const_p3 = nullptr;
+  const int *const_p4 = (int)0.0f;
+  // CHECK: const int *const_p4 = nullptr;
+  const int *const_p5 = (int*)0;
+  // CHECK: const int *const_p5 = (int*)nullptr;
+}
+
+// Test for ambiguous funcions with const pointer arguments
+void const_ambiguous_function(const int *p) {}
+void const_ambiguous_function(const float *p) {}
+
+void test_const_pointers_abiguous() {
+  const_ambiguous_function((int*)0);
+  // CHECK: const_ambiguous_function((int*)nullptr);
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D627.1.patch
Type: text/x-patch
Size: 1519 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130404/be052686/attachment.bin>


More information about the cfe-commits mailing list