Matthias Gehre | dc48412 | 2015-10-12 21:53:19 +0000 | [diff] [blame] | 1 | //===--- ProBoundsPointerArithmeticCheck.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 "ProBoundsPointerArithmeticCheck.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 { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 18 | namespace cppcoreguidelines { |
Matthias Gehre | dc48412 | 2015-10-12 21:53:19 +0000 | [diff] [blame] | 19 | |
| 20 | void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) { |
| 21 | if (!getLangOpts().CPlusPlus) |
| 22 | return; |
| 23 | |
| 24 | // Flag all operators +, -, +=, -=, ++, -- that result in a pointer |
| 25 | Finder->addMatcher( |
Matthias Gehre | 4241ced | 2015-11-26 22:32:11 +0000 | [diff] [blame] | 26 | binaryOperator( |
| 27 | anyOf(hasOperatorName("+"), hasOperatorName("-"), |
| 28 | hasOperatorName("+="), hasOperatorName("-=")), |
| 29 | hasType(pointerType()), |
| 30 | unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit())))))) |
Matthias Gehre | dc48412 | 2015-10-12 21:53:19 +0000 | [diff] [blame] | 31 | .bind("expr"), |
| 32 | this); |
| 33 | |
| 34 | Finder->addMatcher( |
| 35 | unaryOperator(anyOf(hasOperatorName("++"), hasOperatorName("--")), |
| 36 | hasType(pointerType())) |
| 37 | .bind("expr"), |
| 38 | this); |
| 39 | |
| 40 | // Array subscript on a pointer (not an array) is also pointer arithmetic |
| 41 | Finder->addMatcher( |
Matthias Gehre | 4241ced | 2015-11-26 22:32:11 +0000 | [diff] [blame] | 42 | arraySubscriptExpr( |
| 43 | hasBase(ignoringImpCasts( |
| 44 | anyOf(hasType(pointerType()), |
| 45 | hasType(decayedType(hasDecayedType(pointerType()))))))) |
Matthias Gehre | dc48412 | 2015-10-12 21:53:19 +0000 | [diff] [blame] | 46 | .bind("expr"), |
| 47 | this); |
| 48 | } |
| 49 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 50 | void ProBoundsPointerArithmeticCheck::check( |
| 51 | const MatchFinder::MatchResult &Result) { |
Matthias Gehre | dc48412 | 2015-10-12 21:53:19 +0000 | [diff] [blame] | 52 | const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>("expr"); |
| 53 | |
| 54 | diag(MatchedExpr->getExprLoc(), "do not use pointer arithmetic"); |
| 55 | } |
| 56 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 57 | } // namespace cppcoreguidelines |
Matthias Gehre | dc48412 | 2015-10-12 21:53:19 +0000 | [diff] [blame] | 58 | } // namespace tidy |
| 59 | } // namespace clang |