clang-tools 8.0.0

ProBoundsArrayToPointerDecayCheck.cpp
Go to the documentation of this file.
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 {
18 namespace cppcoreguidelines {
19
20 namespace {
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
29 AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) {
30 return stmt(hasAncestor(cxxForRangeStmt(
31 hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node))))))
32 .matches(Node, Finder, Builder);
33 }
34
35 AST_MATCHER_P(Expr, hasParentIgnoringImpCasts,
36 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
37 const Expr *E = &Node;
38 do {
39 ASTContext::DynTypedNodeList Parents =
40 Finder->getASTContext().getParents(*E);
41 if (Parents.size() != 1)
42 return false;
43 E = Parents[0].get<Expr>();
44 if (!E)
45 return false;
46 } while (isa<ImplicitCastExpr>(E));
47
48 return InnerMatcher.matches(*E, Finder, Builder);
49 }
50 } // namespace
51
52 void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
53 if (!getLangOpts().CPlusPlus)
54 return;
55
56 // The only allowed array to pointer decay
57 // 1) just before array subscription
58 // 2) inside a range-for over an array
59 // 3) if it converts a string literal to a pointer
60 Finder->addMatcher(
61 implicitCastExpr(
62 unless(hasParent(arraySubscriptExpr())),
63 unless(hasParentIgnoringImpCasts(explicitCastExpr())),
64 unless(isInsideOfRangeBeginEndStmt()),
65 unless(hasSourceExpression(ignoringParens(stringLiteral()))))
66 .bind("cast"),
67 this);
68 }
69
70 void ProBoundsArrayToPointerDecayCheck::check(
71 const MatchFinder::MatchResult &Result) {
72 const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>("cast");
73 if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay)
74 return;
75
76 diag(MatchedCast->getExprLoc(), "do not implicitly decay an array into a "
77 "pointer; consider using gsl::array_view or "
78 "an explicit cast instead");
79 }
80
81 } // namespace cppcoreguidelines
82 } // namespace tidy
83 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::matchers::AST_MATCHER
AST_MATCHER(BinaryOperator, isAssignmentOperator)
Definition: Matchers.h:20
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
ProBoundsArrayToPointerDecayCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::ast_matchers::AST_MATCHER_P
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)
Definition: ExceptionEscapeCheck.cpp:139