diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index ecf6e522..46a9afc6 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -42,6 +42,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/ | `fairmq-modernize-parts-api` | Calls of `Parts::AtRef()` and of `Parts::AddPart(Message*)` | `At()` with `->` or `*`, and `MessagePtr(p)` | | `fairmq-modernize-progoptions-accessors` | Calls of the deprecated accessors `ProgOptions::GetValue()`, `GetStringValue()` and `SetValue()` | `GetProperty()`, `GetPropertyAsString()` and `SetProperty()` | | `fairmq-modernize-run-device` | A `getDevice()` for `` that returns a raw pointer | ``, `std::unique_ptr` and `std::make_unique` | +| `fairmq-readability-transfer-code` | Comparisons of the result of `Send()` or `Receive()` with `-1`, `-2` or `-3` | `static_cast(fair::mq::TransferCode::...)` | The checks report a name in a macro, but they do not fix it. Like all clang-tidy checks, they show findings in headers only if the header matches `--header-filter=`. @@ -167,6 +168,16 @@ The check follows the control flow of the function, like the upstream check `bug The check looks only at a `Send()` whose result the code does not use. If the code checks the result, the send can have failed, and a use of the message can be correct. +### fairmq-readability-transfer-code + +| Code | Fix | +| --- | --- | +| `Receive(msg, "data") == -2` | `Receive(msg, "data") == static_cast(fair::mq::TransferCode::timeout)` | +| `Send(msg, "data") != -1` | `Send(msg, "data") != static_cast(fair::mq::TransferCode::error)` | +| `-3 == ch.Send(msg)` | `static_cast(fair::mq::TransferCode::interrupted) == ch.Send(msg)` | + +The check looks only at `==` and `!=`. See [Transfer codes](/reference/transfer-codes/). + ## Build fairmq-tidy Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ: diff --git a/fairmq/tidy/CMakeLists.txt b/fairmq/tidy/CMakeLists.txt index 329e6c62..b05cba87 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -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 diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index 4cdc9c32..1ed8cd04 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -37,6 +38,7 @@ class Module : public clang::tidy::ClangTidyModule factories.registerCheck("fairmq-modernize-parts-api"); factories.registerCheck("fairmq-modernize-progoptions-accessors"); factories.registerCheck("fairmq-modernize-run-device"); + factories.registerCheck("fairmq-readability-transfer-code"); } }; diff --git a/fairmq/tidy/TransferCodeCheck.cxx b/fairmq/tidy/TransferCodeCheck.cxx new file mode 100644 index 00000000..66464f86 --- /dev/null +++ b/fairmq/tidy/TransferCodeCheck.cxx @@ -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 +#include + +#include +#include + +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("code"); + auto const* literal = result.Nodes.getNodeAs("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(literal->getValue().getLimitedValue()) << name; + if (code->getBeginLoc().isMacroID() || code->getEndLoc().isMacroID()) { + return; + } + diagnostic << FixItHint::CreateReplacement( + code->getSourceRange(), + (llvm::Twine("static_cast(fair::mq::TransferCode::") + name + ")").str()); +} + +} // namespace fair::mq::tidy diff --git a/fairmq/tidy/TransferCodeCheck.h b/fairmq/tidy/TransferCodeCheck.h new file mode 100644 index 00000000..6f9743e6 --- /dev/null +++ b/fairmq/tidy/TransferCodeCheck.h @@ -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 + +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(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 */ diff --git a/test/tidy/fairmq-readability-transfer-code.cxx b/test/tidy/fairmq-readability-transfer-code.cxx new file mode 100644 index 00000000..0992ce91 --- /dev/null +++ b/test/tidy/fairmq-readability-transfer-code.cxx @@ -0,0 +1,31 @@ +#include + +#include + +struct Sink : fair::mq::Device +{ + bool ConditionalRun() override + { + auto msg(NewMessage()); + auto const result = Receive(msg, "data"); + // CHECK-MESSAGES: :[[@LINE+1]]:45: warning: magic number -2 for 'fair::mq::TransferCode::timeout' [fairmq-readability-transfer-code] + if (Receive(msg, "data", 0, 100) == -2) { + // CHECK-FIXES: if (Receive(msg, "data", 0, 100) == static_cast(fair::mq::TransferCode::timeout)) { + return true; + } + // CHECK-MESSAGES: :[[@LINE+1]]:13: warning: magic number -3 for 'fair::mq::TransferCode::interrupted' [fairmq-readability-transfer-code] + if (-3 != GetChannel("data").Send(msg)) { + // CHECK-FIXES: if (static_cast(fair::mq::TransferCode::interrupted) != GetChannel("data").Send(msg)) { + return true; + } + // CHECK-MESSAGES: :[[@LINE+1]]:38: warning: magic number -1 for 'fair::mq::TransferCode::error' [fairmq-readability-transfer-code] + return Send(msg, "data") != (-1); + // CHECK-FIXES: return Send(msg, "data") != (static_cast(fair::mq::TransferCode::error)); + + // Not reported: other numbers, other operators, other functions. + static_cast(result < 0); + static_cast(Send(msg, "data") == -4); + static_cast(Send(msg, "data") < -1); + static_cast(msg->GetSize() == 0); + } +};