diff options
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 3 | ||||
-rw-r--r-- | lib/Sema/Sema.h | 2 | ||||
-rw-r--r-- | lib/Sema/SemaChecking.cpp | 14 | ||||
-rw-r--r-- | test/Sema/nonnull-check.c | 26 |
4 files changed, 45 insertions, 0 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index f9a462b2de..9e7e94f085 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1621,6 +1621,9 @@ def warn_printf_invalid_conversion : Warning< "invalid conversion '%0'">, InGroup<Format>; def warn_printf_missing_format_string : Warning< "format string missing">, InGroup<Format>; +def warn_null_arg : Warning< + "argument is null where non-null is required">, + InGroup<DiagGroup<"nonnull">>, DefaultIgnore; def warn_printf_empty_format_string : Warning< "format string is empty">, InGroup<FormatZeroLength>; def warn_printf_format_string_is_wide_literal : Warning< diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 9c294e0b10..25284318a3 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -2741,6 +2741,8 @@ private: void CheckPrintfString(const StringLiteral *FExpr, const Expr *OrigFormatExpr, const CallExpr *TheCall, bool HasVAListArg, unsigned format_idx, unsigned firstDataArg); + void CheckNonNullArguments(const NonNullAttr *NonNull, + const CallExpr *TheCall); void CheckPrintfArguments(const CallExpr *TheCall, bool HasVAListArg, unsigned format_idx, unsigned firstDataArg); void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index e9ddce33ce..5a6babb321 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -178,6 +178,10 @@ Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { HasVAListArg ? 0 : Format->getFirstArg() - 1); } } + for (const Attr *attr = FDecl->getAttrs(); attr; attr = attr->getNext()) { + if (const NonNullAttr *NonNull = dyn_cast<NonNullAttr>(attr)) + CheckNonNullArguments(NonNull, TheCall); + } return move(TheCallResult); } @@ -784,6 +788,16 @@ bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, } } +void +Sema::CheckNonNullArguments(const NonNullAttr *NonNull, const CallExpr *TheCall) +{ + for (NonNullAttr::iterator i = NonNull->begin(), e = NonNull->end(); + i != e; ++i) { + const Expr *ArgExpr = TheCall->getArg(*i)->IgnoreParenCasts(); + if (ArgExpr->isNullPointerConstant(Context)) + Diag(ArgExpr->getLocStart(), diag::warn_null_arg); + } +} /// CheckPrintfArguments - Check calls to printf (and similar functions) for /// correct use of format strings. diff --git a/test/Sema/nonnull-check.c b/test/Sema/nonnull-check.c new file mode 100644 index 0000000000..2d2a9f4055 --- /dev/null +++ b/test/Sema/nonnull-check.c @@ -0,0 +1,26 @@ +// RUN: clang-cc -Wnonnull -fsyntax-only -verify %s + +extern void func1 (void (^block1)(), void (^block2)(), int) __attribute__((nonnull)); + +extern void func3 (void (^block1)(), int, void (^block2)(), int) + __attribute__((nonnull(1,3))); + +extern void func4 (void (^block1)(), void (^block2)()) __attribute__((nonnull(1))) + __attribute__((nonnull(2))); + +void +foo (int i1, int i2, int i3, void (^cp1)(), void (^cp2)(), void (^cp3)()) +{ + func1(cp1, cp2, i1); + + func1(0, cp2, i1); // expected-warning {{argument is null where non-null is required}} + func1(cp1, 0, i1); // expected-warning {{argument is null where non-null is required}} + func1(cp1, cp2, 0); + + + func3(0, i2, cp3, i3); // expected-warning {{argument is null where non-null is required}} + func3(cp3, i2, 0, i3); // expected-warning {{argument is null where non-null is required}} + + func4(0, cp1); // expected-warning {{argument is null where non-null is required}} + func4(cp1, 0); // expected-warning {{argument is null where non-null is required}} +} |