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