10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
17 namespace cppcoreguidelines {
19 void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
20 if (!getLangOpts().CPlusPlus)
23 const auto AllPointerTypes = anyOf(
24 hasType(pointerType()), hasType(autoType(hasDeducedType(pointerType()))),
25 hasType(decltypeType(hasUnderlyingType(pointerType()))));
30 hasAnyOperatorName(
"+",
"-",
"+=",
"-="), AllPointerTypes,
31 unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit()))))))
36 unaryOperator(hasAnyOperatorName(
"++",
"--"), hasType(pointerType()))
43 hasBase(ignoringImpCasts(
44 anyOf(AllPointerTypes,
45 hasType(decayedType(hasDecayedType(pointerType())))))))
50 void ProBoundsPointerArithmeticCheck::check(
51 const MatchFinder::MatchResult &Result) {
52 const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>(
"expr");
54 diag(MatchedExpr->getExprLoc(),
"do not use pointer arithmetic");