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

@@ -37,6 +37,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/
| `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 `<FairMQDevice.h>` | The new header, for example `<fairmq/Device.h>` |
| `fairmq-modernize-nonnamespaced-types` | Uses of the deprecated global type aliases, for example `FairMQDevice` or `InputMsgCallback` | The namespaced type, for example `fair::mq::Device` |
| `fairmq-modernize-parts-api` | Calls of `Parts::AtRef()` and of `Parts::AddPart(Message*)` | `At()` with `->` or `*`, and `MessagePtr(p)` |
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=<regex>`.
@@ -97,6 +98,14 @@ The check finds uses of `fChannels` outside of `fair::mq::Device`:
The check does not fix a call on an object expression with side effects, for example `makeDevice()->GetStateName(s)`.
### fairmq-modernize-parts-api
| Code | Fix |
| --- | --- |
| `parts.AtRef(i).GetSize()` | `parts.At(i)->GetSize()` |
| `auto& msg = parts.AtRef(i);` | `auto& msg = *parts.At(i);` |
| `parts.AddPart(rawPtr)` | `parts.AddPart(fair::mq::MessagePtr(rawPtr))` |
## Build fairmq-tidy
Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ:

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 */

View File

@@ -0,0 +1,21 @@
#include <fairmq/Message.h>
#include <fairmq/Parts.h>
#include <cstddef>
std::size_t sizes(fair::mq::Parts& parts, fair::mq::Message* raw)
{
// CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'Parts::AddPart(Message*)' is deprecated; pass a 'fair::mq::MessagePtr' [fairmq-modernize-parts-api]
parts.AddPart(raw);
// CHECK-FIXES: parts.AddPart(fair::mq::MessagePtr(raw));
// CHECK-MESSAGES: :[[@LINE+1]]:33: warning: 'Parts::AtRef()' is deprecated; use 'Parts::At()' [fairmq-modernize-parts-api]
std::size_t const a = parts.AtRef(0).GetSize();
// CHECK-FIXES: std::size_t const a = parts.At(0)->GetSize();
// CHECK-MESSAGES: :[[@LINE+1]]:38: warning: 'Parts::AtRef()' is deprecated; use 'Parts::At()' [fairmq-modernize-parts-api]
fair::mq::Message& first = parts.AtRef(0);
// CHECK-FIXES: fair::mq::Message& first = *parts.At(0);
// The current API is not reported.
parts.AddPart(fair::mq::MessagePtr(nullptr));
std::size_t const b = parts.At(0)->GetSize() + first.GetSize();
return a + b;
}