mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
87
fairmq/tidy/ChannelsAccessCheck.cxx
Normal file
87
fairmq/tidy/ChannelsAccessCheck.cxx
Normal file
@@ -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 <fairmq/tidy/ChannelsAccessCheck.h>
|
||||
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
#include <clang/ASTMatchers/ASTMatchers.h>
|
||||
#include <clang/Lex/Lexer.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
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<MemberExpr>() : nullptr;
|
||||
if (!member) {
|
||||
return nullptr;
|
||||
}
|
||||
auto const grandParents = ctx.getParents(*member);
|
||||
auto const* call = grandParents.size() == 1 ? grandParents[0].get<CXXMemberCallExpr>() : 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<MemberExpr>("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
|
||||
34
fairmq/tidy/ChannelsAccessCheck.h
Normal file
34
fairmq/tidy/ChannelsAccessCheck.h
Normal file
@@ -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 <clang-tidy/ClangTidyCheck.h>
|
||||
|
||||
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 */
|
||||
@@ -6,6 +6,7 @@
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/tidy/ChannelsAccessCheck.h>
|
||||
#include <fairmq/tidy/DeprecatedHeadersCheck.h>
|
||||
#include <fairmq/tidy/NonNamespacedTypesCheck.h>
|
||||
|
||||
@@ -21,6 +22,7 @@ class Module : public clang::tidy::ClangTidyModule
|
||||
public:
|
||||
void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override
|
||||
{
|
||||
factories.registerCheck<ChannelsAccessCheck>("fairmq-modernize-channels-access");
|
||||
factories.registerCheck<DeprecatedHeadersCheck>("fairmq-modernize-deprecated-headers");
|
||||
factories.registerCheck<NonNamespacedTypesCheck>("fairmq-modernize-nonnamespaced-types");
|
||||
}
|
||||
|
||||
51
test/tidy/fairmq-modernize-channels-access.cxx
Normal file
51
test/tidy/fairmq-modernize-channels-access.cxx
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <fairmq/Device.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
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<void>(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<void>(ch);
|
||||
static_cast<void>(other);
|
||||
static_cast<void>(n);
|
||||
static_cast<void>(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<void>(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<void>(ch);
|
||||
static_cast<void>(n);
|
||||
}
|
||||
Reference in New Issue
Block a user