From ba273a2094255d53095a0dbba6a85fc54cc2b068 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 7 Apr 2015 06:01:53 +0000 Subject: [PATCH] [Sema] Don't crash when __attribute__((nonnull)) is applied to blocks A simple case of asserting isFunctionOrMethod when we should have asserted isFunctionOrMethodOrBlock. This fixes PR23117. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@234297 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDeclAttr.cpp | 9 +++++++-- test/SemaObjC/nonnull.m | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 05a81a8293..fa61b974e0 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -51,6 +51,11 @@ namespace AttributeLangSupport { static bool isFunctionOrMethod(const Decl *D) { return (D->getFunctionType() != nullptr) || isa(D); } +/// \brief Return true if the given decl has function type (function or +/// function-typed variable) or an Objective-C method or a block. +static bool isFunctionOrMethodOrBlock(const Decl *D) { + return isFunctionOrMethod(D) || isa(D); +} /// Return true if the given decl has a declarator that should have /// been processed by Sema::GetTypeForDeclarator. @@ -257,7 +262,7 @@ static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, unsigned AttrArgNum, const Expr *IdxExpr, uint64_t &Idx) { - assert(isFunctionOrMethod(D)); + assert(isFunctionOrMethodOrBlock(D)); // In C++ the implicit 'this' function parameter also counts. // Parameters are counted from one. @@ -1601,7 +1606,7 @@ static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, // The checking path for 'noreturn' and 'analyzer_noreturn' are different // because 'analyzer_noreturn' does not impact the type. - if (!isFunctionOrMethod(D) && !isa(D)) { + if (!isFunctionOrMethodOrBlock(D)) { ValueDecl *VD = dyn_cast(D); if (!VD || (!VD->getType()->isBlockPointerType() && !VD->getType()->isFunctionPointerType())) { diff --git a/test/SemaObjC/nonnull.m b/test/SemaObjC/nonnull.m index cacca07240..e1f31937a5 100644 --- a/test/SemaObjC/nonnull.m +++ b/test/SemaObjC/nonnull.m @@ -123,3 +123,5 @@ void PR18795(int (^g)(const char *h, ...) __attribute__((nonnull(1))) __attribut void PR18795_helper() { PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}} } + +void (^PR23117)(int *) = ^(int *p1) __attribute__((nonnull(1))) {}; -- 2.40.0