From 3b3446bfcbfb6d89228ed66b2ccc5670b8265f81 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 23:14:00 +0000 Subject: [PATCH] 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 --- docs/src/content/docs/tools/fairmq-tidy.mdx | 24 +++++++- fairmq/tidy/CMakeLists.txt | 4 ++ fairmq/tidy/Module.cxx | 2 + fairmq/tidy/Transfer.cxx | 57 +++++++++++++++++++ fairmq/tidy/Transfer.h | 45 +++++++++++++++ fairmq/tidy/UncheckedTransferResultCheck.cxx | 53 +++++++++++++++++ fairmq/tidy/UncheckedTransferResultCheck.h | 44 ++++++++++++++ ...rmq-bugprone-unchecked-transfer-result.cxx | 26 +++++++++ 8 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 fairmq/tidy/Transfer.cxx create mode 100644 fairmq/tidy/Transfer.h create mode 100644 fairmq/tidy/UncheckedTransferResultCheck.cxx create mode 100644 fairmq/tidy/UncheckedTransferResultCheck.h create mode 100644 test/tidy/fairmq-bugprone-unchecked-transfer-result.cxx diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index 16e3f70c..14e418d5 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -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 `` | The new header, for example `` | @@ -130,6 +131,27 @@ The check moves a device main file from `` to ` #include #include +#include #include #include @@ -26,6 +27,7 @@ class Module : public clang::tidy::ClangTidyModule public: void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override { + factories.registerCheck("fairmq-bugprone-unchecked-transfer-result"); factories.registerCheck("fairmq-modernize-channels-access"); factories.registerCheck("fairmq-modernize-deprecated-functions"); factories.registerCheck("fairmq-modernize-deprecated-headers"); diff --git a/fairmq/tidy/Transfer.cxx b/fairmq/tidy/Transfer.cxx new file mode 100644 index 00000000..4bee933c --- /dev/null +++ b/fairmq/tidy/Transfer.cxx @@ -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 + +#include +#include +#include + +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() || parent.get()) { + node = parent; + continue; + } + auto const* stmt = node.get(); + if (parent.get() || parent.get() || parent.get() + || parent.get() || parent.get()) { + return true; + } + if (auto const* s = parent.get()) { + return stmt == s->getThen() || stmt == s->getElse(); + } + if (auto const* s = parent.get()) { + return stmt == s->getBody(); + } + if (auto const* s = parent.get()) { + return stmt == s->getBody(); + } + if (auto const* s = parent.get()) { + return stmt == s->getBody() || stmt == s->getInc(); + } + if (auto const* s = parent.get()) { + return stmt == s->getBody(); + } + return false; + } +} + +} // namespace fair::mq::tidy diff --git a/fairmq/tidy/Transfer.h b/fairmq/tidy/Transfer.h new file mode 100644 index 00000000..ef465d9a --- /dev/null +++ b/fairmq/tidy/Transfer.h @@ -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 +#include +#include + +#include + +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 */ diff --git a/fairmq/tidy/UncheckedTransferResultCheck.cxx b/fairmq/tidy/UncheckedTransferResultCheck.cxx new file mode 100644 index 00000000..1c05c803 --- /dev/null +++ b/fairmq/tidy/UncheckedTransferResultCheck.cxx @@ -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 +#include + +#include +#include + +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(): 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("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 diff --git a/fairmq/tidy/UncheckedTransferResultCheck.h b/fairmq/tidy/UncheckedTransferResultCheck.h new file mode 100644 index 00000000..6df51a3e --- /dev/null +++ b/fairmq/tidy/UncheckedTransferResultCheck.h @@ -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 + +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 */ diff --git a/test/tidy/fairmq-bugprone-unchecked-transfer-result.cxx b/test/tidy/fairmq-bugprone-unchecked-transfer-result.cxx new file mode 100644 index 00000000..7df1bd06 --- /dev/null +++ b/test/tidy/fairmq-bugprone-unchecked-transfer-result.cxx @@ -0,0 +1,26 @@ +// TIDY-CONFIG: {CheckOptions: {fairmq-bugprone-unchecked-transfer-result.CheckSend: true}} +#include + +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(Send(msg, "out")); + (void)Receive(msg, "in"); + return bytes >= 0; + } +};