Files
sousa-gecko/build/clang-plugin/MustOverrideChecker.cpp
T
serge-sans-paille 60e367c3c4 Bug 2019521 - Move clang-tidy logic from checker to matcher r=sylvestre,firefox-static-analysis-reviewers
This should save memory and compilation time by not creating an unused
context. It also clearly conveys the pattern caught by the pass.

Concerning compilation speed, let's take the example of
`Unified_cpp_js_src_jit5.cpp`.

Without clang-plugin, it takes:         10.603 s ±  0.183 s to compile.
With clang-plugin, before this patch:   14.188 s ±  0.230 s to compile
With clang-plugin, after this patch:    14.108 s ±  0.772 s to compile

So that's roughly 3.59s spent into the plugin, down to 3.51 with this
patch, a marginal 2% speedup on the plugin efficiency.

Differential Revision: https://phabricator.services.mozilla.com/D285008
2026-03-05 09:08:52 +00:00

65 lines
2.4 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MustOverrideChecker.h"
#include "CustomMatchers.h"
void MustOverrideChecker::registerMatchers(MatchFinder *AstMatcher) {
AstMatcher->addMatcher(
cxxRecordDecl(isDefinition(), hasAnyBase(hasType(cxxRecordDecl(
hasMethod(isMarkedMustOverride())))))
.bind("class"),
this);
}
void MustOverrideChecker::registerPPCallbacks(CompilerInstance &CI) {
this->CI = &CI;
}
void MustOverrideChecker::check(const MatchFinder::MatchResult &Result) {
auto D = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
// Look through all of our immediate bases to find methods that need to be
// overridden
typedef std::vector<CXXMethodDecl *> OverridesVector;
OverridesVector MustOverrides;
for (const auto &Base : D->bases()) {
// The base is either a class (CXXRecordDecl) or it's a templated class...
CXXRecordDecl *Parent = Base.getType()
.getDesugaredType(D->getASTContext())
->getAsCXXRecordDecl();
// The parent might not be resolved to a type yet. In this case, we can't
// do any checking here. For complete correctness, we should visit
// template instantiations, but this case is likely to be rare, so we will
// ignore it until it becomes important.
if (!Parent) {
continue;
}
Parent = Parent->getDefinition();
for (const auto &M : Parent->methods()) {
if (hasCustomAttribute<moz_must_override>(M))
MustOverrides.push_back(M);
}
}
for (auto &O : MustOverrides) {
bool Overridden = false;
for (const auto &M : D->methods()) {
// The way that Clang checks if a method M overrides its parent method
// is if the method has the same name but would not overload.
if (getNameChecked(M) == getNameChecked(O) &&
!CI->getSema().IsOverload(M, O, false)) {
Overridden = true;
break;
}
}
if (!Overridden) {
diag(D->getLocation(), "%0 must override %1", DiagnosticIDs::Error)
<< D->getDeclName() << O->getDeclName();
diag(O->getLocation(), "function to override is here",
DiagnosticIDs::Note);
}
}
}