2016-12-19 03:42:04 +03:00
|
|
|
/* 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"
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void MustOverrideChecker::registerMatchers(MatchFinder *AstMatcher) {
|
2016-12-19 03:42:04 +03:00
|
|
|
AstMatcher->addMatcher(cxxRecordDecl(isDefinition()).bind("class"), this);
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:27:02 +03:00
|
|
|
void MustOverrideChecker::registerCompilerInstance(CompilerInstance &CI) {
|
2016-12-19 03:42:04 +03:00
|
|
|
this->CI = &CI;
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void MustOverrideChecker::check(const MatchFinder::MatchResult &Result) {
|
2016-12-19 03:42:04 +03:00
|
|
|
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;
|
2017-10-20 20:11:50 +03:00
|
|
|
for (const auto &Base : D->bases()) {
|
2016-12-19 03:42:04 +03:00
|
|
|
// 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();
|
2017-10-20 20:11:50 +03:00
|
|
|
for (const auto &M : Parent->methods()) {
|
2018-09-18 16:03:33 +03:00
|
|
|
if (hasCustomAttribute<moz_must_override>(M))
|
2016-12-19 03:42:04 +03:00
|
|
|
MustOverrides.push_back(M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
for (auto &O : MustOverrides) {
|
2016-12-19 03:42:04 +03:00
|
|
|
bool Overridden = false;
|
2017-10-20 20:11:50 +03:00
|
|
|
for (const auto &M : D->methods()) {
|
2016-12-19 03:42:04 +03:00
|
|
|
// 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) {
|
2017-10-20 20:11:50 +03:00
|
|
|
diag(D->getLocation(), "%0 must override %1", DiagnosticIDs::Error)
|
|
|
|
<< D->getDeclName() << O->getDeclName();
|
2016-12-19 03:42:04 +03:00
|
|
|
diag(O->getLocation(), "function to override is here",
|
|
|
|
DiagnosticIDs::Note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|