| 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::tidy::cppcoreguidelines { |
| 17 |
|
| 18 |
namespace { |
| 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)) |
| 23 |
return true; |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) { |
| 28 |
return stmt(hasAncestor(cxxForRangeStmt( |
| 29 |
hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node)))))) |
| 30 |
.matches(Node, Finder, Builder); |
| 31 |
} |
| 32 |
|
| 33 |
AST_MATCHER_P(Expr, hasParentIgnoringImpCasts, |
| 34 |
ast_matchers::internal::Matcher<Expr>, InnerMatcher) { |
| 35 |
const Expr *E = &Node; |
| 36 |
do { |
| 37 |
DynTypedNodeList Parents = Finder->getASTContext().getParents(*E); |
| 38 |
if (Parents.size() != 1) |
| 39 |
return false; |
| 40 |
E = Parents[0].get<Expr>(); |
| 41 |
if (!E) |
| 42 |
return false; |
| 43 |
} while (isa<ImplicitCastExpr>(E)); |
| 44 |
|
| 45 |
return InnerMatcher.matches(*E, Finder, Builder); |
| 46 |
} |
| 47 |
} // namespace |
| 48 |
|
| 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 |
| 54 |
Finder->addMatcher( |
| 55 |
traverse( |
| 56 |
TK_AsIs, |
| 57 |
implicitCastExpr( |
| 58 |
unless(hasParent(arraySubscriptExpr())), |
| 59 |
unless(hasParentIgnoringImpCasts(explicitCastExpr())), |
| 60 |
unless(isInsideOfRangeBeginEndStmt()), |
| 61 |
unless(hasSourceExpression(ignoringParens(stringLiteral()))), |
| 62 |
unless(hasSourceExpression(ignoringParens(conditionalOperator( |
| 63 |
allOf(hasTrueExpression(stringLiteral()), |
| 64 |
hasFalseExpression(stringLiteral()))))))) |
| 65 |
.bind("cast")), |
| 66 |
this); |
| 67 |
} |
| 68 |
|
| 69 |
void ProBoundsArrayToPointerDecayCheck::check( |
| 70 |
const MatchFinder::MatchResult &Result) { |
| 71 |
const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>("cast"); |
| 72 |
if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay) |
| 73 |
return; |
| 74 |
|
| 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"); |
| 78 |
} |
| 79 |
|
| 80 |
} // namespace clang::tidy::cppcoreguidelines |