[cfe-commits] r72219 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/Sema.h lib/Sema/SemaChecking.cpp test/Sema/nonnull-check.c
Fariborz Jahanian
fjahanian at apple.com
Thu May 21 11:48:52 PDT 2009
Author: fjahanian
Date: Thu May 21 13:48:51 2009
New Revision: 72219
URL: http://llvm.org/viewvc/llvm-project?rev=72219&view=rev
Log:
Check on null arguments in the presense of nonnull attribute.
Added:
cfe/trunk/test/Sema/nonnull-check.c
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaChecking.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=72219&r1=72218&r2=72219&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu May 21 13:48:51 2009
@@ -1621,6 +1621,9 @@
"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<
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=72219&r1=72218&r2=72219&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu May 21 13:48:51 2009
@@ -2741,6 +2741,8 @@
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,
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=72219&r1=72218&r2=72219&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu May 21 13:48:51 2009
@@ -178,6 +178,10 @@
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 @@
}
}
+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.
Added: cfe/trunk/test/Sema/nonnull-check.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/nonnull-check.c?rev=72219&view=auto
==============================================================================
--- cfe/trunk/test/Sema/nonnull-check.c (added)
+++ cfe/trunk/test/Sema/nonnull-check.c Thu May 21 13:48:51 2009
@@ -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}}
+}
More information about the cfe-commits
mailing list