10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
18 return Node.getBeginLoc().isValid();
22 clang::ast_matchers::internal::Matcher<clang::Type>,
25 return TypeNode !=
nullptr &&
26 InnerMatcher.matches(*TypeNode, Finder,
Builder);
30 return Node.isExternCContext();
34 const clang::DeclContext *DC = Node.getDeclContext();
35 const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
36 return FD ? FD->isMain() :
false;
45 void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
47 typeLoc(hasValidBeginLoc(), hasType(arrayType()),
48 unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
49 hasParent(varDecl(isExternC())),
51 hasParent(recordDecl(isExternCContext())))),
52 hasAncestor(functionDecl(isExternC())))))
57 void AvoidCArraysCheck::check(
const MatchFinder::MatchResult &Result) {
58 const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>(
"typeloc");
60 static constexpr llvm::StringLiteral UseArray = llvm::StringLiteral(
61 "do not declare C-style arrays, use std::array<> instead");
62 static constexpr llvm::StringLiteral UseVector = llvm::StringLiteral(
63 "do not declare C VLA arrays, use std::vector<> instead");
65 diag(ArrayType->getBeginLoc(),
66 ArrayType->getTypePtr()->isVariableArrayType() ? UseVector : UseArray);