1 //===--- ProBoundsArrayToPointerDecayCheck.cpp - clang-tidy----------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ProBoundsArrayToPointerDecayCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/ParentMapContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers
;
16 namespace clang::tidy::cppcoreguidelines
{
19 AST_MATCHER_P(CXXForRangeStmt
, hasRangeBeginEndStmt
,
20 ast_matchers::internal::Matcher
<DeclStmt
>, InnerMatcher
) {
21 for (const DeclStmt
*Stmt
: {Node
.getBeginStmt(), Node
.getEndStmt()})
22 if (Stmt
!= nullptr && InnerMatcher
.matches(*Stmt
, Finder
, Builder
))
27 AST_MATCHER(Stmt
, isInsideOfRangeBeginEndStmt
) {
28 return stmt(hasAncestor(cxxForRangeStmt(
29 hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node
))))))
30 .matches(Node
, Finder
, Builder
);
33 AST_MATCHER_P(Expr
, hasParentIgnoringImpCasts
,
34 ast_matchers::internal::Matcher
<Expr
>, InnerMatcher
) {
35 const Expr
*E
= &Node
;
37 DynTypedNodeList Parents
= Finder
->getASTContext().getParents(*E
);
38 if (Parents
.size() != 1)
40 E
= Parents
[0].get
<Expr
>();
43 } while (isa
<ImplicitCastExpr
>(E
));
45 return InnerMatcher
.matches(*E
, Finder
, Builder
);
49 void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder
*Finder
) {
50 // The only allowed array to pointer decay
51 // 1) just before array subscription
52 // 2) inside a range-for over an array
53 // 3) if it converts a string literal to a pointer
58 unless(hasParent(arraySubscriptExpr())),
59 unless(hasParentIgnoringImpCasts(explicitCastExpr())),
60 unless(isInsideOfRangeBeginEndStmt()),
61 unless(hasSourceExpression(ignoringParens(stringLiteral()))),
62 unless(hasSourceExpression(ignoringParens(
63 conditionalOperator(hasTrueExpression(stringLiteral()),
64 hasFalseExpression(stringLiteral()))))))
69 void ProBoundsArrayToPointerDecayCheck::check(
70 const MatchFinder::MatchResult
&Result
) {
71 const auto *MatchedCast
= Result
.Nodes
.getNodeAs
<ImplicitCastExpr
>("cast");
72 if (MatchedCast
->getCastKind() != CK_ArrayToPointerDecay
)
75 diag(MatchedCast
->getExprLoc(), "do not implicitly decay an array into a "
76 "pointer; consider using gsl::array_view or "
77 "an explicit cast instead");
80 } // namespace clang::tidy::cppcoreguidelines