UNSUPPORT test on 64-bit AIX too
[llvm-project.git] / clang-tools-extra / clang-tidy / modernize / AvoidCArraysCheck.cpp
blob872dd1867594080dce75252e31f39a7ee5c3b0a7
1 //===--- AvoidCArraysCheck.cpp - clang-tidy -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "AvoidCArraysCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers;
15 namespace {
17 AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
18 return Node.getBeginLoc().isValid();
21 AST_MATCHER_P(clang::TypeLoc, hasType,
22 clang::ast_matchers::internal::Matcher<clang::Type>,
23 InnerMatcher) {
24 const clang::Type *TypeNode = Node.getTypePtr();
25 return TypeNode != nullptr &&
26 InnerMatcher.matches(*TypeNode, Finder, Builder);
29 AST_MATCHER(clang::RecordDecl, isExternCContext) {
30 return Node.isExternCContext();
33 AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) {
34 const clang::DeclContext *DC = Node.getDeclContext();
35 const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
36 return FD ? FD->isMain() : false;
39 } // namespace
41 namespace clang {
42 namespace tidy {
43 namespace modernize {
45 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
46 Finder->addMatcher(
47 typeLoc(hasValidBeginLoc(), hasType(arrayType()),
48 unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
49 hasParent(varDecl(isExternC())),
50 hasParent(fieldDecl(
51 hasParent(recordDecl(isExternCContext())))),
52 hasAncestor(functionDecl(isExternC())))))
53 .bind("typeloc"),
54 this);
57 void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
58 const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
60 diag(ArrayType->getBeginLoc(),
61 "do not declare %select{C-style|C VLA}0 arrays, use "
62 "%select{std::array<>|std::vector<>}0 instead")
63 << ArrayType->getTypePtr()->isVariableArrayType();
66 } // namespace modernize
67 } // namespace tidy
68 } // namespace clang