11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers;
20 ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
21 const DeclStmt *
const Stmt = Node.getBeginEndStmt();
22 return (Stmt !=
nullptr && InnerMatcher.matches(*Stmt,
Finder, Builder));
26 return stmt(hasAncestor(cxxForRangeStmt(
27 hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node))))))
28 .matches(Node,
Finder, Builder);
32 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
33 const Expr *E = &Node;
35 ASTContext::DynTypedNodeList Parents =
36 Finder->getASTContext().getParents(*E);
37 if (Parents.size() != 1)
39 E = Parents[0].get<Expr>();
42 }
while (isa<ImplicitCastExpr>(E));
44 return InnerMatcher.matches(*E,
Finder, Builder);
47 void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *
Finder) {
48 if (!getLangOpts().CPlusPlus)
56 implicitCastExpr(unless(hasParent(arraySubscriptExpr())),
57 unless(hasParentIgnoringImpCasts(explicitCastExpr())),
58 unless(isInsideOfRangeBeginEndStmt()),
59 unless(hasSourceExpression(stringLiteral())))
64 void ProBoundsArrayToPointerDecayCheck::check(
65 const MatchFinder::MatchResult &
Result) {
66 const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"cast");
67 if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay)
70 diag(MatchedCast->getExprLoc(),
"do not implicitly decay an array into a "
71 "pointer; consider using gsl::array_view or "
72 "an explicit cast instead");
std::unique_ptr< ast_matchers::MatchFinder > Finder
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
AST_MATCHER_P(Expr, hasParentIgnoringImpCasts, ast_matchers::internal::Matcher< Expr >, InnerMatcher)