]> granicus.if.org Git - clang/commitdiff
Add a new warning on NULL pointer constant to integer conversion.
authorRichard Trieu <rtrieu@google.com>
Sun, 29 May 2011 19:59:02 +0000 (19:59 +0000)
committerRichard Trieu <rtrieu@google.com>
Sun, 29 May 2011 19:59:02 +0000 (19:59 +0000)
This path was reviewed by Chandler Carruth at http://codereview.appspot.com/4538074/

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132297 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/SemaCXX/conversion.cpp

index 5e1f3f58188014bb1cac980be10918220aba3bbe..8fb4eaa166cf3d0ac6cc7b559051691184a8a7bc 100644 (file)
@@ -1284,7 +1284,9 @@ def warn_impcast_different_enum_types : Warning<
 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">,
index 169b242cd9f37928fb79f591ab7f10b8019c24c3..7430cfb119e39700a44da1e5b0fcef828193969a 100644 (file)
@@ -2968,6 +2968,13 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
   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);
 
index fdda7ac76acd5585515ff8e967221808952b50c0..b069abc263f4aa129820a45c5078fe0a2b50aaa1 100644 (file)
@@ -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 @@ namespace test2 {
     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}}
+}