From 397501718363e785db2972329e5a87c65a98080f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 22:57:09 +0000 Subject: [PATCH] feat(tidy): add fairmq-modernize-deprecated-functions - Replace Device::GetStateName() and Device::GetTransitionName() with the free functions fair::mq::GetStateName() and GetTransitionName(). - Replace Socket::GetConstant() of the zeromq and shmem transports with fair::mq::zmq::getConstant() and include . - Report Socket::Close() without a fix: the socket closes when it is destroyed, so the call must go away, not be renamed. - Skip the fix if the object expression has side effects. Co-Authored-By: Claude Opus 5.5 --- docs/src/content/docs/tools/fairmq-tidy.mdx | 12 ++ fairmq/tidy/CMakeLists.txt | 2 + fairmq/tidy/DeprecatedFunctionsCheck.cxx | 124 ++++++++++++++++++ fairmq/tidy/DeprecatedFunctionsCheck.h | 47 +++++++ fairmq/tidy/Module.cxx | 2 + .../fairmq-modernize-deprecated-functions.cxx | 38 ++++++ 6 files changed, 225 insertions(+) create mode 100644 fairmq/tidy/DeprecatedFunctionsCheck.cxx create mode 100644 fairmq/tidy/DeprecatedFunctionsCheck.h create mode 100644 test/tidy/fairmq-modernize-deprecated-functions.cxx diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index 898d289e..f50eb926 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -34,6 +34,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/ | Check | Finds | 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 `` | | `fairmq-modernize-nonnamespaced-types` | Uses of the deprecated global type aliases, for example `FairMQDevice` or `InputMsgCallback` | The namespaced type, for example `fair::mq::Device` | @@ -85,6 +86,17 @@ The check finds uses of `fChannels` outside of `fair::mq::Device`: | `fChannels.count("data")` | `GetChannels().count("data")` | | `for (auto& kv : fChannels)` | `for (auto& kv : GetChannels())` | +### fairmq-modernize-deprecated-functions + +| Code | Fix | +| --- | --- | +| `Device::GetStateName(state)` | `fair::mq::GetStateName(state)` | +| `Device::GetTransitionName(transition)` | `fair::mq::GetTransitionName(transition)` | +| `zmq::Socket::GetConstant("pub")`, `shmem::Socket::GetConstant("pub")` | `fair::mq::zmq::getConstant("pub")`, and `#include ` | +| `socket->Close()` | No fix. The socket closes when it is destroyed. Remove the call. | + +The check does not fix a call on an object expression with side effects, for example `makeDevice()->GetStateName(s)`. + ## 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 266c221b..6a753c43 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -34,6 +34,8 @@ endif() add_library(fairmq-tidy-module OBJECT ChannelsAccessCheck.cxx ChannelsAccessCheck.h + DeprecatedFunctionsCheck.cxx + DeprecatedFunctionsCheck.h DeprecatedHeadersCheck.cxx DeprecatedHeadersCheck.h Module.cxx diff --git a/fairmq/tidy/DeprecatedFunctionsCheck.cxx b/fairmq/tidy/DeprecatedFunctionsCheck.cxx new file mode 100644 index 00000000..093c42ce --- /dev/null +++ b/fairmq/tidy/DeprecatedFunctionsCheck.cxx @@ -0,0 +1,124 @@ +/******************************************************************************** + * 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; + +namespace { + +struct Successor +{ + StringRef function; // fully qualified successor + StringRef header; // header that declares it, empty if always available +}; + +Successor successorOf(CXXMethodDecl const& method) +{ + auto const name = method.getName(); + if (name == "GetStateName") { + return {"fair::mq::GetStateName", ""}; + } + if (name == "GetTransitionName") { + return {"fair::mq::GetTransitionName", ""}; + } + if (name == "GetConstant") { + return {"fair::mq::zmq::getConstant", ""}; + } + return {}; +} + +} // namespace + +DeprecatedFunctionsCheck::DeprecatedFunctionsCheck(StringRef name, clang::tidy::ClangTidyContext* context) + : ClangTidyCheck(name, context) + , fInserter(Options.getLocalOrGlobal("IncludeStyle", clang::tidy::utils::IncludeSorter::IS_LLVM), + areDiagsSelfContained()) +{} + +void DeprecatedFunctionsCheck::registerPPCallbacks(SourceManager const& /*sm*/, + Preprocessor* pp, + Preprocessor* /*moduleExpanderPP*/) +{ + fInserter.registerPreprocessor(pp); +} + +void DeprecatedFunctionsCheck::storeOptions(clang::tidy::ClangTidyOptions::OptionMap& opts) +{ + Options.store(opts, "IncludeStyle", fInserter.getStyle()); +} + +void DeprecatedFunctionsCheck::registerMatchers(MatchFinder* finder) +{ + auto const withSuccessor = cxxMethodDecl( + anyOf(allOf(hasAnyName("GetStateName", "GetTransitionName"), + ofClass(hasName("::fair::mq::Device"))), + allOf(hasName("GetConstant"), + ofClass(hasAnyName("::fair::mq::zmq::Socket", "::fair::mq::shmem::Socket"))))); + finder->addMatcher( + callExpr(callee(withSuccessor.bind("method")), + // The successors forward to the same implementation. + unless(hasAncestor(cxxMethodDecl(ofClass( + hasAnyName("::fair::mq::Device", "::fair::mq::zmq::Socket", "::fair::mq::shmem::Socket"))))), + unless(isInTemplateInstantiation())) + .bind("call"), + this); + + finder->addMatcher( + cxxMemberCallExpr(callee(cxxMethodDecl(hasName("Close"), + ofClass(isSameOrDerivedFrom("::fair::mq::Socket")))), + unless(hasAncestor(cxxRecordDecl(isSameOrDerivedFrom("::fair::mq::Socket")))), + unless(isInTemplateInstantiation())) + .bind("close"), + this); +} + +void DeprecatedFunctionsCheck::check(MatchFinder::MatchResult const& result) +{ + if (auto const* close = result.Nodes.getNodeAs("close")) { + diag(close->getExprLoc(), + "'Socket::Close()' is deprecated; the socket closes when it is destroyed"); + return; + } + + auto const* call = result.Nodes.getNodeAs("call"); + auto const* method = result.Nodes.getNodeAs("method"); + auto const successor = successorOf(*method); + if (successor.function.empty()) { + return; + } + + // The callee spelling: `GetStateName`, `Device::GetStateName`, + // `device.GetStateName` or `device->GetStateName`. + Expr const* callee = call->getCallee()->IgnoreImplicit(); + auto diagnostic = diag(call->getBeginLoc(), "'%0' is deprecated; use '%1'") + << method->getQualifiedNameAsString() << successor.function; + + if (callee->getBeginLoc().isMacroID() || callee->getEndLoc().isMacroID()) { + return; + } + // Do not drop an object expression that has side effects. + if (auto const* member = dyn_cast(callee); + member && member->getBase()->HasSideEffects(*result.Context)) { + return; + } + diagnostic << FixItHint::CreateReplacement(callee->getSourceRange(), successor.function); + if (!successor.header.empty()) { + diagnostic << fInserter.createIncludeInsertion( + result.SourceManager->getFileID(call->getBeginLoc()), successor.header); + } +} + +} // namespace fair::mq::tidy diff --git a/fairmq/tidy/DeprecatedFunctionsCheck.h b/fairmq/tidy/DeprecatedFunctionsCheck.h new file mode 100644 index 00000000..6a9729a0 --- /dev/null +++ b/fairmq/tidy/DeprecatedFunctionsCheck.h @@ -0,0 +1,47 @@ +/******************************************************************************** + * 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_DEPRECATEDFUNCTIONSCHECK_H +#define FAIR_MQ_TIDY_DEPRECATEDFUNCTIONSCHECK_H + +#include +#include + +namespace fair::mq::tidy { + +/// Finds calls of deprecated static member functions that have a free +/// function successor and calls the successor instead: +/// - `fair::mq::Device::GetStateName()` -> `fair::mq::GetStateName()` +/// - `fair::mq::Device::GetTransitionName()` -> `fair::mq::GetTransitionName()` +/// - `fair::mq::{zmq,shmem}::Socket::GetConstant()` -> `fair::mq::zmq::getConstant()` +/// +/// It also reports calls of the deprecated `fair::mq::Socket::Close()`. This +/// one has no automatic fix: the socket closes in its destructor. +class DeprecatedFunctionsCheck : public clang::tidy::ClangTidyCheck +{ + public: + DeprecatedFunctionsCheck(llvm::StringRef name, clang::tidy::ClangTidyContext* context); + + bool isLanguageVersionSupported(clang::LangOptions const& opts) const override + { + return opts.CPlusPlus17; + } + void registerPPCallbacks(clang::SourceManager const& sm, + clang::Preprocessor* pp, + clang::Preprocessor* moduleExpanderPP) override; + void registerMatchers(clang::ast_matchers::MatchFinder* finder) override; + void check(clang::ast_matchers::MatchFinder::MatchResult const& result) override; + void storeOptions(clang::tidy::ClangTidyOptions::OptionMap& opts) override; + + private: + clang::tidy::utils::IncludeInserter fInserter; +}; + +} // namespace fair::mq::tidy + +#endif /* FAIR_MQ_TIDY_DEPRECATEDFUNCTIONSCHECK_H */ diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index 42c46599..f787f0fd 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -7,6 +7,7 @@ ********************************************************************************/ #include +#include #include #include @@ -23,6 +24,7 @@ class Module : public clang::tidy::ClangTidyModule void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override { factories.registerCheck("fairmq-modernize-channels-access"); + factories.registerCheck("fairmq-modernize-deprecated-functions"); factories.registerCheck("fairmq-modernize-deprecated-headers"); factories.registerCheck("fairmq-modernize-nonnamespaced-types"); } diff --git a/test/tidy/fairmq-modernize-deprecated-functions.cxx b/test/tidy/fairmq-modernize-deprecated-functions.cxx new file mode 100644 index 00000000..025bc09a --- /dev/null +++ b/test/tidy/fairmq-modernize-deprecated-functions.cxx @@ -0,0 +1,38 @@ +#include +#include +#include // zeromq/Message.h needs operator<<(Transport) +#include + +#include + +struct Sampler : fair::mq::Device +{ + void InitTask() override + { + // CHECK-MESSAGES: :[[@LINE+1]]:31: warning: 'fair::mq::Device::GetStateName' is deprecated; use 'fair::mq::GetStateName' [fairmq-modernize-deprecated-functions] + std::string const s = GetStateName(fair::mq::State::Ready); + // CHECK-FIXES: std::string const s = fair::mq::GetStateName(fair::mq::State::Ready); + // CHECK-MESSAGES: :[[@LINE+1]]:31: warning: 'fair::mq::Device::GetTransitionName' is deprecated; use 'fair::mq::GetTransitionName' [fairmq-modernize-deprecated-functions] + std::string const t = fair::mq::Device::GetTransitionName(fair::mq::Transition::Stop); + // CHECK-FIXES: std::string const t = fair::mq::GetTransitionName(fair::mq::Transition::Stop); + static_cast(s); + static_cast(t); + } +}; + +void useDevice(fair::mq::Device& device, fair::mq::Socket& socket) +{ + // CHECK-MESSAGES: :[[@LINE+1]]:23: warning: 'fair::mq::Device::GetStateName' is deprecated; use 'fair::mq::GetStateName' [fairmq-modernize-deprecated-functions] + auto const name = device.GetStateName(device.GetCurrentState()); + // CHECK-FIXES: auto const name = fair::mq::GetStateName(device.GetCurrentState()); + // CHECK-MESSAGES: :[[@LINE+1]]:24: warning: 'fair::mq::zmq::Socket::GetConstant' is deprecated; use 'fair::mq::zmq::getConstant' [fairmq-modernize-deprecated-functions] + int const linger = fair::mq::zmq::Socket::GetConstant("linger"); + // CHECK-FIXES: int const linger = fair::mq::zmq::getConstant("linger"); + // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'Socket::Close()' is deprecated; the socket closes when it is destroyed [fairmq-modernize-deprecated-functions] + socket.Close(); + static_cast(name); + static_cast(linger); +} + +// The successors are not reported. +std::string const current = fair::mq::GetStateName(fair::mq::State::Idle);