1 | //===--- ProBoundsArrayToPointerDecayCheck.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 "ProBoundsArrayToPointerDecayCheck.h" |
10 | #include "clang/AST/ASTContext.h" |
11 | #include "clang/AST/ParentMapContext.h" |
12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
13 | #include "clang/ASTMatchers/ASTMatchers.h" |
14 | |
15 | using namespace clang::ast_matchers; |
16 | |
17 | namespace clang::tidy::cppcoreguidelines { |
18 | |
19 | namespace { |
20 | AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt, |
21 | ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) { |
22 | for (const DeclStmt *Stmt : {Node.getBeginStmt(), Node.getEndStmt()}) |
23 | if (Stmt != nullptr && InnerMatcher.matches(Node: *Stmt, Finder, Builder)) |
24 | return true; |
25 | return false; |
26 | } |
27 | |
28 | AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) { |
29 | return stmt(hasAncestor(cxxForRangeStmt( |
30 | hasRangeBeginEndStmt(InnerMatcher: hasDescendant(equalsNode(Other: &Node)))))) |
31 | .matches(Node, Finder, Builder); |
32 | } |
33 | |
34 | AST_MATCHER_P(Expr, hasParentIgnoringImpCasts, |
35 | ast_matchers::internal::Matcher<Expr>, InnerMatcher) { |
36 | const Expr *E = &Node; |
37 | do { |
38 | DynTypedNodeList Parents = Finder->getASTContext().getParents(Node: *E); |
39 | if (Parents.size() != 1) |
40 | return false; |
41 | E = Parents[0].get<Expr>(); |
42 | if (!E) |
43 | return false; |
44 | } while (isa<ImplicitCastExpr>(Val: E)); |
45 | |
46 | return InnerMatcher.matches(Node: *E, Finder, Builder); |
47 | } |
48 | } // namespace |
49 | |
50 | void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) { |
51 | // The only allowed array to pointer decay |
52 | // 1) just before array subscription |
53 | // 2) inside a range-for over an array |
54 | // 3) if it converts a string literal to a pointer |
55 | Finder->addMatcher( |
56 | NodeMatch: traverse( |
57 | TK: TK_AsIs, |
58 | InnerMatcher: implicitCastExpr( |
59 | unless(hasParent(arraySubscriptExpr())), |
60 | unless(hasSourceExpression(InnerMatcher: predefinedExpr())), |
61 | unless(hasParentIgnoringImpCasts(InnerMatcher: explicitCastExpr())), |
62 | unless(isInsideOfRangeBeginEndStmt()), |
63 | unless(hasSourceExpression(InnerMatcher: ignoringParens(InnerMatcher: stringLiteral()))), |
64 | unless(hasSourceExpression(InnerMatcher: ignoringParens( |
65 | InnerMatcher: conditionalOperator(hasTrueExpression(InnerMatcher: stringLiteral()), |
66 | hasFalseExpression(InnerMatcher: stringLiteral())))))) |
67 | .bind(ID: "cast" )), |
68 | Action: this); |
69 | } |
70 | |
71 | void ProBoundsArrayToPointerDecayCheck::check( |
72 | const MatchFinder::MatchResult &Result) { |
73 | const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>(ID: "cast" ); |
74 | if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay) |
75 | return; |
76 | |
77 | diag(MatchedCast->getExprLoc(), "do not implicitly decay an array into a " |
78 | "pointer; consider using gsl::array_view or " |
79 | "an explicit cast instead" ); |
80 | } |
81 | |
82 | } // namespace clang::tidy::cppcoreguidelines |
83 | |