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:
Claude
2026-11-09 23:14:00 +00:00
parent 64ec4d7afa
commit 3b3446bfcb
8 changed files with 254 additions and 1 deletions

View File

@@ -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

View File

@@ -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
View 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
View 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 */

View 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

View 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 */