Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/bugprone/SmartPtrArrayMismatchCheck.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

115 lines
4.4 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 "SmartPtrArrayMismatchCheck.h"
#include "../utils/ASTUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
static constexpr char ConstructExprN[] = "found_construct_expr";
static constexpr char NewExprN[] = "found_new_expr";
static constexpr char ConstructorN[] = "found_constructor";
static bool isInSingleDeclStmt(const DeclaratorDecl *D) {
const DynTypedNodeList Parents =
D->getASTContext().getParentMapContext().getParents(*D);
for (const DynTypedNode &PNode : Parents)
if (const auto *PDecl = PNode.get<DeclStmt>())
return PDecl->isSingleDecl();
return false;
}
static const DeclaratorDecl *
getConstructedVarOrField(const Expr *FoundConstructExpr, ASTContext &Ctx) {
const DynTypedNodeList ConstructParents =
Ctx.getParentMapContext().getParents(*FoundConstructExpr);
if (ConstructParents.size() != 1)
return nullptr;
const auto *ParentDecl = ConstructParents.begin()->get<DeclaratorDecl>();
if (isa_and_nonnull<VarDecl, FieldDecl>(ParentDecl))
return ParentDecl;
return nullptr;
}
const char SmartPtrArrayMismatchCheck::PointerTypeN[] = "pointer_type";
SmartPtrArrayMismatchCheck::SmartPtrArrayMismatchCheck(
StringRef Name, ClangTidyContext *Context, StringRef SmartPointerName)
: ClangTidyCheck(Name, Context), SmartPointerName(SmartPointerName) {}
void SmartPtrArrayMismatchCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {}
void SmartPtrArrayMismatchCheck::registerMatchers(MatchFinder *Finder) {
// For both shared and unique pointers, we need to find constructor with
// exactly one parameter that has the pointer type. Other constructors are
// not applicable for this check.
auto FindConstructor =
cxxConstructorDecl(ofClass(getSmartPointerClassMatcher()),
parameterCountIs(1), isExplicit())
.bind(ConstructorN);
auto FindConstructExpr =
cxxConstructExpr(
hasDeclaration(FindConstructor), argumentCountIs(1),
hasArgument(0,
cxxNewExpr(isArray(),
hasType(hasCanonicalType(pointerType(
pointee(equalsBoundNode(PointerTypeN))))))
.bind(NewExprN)))
.bind(ConstructExprN);
Finder->addMatcher(FindConstructExpr, this);
}
void SmartPtrArrayMismatchCheck::check(const MatchFinder::MatchResult &Result) {
const auto *FoundNewExpr = Result.Nodes.getNodeAs<CXXNewExpr>(NewExprN);
const auto *FoundConstructExpr =
Result.Nodes.getNodeAs<CXXConstructExpr>(ConstructExprN);
const auto *FoundConstructorDecl =
Result.Nodes.getNodeAs<CXXConstructorDecl>(ConstructorN);
ASTContext &Ctx = FoundConstructorDecl->getASTContext();
const DeclaratorDecl *VarOrField =
getConstructedVarOrField(FoundConstructExpr, Ctx);
auto D = diag(FoundNewExpr->getBeginLoc(),
"%0 pointer to non-array is initialized with array")
<< SmartPointerName;
D << FoundNewExpr->getSourceRange();
if (VarOrField) {
auto TSTypeLoc = VarOrField->getTypeSourceInfo()
->getTypeLoc()
.getAsAdjusted<TemplateSpecializationTypeLoc>();
assert(TSTypeLoc.getNumArgs() >= 1 &&
"Matched type should have at least 1 template argument.");
const SourceRange TemplateArgumentRange = TSTypeLoc.getArgLoc(0)
.getTypeSourceInfo()
->getTypeLoc()
.getSourceRange();
D << TemplateArgumentRange;
if (isInSingleDeclStmt(VarOrField)) {
const SourceManager &SM = Ctx.getSourceManager();
if (!utils::rangeCanBeFixed(TemplateArgumentRange, &SM))
return;
const SourceLocation InsertLoc = Lexer::getLocForEndOfToken(
TemplateArgumentRange.getEnd(), 0, SM, Ctx.getLangOpts());
D << FixItHint::CreateInsertion(InsertLoc, "[]");
}
}
}
} // namespace clang::tidy::bugprone