mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
feat(tidy): add fairmq-bugprone-unchecked-transfer-result
- Report Receive() calls whose result is discarded. On a timeout, an error or an interrupt the message stays empty, and only the result tells. - Report Send() too with the option CheckSend: true. Off by default, because many devices ignore the result of Send() on purpose. - A cast to void discards the result on purpose and is not reported. - Add Transfer.h with matchers for Send() and Receive() of Device, Channel and Socket, and isDiscarded(), for this and later checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,7 +9,7 @@ code: [fairmq/tidy/Module.cxx, fairmq/tidy/runTidy.cxx, fairmq/tidy/runClangd.cx
|
||||
import Snippet from '../../../components/Snippet.astro';
|
||||
import { Steps } from '@astrojs/starlight/components';
|
||||
|
||||
`fairmq-tidy` is a static analysis tool for code that uses FairMQ. It is `clang-tidy` with the `fairmq-*` checks built in. The checks find deprecated FairMQ names and APIs in your code, and most checks also give a fix that the tool can apply.
|
||||
`fairmq-tidy` is a static analysis tool for code that uses FairMQ. It is `clang-tidy` with the `fairmq-*` checks built in. The checks find deprecated FairMQ names and APIs and wrong uses of the FairMQ API in your code. Most `modernize` checks also give a fix that the tool can apply.
|
||||
|
||||
The same checks are also available in two other forms:
|
||||
|
||||
@@ -33,6 +33,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/
|
||||
|
||||
| Check | Finds | 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-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()`. |
|
||||
| `fairmq-modernize-deprecated-functions` | Calls of deprecated functions: `Device::GetStateName()`, `Device::GetTransitionName()`, `Socket::GetConstant()`, `Socket::Close()` | The free function, for example `fair::mq::GetStateName()`. No fix for `Close()`. |
|
||||
| `fairmq-modernize-deprecated-headers` | `#include` lines of the deprecated headers, for example `<FairMQDevice.h>` | The new header, for example `<fairmq/Device.h>` |
|
||||
@@ -130,6 +131,27 @@ The check moves a device main file from `<runFairMQDevice.h>` to `<fairmq/runDev
|
||||
|
||||
The check changes all of these lines or none. If a `return` statement of `getDevice()` does not return a `new` expression, the check reports the function without a fix.
|
||||
|
||||
### fairmq-bugprone-unchecked-transfer-result
|
||||
|
||||
`Receive()` returns a negative [transfer code](/reference/transfer-codes/) on a timeout, an error or an interrupt. Then the message is empty. The check reports a call of `Receive()` of `Device`, `Channel` or `Socket` if the code does not use the result:
|
||||
|
||||
```cpp
|
||||
Receive(msg, "data"); // reported
|
||||
if (Receive(msg, "data") < 0) // not reported
|
||||
(void)Receive(msg, "data"); // not reported: the cast tells that you ignore the result
|
||||
```
|
||||
|
||||
| Option | Default | Description |
|
||||
| --- | --- | --- |
|
||||
| `CheckSend` | `false` | Also report `Send()`. Many devices do not use the result of `Send()`, so the default is off. |
|
||||
|
||||
Set the option in the `.clang-tidy` file:
|
||||
|
||||
```yaml
|
||||
CheckOptions:
|
||||
fairmq-bugprone-unchecked-transfer-result.CheckSend: true
|
||||
```
|
||||
|
||||
## Build fairmq-tidy
|
||||
|
||||
Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ:
|
||||
|
||||
@@ -47,6 +47,10 @@ add_library(fairmq-tidy-module OBJECT
|
||||
ProgOptionsAccessorsCheck.h
|
||||
RunDeviceCheck.cxx
|
||||
RunDeviceCheck.h
|
||||
Transfer.cxx
|
||||
Transfer.h
|
||||
UncheckedTransferResultCheck.cxx
|
||||
UncheckedTransferResultCheck.h
|
||||
)
|
||||
target_compile_features(fairmq-tidy-module PUBLIC cxx_std_17)
|
||||
target_include_directories(fairmq-tidy-module
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <fairmq/tidy/PartsApiCheck.h>
|
||||
#include <fairmq/tidy/ProgOptionsAccessorsCheck.h>
|
||||
#include <fairmq/tidy/RunDeviceCheck.h>
|
||||
#include <fairmq/tidy/UncheckedTransferResultCheck.h>
|
||||
|
||||
#include <clang-tidy/ClangTidyModule.h>
|
||||
#include <clang-tidy/ClangTidyModuleRegistry.h>
|
||||
@@ -26,6 +27,7 @@ class Module : public clang::tidy::ClangTidyModule
|
||||
public:
|
||||
void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override
|
||||
{
|
||||
factories.registerCheck<UncheckedTransferResultCheck>("fairmq-bugprone-unchecked-transfer-result");
|
||||
factories.registerCheck<ChannelsAccessCheck>("fairmq-modernize-channels-access");
|
||||
factories.registerCheck<DeprecatedFunctionsCheck>("fairmq-modernize-deprecated-functions");
|
||||
factories.registerCheck<DeprecatedHeadersCheck>("fairmq-modernize-deprecated-headers");
|
||||
|
||||
57
fairmq/tidy/Transfer.cxx
Normal file
57
fairmq/tidy/Transfer.cxx
Normal file
@@ -0,0 +1,57 @@
|
||||
/********************************************************************************
|
||||
* 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/Transfer.h>
|
||||
|
||||
#include <clang/AST/ParentMapContext.h>
|
||||
#include <clang/AST/Stmt.h>
|
||||
#include <clang/AST/StmtCXX.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
using namespace clang;
|
||||
|
||||
bool isDiscarded(Expr const& e, ASTContext& ctx)
|
||||
{
|
||||
DynTypedNode node = DynTypedNode::create(e);
|
||||
while (true) {
|
||||
auto const parents = ctx.getParents(node);
|
||||
if (parents.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
auto const& parent = parents[0];
|
||||
if (parent.get<ExprWithCleanups>() || parent.get<ParenExpr>()) {
|
||||
node = parent;
|
||||
continue;
|
||||
}
|
||||
auto const* stmt = node.get<Stmt>();
|
||||
if (parent.get<CompoundStmt>() || parent.get<CaseStmt>() || parent.get<DefaultStmt>()
|
||||
|| parent.get<LabelStmt>() || parent.get<AttributedStmt>()) {
|
||||
return true;
|
||||
}
|
||||
if (auto const* s = parent.get<IfStmt>()) {
|
||||
return stmt == s->getThen() || stmt == s->getElse();
|
||||
}
|
||||
if (auto const* s = parent.get<WhileStmt>()) {
|
||||
return stmt == s->getBody();
|
||||
}
|
||||
if (auto const* s = parent.get<DoStmt>()) {
|
||||
return stmt == s->getBody();
|
||||
}
|
||||
if (auto const* s = parent.get<ForStmt>()) {
|
||||
return stmt == s->getBody() || stmt == s->getInc();
|
||||
}
|
||||
if (auto const* s = parent.get<CXXForRangeStmt>()) {
|
||||
return stmt == s->getBody();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
45
fairmq/tidy/Transfer.h
Normal file
45
fairmq/tidy/Transfer.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/********************************************************************************
|
||||
* 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_TRANSFER_H
|
||||
#define FAIR_MQ_TIDY_TRANSFER_H
|
||||
|
||||
#include <clang/AST/ASTContext.h>
|
||||
#include <clang/AST/Expr.h>
|
||||
#include <clang/ASTMatchers/ASTMatchers.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
/// Matches `fair::mq::Device`, `fair::mq::Channel`, `fair::mq::Socket` and
|
||||
/// classes derived from them, i.e. the classes with `Send()` and `Receive()`.
|
||||
inline auto transferClass()
|
||||
{
|
||||
using namespace clang::ast_matchers;
|
||||
return cxxRecordDecl(anyOf(isSameOrDerivedFrom("::fair::mq::Device"),
|
||||
isSameOrDerivedFrom("::fair::mq::Channel"),
|
||||
isSameOrDerivedFrom("::fair::mq::Socket")));
|
||||
}
|
||||
|
||||
/// Matches a call of `Send()` or `Receive()` (as given) of a transfer class.
|
||||
inline auto transferCall(std::string const& name)
|
||||
{
|
||||
using namespace clang::ast_matchers;
|
||||
return cxxMemberCallExpr(callee(cxxMethodDecl(hasName(name), ofClass(transferClass()))));
|
||||
}
|
||||
|
||||
/// True if the value of `e` is discarded: `e` is an expression statement or
|
||||
/// the increment of a for loop. A cast to void does not count, because it
|
||||
/// discards the value on purpose.
|
||||
bool isDiscarded(clang::Expr const& e, clang::ASTContext& ctx);
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
|
||||
#endif /* FAIR_MQ_TIDY_TRANSFER_H */
|
||||
53
fairmq/tidy/UncheckedTransferResultCheck.cxx
Normal file
53
fairmq/tidy/UncheckedTransferResultCheck.cxx
Normal file
@@ -0,0 +1,53 @@
|
||||
/********************************************************************************
|
||||
* 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/UncheckedTransferResultCheck.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;
|
||||
|
||||
UncheckedTransferResultCheck::UncheckedTransferResultCheck(llvm::StringRef name,
|
||||
clang::tidy::ClangTidyContext* context)
|
||||
: ClangTidyCheck(name, context)
|
||||
// Not Options.get<bool>(): its out-of-line specialization does not link
|
||||
// across compilers (GCC and Clang mangle its enable_if differently).
|
||||
, fCheckSend(Options.get("CheckSend", "false").equals_insensitive("true")
|
||||
|| Options.get("CheckSend", "false") == "1")
|
||||
{}
|
||||
|
||||
void UncheckedTransferResultCheck::storeOptions(clang::tidy::ClangTidyOptions::OptionMap& opts)
|
||||
{
|
||||
Options.store(opts, "CheckSend", fCheckSend ? "true" : "false");
|
||||
}
|
||||
|
||||
void UncheckedTransferResultCheck::registerMatchers(MatchFinder* finder)
|
||||
{
|
||||
auto const call = fCheckSend ? cxxMemberCallExpr(anyOf(transferCall("Receive"), transferCall("Send")))
|
||||
: cxxMemberCallExpr(transferCall("Receive"));
|
||||
finder->addMatcher(call.bind("call"), this);
|
||||
}
|
||||
|
||||
void UncheckedTransferResultCheck::check(MatchFinder::MatchResult const& result)
|
||||
{
|
||||
auto const* call = result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
|
||||
if (!isDiscarded(*call, *result.Context)) {
|
||||
return;
|
||||
}
|
||||
diag(call->getExprLoc(), "result of '%0()' is not checked; it is negative on a timeout, an "
|
||||
"error or an interrupt")
|
||||
<< call->getMethodDecl()->getName();
|
||||
}
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
44
fairmq/tidy/UncheckedTransferResultCheck.h
Normal file
44
fairmq/tidy/UncheckedTransferResultCheck.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/********************************************************************************
|
||||
* 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_UNCHECKEDTRANSFERRESULTCHECK_H
|
||||
#define FAIR_MQ_TIDY_UNCHECKEDTRANSFERRESULTCHECK_H
|
||||
|
||||
#include <clang-tidy/ClangTidyCheck.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
/// Finds calls of `Receive()` whose result is discarded. On a timeout, an
|
||||
/// error or an interrupt (e.g. a state change request) the message stays
|
||||
/// empty, and only the result tells.
|
||||
///
|
||||
/// Option `CheckSend` (default `false`): also report `Send()`. Many devices
|
||||
/// ignore the result of `Send()` on purpose, so this is off by default.
|
||||
///
|
||||
/// Cast the call to `void` to discard the result on purpose.
|
||||
class UncheckedTransferResultCheck : public clang::tidy::ClangTidyCheck
|
||||
{
|
||||
public:
|
||||
UncheckedTransferResultCheck(llvm::StringRef name, clang::tidy::ClangTidyContext* context);
|
||||
|
||||
bool isLanguageVersionSupported(clang::LangOptions const& opts) const override
|
||||
{
|
||||
return opts.CPlusPlus17;
|
||||
}
|
||||
void storeOptions(clang::tidy::ClangTidyOptions::OptionMap& opts) override;
|
||||
void registerMatchers(clang::ast_matchers::MatchFinder* finder) override;
|
||||
void check(clang::ast_matchers::MatchFinder::MatchResult const& result) override;
|
||||
|
||||
private:
|
||||
bool const fCheckSend;
|
||||
};
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
|
||||
#endif /* FAIR_MQ_TIDY_UNCHECKEDTRANSFERRESULTCHECK_H */
|
||||
26
test/tidy/fairmq-bugprone-unchecked-transfer-result.cxx
Normal file
26
test/tidy/fairmq-bugprone-unchecked-transfer-result.cxx
Normal file
@@ -0,0 +1,26 @@
|
||||
// TIDY-CONFIG: {CheckOptions: {fairmq-bugprone-unchecked-transfer-result.CheckSend: true}}
|
||||
#include <fairmq/Device.h>
|
||||
|
||||
struct Processor : fair::mq::Device
|
||||
{
|
||||
bool ConditionalRun() override
|
||||
{
|
||||
auto msg(NewMessage());
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:9: warning: result of 'Receive()' is not checked; it is negative on a timeout, an error or an interrupt [fairmq-bugprone-unchecked-transfer-result]
|
||||
Receive(msg, "in");
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:29: warning: result of 'Receive()' is not checked; it is negative on a timeout, an error or an interrupt [fairmq-bugprone-unchecked-transfer-result]
|
||||
GetChannel("in", 0).Receive(msg);
|
||||
for (int i = 0; i < 3; ++i)
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:13: warning: result of 'Send()' is not checked; it is negative on a timeout, an error or an interrupt [fairmq-bugprone-unchecked-transfer-result]
|
||||
Send(msg, "out");
|
||||
|
||||
// Not reported: the result is used, or discarded on purpose.
|
||||
if (Receive(msg, "in") < 0) {
|
||||
return false;
|
||||
}
|
||||
auto const bytes = Send(msg, "out");
|
||||
static_cast<void>(Send(msg, "out"));
|
||||
(void)Receive(msg, "in");
|
||||
return bytes >= 0;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user