From d7a95971bd91b21922e5e878ba05bbe42b585cc9 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 8 Jun 2010 17:35:15 +0000 Subject: [PATCH] Implement a warning when converting the literal 'false' to a pointer. Original patch by Troy D. Straszheim; fixes PR7283. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105621 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticGroups.td | 3 ++- include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++ lib/Sema/SemaOverload.cpp | 8 +++++++- test/SemaCXX/warn_false_to_pointer.cpp | 9 +++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test/SemaCXX/warn_false_to_pointer.cpp diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 430ad96efb..5dc613a7a9 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -23,6 +23,7 @@ def : DiagGroup<"aggregate-return">; def AmbigMemberTemplate : DiagGroup<"ambiguous-member-template">; def : DiagGroup<"attributes">; def : DiagGroup<"bad-function-cast">; +def BoolConversions : DiagGroup<"bool-conversions">; def : DiagGroup<"c++-compat">; def : DiagGroup<"cast-align">; def : DiagGroup<"cast-qual">; @@ -141,7 +142,7 @@ def Parentheses : DiagGroup<"parentheses", [DiagGroup<"idiomatic-parentheses">]> // -Wconversion has its own warnings, but we split this one out for // legacy reasons. def Conversion : DiagGroup<"conversion", - [DiagGroup<"shorten-64-to-32">]>, + [DiagGroup<"shorten-64-to-32">, BoolConversions]>, DiagCategory<"Value Conversion Issue">; def Unused : DiagGroup<"unused", diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2412267bb6..efc171f3bb 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1098,6 +1098,10 @@ def note_ovl_candidate : Note<"candidate " "is the implicit copy constructor|" "is the implicit copy assignment operator}0%1">; +def warn_init_pointer_from_false : Warning< + "initialization of pointer of type %0 from literal 'false'">, + InGroup; + def note_ovl_candidate_bad_deduction : Note< "candidate template ignored: failed template argument deduction">; def note_ovl_candidate_incomplete_deduction : Note<"candidate template ignored: " diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 2754d443b2..6a020d57d3 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -1622,6 +1622,12 @@ bool Sema::CheckPointerConversion(Expr *From, QualType ToType, bool IgnoreBaseAccess) { QualType FromType = From->getType(); + if (CXXBoolLiteralExpr* LitBool + = dyn_cast(From->IgnoreParens())) + if (LitBool->getValue() == false) + Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false) + << ToType; + if (const PointerType *FromPtrType = FromType->getAs()) if (const PointerType *ToPtrType = ToType->getAs()) { QualType FromPointeeType = FromPtrType->getPointeeType(), @@ -6159,7 +6165,7 @@ BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), CommaLocs, RParenLoc); } - + /// ResolveOverloadedCallFn - Given the call expression that calls Fn /// (which eventually refers to the declaration Func) and the call /// arguments Args/NumArgs, attempt to resolve the function call down diff --git a/test/SemaCXX/warn_false_to_pointer.cpp b/test/SemaCXX/warn_false_to_pointer.cpp new file mode 100644 index 0000000000..3a873d886f --- /dev/null +++ b/test/SemaCXX/warn_false_to_pointer.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +int* j = false; // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}} + +void foo(int* i, int *j=(false)) // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}} +{ + foo(false); // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}} +} +