mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <fairmq/tidy/DeprecatedFunctionsCheck.h>
|
||||
#include <fairmq/tidy/DeprecatedHeadersCheck.h>
|
||||
#include <fairmq/tidy/NonNamespacedTypesCheck.h>
|
||||
#include <fairmq/tidy/OnDataWithConditionalRunCheck.h>
|
||||
#include <fairmq/tidy/PartsApiCheck.h>
|
||||
#include <fairmq/tidy/ProgOptionsAccessorsCheck.h>
|
||||
#include <fairmq/tidy/RunDeviceCheck.h>
|
||||
@@ -29,6 +30,7 @@ class Module : public clang::tidy::ClangTidyModule
|
||||
public:
|
||||
void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override
|
||||
{
|
||||
factories.registerCheck<OnDataWithConditionalRunCheck>("fairmq-bugprone-ondata-with-conditionalrun");
|
||||
factories.registerCheck<UncheckedTransferResultCheck>("fairmq-bugprone-unchecked-transfer-result");
|
||||
factories.registerCheck<UseAfterSendCheck>("fairmq-bugprone-use-after-send");
|
||||
factories.registerCheck<ChannelsAccessCheck>("fairmq-modernize-channels-access");
|
||||
|
||||
69
fairmq/tidy/OnDataWithConditionalRunCheck.cxx
Normal file
69
fairmq/tidy/OnDataWithConditionalRunCheck.cxx
Normal file
@@ -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 <fairmq/tidy/OnDataWithConditionalRunCheck.h>
|
||||
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
#include <clang/ASTMatchers/ASTMatchers.h>
|
||||
|
||||
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<CXXRecordDecl>("class")->getCanonicalDecl();
|
||||
if (auto const* run = result.Nodes.getNodeAs<CXXMethodDecl>("conditionalRun")) {
|
||||
// prefer the definition, if any
|
||||
if (!fConditionalRuns.count(cls) || run->isThisDeclarationADefinition()) {
|
||||
fConditionalRuns[cls] = run;
|
||||
}
|
||||
} else if (auto const* call = result.Nodes.getNodeAs<Expr>("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
|
||||
40
fairmq/tidy/OnDataWithConditionalRunCheck.h
Normal file
40
fairmq/tidy/OnDataWithConditionalRunCheck.h
Normal file
@@ -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 <clang-tidy/ClangTidyCheck.h>
|
||||
#include <llvm/ADT/MapVector.h>
|
||||
|
||||
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<clang::CXXRecordDecl const*, clang::CXXMethodDecl const*> fConditionalRuns;
|
||||
llvm::MapVector<clang::CXXRecordDecl const*, clang::Expr const*> fOnDataCalls;
|
||||
};
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
|
||||
#endif /* FAIR_MQ_TIDY_ONDATAWITHCONDITIONALRUNCHECK_H */
|
||||
32
test/tidy/fairmq-bugprone-ondata-with-conditionalrun.cxx
Normal file
32
test/tidy/fairmq-bugprone-ondata-with-conditionalrun.cxx
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <fairmq/Device.h>
|
||||
|
||||
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; }
|
||||
};
|
||||
Reference in New Issue
Block a user