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