10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 return Node.getBeginLoc().isValid();
24 clang::ast_matchers::internal::Matcher<clang::Type>,
26 const clang::Type *TypeNode =
Node.getTypePtr();
27 return TypeNode !=
nullptr &&
28 InnerMatcher.matches(*TypeNode, Finder,
Builder);
32 return Node.isExternCContext();
36 const clang::DeclContext *DC =
Node.getDeclContext();
37 const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(DC);
38 return FD ? FD->isMain() :
false;
45 AllowStringArrays(Options.get(
"AllowStringArrays", false)) {}
48 Options.
store(Opts,
"AllowStringArrays", AllowStringArrays);
52 ast_matchers::internal::Matcher<TypeLoc> IgnoreStringArrayIfNeededMatcher =
54 if (AllowStringArrays)
55 IgnoreStringArrayIfNeededMatcher =
56 unless(typeLoc(loc(hasCanonicalType(incompleteArrayType(
57 hasElementType(isAnyCharacter())))),
58 hasParent(varDecl(hasInitializer(stringLiteral()),
59 unless(parmVarDecl())))));
62 typeLoc(hasValidBeginLoc(), hasType(arrayType()),
63 unless(anyOf(hasParent(parmVarDecl(isArgvOfMain())),
64 hasParent(varDecl(isExternC())),
66 hasParent(recordDecl(isExternCContext())))),
67 hasAncestor(functionDecl(isExternC())))),
68 std::move(IgnoreStringArrayIfNeededMatcher))
74 const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>(
"typeloc");
76 diag(ArrayType->getBeginLoc(),
77 "do not declare %select{C-style|C VLA}0 arrays, use "
78 "%select{std::array<>|std::vector<>}0 instead")
79 << ArrayType->getTypePtr()->isVariableArrayType();
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
llvm::StringMap< ClangTidyValue > OptionMap