From 67fd7f2305f959394daac0c081da9030928b34cd 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-progoptions-accessors - Replace GetValue(key) with GetProperty(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 --- docs/src/content/docs/tools/fairmq-tidy.mdx | 11 +++ fairmq/tidy/CMakeLists.txt | 2 + fairmq/tidy/Module.cxx | 2 + fairmq/tidy/ProgOptionsAccessorsCheck.cxx | 70 +++++++++++++++++++ fairmq/tidy/ProgOptionsAccessorsCheck.h | 40 +++++++++++ ...fairmq-modernize-progoptions-accessors.cxx | 23 ++++++ 6 files changed, 148 insertions(+) create mode 100644 fairmq/tidy/ProgOptionsAccessorsCheck.cxx create mode 100644 fairmq/tidy/ProgOptionsAccessorsCheck.h create mode 100644 test/tidy/fairmq-modernize-progoptions-accessors.cxx diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index c122520a..69b844b6 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -38,6 +38,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/ | `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)` | +| `fairmq-modernize-progoptions-accessors` | Calls of the deprecated accessors `ProgOptions::GetValue()`, `GetStringValue()` and `SetValue()` | `GetProperty()`, `GetPropertyAsString()` and `SetProperty()` | 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=`. @@ -106,6 +107,16 @@ The check does not fix a call on an object expression with side effects, for exa | `auto& msg = parts.AtRef(i);` | `auto& msg = *parts.At(i);` | | `parts.AddPart(rawPtr)` | `parts.AddPart(fair::mq::MessagePtr(rawPtr))` | +### fairmq-modernize-progoptions-accessors + +| Code | Fix | +| --- | --- | +| `config->GetValue("rate")` | `config->GetProperty("rate", {})` | +| `config->GetStringValue("id")` | `config->GetPropertyAsString("id", {})` | +| `config->SetValue("rate", 5);` | `config->SetProperty("rate", 5);` | + +For a key that does not exist, the old accessors return a default-constructed value, and `GetProperty()` throws. The default `{}` keeps the old behavior. If you know that the key exists, remove the `{}`. The check fixes `SetValue()` only if the code does not use its result, because `SetProperty()` returns `void`. + ## 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 baf4e341..f3e7443e 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -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 diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index ac15ce70..5175a568 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,7 @@ class Module : public clang::tidy::ClangTidyModule factories.registerCheck("fairmq-modernize-deprecated-headers"); factories.registerCheck("fairmq-modernize-nonnamespaced-types"); factories.registerCheck("fairmq-modernize-parts-api"); + factories.registerCheck("fairmq-modernize-progoptions-accessors"); } }; diff --git a/fairmq/tidy/ProgOptionsAccessorsCheck.cxx b/fairmq/tidy/ProgOptionsAccessorsCheck.cxx new file mode 100644 index 00000000..fe190b84 --- /dev/null +++ b/fairmq/tidy/ProgOptionsAccessorsCheck.cxx @@ -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 + +#include +#include + +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("call"); + auto const* method = result.Nodes.getNodeAs("method"); + auto const* callee = dyn_cast(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("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 diff --git a/fairmq/tidy/ProgOptionsAccessorsCheck.h b/fairmq/tidy/ProgOptionsAccessorsCheck.h new file mode 100644 index 00000000..e965ea19 --- /dev/null +++ b/fairmq/tidy/ProgOptionsAccessorsCheck.h @@ -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 + +namespace fair::mq::tidy { + +/// Finds calls of the old `fair::mq::ProgOptions` accessors and calls the +/// property API instead: +/// - `GetValue(key)` -> `GetProperty(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 */ diff --git a/test/tidy/fairmq-modernize-progoptions-accessors.cxx b/test/tidy/fairmq-modernize-progoptions-accessors.cxx new file mode 100644 index 00000000..33c79a22 --- /dev/null +++ b/test/tidy/fairmq-modernize-progoptions-accessors.cxx @@ -0,0 +1,23 @@ +#include + +#include + +void configure(fair::mq::ProgOptions& config, fair::mq::ProgOptions* cfg) +{ + // CHECK-MESSAGES: :[[@LINE+1]]:29: warning: 'ProgOptions::GetValue()' is an old accessor; use 'GetProperty()' [fairmq-modernize-progoptions-accessors] + int const rate = config.GetValue("rate"); + // CHECK-FIXES: int const rate = config.GetProperty("rate", {}); + // CHECK-MESSAGES: :[[@LINE+1]]:35: warning: 'ProgOptions::GetStringValue()' is an old accessor; use 'GetPropertyAsString()' [fairmq-modernize-progoptions-accessors] + std::string const name = cfg->GetStringValue("name"); + // CHECK-FIXES: std::string const name = cfg->GetPropertyAsString("name", {}); + // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'ProgOptions::SetValue()' is an old accessor; use 'SetProperty()' [fairmq-modernize-progoptions-accessors] + config.SetValue("rate", rate + 1); + // CHECK-FIXES: config.SetProperty("rate", rate + 1); + // The result of SetValue() is used: report, but no fix. + // CHECK-MESSAGES: :[[@LINE+1]]:27: warning: 'ProgOptions::SetValue()' is an old accessor; use 'SetProperty()' [fairmq-modernize-progoptions-accessors] + int const rc = config.SetValue("name", name); + // CHECK-FIXES: int const rc = config.SetValue("name", name); + // The property API is not reported. + config.SetProperty("rc", rc); + static_cast(config.GetProperty("rc")); +}