Index: clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp @@ -21,10 +21,17 @@ static internal::Matcher loopEndingStmt(internal::Matcher Internal) { - // FIXME: Cover noreturn ObjC methods (and blocks?). + internal::Matcher isNoReturnFunType = + ignoringParens(functionType(typeHasNoReturnAttr())); + internal::Matcher isNoReturnDecl = + anyOf(declHasNoReturnAttr(), functionDecl(hasType(isNoReturnFunType)), + varDecl(hasType(blockPointerType(pointee(isNoReturnFunType))))); + return stmt(anyOf( mapAnyOf(breakStmt, returnStmt, gotoStmt, cxxThrowExpr).with(Internal), - callExpr(Internal, callee(functionDecl(isNoReturn()))))); + callExpr(Internal, callee(functionDecl(isNoReturnDecl))), + objcMessageExpr(Internal, objcMessageCallee(isNoReturnDecl)), + callExpr(Internal, callee(varDecl(isNoReturnDecl))))); } /// Return whether `Var` was changed in `LoopStmt`. Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/infinite-loop-noreturn.mm =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/infinite-loop-noreturn.mm @@ -0,0 +1,62 @@ +// RUN: %check_clang_tidy %s bugprone-infinite-loop %t -- -- -fblocks -fexceptions +// RUN: %check_clang_tidy %s bugprone-infinite-loop %t -- -- -fblocks -fobjc-arc -fexceptions + +@interface I ++ (void)foo; ++ (void)bar; ++ (void)baz __attribute__((noreturn)); ++ (instancetype)alloc; +- (instancetype)init; +@end + +_Noreturn void term(); + +void plainCFunction() { + int i = 0; + int j = 0; + int a[10]; + + while (i < 10) { + // no warning, function term has C noreturn attribute + term(); + } + while (i < 10) { + // no warning, class method baz has noreturn attribute + [I baz]; + } + while (i + j < 10) { + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i, j) are updated in the loop body [bugprone-infinite-loop] + [I foo]; + } + while (i + j < 10) { + [I foo]; + [I baz]; // no warning, class method baz has noreturn attribute + } + + void (^block)() = ^{ + }; + void __attribute__((noreturn)) (^block_nr)(void) = ^void __attribute__((noreturn)) (void) { throw "err"; }; + + while (i < 10) { + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop] + block(); + } + while (i < 10) { + // no warning, the block has "noreturn" arribute + block_nr(); + } +} + +@implementation I ++ (void)bar { +} + ++ (void)foo { + static int i = 0; + + while (i < 10) { + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop] + [I bar]; + } +} +@end Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -3829,6 +3829,25 @@ InnerMatcher.matches(*ExprNode, Finder, Builder)); } +/// matches if ObjCMessageExpr's callee declaration matches +/// +/// Given +/// \code +/// @interface I: NSObject +/// +(void)foo; +/// @end +/// ... +/// [I foo] +/// \endcode +/// The example above matches \code [I foo] \endcode with +/// objcMessageExpr(objcMessageCallee(objcMethodDecl(hasName("foo")))) +AST_MATCHER_P(ObjCMessageExpr, objcMessageCallee, + internal::Matcher, InnerMatcher) { + const ObjCMethodDecl *msgDecl = Node.getMethodDecl(); + return (msgDecl != nullptr && + InnerMatcher.matches(*msgDecl, Finder, Builder)); +} + /// Matches if the call expression's callee's declaration matches the /// given matcher. /// @@ -5070,6 +5089,17 @@ /// \endcode AST_MATCHER(FunctionDecl, isNoReturn) { return Node.isNoReturn(); } +/// matches a Decl if it has a "no return" attribute of any kind +AST_MATCHER(Decl, declHasNoReturnAttr) { + return Node.hasAttr() || Node.hasAttr() || + Node.hasAttr(); +} + +/// matches a FunctionType if the type includes the GNU no return attribute +AST_MATCHER(FunctionType, typeHasNoReturnAttr) { + return Node.getNoReturnAttr(); +} + /// Matches the return type of a function declaration. /// /// Given: