annotate clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp @ 173:0572611fdcc8 llvm10 llvm12

reorgnization done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 11:55:54 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- ProBoundsPointerArithmeticCheck.cpp - clang-tidy------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "ProBoundsPointerArithmeticCheck.h"
anatofuz
parents:
diff changeset
10 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 namespace clang {
anatofuz
parents:
diff changeset
16 namespace tidy {
anatofuz
parents:
diff changeset
17 namespace cppcoreguidelines {
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
anatofuz
parents:
diff changeset
20 if (!getLangOpts().CPlusPlus)
anatofuz
parents:
diff changeset
21 return;
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 const auto AllPointerTypes = anyOf(
anatofuz
parents:
diff changeset
24 hasType(pointerType()), hasType(autoType(hasDeducedType(pointerType()))),
anatofuz
parents:
diff changeset
25 hasType(decltypeType(hasUnderlyingType(pointerType()))));
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 // Flag all operators +, -, +=, -=, ++, -- that result in a pointer
anatofuz
parents:
diff changeset
28 Finder->addMatcher(
anatofuz
parents:
diff changeset
29 binaryOperator(
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
30 hasAnyOperatorName("+", "-", "+=", "-="), AllPointerTypes,
150
anatofuz
parents:
diff changeset
31 unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit()))))))
anatofuz
parents:
diff changeset
32 .bind("expr"),
anatofuz
parents:
diff changeset
33 this);
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 Finder->addMatcher(
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
36 unaryOperator(hasAnyOperatorName("++", "--"), hasType(pointerType()))
150
anatofuz
parents:
diff changeset
37 .bind("expr"),
anatofuz
parents:
diff changeset
38 this);
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 // Array subscript on a pointer (not an array) is also pointer arithmetic
anatofuz
parents:
diff changeset
41 Finder->addMatcher(
anatofuz
parents:
diff changeset
42 arraySubscriptExpr(
anatofuz
parents:
diff changeset
43 hasBase(ignoringImpCasts(
anatofuz
parents:
diff changeset
44 anyOf(AllPointerTypes,
anatofuz
parents:
diff changeset
45 hasType(decayedType(hasDecayedType(pointerType())))))))
anatofuz
parents:
diff changeset
46 .bind("expr"),
anatofuz
parents:
diff changeset
47 this);
anatofuz
parents:
diff changeset
48 }
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 void ProBoundsPointerArithmeticCheck::check(
anatofuz
parents:
diff changeset
51 const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
52 const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>("expr");
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 diag(MatchedExpr->getExprLoc(), "do not use pointer arithmetic");
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 } // namespace cppcoreguidelines
anatofuz
parents:
diff changeset
58 } // namespace tidy
anatofuz
parents:
diff changeset
59 } // namespace clang