CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
131 lines
4.7 KiB
C++
131 lines
4.7 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "DerivedMethodShadowingBaseMethodCheck.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
namespace clang::tidy::bugprone {
|
|
|
|
static bool sameBasicType(const ParmVarDecl *Lhs, const ParmVarDecl *Rhs) {
|
|
return Lhs && Rhs &&
|
|
Lhs->getType()
|
|
.getCanonicalType()
|
|
.getNonReferenceType()
|
|
.getUnqualifiedType() == Rhs->getType()
|
|
.getCanonicalType()
|
|
.getNonReferenceType()
|
|
.getUnqualifiedType();
|
|
}
|
|
|
|
static bool namesCollide(const CXXMethodDecl &Lhs, const CXXMethodDecl &Rhs) {
|
|
if (Lhs.getNameAsString() != Rhs.getNameAsString())
|
|
return false;
|
|
if (Lhs.isConst() != Rhs.isConst())
|
|
return false;
|
|
if (Lhs.getNumParams() != Rhs.getNumParams())
|
|
return false;
|
|
for (unsigned int It = 0; It < Lhs.getNumParams(); ++It)
|
|
if (!sameBasicType(Lhs.getParamDecl(It), Rhs.getParamDecl(It)))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
namespace {
|
|
|
|
AST_MATCHER(CXXMethodDecl, nameCollidesWithMethodInBase) {
|
|
const CXXRecordDecl *DerivedClass = Node.getParent();
|
|
for (const auto &Base : DerivedClass->bases()) {
|
|
SmallVector<const CXXBaseSpecifier *, 8> Stack;
|
|
Stack.push_back(&Base);
|
|
while (!Stack.empty()) {
|
|
const CXXBaseSpecifier *CurrentBaseSpec = Stack.back();
|
|
Stack.pop_back();
|
|
|
|
if (CurrentBaseSpec->getAccessSpecifier() == AccessSpecifier::AS_private)
|
|
continue;
|
|
|
|
const CXXRecordDecl *CurrentRecord =
|
|
CurrentBaseSpec->getType()->getAsCXXRecordDecl();
|
|
if (!CurrentRecord)
|
|
continue;
|
|
|
|
// For multiple inheritance, we ignore only the bases that come from the
|
|
// std:: namespace
|
|
if (CurrentRecord->isInStdNamespace())
|
|
continue;
|
|
|
|
for (const auto &BaseMethod : CurrentRecord->methods()) {
|
|
if (namesCollide(*BaseMethod, Node)) {
|
|
const ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
|
|
Builder->setBinding("base_method", DynTypedNode::create(*BaseMethod));
|
|
return true;
|
|
}
|
|
}
|
|
|
|
for (const auto &SubBase : CurrentRecord->bases())
|
|
Stack.push_back(&SubBase);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Same as clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp,
|
|
// similar matchers are used elsewhere in LLVM
|
|
AST_MATCHER(CXXMethodDecl, isOutOfLine) { return Node.isOutOfLine(); }
|
|
|
|
AST_MATCHER(CXXMethodDecl, isTemplate) {
|
|
return Node.getDescribedFunctionTemplate() != nullptr;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
DerivedMethodShadowingBaseMethodCheck::DerivedMethodShadowingBaseMethodCheck(
|
|
StringRef Name, ClangTidyContext *Context)
|
|
: ClangTidyCheck(Name, Context) {}
|
|
|
|
void DerivedMethodShadowingBaseMethodCheck::registerMatchers(
|
|
MatchFinder *Finder) {
|
|
Finder->addMatcher(
|
|
cxxMethodDecl(
|
|
unless(anyOf(isOutOfLine(), isStaticStorageClass(), isImplicit(),
|
|
cxxConstructorDecl(), isOverride(), isPrivate(),
|
|
// isFinal(), //included with isOverride,
|
|
// TODO: Templates are not handled yet
|
|
isTemplate(), ast_matchers::isTemplateInstantiation(),
|
|
ast_matchers::isExplicitTemplateSpecialization())),
|
|
ofClass(cxxRecordDecl(isDerivedFrom(cxxRecordDecl()))
|
|
.bind("derived_class")),
|
|
nameCollidesWithMethodInBase())
|
|
.bind("shadowing_method"),
|
|
this);
|
|
}
|
|
|
|
void DerivedMethodShadowingBaseMethodCheck::check(
|
|
const MatchFinder::MatchResult &Result) {
|
|
const auto *ShadowingMethod =
|
|
Result.Nodes.getNodeAs<CXXMethodDecl>("shadowing_method");
|
|
const auto *DerivedClass =
|
|
Result.Nodes.getNodeAs<CXXRecordDecl>("derived_class");
|
|
const auto *BaseMethod = Result.Nodes.getNodeAs<CXXMethodDecl>("base_method");
|
|
|
|
if (!ShadowingMethod || !DerivedClass || !BaseMethod)
|
|
llvm_unreachable("Required binding not found");
|
|
|
|
diag(ShadowingMethod->getBeginLoc(),
|
|
"'%0' shadows method with the same name in class %1")
|
|
<< ShadowingMethod->getQualifiedNameAsString() << BaseMethod->getParent();
|
|
diag(BaseMethod->getBeginLoc(), "previous definition of %0 is here",
|
|
DiagnosticIDs::Note)
|
|
<< ShadowingMethod;
|
|
}
|
|
|
|
} // namespace clang::tidy::bugprone
|