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

162 lines
5.8 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 "AvoidCArraysCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
template <typename TargetType, typename NodeType>
static const TargetType *getAs(const NodeType *Node) {
if constexpr (std::is_same_v<NodeType, DynTypedNode>)
return Node->template get<TargetType>();
else
return dyn_cast<TargetType>(Node);
}
namespace {
AST_MATCHER(TypeLoc, hasValidBeginLoc) { return Node.getBeginLoc().isValid(); }
AST_MATCHER_P(TypeLoc, hasType, ast_matchers::internal::Matcher<Type>,
InnerMatcher) {
const Type *TypeNode = Node.getTypePtr();
return TypeNode != nullptr &&
InnerMatcher.matches(*TypeNode, Finder, Builder);
}
AST_MATCHER(RecordDecl, isExternCContext) { return Node.isExternCContext(); }
AST_MATCHER(ParmVarDecl, isArgvOfMain) {
const DeclContext *DC = Node.getDeclContext();
const auto *FD = dyn_cast<FunctionDecl>(DC);
return FD ? FD->isMain() : false;
}
AST_MATCHER(TypeLoc, isWithinImplicitTemplateInstantiation) {
const auto IsImplicitTemplateInstantiation = [](const auto *Node) {
const auto IsImplicitInstantiation = [](const auto *Node) {
return (Node != nullptr) && (Node->getTemplateSpecializationKind() ==
TSK_ImplicitInstantiation);
};
return (IsImplicitInstantiation(getAs<CXXRecordDecl>(Node)) ||
IsImplicitInstantiation(getAs<FunctionDecl>(Node)) ||
IsImplicitInstantiation(getAs<VarDecl>(Node)));
};
DynTypedNodeList ParentNodes = Finder->getASTContext().getParents(Node);
const NamedDecl *ParentDecl = nullptr;
while (!ParentNodes.empty()) {
const DynTypedNode &ParentNode = ParentNodes[0];
if (IsImplicitTemplateInstantiation(&ParentNode))
return true;
// in case of a `NamedDecl` as parent node, it is more efficient to proceed
// with the upward traversal via DeclContexts (see below) instead of via
// parent nodes
if ((ParentDecl = ParentNode.get<NamedDecl>()))
break;
ParentNodes = Finder->getASTContext().getParents(ParentNode);
}
if (ParentDecl != nullptr) {
const DeclContext *DeclContext = ParentDecl->getDeclContext();
while (DeclContext != nullptr) {
if (IsImplicitTemplateInstantiation(DeclContext))
return true;
DeclContext = DeclContext->getParent();
}
}
return false;
}
} // namespace
AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
AllowStringArrays(Options.get("AllowStringArrays", false)) {}
void AvoidCArraysCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "AllowStringArrays", AllowStringArrays);
}
void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
ast_matchers::internal::Matcher<TypeLoc> IgnoreStringArrayIfNeededMatcher =
anything();
if (AllowStringArrays)
IgnoreStringArrayIfNeededMatcher =
unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
hasElementType(isAnyCharacter())))),
hasParent(varDecl(hasInitializer(stringLiteral()),
unless(parmVarDecl())))));
Finder->addMatcher(
typeLoc(hasValidBeginLoc(), hasType(arrayType()),
optionally(hasParent(parmVarDecl().bind("param_decl"))),
unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext())))),
hasAncestor(functionDecl(isExternC())),
isWithinImplicitTemplateInstantiation())),
IgnoreStringArrayIfNeededMatcher)
.bind("typeloc"),
this);
Finder->addMatcher(
templateArgumentLoc(hasTypeLoc(
typeLoc(hasType(arrayType())).bind("template_arg_array_typeloc"))),
this);
}
void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
TypeLoc ArrayTypeLoc{};
if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs<TypeLoc>("typeloc"))
ArrayTypeLoc = *MatchedTypeLoc;
if (const auto *TemplateArgArrayTypeLoc =
Result.Nodes.getNodeAs<TypeLoc>("template_arg_array_typeloc"))
ArrayTypeLoc = *TemplateArgArrayTypeLoc;
assert(!ArrayTypeLoc.isNull());
const bool IsInParam =
Result.Nodes.getNodeAs<ParmVarDecl>("param_decl") != nullptr;
const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType();
enum class RecommendType { Array, Vector, Span };
SmallVector<const char *> RecommendTypes{};
if (IsVLA) {
RecommendTypes.push_back("'std::vector'");
} else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) {
// in function parameter, we also don't know the size of
// IncompleteArrayType.
if (Result.Context->getLangOpts().CPlusPlus20) {
RecommendTypes.push_back("'std::span'");
} else {
RecommendTypes.push_back("'std::array'");
RecommendTypes.push_back("'std::vector'");
}
} else {
RecommendTypes.push_back("'std::array'");
}
diag(ArrayTypeLoc.getBeginLoc(),
"do not declare %select{C-style|C VLA}0 arrays, use %1 instead")
<< IsVLA << llvm::join(RecommendTypes, " or ")
<< ArrayTypeLoc.getSourceRange();
}
} // namespace clang::tidy::modernize