1f3331969SMatthias Gehre //===--- ProBoundsArrayToPointerDecayCheck.cpp - clang-tidy----------------===//
2f3331969SMatthias Gehre //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f3331969SMatthias Gehre //
7f3331969SMatthias Gehre //===----------------------------------------------------------------------===//
8f3331969SMatthias Gehre
9f3331969SMatthias Gehre #include "ProBoundsArrayToPointerDecayCheck.h"
10f3331969SMatthias Gehre #include "clang/AST/ASTContext.h"
118a81daaaSReid Kleckner #include "clang/AST/ParentMapContext.h"
12f3331969SMatthias Gehre #include "clang/ASTMatchers/ASTMatchFinder.h"
13f3331969SMatthias Gehre
14f3331969SMatthias Gehre using namespace clang::ast_matchers;
15f3331969SMatthias Gehre
16f3331969SMatthias Gehre namespace clang {
17f3331969SMatthias Gehre namespace tidy {
18456177b9SEtienne Bergeron namespace cppcoreguidelines {
19f3331969SMatthias Gehre
20f8c99297SBenjamin Kramer namespace {
AST_MATCHER_P(CXXForRangeStmt,hasRangeBeginEndStmt,ast_matchers::internal::Matcher<DeclStmt>,InnerMatcher)21f3331969SMatthias Gehre AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt,
22f3331969SMatthias Gehre ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
2374e4d55dSBenjamin Kramer for (const DeclStmt *Stmt : {Node.getBeginStmt(), Node.getEndStmt()})
2474e4d55dSBenjamin Kramer if (Stmt != nullptr && InnerMatcher.matches(*Stmt, Finder, Builder))
2574e4d55dSBenjamin Kramer return true;
2674e4d55dSBenjamin Kramer return false;
27f3331969SMatthias Gehre }
28f3331969SMatthias Gehre
AST_MATCHER(Stmt,isInsideOfRangeBeginEndStmt)29f3331969SMatthias Gehre AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) {
30f3331969SMatthias Gehre return stmt(hasAncestor(cxxForRangeStmt(
31f3331969SMatthias Gehre hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node))))))
32f3331969SMatthias Gehre .matches(Node, Finder, Builder);
33f3331969SMatthias Gehre }
34f3331969SMatthias Gehre
AST_MATCHER_P(Expr,hasParentIgnoringImpCasts,ast_matchers::internal::Matcher<Expr>,InnerMatcher)354722f192SMatthias Gehre AST_MATCHER_P(Expr, hasParentIgnoringImpCasts,
364722f192SMatthias Gehre ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
374722f192SMatthias Gehre const Expr *E = &Node;
384722f192SMatthias Gehre do {
398a81daaaSReid Kleckner DynTypedNodeList Parents = Finder->getASTContext().getParents(*E);
404722f192SMatthias Gehre if (Parents.size() != 1)
414722f192SMatthias Gehre return false;
424722f192SMatthias Gehre E = Parents[0].get<Expr>();
434722f192SMatthias Gehre if (!E)
444722f192SMatthias Gehre return false;
454722f192SMatthias Gehre } while (isa<ImplicitCastExpr>(E));
464722f192SMatthias Gehre
474722f192SMatthias Gehre return InnerMatcher.matches(*E, Finder, Builder);
484722f192SMatthias Gehre }
49f8c99297SBenjamin Kramer } // namespace
504722f192SMatthias Gehre
registerMatchers(MatchFinder * Finder)51f3331969SMatthias Gehre void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
52f3331969SMatthias Gehre // The only allowed array to pointer decay
53f3331969SMatthias Gehre // 1) just before array subscription
54f3331969SMatthias Gehre // 2) inside a range-for over an array
55f3331969SMatthias Gehre // 3) if it converts a string literal to a pointer
56f3331969SMatthias Gehre Finder->addMatcher(
57*36af0733SElvis Stansvik traverse(
58*36af0733SElvis Stansvik TK_AsIs,
59ef67ce0fSJonas Toth implicitCastExpr(
60ef67ce0fSJonas Toth unless(hasParent(arraySubscriptExpr())),
614722f192SMatthias Gehre unless(hasParentIgnoringImpCasts(explicitCastExpr())),
62f3331969SMatthias Gehre unless(isInsideOfRangeBeginEndStmt()),
63*36af0733SElvis Stansvik unless(hasSourceExpression(ignoringParens(stringLiteral()))),
64*36af0733SElvis Stansvik unless(hasSourceExpression(ignoringParens(conditionalOperator(
65*36af0733SElvis Stansvik allOf(hasTrueExpression(stringLiteral()),
66*36af0733SElvis Stansvik hasFalseExpression(stringLiteral())))))))
67a72307c3SStephen Kelly .bind("cast")),
68f3331969SMatthias Gehre this);
69f3331969SMatthias Gehre }
70f3331969SMatthias Gehre
check(const MatchFinder::MatchResult & Result)71f3331969SMatthias Gehre void ProBoundsArrayToPointerDecayCheck::check(
72f3331969SMatthias Gehre const MatchFinder::MatchResult &Result) {
73f3331969SMatthias Gehre const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>("cast");
7473f283e6SAaron Ballman if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay)
7573f283e6SAaron Ballman return;
76f3331969SMatthias Gehre
77f3331969SMatthias Gehre diag(MatchedCast->getExprLoc(), "do not implicitly decay an array into a "
78f3331969SMatthias Gehre "pointer; consider using gsl::array_view or "
79f3331969SMatthias Gehre "an explicit cast instead");
80f3331969SMatthias Gehre }
81f3331969SMatthias Gehre
82456177b9SEtienne Bergeron } // namespace cppcoreguidelines
83f3331969SMatthias Gehre } // namespace tidy
84f3331969SMatthias Gehre } // namespace clang
85