clang-tools 10.0.0

AvoidCArraysCheck.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
8
9 #include "AvoidCArraysCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace {
16
17 AST_MATCHER(clang::TypeLoc, hasValidBeginLoc) {
18 return Node.getBeginLoc().isValid();
19 }
20
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);
27 }
28
29 AST_MATCHER(clang::RecordDecl, isExternCContext) {
30 return Node.isExternCContext();
31 }
32
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;
37 }
38
39 } // namespace
40
41 namespace clang {
42 namespace tidy {
43 namespace modernize {
44
45 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
46 // std::array<> is available since C++11.
47 if (!getLangOpts().CPlusPlus11)
48 return;
49
50 Finder->addMatcher(
51 typeLoc(hasValidBeginLoc(), hasType(arrayType()),
52 unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
53 hasParent(varDecl(isExternC())),
54 hasParent(fieldDecl(
55 hasParent(recordDecl(isExternCContext())))),
56 hasAncestor(functionDecl(isExternC())))))
57 .bind("typeloc"),
58 this);
59 }
60
61 void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
62 const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
63
64 static constexpr llvm::StringLiteral UseArray = llvm::StringLiteral(
65 "do not declare C-style arrays, use std::array<> instead");
66 static constexpr llvm::StringLiteral UseVector = llvm::StringLiteral(
67 "do not declare C VLA arrays, use std::vector<> instead");
68
69 diag(ArrayType->getBeginLoc(),
70 ArrayType->getTypePtr()->isVariableArrayType() ? UseVector : UseArray);
71 }
72
73 } // namespace modernize
74 } // namespace tidy
75 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
AvoidCArraysCheck.h
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:18
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Type
NodeType Type
Definition: HTMLGenerator.cpp:73
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:52