From: Richard Trieu Date: Sun, 29 May 2011 19:59:02 +0000 (+0000) Subject: Add a new warning on NULL pointer constant to integer conversion. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1838ca5618bc6c84ca3d13d84717bf32d9862bb3;p=clang Add a new warning on NULL pointer constant to integer conversion. 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 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 5e1f3f5818..8fb4eaa166 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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; - +def warn_impcast_null_pointer_to_integer : Warning< + "implicit conversion of NULL constant to integer">, + InGroup>, DefaultIgnore; def warn_cast_align : Warning< "cast from %0 to %1 increases required alignment from %2 to %3">, diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 169b242cd9..7430cfb119 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -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); diff --git a/test/SemaCXX/conversion.cpp b/test/SemaCXX/conversion.cpp index fdda7ac76a..b069abc263 100644 --- a/test/SemaCXX/conversion.cpp +++ b/test/SemaCXX/conversion.cpp @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -verify %s +#include + 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}} +}