[cfe-commits] r132297 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp test/SemaCXX/conversion.cpp

Richard Trieu rtrieu at google.com
Sun May 29 12:59:02 PDT 2011


Author: rtrieu
Date: Sun May 29 14:59:02 2011
New Revision: 132297

URL: http://llvm.org/viewvc/llvm-project?rev=132297&view=rev
Log:
Add a new warning on NULL pointer constant to integer conversion.

This path was reviewed by Chandler Carruth at http://codereview.appspot.com/4538074/


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/conversion.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=132297&r1=132296&r2=132297&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sun May 29 14:59:02 2011
@@ -1284,7 +1284,9 @@
 def warn_impcast_bool_to_null_pointer : Warning<
     "initialization of pointer of type %0 to NULL from a constant boolean "
     "expression">, InGroup<BoolConversions>;
-
+def warn_impcast_null_pointer_to_integer : Warning<
+    "implicit conversion of NULL constant to integer">,
+    InGroup<DiagGroup<"conversion">>, DefaultIgnore;
 
 def warn_cast_align : Warning<
   "cast from %0 to %1 increases required alignment from %2 to %3">,

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=132297&r1=132296&r2=132297&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Sun May 29 14:59:02 2011
@@ -2968,6 +2968,13 @@
   if (!Source->isIntegerType() || !Target->isIntegerType())
     return;
 
+  if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
+           == Expr::NPCK_GNUNull) && Target->isIntegerType()) {
+    S.Diag(E->getExprLoc(), diag::warn_impcast_null_pointer_to_integer)
+        << E->getSourceRange() << clang::SourceRange(CC);
+    return;
+  }
+
   IntRange SourceRange = GetExprRange(S.Context, E);
   IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
 

Modified: cfe/trunk/test/SemaCXX/conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/conversion.cpp?rev=132297&r1=132296&r2=132297&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/conversion.cpp Sun May 29 14:59:02 2011
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -verify %s
 
+#include <stddef.h>
+
 typedef   signed char  int8_t;
 typedef   signed short int16_t;
 typedef   signed int   int32_t;
@@ -50,3 +52,12 @@
     A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
   };
 }
+
+void test3() {
+  int a = NULL; // expected-warning {{implicit conversion of NULL constant to integer}}
+  int b;
+  b = NULL; // expected-warning {{implicit conversion of NULL constant to integer}}
+  int c = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to integer}}
+  int d;
+  d = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to integer}}
+}





More information about the cfe-commits mailing list