From: George Burgess IV Date: Tue, 8 Dec 2015 22:02:00 +0000 (+0000) Subject: [Sema] Add warning when comparing nonnull and null X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=543c2ee4e169a0192650da497b414e63c48981ed;p=clang [Sema] Add warning when comparing nonnull and null Currently, we emit warnings in some cases where nonnull function parameters are compared against null. This patch extends this support to warn when comparing the result of `returns_nonnull` functions against null. More specifically, we will now warn cases like: int *foo() __attribute__((returns_nonnull)); int main() { if (foo() == NULL) {} // warning: will always evaluate to false } Differential Revision: http://reviews.llvm.org/D15324 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@255058 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 230f361698..b63fe637da 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2727,7 +2727,7 @@ def warn_impcast_pointer_to_bool : Warning< "'true'">, InGroup; def warn_cast_nonnull_to_bool : Warning< - "nonnull parameter '%0' will evaluate to " + "nonnull %select{function call|parameter}0 '%1' will evaluate to " "'true' on first encounter">, InGroup; def warn_this_bool_conversion : Warning< @@ -2742,9 +2742,10 @@ def warn_null_pointer_compare : Warning< "comparison of %select{address of|function|array}0 '%1' %select{not |}2" "equal to a null pointer is always %select{true|false}2">, InGroup; -def warn_nonnull_parameter_compare : Warning< - "comparison of nonnull parameter '%0' %select{not |}1" - "equal to a null pointer is %select{true|false}1 on first encounter">, +def warn_nonnull_expr_compare : Warning< + "comparison of nonnull %select{function call|parameter}0 '%1' " + "%select{not |}2equal to a null pointer is '%select{true|false}2' on first " + "encounter">, InGroup; def warn_this_null_compare : Warning< "'this' pointer cannot be null in well-defined C++ code; comparison may be " diff --git a/include/clang/Sema/ScopeInfo.h b/include/clang/Sema/ScopeInfo.h index 77801bd44e..d13667e800 100644 --- a/include/clang/Sema/ScopeInfo.h +++ b/include/clang/Sema/ScopeInfo.h @@ -164,7 +164,7 @@ public: /// \brief A list of parameters which have the nonnull attribute and are /// modified in the function. - llvm::SmallPtrSet ModifiedNonNullParams; + llvm::SmallPtrSet ModifiedNonNullParams; public: /// Represents a simple identification of a weak object. diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 9d8a3f5240..d5c80c9e20 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -1180,8 +1180,7 @@ bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, /// Checks if a the given expression evaluates to null. /// /// \brief Returns true if the value evaluates to null. -static bool CheckNonNullExpr(Sema &S, - const Expr *Expr) { +static bool CheckNonNullExpr(Sema &S, const Expr *Expr) { // If the expression has non-null type, it doesn't evaluate to null. if (auto nullability = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) { @@ -7666,6 +7665,26 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, } } + auto ComplainAboutNonnullParamOrCall = [&](bool IsParam) { + std::string Str; + llvm::raw_string_ostream S(Str); + E->printPretty(S, nullptr, getPrintingPolicy()); + unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare + : diag::warn_cast_nonnull_to_bool; + Diag(E->getExprLoc(), DiagID) << IsParam << S.str() + << E->getSourceRange() << Range << IsEqual; + }; + + // If we have a CallExpr that is tagged with returns_nonnull, we can complain. + if (auto *Call = dyn_cast(E->IgnoreParenImpCasts())) { + if (auto *Callee = Call->getDirectCallee()) { + if (Callee->hasAttr()) { + ComplainAboutNonnullParamOrCall(false); + return; + } + } + } + // Expect to find a single Decl. Skip anything more complicated. ValueDecl *D = nullptr; if (DeclRefExpr *R = dyn_cast(E)) { @@ -7677,40 +7696,38 @@ void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, // Weak Decls can be null. if (!D || D->isWeak()) return; - + // Check for parameter decl with nonnull attribute - if (const ParmVarDecl* PV = dyn_cast(D)) { - if (getCurFunction() && !getCurFunction()->ModifiedNonNullParams.count(PV)) - if (const FunctionDecl* FD = dyn_cast(PV->getDeclContext())) { - unsigned NumArgs = FD->getNumParams(); - llvm::SmallBitVector AttrNonNull(NumArgs); + if (const auto* PV = dyn_cast(D)) { + if (getCurFunction() && + !getCurFunction()->ModifiedNonNullParams.count(PV)) { + if (PV->hasAttr()) { + ComplainAboutNonnullParamOrCall(true); + return; + } + + if (const auto *FD = dyn_cast(PV->getDeclContext())) { + auto ParamIter = std::find(FD->param_begin(), FD->param_end(), PV); + assert(ParamIter != FD->param_end()); + unsigned ParamNo = std::distance(FD->param_begin(), ParamIter); + for (const auto *NonNull : FD->specific_attrs()) { if (!NonNull->args_size()) { - AttrNonNull.set(0, NumArgs); - break; - } - for (unsigned Val : NonNull->args()) { - if (Val >= NumArgs) - continue; - AttrNonNull.set(Val); + ComplainAboutNonnullParamOrCall(true); + return; } - } - if (!AttrNonNull.empty()) - for (unsigned i = 0; i < NumArgs; ++i) - if (FD->getParamDecl(i) == PV && - (AttrNonNull[i] || PV->hasAttr())) { - std::string Str; - llvm::raw_string_ostream S(Str); - E->printPretty(S, nullptr, getPrintingPolicy()); - unsigned DiagID = IsCompare ? diag::warn_nonnull_parameter_compare - : diag::warn_cast_nonnull_to_bool; - Diag(E->getExprLoc(), DiagID) << S.str() << E->getSourceRange() - << Range << IsEqual; + + for (unsigned ArgNo : NonNull->args()) { + if (ArgNo == ParamNo) { + ComplainAboutNonnullParamOrCall(true); return; } + } + } } } - + } + QualType T = D->getType(); const bool IsArray = T->isArrayType(); const bool IsFunction = T->isFunctionType(); diff --git a/test/Sema/nonnull.c b/test/Sema/nonnull.c index 4b3df8518c..9503e7c32a 100644 --- a/test/Sema/nonnull.c +++ b/test/Sema/nonnull.c @@ -89,7 +89,7 @@ void redecl_test(void *p) { __attribute__((__nonnull__)) int evil_nonnull_func(int* pointer, void * pv) { - if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}} + if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}} return 0; } else { return *pointer; @@ -101,13 +101,13 @@ int evil_nonnull_func(int* pointer, void * pv) else return *pointer; - if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}} + if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}} } void set_param_to_null(int**); int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3))); int another_evil_nonnull_func(int* pointer, char ch, void * pv) { - if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}} + if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}} return 0; } else { return *pointer; @@ -119,7 +119,7 @@ int another_evil_nonnull_func(int* pointer, char ch, void * pv) { else return *pointer; - if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}} + if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}} } extern void *returns_null(void**); @@ -153,3 +153,17 @@ void pr21668_2(__attribute__((nonnull)) const char *p) { if (p) // No warning ; } + +__attribute__((returns_nonnull)) void *returns_nonnull_whee(); + +void returns_nonnull_warning_tests() { + if (returns_nonnull_whee() == NULL) {} // expected-warning {{comparison of nonnull function call 'returns_nonnull_whee()' equal to a null pointer is 'false' on first encounter}} + + if (returns_nonnull_whee() != NULL) {} // expected-warning {{comparison of nonnull function call 'returns_nonnull_whee()' not equal to a null pointer is 'true' on first encounter}} + + if (returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} + if (!returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} + + int and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} + and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}} +}