feat(tidy): add fairmq-bugprone-run-loop-ignores-state

- Report endless loops (while (true), for (;;), ...) in an override of
  Device::Run() that do not call NewStatePending() or WaitFor(). Such a
  device does not stop when a state change is requested.
- Skip loops that call Send() or Receive(), which an interrupt stops,
  and loops with a break, return, throw or goto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-09 23:14:01 +00:00
parent 42952bfa42
commit 349573fb5f
6 changed files with 176 additions and 0 deletions

View File

@@ -49,6 +49,8 @@ add_library(fairmq-tidy-module OBJECT
ProgOptionsAccessorsCheck.h
RunDeviceCheck.cxx
RunDeviceCheck.h
RunLoopIgnoresStateCheck.cxx
RunLoopIgnoresStateCheck.h
Transfer.cxx
Transfer.h
TransferCodeCheck.cxx

View File

@@ -14,6 +14,7 @@
#include <fairmq/tidy/PartsApiCheck.h>
#include <fairmq/tidy/ProgOptionsAccessorsCheck.h>
#include <fairmq/tidy/RunDeviceCheck.h>
#include <fairmq/tidy/RunLoopIgnoresStateCheck.h>
#include <fairmq/tidy/TransferCodeCheck.h>
#include <fairmq/tidy/UncheckedTransferResultCheck.h>
#include <fairmq/tidy/UseAfterSendCheck.h>
@@ -31,6 +32,7 @@ class Module : public clang::tidy::ClangTidyModule
void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override
{
factories.registerCheck<OnDataWithConditionalRunCheck>("fairmq-bugprone-ondata-with-conditionalrun");
factories.registerCheck<RunLoopIgnoresStateCheck>("fairmq-bugprone-run-loop-ignores-state");
factories.registerCheck<UncheckedTransferResultCheck>("fairmq-bugprone-unchecked-transfer-result");
factories.registerCheck<UseAfterSendCheck>("fairmq-bugprone-use-after-send");
factories.registerCheck<ChannelsAccessCheck>("fairmq-modernize-channels-access");

View File

@@ -0,0 +1,67 @@
/********************************************************************************
* Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/tidy/RunLoopIgnoresStateCheck.h>
#include <fairmq/tidy/Transfer.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
namespace fair::mq::tidy {
using namespace clang;
using namespace clang::ast_matchers;
namespace {
AST_MATCHER(Expr, isConstantTrue)
{
if (Node.isValueDependent()) {
return false;
}
bool value = false;
return Node.EvaluateAsBooleanCondition(value, Finder->getASTContext()) && value;
}
} // namespace
void RunLoopIgnoresStateCheck::registerMatchers(MatchFinder* finder)
{
auto const device = cxxRecordDecl(isSameOrDerivedFrom("::fair::mq::Device"));
auto const run = cxxMethodDecl(hasName("Run"), parameterCountIs(0), isOverride(), ofClass(device));
auto const watchesState = hasDescendant(cxxMemberCallExpr(callee(cxxMethodDecl(
hasAnyName("NewStatePending", "WaitFor"), ofClass(hasName("::fair::mq::Device"))))));
auto const transfers =
hasDescendant(cxxMemberCallExpr(anyOf(transferCall("Send"), transferCall("Receive"))));
auto const leaves = hasDescendant(stmt(anyOf(breakStmt(), returnStmt(), cxxThrowExpr(), gotoStmt())));
auto const endless = anyOf(whileStmt(hasCondition(expr(isConstantTrue()))),
doStmt(hasCondition(expr(isConstantTrue()))),
forStmt(unless(hasCondition(expr()))),
forStmt(hasCondition(expr(isConstantTrue()))));
finder->addMatcher(stmt(endless,
forFunction(run),
unless(watchesState),
unless(transfers),
unless(leaves),
unless(isInTemplateInstantiation()))
.bind("loop"),
this);
}
void RunLoopIgnoresStateCheck::check(MatchFinder::MatchResult const& result)
{
auto const* loop = result.Nodes.getNodeAs<Stmt>("loop");
diag(loop->getBeginLoc(),
"endless loop in 'Run()' does not check for a state change; the device does not stop "
"on request; use 'while (!NewStatePending())'");
}
} // namespace fair::mq::tidy

View File

@@ -0,0 +1,39 @@
/********************************************************************************
* Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#ifndef FAIR_MQ_TIDY_RUNLOOPIGNORESSTATECHECK_H
#define FAIR_MQ_TIDY_RUNLOOPIGNORESSTATECHECK_H
#include <clang-tidy/ClangTidyCheck.h>
namespace fair::mq::tidy {
/// Finds endless loops (`while (true)`, `for (;;)`, ...) in an override of
/// `fair::mq::Device::Run()` that do not look at the state machine. Such a
/// device does not stop when a state change is requested.
///
/// A loop is fine if it calls `NewStatePending()` or `WaitFor()`, calls
/// `Send()` or `Receive()` (an interrupt stops a transfer on a state
/// change), or has a `break`, `return`, `throw` or `goto`.
class RunLoopIgnoresStateCheck : public clang::tidy::ClangTidyCheck
{
public:
using ClangTidyCheck::ClangTidyCheck;
bool isLanguageVersionSupported(clang::LangOptions const& opts) const override
{
return opts.CPlusPlus17;
}
void registerMatchers(clang::ast_matchers::MatchFinder* finder) override;
void check(clang::ast_matchers::MatchFinder::MatchResult const& result) override;
};
} // namespace fair::mq::tidy
#endif /* FAIR_MQ_TIDY_RUNLOOPIGNORESSTATECHECK_H */