clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
19 AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt,
20 ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
21 const DeclStmt *const Stmt = Node.getBeginEndStmt();
22 return (Stmt != nullptr && InnerMatcher.matches(*Stmt, Finder, Builder));
23 }
24
25 AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) {
26 return stmt(hasAncestor(cxxForRangeStmt(
27 hasRangeBeginEndStmt(hasDescendant(equalsNode(&Node))))))
28 .matches(Node, Finder, Builder);
29 }
30
31 AST_MATCHER_P(Expr, hasParentIgnoringImpCasts,
32 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
33 const Expr *E = &Node;
34 do {
35 ASTContext::DynTypedNodeList Parents =
36 Finder->getASTContext().getParents(*E);
37 if (Parents.size() != 1)
38 return false;
39 E = Parents[0].get<Expr>();
40 if (!E)
41 return false;
42 } while (isa<ImplicitCastExpr>(E));
43
44 return InnerMatcher.matches(*E, Finder, Builder);
45 }
46
47 void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) {
48 if (!getLangOpts().CPlusPlus)
49 return;
50
51 // The only allowed array to pointer decay
52 // 1) just before array subscription
53 // 2) inside a range-for over an array
54 // 3) if it converts a string literal to a pointer
55 Finder->addMatcher(
56 implicitCastExpr(unless(hasParent(arraySubscriptExpr())),
57 unless(hasParentIgnoringImpCasts(explicitCastExpr())),
58 unless(isInsideOfRangeBeginEndStmt()),
59 unless(hasSourceExpression(stringLiteral())))
60 .bind("cast"),
61 this);
62 }
63
64 void ProBoundsArrayToPointerDecayCheck::check(
65 const MatchFinder::MatchResult &Result) {
66 const auto *MatchedCast = Result.Nodes.getNodeAs<ImplicitCastExpr>("cast");
67 if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay)
68 return;
69
70 diag(MatchedCast->getExprLoc(), "do not implicitly decay an array into a "
71 "pointer; consider using gsl::array_view or "
72 "an explicit cast instead");
73 }
74
75 } // namespace tidy
76 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::AST_MATCHER
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
Definition: ProBoundsArrayToPointerDecayCheck.cpp:25
clang::tidy::AST_MATCHER_P
AST_MATCHER_P(Expr, hasParentIgnoringImpCasts, ast_matchers::internal::Matcher< Expr >, InnerMatcher)
Definition: ProBoundsArrayToPointerDecayCheck.cpp:31
ProBoundsArrayToPointerDecayCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121