From 42952bfa42f2f8cc5a6765e1f9908436f8e580a0 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 23:14:01 +0000 Subject: [PATCH] feat(tidy): add fairmq-bugprone-ondata-with-conditionalrun - Report devices that register a data callback with OnData() and also override ConditionalRun(). With data callbacks, Device::RunWrapper() runs only the callbacks, so ConditionalRun() is dead code. Co-Authored-By: Claude Opus 5.5 --- docs/src/content/docs/tools/fairmq-tidy.mdx | 5 ++ fairmq/tidy/CMakeLists.txt | 2 + fairmq/tidy/Module.cxx | 2 + fairmq/tidy/OnDataWithConditionalRunCheck.cxx | 69 +++++++++++++++++++ fairmq/tidy/OnDataWithConditionalRunCheck.h | 40 +++++++++++ ...mq-bugprone-ondata-with-conditionalrun.cxx | 32 +++++++++ 6 files changed, 150 insertions(+) create mode 100644 fairmq/tidy/OnDataWithConditionalRunCheck.cxx create mode 100644 fairmq/tidy/OnDataWithConditionalRunCheck.h create mode 100644 test/tidy/fairmq-bugprone-ondata-with-conditionalrun.cxx diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index 46a9afc6..f7560c72 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -33,6 +33,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-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()`. | @@ -178,6 +179,10 @@ The check looks only at a `Send()` whose result the code does not use. If the co The check looks only at `==` and `!=`. See [Transfer codes](/reference/transfer-codes/). +### fairmq-bugprone-ondata-with-conditionalrun + +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). + ## 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 b05cba87..265ac4e6 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -41,6 +41,8 @@ add_library(fairmq-tidy-module OBJECT Module.cxx NonNamespacedTypesCheck.cxx NonNamespacedTypesCheck.h + OnDataWithConditionalRunCheck.cxx + OnDataWithConditionalRunCheck.h PartsApiCheck.cxx PartsApiCheck.h ProgOptionsAccessorsCheck.cxx diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index 1ed8cd04..89ad28ec 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,7 @@ class Module : public clang::tidy::ClangTidyModule public: void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override { + factories.registerCheck("fairmq-bugprone-ondata-with-conditionalrun"); 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/OnDataWithConditionalRunCheck.cxx b/fairmq/tidy/OnDataWithConditionalRunCheck.cxx new file mode 100644 index 00000000..4d803eab --- /dev/null +++ b/fairmq/tidy/OnDataWithConditionalRunCheck.cxx @@ -0,0 +1,69 @@ +/******************************************************************************** + * 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 + +namespace fair::mq::tidy { + +using namespace clang; +using namespace clang::ast_matchers; + +void OnDataWithConditionalRunCheck::registerMatchers(MatchFinder* finder) +{ + auto const device = cxxRecordDecl(isDerivedFrom("::fair::mq::Device")); + + finder->addMatcher( + cxxMethodDecl(hasName("ConditionalRun"), isOverride(), ofClass(device.bind("class"))) + .bind("conditionalRun"), + this); + + // OnData() on the device itself, in any member function of the device. + finder->addMatcher( + cxxMemberCallExpr( + callee(cxxMethodDecl(hasName("OnData"), ofClass(hasName("::fair::mq::Device")))), + on(ignoringImpCasts(cxxThisExpr())), + hasAncestor(cxxMethodDecl(ofClass(device.bind("class")))), + unless(isInTemplateInstantiation())) + .bind("onData"), + this); +} + +void OnDataWithConditionalRunCheck::check(MatchFinder::MatchResult const& result) +{ + auto const* cls = result.Nodes.getNodeAs("class")->getCanonicalDecl(); + if (auto const* run = result.Nodes.getNodeAs("conditionalRun")) { + // prefer the definition, if any + if (!fConditionalRuns.count(cls) || run->isThisDeclarationADefinition()) { + fConditionalRuns[cls] = run; + } + } else if (auto const* call = result.Nodes.getNodeAs("onData")) { + fOnDataCalls.insert({cls, call}); + } +} + +void OnDataWithConditionalRunCheck::onEndOfTranslationUnit() +{ + for (auto const& [cls, run] : fConditionalRuns) { + auto const it = fOnDataCalls.find(cls); + if (it == fOnDataCalls.end()) { + continue; + } + diag(run->getLocation(), + "%0 overrides 'ConditionalRun()' and registers a data callback with 'OnData()'; " + "'ConditionalRun()' never runs") + << cls; + diag(it->second->getExprLoc(), "data callback registered here", DiagnosticIDs::Note); + } + fConditionalRuns.clear(); + fOnDataCalls.clear(); +} + +} // namespace fair::mq::tidy diff --git a/fairmq/tidy/OnDataWithConditionalRunCheck.h b/fairmq/tidy/OnDataWithConditionalRunCheck.h new file mode 100644 index 00000000..91faff4d --- /dev/null +++ b/fairmq/tidy/OnDataWithConditionalRunCheck.h @@ -0,0 +1,40 @@ +/******************************************************************************** + * 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_ONDATAWITHCONDITIONALRUNCHECK_H +#define FAIR_MQ_TIDY_ONDATAWITHCONDITIONALRUNCHECK_H + +#include +#include + +namespace fair::mq::tidy { + +/// Finds devices that register a data callback with `OnData()` and also +/// override `ConditionalRun()`. A device with data callbacks runs only the +/// callbacks, so `ConditionalRun()` never runs. +class OnDataWithConditionalRunCheck : 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; + void onEndOfTranslationUnit() override; + + private: + llvm::MapVector fConditionalRuns; + llvm::MapVector fOnDataCalls; +}; + +} // namespace fair::mq::tidy + +#endif /* FAIR_MQ_TIDY_ONDATAWITHCONDITIONALRUNCHECK_H */ diff --git a/test/tidy/fairmq-bugprone-ondata-with-conditionalrun.cxx b/test/tidy/fairmq-bugprone-ondata-with-conditionalrun.cxx new file mode 100644 index 00000000..46efb0c9 --- /dev/null +++ b/test/tidy/fairmq-bugprone-ondata-with-conditionalrun.cxx @@ -0,0 +1,32 @@ +#include + +struct Both : fair::mq::Device +{ + Both() { OnData("data", &Both::HandleData); } + // CHECK-MESSAGES: :[[@LINE+1]]:10: warning: 'Both' overrides 'ConditionalRun()' and registers a data callback with 'OnData()'; 'ConditionalRun()' never runs [fairmq-bugprone-ondata-with-conditionalrun] + bool ConditionalRun() override { return true; } + bool HandleData(fair::mq::MessagePtr&, int) { return true; } +}; + +struct Lambda : fair::mq::Device +{ + void InitTask() override + { + OnData("data", [](fair::mq::MessagePtr&, int) { return true; }); + } + bool ConditionalRun() override; +}; +// CHECK-MESSAGES: :[[@LINE+1]]:14: warning: 'Lambda' overrides 'ConditionalRun()' and registers a data callback with 'OnData()'; 'ConditionalRun()' never runs [fairmq-bugprone-ondata-with-conditionalrun] +bool Lambda::ConditionalRun() { return true; } + +// Not reported: only one of the two. +struct OnlyCallback : fair::mq::Device +{ + OnlyCallback() { OnData("data", &OnlyCallback::HandleData); } + bool HandleData(fair::mq::MessagePtr&, int) { return true; } +}; + +struct OnlyConditionalRun : fair::mq::Device +{ + bool ConditionalRun() override { return true; } +};