Matthias Gehre | f333196 | 2015-10-26 21:56:02 +0000 | [diff] [blame] | 1 | //===--- ProBoundsArrayToPointerDecayCheck.cpp - clang-tidy----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ProBoundsArrayToPointerDecayCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 18 | namespace cppcoreguidelines { |
Matthias Gehre | f333196 | 2015-10-26 21:56:02 +0000 | [diff] [blame] | 19 | |
| 20 | AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt, |
| 21 | ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) { |
Benjamin Kramer | 74e4d55 | 2016-03-20 14:24:49 +0000 | [diff] [blame] | 22 | for (const DeclStmt *Stmt : {Node.getBeginStmt(), Node.getEndStmt()}) |
| 23 | if (Stmt != nullptr && InnerMatcher.matches(*Stmt, Finder, Builder)) |
| 24 | return true; |
| 25 | return false; |
Matthias Gehre | f333196 | 2015-10-26 21:56:02 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) { |
| 29 | return stmt(hasAncestor(cxxForRangeStmt( |
| 30 | hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node)))))) |
| 31 | .matches(Node, Finder, Builder); |
| 32 | } |
| 33 | |
Matthias Gehre | 4722f19 | 2015-11-17 23:35:39 +0000 | [diff] [blame] | 34 | AST_MATCHER_P(Expr, hasParentIgnoringImpCasts, |
| 35 | ast_matchers::internal::Matcher<Expr>, InnerMatcher) { |
| 36 | const Expr *E = &Node; |
| 37 | do { |
| 38 | ASTContext::DynTypedNodeList Parents = |
| 39 | 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 | |
Matthias Gehre | f333196 | 2015-10-26 21:56:02 +0000 | [diff] [blame] | 50 | void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) { |
| 51 | if (!getLangOpts().CPlusPlus) |
| 52 | return; |
| 53 | |
| 54 | // The only allowed array to pointer decay |
| 55 | // 1) just before array subscription |
| 56 | // 2) inside a range-for over an array |
| 57 | // 3) if it converts a string literal to a pointer |
| 58 | Finder->addMatcher( |
| 59 | implicitCastExpr(unless(hasParent(arraySubscriptExpr())), |
Matthias Gehre | 4722f19 | 2015-11-17 23:35:39 +0000 | [diff] [blame] | 60 | unless(hasParentIgnoringImpCasts(explicitCastExpr())), |
Matthias Gehre | f333196 | 2015-10-26 21:56:02 +0000 | [diff] [blame] | 61 | unless(isInsideOfRangeBeginEndStmt()), |
| 62 | unless(hasSourceExpression(stringLiteral()))) |
| 63 | .bind("cast"), |
| 64 | this); |
| 65 | } |
| 66 | |
| 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 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 78 | } // namespace cppcoreguidelines |
Matthias Gehre | f333196 | 2015-10-26 21:56:02 +0000 | [diff] [blame] | 79 | } // namespace tidy |
| 80 | } // namespace clang |