diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index f7560c72..3b20f7d3 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -34,6 +34,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/ | Check | Finds | Fix | | --- | --- | --- | | `fairmq-bugprone-ondata-with-conditionalrun` | Devices that register a data callback with `OnData()` and also override `ConditionalRun()` | No fix | +| `fairmq-bugprone-run-loop-ignores-state` | Endless loops in `Run()` that do not look at the state machine | No fix | | `fairmq-bugprone-unchecked-transfer-result` | Calls of `Receive()` whose result is not used. With the option `CheckSend`, also calls of `Send()`. | No fix | | `fairmq-bugprone-use-after-send` | Uses of a `MessagePtr` after a `Send()` whose result is not used | No fix | | `fairmq-modernize-channels-access` | Uses of the deprecated member `fair::mq::Device::fChannels` | `fChannels.at(name).at(index)` becomes `GetChannel(name, index)`. Other uses become `GetChannels()`. | @@ -183,6 +184,16 @@ The check looks only at `==` and `!=`. See [Transfer codes](/reference/transfer- A device with a data callback runs only its callbacks in the state Running. It never calls `ConditionalRun()` and `Run()`. The check reports the override of `ConditionalRun()`, with a note at the call of `OnData()`. See [Data callbacks](/concepts/devices/#data-callbacks). +### fairmq-bugprone-run-loop-ignores-state + +A device leaves the state Running only when `Run()` returns. An endless loop in an override of `Device::Run()`, for example `while (true)` or `for (;;)`, must stop on a state change request. The check reports the loop if it has none of these: + +- a call of `NewStatePending()` or `WaitFor()`, +- a call of `Send()` or `Receive()`. A state change interrupts the transfer. +- a `break`, `return`, `throw` or `goto`. + +Use `while (!NewStatePending())` as the loop condition. + ## Build fairmq-tidy Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ: diff --git a/fairmq/tidy/CMakeLists.txt b/fairmq/tidy/CMakeLists.txt index 265ac4e6..984cc18e 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -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 diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index 89ad28ec..b3e18b85 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ class Module : public clang::tidy::ClangTidyModule void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override { factories.registerCheck("fairmq-bugprone-ondata-with-conditionalrun"); + factories.registerCheck("fairmq-bugprone-run-loop-ignores-state"); factories.registerCheck("fairmq-bugprone-unchecked-transfer-result"); factories.registerCheck("fairmq-bugprone-use-after-send"); factories.registerCheck("fairmq-modernize-channels-access"); diff --git a/fairmq/tidy/RunLoopIgnoresStateCheck.cxx b/fairmq/tidy/RunLoopIgnoresStateCheck.cxx new file mode 100644 index 00000000..537b8987 --- /dev/null +++ b/fairmq/tidy/RunLoopIgnoresStateCheck.cxx @@ -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 +#include + +#include +#include + +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("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 diff --git a/fairmq/tidy/RunLoopIgnoresStateCheck.h b/fairmq/tidy/RunLoopIgnoresStateCheck.h new file mode 100644 index 00000000..03fe1c69 --- /dev/null +++ b/fairmq/tidy/RunLoopIgnoresStateCheck.h @@ -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 + +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 */ diff --git a/test/tidy/fairmq-bugprone-run-loop-ignores-state.cxx b/test/tidy/fairmq-bugprone-run-loop-ignores-state.cxx new file mode 100644 index 00000000..be9e0805 --- /dev/null +++ b/test/tidy/fairmq-bugprone-run-loop-ignores-state.cxx @@ -0,0 +1,55 @@ +#include + +#include + +struct Endless : fair::mq::Device +{ + void Run() override + { + int n = 0; + // CHECK-MESSAGES: :[[@LINE+1]]:9: warning: endless loop in 'Run()' does not check for a state change; the device does not stop on request; use 'while (!NewStatePending())' [fairmq-bugprone-run-loop-ignores-state] + while (true) { + ++n; + } + // CHECK-MESSAGES: :[[@LINE+1]]:9: warning: endless loop in 'Run()' does not check for a state change; the device does not stop on request; use 'while (!NewStatePending())' [fairmq-bugprone-run-loop-ignores-state] + for (;;) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } +}; + +// Not reported: the loops look at the state, transfer data or end. +struct Fine : fair::mq::Device +{ + void Run() override + { + while (!NewStatePending()) { + } + while (true) { + if (NewStatePending()) { + break; + } + } + for (;;) { + if (!WaitFor(std::chrono::seconds(1))) { + return; + } + } + while (1) { + auto msg(NewMessage()); + Send(msg, "data"); + } + for (int i = 0; i < 10; ++i) { + } + } +}; + +// Not reported: not Run() of a device. +struct Other +{ + void Run() + { + while (true) { + } + } +};