feat(tidy): add fairmq-readability-transfer-code

- Replace -1, -2 and -3 in == and != comparisons with the result of
  Send() or Receive() by static_cast<int64_t>(fair::mq::TransferCode::X).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-09 23:14:01 +00:00
parent e9afe76155
commit b113f0a7d8
6 changed files with 141 additions and 0 deletions

View File

@@ -49,6 +49,8 @@ add_library(fairmq-tidy-module OBJECT
RunDeviceCheck.h
Transfer.cxx
Transfer.h
TransferCodeCheck.cxx
TransferCodeCheck.h
UncheckedTransferResultCheck.cxx
UncheckedTransferResultCheck.h
UseAfterSendCheck.cxx

View File

@@ -13,6 +13,7 @@
#include <fairmq/tidy/PartsApiCheck.h>
#include <fairmq/tidy/ProgOptionsAccessorsCheck.h>
#include <fairmq/tidy/RunDeviceCheck.h>
#include <fairmq/tidy/TransferCodeCheck.h>
#include <fairmq/tidy/UncheckedTransferResultCheck.h>
#include <fairmq/tidy/UseAfterSendCheck.h>
@@ -37,6 +38,7 @@ class Module : public clang::tidy::ClangTidyModule
factories.registerCheck<PartsApiCheck>("fairmq-modernize-parts-api");
factories.registerCheck<ProgOptionsAccessorsCheck>("fairmq-modernize-progoptions-accessors");
factories.registerCheck<RunDeviceCheck>("fairmq-modernize-run-device");
factories.registerCheck<TransferCodeCheck>("fairmq-readability-transfer-code");
}
};

View File

@@ -0,0 +1,58 @@
/********************************************************************************
* 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/TransferCodeCheck.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;
void TransferCodeCheck::registerMatchers(MatchFinder* finder)
{
auto const transfer = expr(ignoringParenImpCasts(
cxxMemberCallExpr(anyOf(transferCall("Send"), transferCall("Receive")))));
auto const negativeLiteral = expr(ignoringParenImpCasts(
unaryOperator(hasOperatorName("-"), hasUnaryOperand(integerLiteral().bind("literal")))
.bind("code")));
finder->addMatcher(binaryOperator(hasAnyOperatorName("==", "!="),
hasOperands(transfer, negativeLiteral),
unless(isInTemplateInstantiation())),
this);
}
void TransferCodeCheck::check(MatchFinder::MatchResult const& result)
{
auto const* code = result.Nodes.getNodeAs<UnaryOperator>("code");
auto const* literal = result.Nodes.getNodeAs<IntegerLiteral>("literal");
char const* name = nullptr;
switch (literal->getValue().getLimitedValue()) {
case 1: name = "error"; break;
case 2: name = "timeout"; break;
case 3: name = "interrupted"; break;
default: return;
}
auto diagnostic = diag(code->getBeginLoc(), "magic number -%0 for 'fair::mq::TransferCode::%1'")
<< static_cast<unsigned>(literal->getValue().getLimitedValue()) << name;
if (code->getBeginLoc().isMacroID() || code->getEndLoc().isMacroID()) {
return;
}
diagnostic << FixItHint::CreateReplacement(
code->getSourceRange(),
(llvm::Twine("static_cast<int64_t>(fair::mq::TransferCode::") + name + ")").str());
}
} // namespace fair::mq::tidy

View File

@@ -0,0 +1,37 @@
/********************************************************************************
* 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_TRANSFERCODECHECK_H
#define FAIR_MQ_TIDY_TRANSFERCODECHECK_H
#include <clang-tidy/ClangTidyCheck.h>
namespace fair::mq::tidy {
/// Finds comparisons of the result of `Send()` or `Receive()` with the
/// magic numbers -1, -2 and -3 and uses `fair::mq::TransferCode` instead:
///
/// Receive(msg, "data") == -2
/// -> Receive(msg, "data") == static_cast<int64_t>(fair::mq::TransferCode::timeout)
class TransferCodeCheck : 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;
};
} // namespace fair::mq::tidy
#endif /* FAIR_MQ_TIDY_TRANSFERCODECHECK_H */