Check on null arguments in the presense of nonnull attribute.

llvm-svn: 72219
This commit is contained in:
Fariborz Jahanian 2009-05-21 18:48:51 +00:00
parent aa85d8221c
commit cd1a88da02
4 changed files with 45 additions and 0 deletions

View File

@ -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<

View File

@ -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,

View File

@ -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.

View File

@ -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}}
}