From 83d0e741f5f77e0bb494e76a32143741b7a0f8fc 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-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 --- docs/src/content/docs/tools/fairmq-tidy.mdx | 9 ++ fairmq/tidy/CMakeLists.txt | 2 + fairmq/tidy/Module.cxx | 2 + fairmq/tidy/PartsApiCheck.cxx | 98 +++++++++++++++++++++ fairmq/tidy/PartsApiCheck.h | 35 ++++++++ test/tidy/fairmq-modernize-parts-api.cxx | 21 +++++ 6 files changed, 167 insertions(+) create mode 100644 fairmq/tidy/PartsApiCheck.cxx create mode 100644 fairmq/tidy/PartsApiCheck.h create mode 100644 test/tidy/fairmq-modernize-parts-api.cxx diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index f50eb926..c122520a 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -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 `` | 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` | +| `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=`. @@ -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: diff --git a/fairmq/tidy/CMakeLists.txt b/fairmq/tidy/CMakeLists.txt index 6a753c43..baf4e341 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -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 diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index f787f0fd..ac15ce70 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -27,6 +28,7 @@ class Module : public clang::tidy::ClangTidyModule factories.registerCheck("fairmq-modernize-deprecated-functions"); factories.registerCheck("fairmq-modernize-deprecated-headers"); factories.registerCheck("fairmq-modernize-nonnamespaced-types"); + factories.registerCheck("fairmq-modernize-parts-api"); } }; diff --git a/fairmq/tidy/PartsApiCheck.cxx b/fairmq/tidy/PartsApiCheck.cxx new file mode 100644 index 00000000..a522d577 --- /dev/null +++ b/fairmq/tidy/PartsApiCheck.cxx @@ -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 + +#include +#include +#include + +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("atRef")) { + auto const* callee = dyn_cast(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() || node.get()) { + continue; + } + outer = node.get(); + 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("addPart")) { + auto const* arg = result.Nodes.getNodeAs("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 diff --git a/fairmq/tidy/PartsApiCheck.h b/fairmq/tidy/PartsApiCheck.h new file mode 100644 index 00000000..ce0db092 --- /dev/null +++ b/fairmq/tidy/PartsApiCheck.h @@ -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 + +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 */ diff --git a/test/tidy/fairmq-modernize-parts-api.cxx b/test/tidy/fairmq-modernize-parts-api.cxx new file mode 100644 index 00000000..88d921fb --- /dev/null +++ b/test/tidy/fairmq-modernize-parts-api.cxx @@ -0,0 +1,21 @@ +#include +#include + +#include + +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; +}