Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/performance/TriviallyDestructibleCheck.cpp
T
derek.holloway 7502bf64a4
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
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

77 lines
3.0 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 "TriviallyDestructibleCheck.h"
#include "../utils/LexerUtils.h"
#include "../utils/Matchers.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
using namespace clang::ast_matchers::internal;
using namespace clang::tidy::matchers;
namespace clang::tidy::performance {
namespace {
// We use isOutOfLine() to find out-of-line defaulted destructor definitions.
// This is more robust than !isFirstDecl() because with C++20 modules, when a
// class is visible through both a header include and a module import, its
// declarations may appear multiple times in the redeclaration chain.
AST_MATCHER(CXXMethodDecl, isOutOfLine) { return Node.isOutOfLine(); }
AST_MATCHER_P(CXXRecordDecl, hasBase, Matcher<QualType>, InnerMatcher) {
return llvm::any_of(Node.bases(), [&](const CXXBaseSpecifier &BaseSpec) {
return InnerMatcher.matches(BaseSpec.getType(), Finder, Builder);
});
}
} // namespace
void TriviallyDestructibleCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxDestructorDecl(
isDefaulted(), isOutOfLine(),
unless(anyOf(isVirtual(),
ofClass(cxxRecordDecl(
anyOf(hasBase(unless(isTriviallyDestructible())),
has(fieldDecl(unless(
hasType(isTriviallyDestructible()))))))))))
.bind("decl"),
this);
}
void TriviallyDestructibleCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXDestructorDecl>("decl");
// Get locations of both first and out-of-line declarations.
const SourceManager &SM = *Result.SourceManager;
const auto *FirstDecl = cast<CXXMethodDecl>(MatchedDecl->getFirstDecl());
const SourceLocation FirstDeclEnd = utils::lexer::findNextTerminator(
FirstDecl->getEndLoc(), SM, getLangOpts());
const CharSourceRange SecondDeclRange = CharSourceRange::getTokenRange(
MatchedDecl->getBeginLoc(),
utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(), SM,
getLangOpts()));
if (FirstDeclEnd.isInvalid() || SecondDeclRange.isInvalid())
return;
// Report diagnostic.
diag(FirstDecl->getLocation(),
"class %0 can be made trivially destructible by defaulting the "
"destructor on its first declaration")
<< FirstDecl->getParent()
<< FixItHint::CreateInsertion(FirstDeclEnd, " = default")
<< FixItHint::CreateRemoval(SecondDeclRange);
diag(MatchedDecl->getLocation(), "destructor definition is here",
DiagnosticIDs::Note);
}
} // namespace clang::tidy::performance