[clang-tools-extra] 9343ec8 - [clang-tidy] Adding the missing handling of "noreturn" attributes for Obj-C nodes in `InfiniteLoopChecker`
via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 26 12:45:27 PDT 2022
Author: ziqingluo-90
Date: 2022-08-26T12:44:59-07:00
New Revision: 9343ec861a2e47f05b3b4339f45f6e755b1c5f96
URL: https://github.com/llvm/llvm-project/commit/9343ec861a2e47f05b3b4339f45f6e755b1c5f96
DIFF: https://github.com/llvm/llvm-project/commit/9343ec861a2e47f05b3b4339f45f6e755b1c5f96.diff
LOG: [clang-tidy] Adding the missing handling of "noreturn" attributes for Obj-C nodes in `InfiniteLoopChecker`
With this commit, the `InfiniteLoopChecker` now recognizes message
expressions to "noreturn" methods as well as calls to "noreturn"
blocks.
Reviewed by NoQ, njames93
Differential Revision: https://reviews.llvm.org/D128314
Added:
clang-tools-extra/test/clang-tidy/checkers/bugprone/infinite-loop-noreturn.mm
Modified:
clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
index 8815de220882e..e2401ff7ec0c0 100644
--- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp
@@ -16,15 +16,35 @@ using namespace clang::ast_matchers;
using clang::tidy::utils::hasPtrOrReferenceInFunc;
namespace clang {
+namespace ast_matchers {
+/// matches a Decl if it has a "no return" attribute of any kind
+AST_MATCHER(Decl, declHasNoReturnAttr) {
+ return Node.hasAttr<NoReturnAttr>() || Node.hasAttr<CXX11NoReturnAttr>() ||
+ Node.hasAttr<C11NoReturnAttr>();
+}
+
+/// matches a FunctionType if the type includes the GNU no return attribute
+AST_MATCHER(FunctionType, typeHasNoReturnAttr) {
+ return Node.getNoReturnAttr();
+}
+} // namespace ast_matchers
namespace tidy {
namespace bugprone {
static internal::Matcher<Stmt>
loopEndingStmt(internal::Matcher<Stmt> Internal) {
- // FIXME: Cover noreturn ObjC methods (and blocks?).
+ internal::Matcher<QualType> isNoReturnFunType =
+ ignoringParens(functionType(typeHasNoReturnAttr()));
+ internal::Matcher<Decl> 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(mapAnyOf(functionDecl, /* block callee */ varDecl)
+ .with(isNoReturnDecl))),
+ objcMessageExpr(Internal, callee(isNoReturnDecl))));
}
/// Return whether `Var` was changed in `LoopStmt`.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/infinite-loop-noreturn.mm b/clang-tools-extra/test/clang-tidy/checkers/bugprone/infinite-loop-noreturn.mm
new file mode 100644
index 0000000000000..142332d33338c
--- /dev/null
+++ b/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
+
+ at interface I
++ (void)foo;
++ (void)bar;
++ (void)baz __attribute__((noreturn));
++ (instancetype)alloc;
+- (instancetype)init;
+ at 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();
+ }
+}
+
+ at 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];
+ }
+}
+ at end
More information about the cfe-commits
mailing list