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:
Claude
2026-11-09 22:57:09 +00:00
parent 75fd9622e2
commit 3975017183
6 changed files with 225 additions and 0 deletions

View File

@@ -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

View 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

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

View File

@@ -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");
}