mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-26 05:31:36 +00:00
feat(tidy): add fairmq-modernize-progoptions-accessors
- Replace GetValue<T>(key) with GetProperty<T>(key, {}) and
GetStringValue(key) with GetPropertyAsString(key, {}). The {} default
keeps the old result for an unknown key, where the property API throws.
- Replace SetValue() with SetProperty() only where its always-zero
result is unused, because SetProperty() returns void.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,6 +43,8 @@ add_library(fairmq-tidy-module OBJECT
|
||||
NonNamespacedTypesCheck.h
|
||||
PartsApiCheck.cxx
|
||||
PartsApiCheck.h
|
||||
ProgOptionsAccessorsCheck.cxx
|
||||
ProgOptionsAccessorsCheck.h
|
||||
)
|
||||
target_compile_features(fairmq-tidy-module PUBLIC cxx_std_17)
|
||||
target_include_directories(fairmq-tidy-module
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <fairmq/tidy/DeprecatedHeadersCheck.h>
|
||||
#include <fairmq/tidy/NonNamespacedTypesCheck.h>
|
||||
#include <fairmq/tidy/PartsApiCheck.h>
|
||||
#include <fairmq/tidy/ProgOptionsAccessorsCheck.h>
|
||||
|
||||
#include <clang-tidy/ClangTidyModule.h>
|
||||
#include <clang-tidy/ClangTidyModuleRegistry.h>
|
||||
@@ -29,6 +30,7 @@ class Module : public clang::tidy::ClangTidyModule
|
||||
factories.registerCheck<DeprecatedHeadersCheck>("fairmq-modernize-deprecated-headers");
|
||||
factories.registerCheck<NonNamespacedTypesCheck>("fairmq-modernize-nonnamespaced-types");
|
||||
factories.registerCheck<PartsApiCheck>("fairmq-modernize-parts-api");
|
||||
factories.registerCheck<ProgOptionsAccessorsCheck>("fairmq-modernize-progoptions-accessors");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
70
fairmq/tidy/ProgOptionsAccessorsCheck.cxx
Normal file
70
fairmq/tidy/ProgOptionsAccessorsCheck.cxx
Normal file
@@ -0,0 +1,70 @@
|
||||
/********************************************************************************
|
||||
* 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/ProgOptionsAccessorsCheck.h>
|
||||
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
#include <clang/ASTMatchers/ASTMatchers.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
using namespace clang;
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
void ProgOptionsAccessorsCheck::registerMatchers(MatchFinder* finder)
|
||||
{
|
||||
auto const progOptions = cxxRecordDecl(hasName("::fair::mq::ProgOptions"));
|
||||
finder->addMatcher(
|
||||
cxxMemberCallExpr(
|
||||
callee(cxxMethodDecl(hasAnyName("GetValue", "SetValue", "GetStringValue"),
|
||||
ofClass(progOptions))
|
||||
.bind("method")),
|
||||
unless(hasAncestor(cxxMethodDecl(ofClass(progOptions)))),
|
||||
unless(isInTemplateInstantiation()),
|
||||
// The result of SetValue() is used if the call is not a statement
|
||||
// of its own.
|
||||
optionally(hasParent(compoundStmt().bind("statement"))),
|
||||
optionally(hasParent(exprWithCleanups(hasParent(compoundStmt().bind("statement"))))))
|
||||
.bind("call"),
|
||||
this);
|
||||
}
|
||||
|
||||
void ProgOptionsAccessorsCheck::check(MatchFinder::MatchResult const& result)
|
||||
{
|
||||
auto const* call = result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
|
||||
auto const* method = result.Nodes.getNodeAs<CXXMethodDecl>("method");
|
||||
auto const* callee = dyn_cast<MemberExpr>(call->getCallee()->IgnoreImplicit());
|
||||
if (!callee) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto const name = method->getName();
|
||||
StringRef const successor = name == "GetValue" ? "GetProperty"
|
||||
: name == "GetStringValue" ? "GetPropertyAsString"
|
||||
: "SetProperty";
|
||||
auto diagnostic =
|
||||
diag(callee->getMemberLoc(), "'ProgOptions::%0()' is an old accessor; use '%1()'")
|
||||
<< name << successor;
|
||||
|
||||
if (callee->getMemberLoc().isMacroID()) {
|
||||
return;
|
||||
}
|
||||
// SetProperty() returns void.
|
||||
if (name == "SetValue" && !result.Nodes.getNodeAs<CompoundStmt>("statement")) {
|
||||
return;
|
||||
}
|
||||
diagnostic << FixItHint::CreateReplacement(callee->getMemberLoc(), successor);
|
||||
// GetValue() and GetStringValue() return a default-constructed value for an
|
||||
// unknown key, GetProperty() and GetPropertyAsString() throw. Pass that
|
||||
// default explicitly to keep the behaviour.
|
||||
if (name != "SetValue" && call->getRParenLoc().isValid() && !call->getRParenLoc().isMacroID()) {
|
||||
diagnostic << FixItHint::CreateInsertion(call->getRParenLoc(), ", {}");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
40
fairmq/tidy/ProgOptionsAccessorsCheck.h
Normal file
40
fairmq/tidy/ProgOptionsAccessorsCheck.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/********************************************************************************
|
||||
* 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_PROGOPTIONSACCESSORSCHECK_H
|
||||
#define FAIR_MQ_TIDY_PROGOPTIONSACCESSORSCHECK_H
|
||||
|
||||
#include <clang-tidy/ClangTidyCheck.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
/// Finds calls of the old `fair::mq::ProgOptions` accessors and calls the
|
||||
/// property API instead:
|
||||
/// - `GetValue<T>(key)` -> `GetProperty<T>(key, {})`
|
||||
/// - `GetStringValue(key)` -> `GetPropertyAsString(key, {})`
|
||||
/// - `SetValue(key, value)` -> `SetProperty(key, value)` (only if the
|
||||
/// always-zero result of `SetValue` is not used)
|
||||
///
|
||||
/// The `{}` default keeps the behaviour for an unknown key: the old accessors
|
||||
/// return a default-constructed value, the property API throws without it.
|
||||
class ProgOptionsAccessorsCheck : 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_PROGOPTIONSACCESSORSCHECK_H */
|
||||
Reference in New Issue
Block a user