Files
sousa-gecko/build/clang-plugin/EmptyBasesChecker.cpp
T
serge-sans-paille a55c1eb7b8 Bug 2039238 - Enforce usage of MOZ_EMPTY_BASES r=tsmith
Unlike VS, we're not constrained by legacy behavior and should always
turn this attribute on where it's needed. There's no VS flag for it, but
we can use clang-tidy to detect when it's needed.

It's okay to use that attribute when it has no effect, so don't bother
trying to be smart in dom/bindings/Codegen.py and always add it.

Differential Revision: https://phabricator.services.mozilla.com/D300299
2026-05-14 14:09:57 +00:00

43 lines
1.2 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 "EmptyBasesChecker.h"
#include "CustomMatchers.h"
namespace clang {
namespace ast_matchers {
AST_MATCHER_P(CXXRecordDecl, emptyBaseCountAtLeast, unsigned, N) {
unsigned NumEmptyBases = 0;
for(const auto & Base : Node.bases()) {
if(const Type* BaseType = Base.getType().getTypePtrOrNull()) {
if(const CXXRecordDecl* CRD = BaseType->getAsCXXRecordDecl(); CRD && CRD->isEmpty())
NumEmptyBases += 1;
}
}
return NumEmptyBases >= N;
}
AST_MATCHER(CXXRecordDecl, hasMozEmptyBasesAttr) {
return Node.hasAttr<EmptyBasesAttr>();
}
}
}
void EmptyBasesChecker::registerMatchers(MatchFinder *AstMatcher) {
AstMatcher->addMatcher(
cxxRecordDecl(isDefinition(), isFirstParty(), emptyBaseCountAtLeast(2), unless(hasMozEmptyBasesAttr()))
.bind("class"),
this);
}
void EmptyBasesChecker::check(const MatchFinder::MatchResult &Result) {
const CXXRecordDecl *Cls = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
diag(Cls->getBeginLoc(),
"Missing MOZ_EMPTY_BASES",
DiagnosticIDs::Error);
}