mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
feat(tidy): add fairmq-modernize-deprecated-functions
- Replace Device::GetStateName() and Device::GetTransitionName() with the free functions fair::mq::GetStateName() and GetTransitionName(). - Replace Socket::GetConstant() of the zeromq and shmem transports with fair::mq::zmq::getConstant() and include <fairmq/zeromq/Common.h>. - Report Socket::Close() without a fix: the socket closes when it is destroyed, so the call must go away, not be renamed. - Skip the fix if the object expression has side effects. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/
|
||||
| Check | Finds | Fix |
|
||||
| --- | --- | --- |
|
||||
| `fairmq-modernize-channels-access` | Uses of the deprecated member `fair::mq::Device::fChannels` | `fChannels.at(name).at(index)` becomes `GetChannel(name, index)`. Other uses become `GetChannels()`. |
|
||||
| `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` |
|
||||
|
||||
@@ -85,6 +86,17 @@ The check finds uses of `fChannels` outside of `fair::mq::Device`:
|
||||
| `fChannels.count("data")` | `GetChannels().count("data")` |
|
||||
| `for (auto& kv : fChannels)` | `for (auto& kv : GetChannels())` |
|
||||
|
||||
### fairmq-modernize-deprecated-functions
|
||||
|
||||
| Code | Fix |
|
||||
| --- | --- |
|
||||
| `Device::GetStateName(state)` | `fair::mq::GetStateName(state)` |
|
||||
| `Device::GetTransitionName(transition)` | `fair::mq::GetTransitionName(transition)` |
|
||||
| `zmq::Socket::GetConstant("pub")`, `shmem::Socket::GetConstant("pub")` | `fair::mq::zmq::getConstant("pub")`, and `#include <fairmq/zeromq/Common.h>` |
|
||||
| `socket->Close()` | No fix. The socket closes when it is destroyed. Remove the call. |
|
||||
|
||||
The check does not fix a call on an object expression with side effects, for example `makeDevice()->GetStateName(s)`.
|
||||
|
||||
## Build fairmq-tidy
|
||||
|
||||
Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ:
|
||||
|
||||
@@ -34,6 +34,8 @@ endif()
|
||||
add_library(fairmq-tidy-module OBJECT
|
||||
ChannelsAccessCheck.cxx
|
||||
ChannelsAccessCheck.h
|
||||
DeprecatedFunctionsCheck.cxx
|
||||
DeprecatedFunctionsCheck.h
|
||||
DeprecatedHeadersCheck.cxx
|
||||
DeprecatedHeadersCheck.h
|
||||
Module.cxx
|
||||
|
||||
124
fairmq/tidy/DeprecatedFunctionsCheck.cxx
Normal file
124
fairmq/tidy/DeprecatedFunctionsCheck.cxx
Normal file
@@ -0,0 +1,124 @@
|
||||
/********************************************************************************
|
||||
* 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/DeprecatedFunctionsCheck.h>
|
||||
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
#include <clang/ASTMatchers/ASTMatchers.h>
|
||||
#include <clang/Lex/Preprocessor.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
using namespace clang;
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Successor
|
||||
{
|
||||
StringRef function; // fully qualified successor
|
||||
StringRef header; // header that declares it, empty if always available
|
||||
};
|
||||
|
||||
Successor successorOf(CXXMethodDecl const& method)
|
||||
{
|
||||
auto const name = method.getName();
|
||||
if (name == "GetStateName") {
|
||||
return {"fair::mq::GetStateName", ""};
|
||||
}
|
||||
if (name == "GetTransitionName") {
|
||||
return {"fair::mq::GetTransitionName", ""};
|
||||
}
|
||||
if (name == "GetConstant") {
|
||||
return {"fair::mq::zmq::getConstant", "<fairmq/zeromq/Common.h>"};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DeprecatedFunctionsCheck::DeprecatedFunctionsCheck(StringRef name, clang::tidy::ClangTidyContext* context)
|
||||
: ClangTidyCheck(name, context)
|
||||
, fInserter(Options.getLocalOrGlobal("IncludeStyle", clang::tidy::utils::IncludeSorter::IS_LLVM),
|
||||
areDiagsSelfContained())
|
||||
{}
|
||||
|
||||
void DeprecatedFunctionsCheck::registerPPCallbacks(SourceManager const& /*sm*/,
|
||||
Preprocessor* pp,
|
||||
Preprocessor* /*moduleExpanderPP*/)
|
||||
{
|
||||
fInserter.registerPreprocessor(pp);
|
||||
}
|
||||
|
||||
void DeprecatedFunctionsCheck::storeOptions(clang::tidy::ClangTidyOptions::OptionMap& opts)
|
||||
{
|
||||
Options.store(opts, "IncludeStyle", fInserter.getStyle());
|
||||
}
|
||||
|
||||
void DeprecatedFunctionsCheck::registerMatchers(MatchFinder* finder)
|
||||
{
|
||||
auto const withSuccessor = cxxMethodDecl(
|
||||
anyOf(allOf(hasAnyName("GetStateName", "GetTransitionName"),
|
||||
ofClass(hasName("::fair::mq::Device"))),
|
||||
allOf(hasName("GetConstant"),
|
||||
ofClass(hasAnyName("::fair::mq::zmq::Socket", "::fair::mq::shmem::Socket")))));
|
||||
finder->addMatcher(
|
||||
callExpr(callee(withSuccessor.bind("method")),
|
||||
// The successors forward to the same implementation.
|
||||
unless(hasAncestor(cxxMethodDecl(ofClass(
|
||||
hasAnyName("::fair::mq::Device", "::fair::mq::zmq::Socket", "::fair::mq::shmem::Socket"))))),
|
||||
unless(isInTemplateInstantiation()))
|
||||
.bind("call"),
|
||||
this);
|
||||
|
||||
finder->addMatcher(
|
||||
cxxMemberCallExpr(callee(cxxMethodDecl(hasName("Close"),
|
||||
ofClass(isSameOrDerivedFrom("::fair::mq::Socket")))),
|
||||
unless(hasAncestor(cxxRecordDecl(isSameOrDerivedFrom("::fair::mq::Socket")))),
|
||||
unless(isInTemplateInstantiation()))
|
||||
.bind("close"),
|
||||
this);
|
||||
}
|
||||
|
||||
void DeprecatedFunctionsCheck::check(MatchFinder::MatchResult const& result)
|
||||
{
|
||||
if (auto const* close = result.Nodes.getNodeAs<CXXMemberCallExpr>("close")) {
|
||||
diag(close->getExprLoc(),
|
||||
"'Socket::Close()' is deprecated; the socket closes when it is destroyed");
|
||||
return;
|
||||
}
|
||||
|
||||
auto const* call = result.Nodes.getNodeAs<CallExpr>("call");
|
||||
auto const* method = result.Nodes.getNodeAs<CXXMethodDecl>("method");
|
||||
auto const successor = successorOf(*method);
|
||||
if (successor.function.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The callee spelling: `GetStateName`, `Device::GetStateName`,
|
||||
// `device.GetStateName` or `device->GetStateName`.
|
||||
Expr const* callee = call->getCallee()->IgnoreImplicit();
|
||||
auto diagnostic = diag(call->getBeginLoc(), "'%0' is deprecated; use '%1'")
|
||||
<< method->getQualifiedNameAsString() << successor.function;
|
||||
|
||||
if (callee->getBeginLoc().isMacroID() || callee->getEndLoc().isMacroID()) {
|
||||
return;
|
||||
}
|
||||
// Do not drop an object expression that has side effects.
|
||||
if (auto const* member = dyn_cast<MemberExpr>(callee);
|
||||
member && member->getBase()->HasSideEffects(*result.Context)) {
|
||||
return;
|
||||
}
|
||||
diagnostic << FixItHint::CreateReplacement(callee->getSourceRange(), successor.function);
|
||||
if (!successor.header.empty()) {
|
||||
diagnostic << fInserter.createIncludeInsertion(
|
||||
result.SourceManager->getFileID(call->getBeginLoc()), successor.header);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
47
fairmq/tidy/DeprecatedFunctionsCheck.h
Normal file
47
fairmq/tidy/DeprecatedFunctionsCheck.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/********************************************************************************
|
||||
* 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_DEPRECATEDFUNCTIONSCHECK_H
|
||||
#define FAIR_MQ_TIDY_DEPRECATEDFUNCTIONSCHECK_H
|
||||
|
||||
#include <clang-tidy/ClangTidyCheck.h>
|
||||
#include <clang-tidy/utils/IncludeInserter.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
/// Finds calls of deprecated static member functions that have a free
|
||||
/// function successor and calls the successor instead:
|
||||
/// - `fair::mq::Device::GetStateName()` -> `fair::mq::GetStateName()`
|
||||
/// - `fair::mq::Device::GetTransitionName()` -> `fair::mq::GetTransitionName()`
|
||||
/// - `fair::mq::{zmq,shmem}::Socket::GetConstant()` -> `fair::mq::zmq::getConstant()`
|
||||
///
|
||||
/// It also reports calls of the deprecated `fair::mq::Socket::Close()`. This
|
||||
/// one has no automatic fix: the socket closes in its destructor.
|
||||
class DeprecatedFunctionsCheck : public clang::tidy::ClangTidyCheck
|
||||
{
|
||||
public:
|
||||
DeprecatedFunctionsCheck(llvm::StringRef name, clang::tidy::ClangTidyContext* context);
|
||||
|
||||
bool isLanguageVersionSupported(clang::LangOptions const& opts) const override
|
||||
{
|
||||
return opts.CPlusPlus17;
|
||||
}
|
||||
void registerPPCallbacks(clang::SourceManager const& sm,
|
||||
clang::Preprocessor* pp,
|
||||
clang::Preprocessor* moduleExpanderPP) override;
|
||||
void registerMatchers(clang::ast_matchers::MatchFinder* finder) override;
|
||||
void check(clang::ast_matchers::MatchFinder::MatchResult const& result) override;
|
||||
void storeOptions(clang::tidy::ClangTidyOptions::OptionMap& opts) override;
|
||||
|
||||
private:
|
||||
clang::tidy::utils::IncludeInserter fInserter;
|
||||
};
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
|
||||
#endif /* FAIR_MQ_TIDY_DEPRECATEDFUNCTIONSCHECK_H */
|
||||
@@ -7,6 +7,7 @@
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/tidy/ChannelsAccessCheck.h>
|
||||
#include <fairmq/tidy/DeprecatedFunctionsCheck.h>
|
||||
#include <fairmq/tidy/DeprecatedHeadersCheck.h>
|
||||
#include <fairmq/tidy/NonNamespacedTypesCheck.h>
|
||||
|
||||
@@ -23,6 +24,7 @@ class Module : public clang::tidy::ClangTidyModule
|
||||
void addCheckFactories(clang::tidy::ClangTidyCheckFactories& factories) override
|
||||
{
|
||||
factories.registerCheck<ChannelsAccessCheck>("fairmq-modernize-channels-access");
|
||||
factories.registerCheck<DeprecatedFunctionsCheck>("fairmq-modernize-deprecated-functions");
|
||||
factories.registerCheck<DeprecatedHeadersCheck>("fairmq-modernize-deprecated-headers");
|
||||
factories.registerCheck<NonNamespacedTypesCheck>("fairmq-modernize-nonnamespaced-types");
|
||||
}
|
||||
|
||||
38
test/tidy/fairmq-modernize-deprecated-functions.cxx
Normal file
38
test/tidy/fairmq-modernize-deprecated-functions.cxx
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <fairmq/Device.h>
|
||||
#include <fairmq/States.h>
|
||||
#include <fairmq/Transports.h> // zeromq/Message.h needs operator<<(Transport)
|
||||
#include <fairmq/zeromq/Socket.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Sampler : fair::mq::Device
|
||||
{
|
||||
void InitTask() override
|
||||
{
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:31: warning: 'fair::mq::Device::GetStateName' is deprecated; use 'fair::mq::GetStateName' [fairmq-modernize-deprecated-functions]
|
||||
std::string const s = GetStateName(fair::mq::State::Ready);
|
||||
// CHECK-FIXES: std::string const s = fair::mq::GetStateName(fair::mq::State::Ready);
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:31: warning: 'fair::mq::Device::GetTransitionName' is deprecated; use 'fair::mq::GetTransitionName' [fairmq-modernize-deprecated-functions]
|
||||
std::string const t = fair::mq::Device::GetTransitionName(fair::mq::Transition::Stop);
|
||||
// CHECK-FIXES: std::string const t = fair::mq::GetTransitionName(fair::mq::Transition::Stop);
|
||||
static_cast<void>(s);
|
||||
static_cast<void>(t);
|
||||
}
|
||||
};
|
||||
|
||||
void useDevice(fair::mq::Device& device, fair::mq::Socket& socket)
|
||||
{
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:23: warning: 'fair::mq::Device::GetStateName' is deprecated; use 'fair::mq::GetStateName' [fairmq-modernize-deprecated-functions]
|
||||
auto const name = device.GetStateName(device.GetCurrentState());
|
||||
// CHECK-FIXES: auto const name = fair::mq::GetStateName(device.GetCurrentState());
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:24: warning: 'fair::mq::zmq::Socket::GetConstant' is deprecated; use 'fair::mq::zmq::getConstant' [fairmq-modernize-deprecated-functions]
|
||||
int const linger = fair::mq::zmq::Socket::GetConstant("linger");
|
||||
// CHECK-FIXES: int const linger = fair::mq::zmq::getConstant("linger");
|
||||
// CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'Socket::Close()' is deprecated; the socket closes when it is destroyed [fairmq-modernize-deprecated-functions]
|
||||
socket.Close();
|
||||
static_cast<void>(name);
|
||||
static_cast<void>(linger);
|
||||
}
|
||||
|
||||
// The successors are not reported.
|
||||
std::string const current = fair::mq::GetStateName(fair::mq::State::Idle);
|
||||
Reference in New Issue
Block a user