From 84ac7aecfdc831f9ee9e0d2c8b518a78af7ff789 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 22:39:03 +0000 Subject: [PATCH] feat(tidy): add fairmq-modernize-channels-access - Report uses of the deprecated member fair::mq::Device::fChannels. - Replace fChannels.at(name).at(index) with GetChannel(name, index) and every other use with GetChannels(). - Ignore uses inside fair::mq::Device itself. Co-Authored-By: Claude Opus 5.5 --- fairmq/tidy/CMakeLists.txt | 2 + fairmq/tidy/ChannelsAccessCheck.cxx | 87 +++++++++++++++++++ fairmq/tidy/ChannelsAccessCheck.h | 34 ++++++++ fairmq/tidy/Module.cxx | 2 + .../tidy/fairmq-modernize-channels-access.cxx | 51 +++++++++++ 5 files changed, 176 insertions(+) create mode 100644 fairmq/tidy/ChannelsAccessCheck.cxx create mode 100644 fairmq/tidy/ChannelsAccessCheck.h create mode 100644 test/tidy/fairmq-modernize-channels-access.cxx diff --git a/fairmq/tidy/CMakeLists.txt b/fairmq/tidy/CMakeLists.txt index 7db5b9df..424f347a 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -32,6 +32,8 @@ endif() # The fairmq-* checks as a clang-tidy module. add_library(fairmq-tidy-module OBJECT + ChannelsAccessCheck.cxx + ChannelsAccessCheck.h DeprecatedHeadersCheck.cxx DeprecatedHeadersCheck.h Module.cxx diff --git a/fairmq/tidy/ChannelsAccessCheck.cxx b/fairmq/tidy/ChannelsAccessCheck.cxx new file mode 100644 index 00000000..f357f908 --- /dev/null +++ b/fairmq/tidy/ChannelsAccessCheck.cxx @@ -0,0 +1,87 @@ +/******************************************************************************** + * 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 + +#include + +namespace fair::mq::tidy { + +using namespace clang; +using namespace clang::ast_matchers; + +void ChannelsAccessCheck::registerMatchers(MatchFinder* finder) +{ + finder->addMatcher( + memberExpr(member(fieldDecl(hasName("fChannels"), + hasDeclContext(cxxRecordDecl(hasName("::fair::mq::Device"))))), + // The Device implementation itself may use its own member. + unless(hasAncestor(cxxMethodDecl(ofClass(hasName("::fair::mq::Device"))))), + unless(isInTemplateInstantiation())) + .bind("channels"), + this); +} + +namespace { + +/// If `object` is the object of a call `object.at(arg)`, return that call. +CXXMemberCallExpr const* atCallOn(Expr const& object, ASTContext& ctx) +{ + auto const parents = ctx.getParents(object); + auto const* member = parents.size() == 1 ? parents[0].get() : nullptr; + if (!member) { + return nullptr; + } + auto const grandParents = ctx.getParents(*member); + auto const* call = grandParents.size() == 1 ? grandParents[0].get() : nullptr; + if (!call || call->getNumArgs() != 1 || !call->getMethodDecl() + || call->getMethodDecl()->getName() != "at") { + return nullptr; + } + return call; +} + +} // namespace + +void ChannelsAccessCheck::check(MatchFinder::MatchResult const& result) +{ + auto const* channels = result.Nodes.getNodeAs("channels"); + if (channels->getBeginLoc().isMacroID() || channels->getEndLoc().isMacroID()) { + return; + } + auto const text = [&](Expr const* e) { + return Lexer::getSourceText(CharSourceRange::getTokenRange(e->getSourceRange()), + *result.SourceManager, + getLangOpts()) + .str(); + }; + + // fChannels.at(name).at(index) -> GetChannel(name, index) + auto const* byName = atCallOn(*channels, *result.Context); + auto const* byIndex = byName ? atCallOn(*byName, *result.Context) : nullptr; + if (byIndex && !byIndex->getEndLoc().isMacroID()) { + std::string prefix; + if (!channels->isImplicitAccess()) { + prefix = text(channels->getBase()) + (channels->isArrow() ? "->" : "."); + } + auto const replacement = + prefix + "GetChannel(" + text(byName->getArg(0)) + ", " + text(byIndex->getArg(0)) + ")"; + diag(channels->getMemberLoc(), "'fChannels' is deprecated; use '%0'") + << replacement << FixItHint::CreateReplacement(byIndex->getSourceRange(), replacement); + return; + } + + diag(channels->getMemberLoc(), "'fChannels' is deprecated; use 'GetChannels()'") + << FixItHint::CreateReplacement(channels->getMemberLoc(), "GetChannels()"); +} + +} // namespace fair::mq::tidy diff --git a/fairmq/tidy/ChannelsAccessCheck.h b/fairmq/tidy/ChannelsAccessCheck.h new file mode 100644 index 00000000..0da642f9 --- /dev/null +++ b/fairmq/tidy/ChannelsAccessCheck.h @@ -0,0 +1,34 @@ +/******************************************************************************** + * 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_CHANNELSACCESSCHECK_H +#define FAIR_MQ_TIDY_CHANNELSACCESSCHECK_H + +#include + +namespace fair::mq::tidy { + +/// Finds uses of the deprecated data member `fair::mq::Device::fChannels`: +/// - `fChannels.at(name).at(index)` becomes `GetChannel(name, index)`, +/// - every other use of `fChannels` becomes `GetChannels()`. +class ChannelsAccessCheck : 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_CHANNELSACCESSCHECK_H */ diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index d0157d18..42c46599 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -6,6 +6,7 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ +#include #include #include @@ -21,6 +22,7 @@ class Module : public clang::tidy::ClangTidyModule public: void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override { + factories.registerCheck("fairmq-modernize-channels-access"); factories.registerCheck("fairmq-modernize-deprecated-headers"); factories.registerCheck("fairmq-modernize-nonnamespaced-types"); } diff --git a/test/tidy/fairmq-modernize-channels-access.cxx b/test/tidy/fairmq-modernize-channels-access.cxx new file mode 100644 index 00000000..e9dcdac8 --- /dev/null +++ b/test/tidy/fairmq-modernize-channels-access.cxx @@ -0,0 +1,51 @@ +#include + +#include + +struct Sampler : fair::mq::Device +{ + void InitTask() override + { + // CHECK-MESSAGES: :[[@LINE+1]]:20: warning: 'fChannels' is deprecated; use 'GetChannel("data", 0)' [fairmq-modernize-channels-access] + auto& ch = fChannels.at("data").at(0); + // CHECK-FIXES: auto& ch = GetChannel("data", 0); + // CHECK-MESSAGES: :[[@LINE+1]]:29: warning: 'fChannels' is deprecated; use 'this->GetChannel(fName, fIndex)' [fairmq-modernize-channels-access] + auto& other = this->fChannels.at(fName).at(fIndex); + // CHECK-FIXES: auto& other = this->GetChannel(fName, fIndex); + // CHECK-MESSAGES: :[[@LINE+1]]:30: warning: 'fChannels' is deprecated; use 'GetChannels()' [fairmq-modernize-channels-access] + auto const n = this->fChannels.count("data"); + // CHECK-FIXES: auto const n = this->GetChannels().count("data"); + // CHECK-MESSAGES: :[[@LINE+1]]:31: warning: 'fChannels' is deprecated; use 'GetChannels()' [fairmq-modernize-channels-access] + for (auto const& kv : fChannels) { + // CHECK-FIXES: for (auto const& kv : GetChannels()) { + static_cast(kv); + } + // CHECK-MESSAGES: :[[@LINE+1]]:21: warning: 'fChannels' is deprecated; use 'GetChannels()' [fairmq-modernize-channels-access] + auto& vec = fChannels.at("data"); + // CHECK-FIXES: auto& vec = GetChannels().at("data"); + static_cast(ch); + static_cast(other); + static_cast(n); + static_cast(vec); + } + + std::string fName{"data"}; + int fIndex{0}; +}; + +void useDevice(fair::mq::Device& device) +{ + // CHECK-MESSAGES: :[[@LINE+1]]:23: warning: 'fChannels' is deprecated; use 'device.GetChannel("data", 1)' [fairmq-modernize-channels-access] + auto& ch = device.fChannels.at("data").at(1); + // CHECK-FIXES: auto& ch = device.GetChannel("data", 1); + static_cast(ch); +} + +// The current API is not reported. +void useCurrentApi(fair::mq::Device& device) +{ + auto& ch = device.GetChannel("data", 0); + auto const n = device.GetChannels().size(); + static_cast(ch); + static_cast(n); +}