clang-tools  10.0.0git
ProBoundsArrayToPointerDecayCheck.cpp
Go to the documentation of this file.
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 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace 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(*Stmt, Finder, Builder))
24  return true;
25  return false;
26 }
27 
28 AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) {
29  return stmt(hasAncestor(cxxForRangeStmt(
30  hasRangeBeginEndStmt(hasDescendant(equalsNode(&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  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 } // namespace
50 
51 void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
52  if (!getLangOpts().CPlusPlus)
53  return;
54 
55  // The only allowed array to pointer decay
56  // 1) just before array subscription
57  // 2) inside a range-for over an array
58  // 3) if it converts a string literal to a pointer
59  Finder->addMatcher(
60  implicitCastExpr(
61  unless(hasParent(arraySubscriptExpr())),
62  unless(hasParentIgnoringImpCasts(explicitCastExpr())),
63  unless(isInsideOfRangeBeginEndStmt()),
64  unless(hasSourceExpression(ignoringParens(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 cppcoreguidelines
81 } // namespace tidy
82 } // namespace clang
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Expr * E
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)