mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 21:21:37 +00:00
feat(tidy): add fairmq-performance-channel-lookup-in-loop
- Report GetChannel() and GetChannels() calls in an override of ConditionalRun() or in a loop in an override of Run(). Each call looks up the channel name in a map. - Skip the short form Send(msg, "name"): it is the usual style. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -32,6 +32,8 @@ endif()
|
||||
|
||||
# The fairmq-* checks as a clang-tidy module.
|
||||
add_library(fairmq-tidy-module OBJECT
|
||||
ChannelLookupInLoopCheck.cxx
|
||||
ChannelLookupInLoopCheck.h
|
||||
ChannelsAccessCheck.cxx
|
||||
ChannelsAccessCheck.h
|
||||
DeprecatedFunctionsCheck.cxx
|
||||
|
||||
91
fairmq/tidy/ChannelLookupInLoopCheck.cxx
Normal file
91
fairmq/tidy/ChannelLookupInLoopCheck.cxx
Normal file
@@ -0,0 +1,91 @@
|
||||
/********************************************************************************
|
||||
* 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/ChannelLookupInLoopCheck.h>
|
||||
|
||||
#include <clang/AST/ParentMapContext.h>
|
||||
#include <clang/AST/StmtCXX.h>
|
||||
#include <clang/ASTMatchers/ASTMatchFinder.h>
|
||||
#include <clang/ASTMatchers/ASTMatchers.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
using namespace clang;
|
||||
using namespace clang::ast_matchers;
|
||||
|
||||
namespace {
|
||||
|
||||
/// True if `call` runs once per iteration of a loop: it is in the condition,
|
||||
/// the increment or the body of a loop, and not in a lambda in between.
|
||||
bool inLoop(Expr const& call, ASTContext& ctx)
|
||||
{
|
||||
DynTypedNode node = DynTypedNode::create(call);
|
||||
while (true) {
|
||||
auto const parents = ctx.getParents(node);
|
||||
if (parents.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
auto const& parent = parents[0];
|
||||
auto const* stmt = node.get<Stmt>();
|
||||
if (parent.get<LambdaExpr>() || parent.get<FunctionDecl>()) {
|
||||
return false;
|
||||
} else if (parent.get<WhileStmt>() || parent.get<DoStmt>()) {
|
||||
return true;
|
||||
} else if (auto const* s = parent.get<ForStmt>()) {
|
||||
if (stmt != s->getInit()) {
|
||||
return true;
|
||||
}
|
||||
} else if (auto const* s = parent.get<CXXForRangeStmt>()) {
|
||||
if (stmt == s->getBody()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
node = parent;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void ChannelLookupInLoopCheck::registerMatchers(MatchFinder* finder)
|
||||
{
|
||||
auto const device = cxxRecordDecl(isSameOrDerivedFrom("::fair::mq::Device"));
|
||||
auto const lookup = cxxMemberCallExpr(
|
||||
callee(cxxMethodDecl(hasAnyName("GetChannel", "GetChannels"), ofClass(hasName("::fair::mq::Device")))),
|
||||
unless(isInTemplateInstantiation()));
|
||||
|
||||
finder->addMatcher(
|
||||
cxxMemberCallExpr(lookup,
|
||||
forFunction(cxxMethodDecl(hasName("ConditionalRun"), isOverride(), ofClass(device))))
|
||||
.bind("conditionalRun"),
|
||||
this);
|
||||
finder->addMatcher(
|
||||
cxxMemberCallExpr(lookup,
|
||||
forFunction(cxxMethodDecl(hasName("Run"), parameterCountIs(0), isOverride(), ofClass(device))))
|
||||
.bind("run"),
|
||||
this);
|
||||
}
|
||||
|
||||
void ChannelLookupInLoopCheck::check(MatchFinder::MatchResult const& result)
|
||||
{
|
||||
auto const* call = result.Nodes.getNodeAs<CXXMemberCallExpr>("conditionalRun");
|
||||
char const* where = "'ConditionalRun()'";
|
||||
if (!call) {
|
||||
call = result.Nodes.getNodeAs<CXXMemberCallExpr>("run");
|
||||
where = "a loop in 'Run()'";
|
||||
if (!inLoop(*call, *result.Context)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
diag(call->getExprLoc(),
|
||||
"'%0()' looks up the channel by name in each iteration of %1; look it up once, e.g. in "
|
||||
"'InitTask()', and keep a reference")
|
||||
<< call->getMethodDecl()->getName() << where;
|
||||
}
|
||||
|
||||
} // namespace fair::mq::tidy
|
||||
39
fairmq/tidy/ChannelLookupInLoopCheck.h
Normal file
39
fairmq/tidy/ChannelLookupInLoopCheck.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/********************************************************************************
|
||||
* 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_CHANNELLOOKUPINLOOPCHECK_H
|
||||
#define FAIR_MQ_TIDY_CHANNELLOOKUPINLOOPCHECK_H
|
||||
|
||||
#include <clang-tidy/ClangTidyCheck.h>
|
||||
|
||||
namespace fair::mq::tidy {
|
||||
|
||||
/// Finds calls of `GetChannel()` and `GetChannels()` that run once per
|
||||
/// iteration of the device loop: in an override of `ConditionalRun()`, or in
|
||||
/// a loop in an override of `Run()`. Each call looks up the channel name in a
|
||||
/// map. Look up the channel once, e.g. in `InitTask()`, and keep a reference.
|
||||
///
|
||||
/// The check does not report `Send(msg, "name")` and `Receive(msg, "name")`,
|
||||
/// which also look up the channel, because they are the usual short form.
|
||||
class ChannelLookupInLoopCheck : 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_CHANNELLOOKUPINLOOPCHECK_H */
|
||||
@@ -6,6 +6,7 @@
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/tidy/ChannelLookupInLoopCheck.h>
|
||||
#include <fairmq/tidy/ChannelsAccessCheck.h>
|
||||
#include <fairmq/tidy/DeprecatedFunctionsCheck.h>
|
||||
#include <fairmq/tidy/DeprecatedHeadersCheck.h>
|
||||
@@ -42,6 +43,7 @@ class Module : public clang::tidy::ClangTidyModule
|
||||
factories.registerCheck<PartsApiCheck>("fairmq-modernize-parts-api");
|
||||
factories.registerCheck<ProgOptionsAccessorsCheck>("fairmq-modernize-progoptions-accessors");
|
||||
factories.registerCheck<RunDeviceCheck>("fairmq-modernize-run-device");
|
||||
factories.registerCheck<ChannelLookupInLoopCheck>("fairmq-performance-channel-lookup-in-loop");
|
||||
factories.registerCheck<TransferCodeCheck>("fairmq-readability-transfer-code");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user