feat(tidy): add fairmq-modernize-parts-api

- Replace Parts::AtRef(i) with Parts::At(i). At() returns a reference
  to the MessagePtr, so a following '.' becomes '->', and a lone call
  gets a leading '*'.
- Wrap the raw pointer of Parts::AddPart(Message*) in
  fair::mq::MessagePtr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-09 22:57:09 +00:00
parent 3975017183
commit 83d0e741f5
6 changed files with 167 additions and 0 deletions

View File

@@ -41,6 +41,8 @@ add_library(fairmq-tidy-module OBJECT
Module.cxx
NonNamespacedTypesCheck.cxx
NonNamespacedTypesCheck.h
PartsApiCheck.cxx
PartsApiCheck.h
)
target_compile_features(fairmq-tidy-module PUBLIC cxx_std_17)
target_include_directories(fairmq-tidy-module

View File

@@ -10,6 +10,7 @@
#include <fairmq/tidy/DeprecatedFunctionsCheck.h>
#include <fairmq/tidy/DeprecatedHeadersCheck.h>
#include <fairmq/tidy/NonNamespacedTypesCheck.h>
#include <fairmq/tidy/PartsApiCheck.h>
#include <clang-tidy/ClangTidyModule.h>
#include <clang-tidy/ClangTidyModuleRegistry.h>
@@ -27,6 +28,7 @@ class Module : public clang::tidy::ClangTidyModule
factories.registerCheck<DeprecatedFunctionsCheck>("fairmq-modernize-deprecated-functions");
factories.registerCheck<DeprecatedHeadersCheck>("fairmq-modernize-deprecated-headers");
factories.registerCheck<NonNamespacedTypesCheck>("fairmq-modernize-nonnamespaced-types");
factories.registerCheck<PartsApiCheck>("fairmq-modernize-parts-api");
}
};

View File

@@ -0,0 +1,98 @@
/********************************************************************************
* 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/PartsApiCheck.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Lex/Lexer.h>
namespace fair::mq::tidy {
using namespace clang;
using namespace clang::ast_matchers;
void PartsApiCheck::registerMatchers(MatchFinder* finder)
{
auto const parts = cxxRecordDecl(hasName("::fair::mq::Parts"));
auto const notInParts = unless(hasAncestor(cxxMethodDecl(ofClass(parts))));
finder->addMatcher(
cxxMemberCallExpr(callee(cxxMethodDecl(hasName("AtRef"), ofClass(parts))),
notInParts,
unless(isInTemplateInstantiation()))
.bind("atRef"),
this);
finder->addMatcher(
cxxMemberCallExpr(
callee(cxxMethodDecl(hasName("AddPart"),
ofClass(parts),
parameterCountIs(1),
hasParameter(0, parmVarDecl(hasType(pointerType()))))),
hasArgument(0, expr().bind("rawPtr")),
notInParts,
unless(isInTemplateInstantiation()))
.bind("addPart"),
this);
}
void PartsApiCheck::check(MatchFinder::MatchResult const& result)
{
auto& sm = *result.SourceManager;
if (auto const* call = result.Nodes.getNodeAs<CXXMemberCallExpr>("atRef")) {
auto const* callee = dyn_cast<MemberExpr>(call->getCallee()->IgnoreImplicit());
auto diagnostic = diag(call->getExprLoc(), "'Parts::AtRef()' is deprecated; use 'Parts::At()'");
if (!callee || call->getBeginLoc().isMacroID() || call->getEndLoc().isMacroID()) {
return;
}
diagnostic << FixItHint::CreateReplacement(callee->getMemberLoc(), "At");
// parts.AtRef(i).GetSize() -> parts.At(i)->GetSize()
// The member access can see the call through implicit nodes, e.g. a
// NoOp cast to const for a const method.
DynTypedNode node = DynTypedNode::create(*call);
MemberExpr const* outer = nullptr;
while (true) {
auto const parents = result.Context->getParents(node);
if (parents.size() != 1) {
break;
}
node = parents[0];
if (node.get<ImplicitCastExpr>() || node.get<MaterializeTemporaryExpr>()) {
continue;
}
outer = node.get<MemberExpr>();
break;
}
if (outer && !outer->isArrow() && outer->getOperatorLoc().isValid()
&& !outer->getOperatorLoc().isMacroID()) {
diagnostic << FixItHint::CreateReplacement(outer->getOperatorLoc(), "->");
} else {
// A postfix call binds tighter than a prefix '*'.
diagnostic << FixItHint::CreateInsertion(call->getBeginLoc(), "*");
}
return;
}
if (auto const* call = result.Nodes.getNodeAs<CXXMemberCallExpr>("addPart")) {
auto const* arg = result.Nodes.getNodeAs<Expr>("rawPtr");
auto diagnostic = diag(call->getExprLoc(),
"'Parts::AddPart(Message*)' is deprecated; pass a 'fair::mq::MessagePtr'");
if (arg->getBeginLoc().isMacroID() || arg->getEndLoc().isMacroID()) {
return;
}
auto const text = Lexer::getSourceText(
CharSourceRange::getTokenRange(arg->getSourceRange()), sm, getLangOpts());
diagnostic << FixItHint::CreateReplacement(
arg->getSourceRange(), ("fair::mq::MessagePtr(" + text + ")").str());
}
}
} // namespace fair::mq::tidy

View File

@@ -0,0 +1,35 @@
/********************************************************************************
* 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_PARTSAPICHECK_H
#define FAIR_MQ_TIDY_PARTSAPICHECK_H
#include <clang-tidy/ClangTidyCheck.h>
namespace fair::mq::tidy {
/// Finds calls of the deprecated `fair::mq::Parts` member functions:
/// - `parts.AtRef(i)` becomes `*parts.At(i)`, or `parts.At(i)->` before a
/// member access,
/// - `parts.AddPart(rawPtr)` becomes `parts.AddPart(fair::mq::MessagePtr(rawPtr))`.
class PartsApiCheck : 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_PARTSAPICHECK_H */