[clang-tools-extra] r178907 - Fix UseNullptr fails to replace explict casts surrounded by another implicit

Ariel J. Bernal ariel.j.bernal at intel.com
Fri Apr 5 13:32:36 PDT 2013


Author: ajbernal
Date: Fri Apr  5 15:32:36 2013
New Revision: 178907

URL: http://llvm.org/viewvc/llvm-project?rev=178907&view=rev
Log:
Fix UseNullptr fails to replace explict casts surrounded by another implicit
cast

UseNullptr previously matched the implicit cast to const pointer as well as
the explicit cast within that has an implicit cast to nullptr as a descendant.

-Refactored UseNullptr to avoid special-casing certain kinds of cast sequences
-Added test cases.

Modified:
    clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrMatchers.cpp
    clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp

Modified: clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrMatchers.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrMatchers.cpp?rev=178907&r1=178906&r2=178907&view=diff
==============================================================================
--- clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrMatchers.cpp (original)
+++ clang-tools-extra/trunk/cpp11-migrate/UseNullptr/NullptrMatchers.cpp Fri Apr  5 15:32:36 2013
@@ -58,10 +58,12 @@ StatementMatcher makeCastSequenceMatcher
     );
 
   return castExpr(
-           unless(hasAncestor(explicitCastExpr())),
            anyOf(
-             hasDescendant(ImplicitCastToNull),
-             ImplicitCastToNull
-           )
+             ImplicitCastToNull,
+             explicitCastExpr(
+               hasDescendant(ImplicitCastToNull)
+             )
+           ),
+           unless(hasAncestor(explicitCastExpr()))
          ).bind(CastSequence);
 }

Modified: clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp?rev=178907&r1=178906&r2=178907&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp (original)
+++ clang-tools-extra/trunk/test/cpp11-migrate/UseNullptr/basic.cpp Fri Apr  5 15:32:36 2013
@@ -221,6 +221,8 @@ struct Bam {
 
 void ambiguous_function(int *a) {}
 void ambiguous_function(float *a) {}
+void const_ambiguous_function(const int *p) {}
+void const_ambiguous_function(const float *p) {}
 
 void test_explicit_cast_ambiguous1() {
   ambiguous_function((int*)0);
@@ -251,3 +253,26 @@ void test_explicit_cast_ambiguous5() {
   k = (int*)0;
   // CHECK: k = (int*)nullptr;
 }
+
+void test_const_pointers_abiguous() {
+  const_ambiguous_function((int*)0);
+  // CHECK: const_ambiguous_function((int*)nullptr);
+}
+
+// Test where the implicit cast to null is surrounded by another implict cast
+// with possible explict casts in-between.
+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;
+  int *t;
+  const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(0));
+  // CHECK: const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));
+}





More information about the cfe-commits mailing list