From 60acea49c1343e5494edb6da20cac6f9d0b6cfb0 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Mon, 27 Sep 2010 19:05:51 +0000 Subject: [PATCH] Issue warning for trivial cases of nonnull attributes (on functions with no pointer arguments) but only when the attribute has not been coming from a macro instantiation in a header file. Fixes first part of radar 6857843. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114860 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaDeclAttr.cpp | 7 ++++++- test/SemaObjC/nonnull.h | 2 ++ test/SemaObjC/nonnull.m | 7 +++++-- 4 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 test/SemaObjC/nonnull.h diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index e3699ab7d5..92a2c1c859 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1020,6 +1020,8 @@ def err_mode_wrong_type : Error< "type of machine mode does not match type of base type">; def err_attr_wrong_decl : Error< "'%0' attribute invalid on this declaration, requires typedef or value">; +def warn_attribute_nonnull_no_pointers : Warning< + "'nonnull' attribute applied to function with no pointer arguments">; def warn_attribute_malloc_pointer_only : Warning< "'malloc' attribute only applies to functions returning a pointer type">; def warn_transparent_union_nonpointer : Warning< diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index dce1bf84fc..09feb50bd3 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -375,8 +375,13 @@ static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { // No pointer arguments? The attribute in this case is // trivially satisfied. - if (NonNullArgs.empty()) + if (NonNullArgs.empty()) { + // Warn the trivial case only if attribute is not coming from a + // macro instantiation. + if (Attr.getLoc().isFileID()) + S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); return; + } } unsigned* start = &NonNullArgs[0]; diff --git a/test/SemaObjC/nonnull.h b/test/SemaObjC/nonnull.h new file mode 100644 index 0000000000..f5a038f5ad --- /dev/null +++ b/test/SemaObjC/nonnull.h @@ -0,0 +1,2 @@ +// rdar: //6857843 +#define NONNULL_ATTR __attribute__((nonnull)) diff --git a/test/SemaObjC/nonnull.m b/test/SemaObjC/nonnull.m index 282fd21a14..cbafc37e8e 100644 --- a/test/SemaObjC/nonnull.m +++ b/test/SemaObjC/nonnull.m @@ -1,8 +1,11 @@ +#include "nonnull.h" + // RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s @class NSObject; -int f1(int x) __attribute__((nonnull)); //no-warning +NONNULL_ATTR +int f1(int x); // no warning int f2(int *x) __attribute__ ((nonnull (1))); int f3(int *x) __attribute__ ((nonnull (0))); // expected-error {{'nonnull' attribute parameter 1 is out of bounds}} int f4(int *x, int *y) __attribute__ ((nonnull (1,2))); @@ -44,4 +47,4 @@ foo (int i1, int i2, int i3, void (^cp1)(), void (^cp2)(), void (^cp3)()) func7((NSObject*) 0); // no-warning } -void func5(int) __attribute__((nonnull)); // no-warning +void func5(int) NONNULL_ATTR; // no warning -- 2.40.0