feat(tidy): add fairmq-bugprone-ondata-with-conditionalrun

- Report devices that register a data callback with OnData() and also
  override ConditionalRun(). With data callbacks, Device::RunWrapper()
  runs only the callbacks, so ConditionalRun() is dead code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-09 23:14:01 +00:00
parent b113f0a7d8
commit 42952bfa42
6 changed files with 150 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
/********************************************************************************
* 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/OnDataWithConditionalRunCheck.h>
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
namespace fair::mq::tidy {
using namespace clang;
using namespace clang::ast_matchers;
void OnDataWithConditionalRunCheck::registerMatchers(MatchFinder* finder)
{
auto const device = cxxRecordDecl(isDerivedFrom("::fair::mq::Device"));
finder->addMatcher(
cxxMethodDecl(hasName("ConditionalRun"), isOverride(), ofClass(device.bind("class")))
.bind("conditionalRun"),
this);
// OnData() on the device itself, in any member function of the device.
finder->addMatcher(
cxxMemberCallExpr(
callee(cxxMethodDecl(hasName("OnData"), ofClass(hasName("::fair::mq::Device")))),
on(ignoringImpCasts(cxxThisExpr())),
hasAncestor(cxxMethodDecl(ofClass(device.bind("class")))),
unless(isInTemplateInstantiation()))
.bind("onData"),
this);
}
void OnDataWithConditionalRunCheck::check(MatchFinder::MatchResult const& result)
{
auto const* cls = result.Nodes.getNodeAs<CXXRecordDecl>("class")->getCanonicalDecl();
if (auto const* run = result.Nodes.getNodeAs<CXXMethodDecl>("conditionalRun")) {
// prefer the definition, if any
if (!fConditionalRuns.count(cls) || run->isThisDeclarationADefinition()) {
fConditionalRuns[cls] = run;
}
} else if (auto const* call = result.Nodes.getNodeAs<Expr>("onData")) {
fOnDataCalls.insert({cls, call});
}
}
void OnDataWithConditionalRunCheck::onEndOfTranslationUnit()
{
for (auto const& [cls, run] : fConditionalRuns) {
auto const it = fOnDataCalls.find(cls);
if (it == fOnDataCalls.end()) {
continue;
}
diag(run->getLocation(),
"%0 overrides 'ConditionalRun()' and registers a data callback with 'OnData()'; "
"'ConditionalRun()' never runs")
<< cls;
diag(it->second->getExprLoc(), "data callback registered here", DiagnosticIDs::Note);
}
fConditionalRuns.clear();
fOnDataCalls.clear();
}
} // namespace fair::mq::tidy