1//===--- PointerArithmeticOnPolymorphicObjectCheck.cpp - clang-tidy--------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "PointerArithmeticOnPolymorphicObjectCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17namespace {
18AST_MATCHER(CXXRecordDecl, isAbstract) { return Node.isAbstract(); }
19AST_MATCHER(CXXRecordDecl, isPolymorphic) { return Node.isPolymorphic(); }
20} // namespace
21
22PointerArithmeticOnPolymorphicObjectCheck::
23 PointerArithmeticOnPolymorphicObjectCheck(StringRef Name,
24 ClangTidyContext *Context)
25 : ClangTidyCheck(Name, Context),
26 IgnoreInheritedVirtualFunctions(
27 Options.get(LocalName: "IgnoreInheritedVirtualFunctions", Default: false)) {}
28
29void PointerArithmeticOnPolymorphicObjectCheck::storeOptions(
30 ClangTidyOptions::OptionMap &Opts) {
31 Options.store(Options&: Opts, LocalName: "IgnoreInheritedVirtualFunctions",
32 Value: IgnoreInheritedVirtualFunctions);
33}
34
35void PointerArithmeticOnPolymorphicObjectCheck::registerMatchers(
36 MatchFinder *Finder) {
37 const auto PolymorphicPointerExpr =
38 expr(hasType(InnerMatcher: hasCanonicalType(InnerMatcher: pointerType(pointee(hasCanonicalType(
39 InnerMatcher: hasDeclaration(InnerMatcher: cxxRecordDecl(unless(isFinal()), isPolymorphic())
40 .bind(ID: "pointee"))))))))
41 .bind(ID: "pointer");
42
43 const auto PointerExprWithVirtualMethod =
44 expr(hasType(InnerMatcher: hasCanonicalType(
45 InnerMatcher: pointerType(pointee(hasCanonicalType(InnerMatcher: hasDeclaration(
46 InnerMatcher: cxxRecordDecl(
47 unless(isFinal()),
48 anyOf(hasMethod(InnerMatcher: isVirtualAsWritten()), isAbstract()))
49 .bind(ID: "pointee"))))))))
50 .bind(ID: "pointer");
51
52 const auto SelectedPointerExpr = IgnoreInheritedVirtualFunctions
53 ? PointerExprWithVirtualMethod
54 : PolymorphicPointerExpr;
55
56 const auto ArraySubscript = arraySubscriptExpr(hasBase(InnerMatcher: SelectedPointerExpr));
57
58 const auto BinaryOperators =
59 binaryOperator(hasAnyOperatorName("+", "-", "+=", "-="),
60 hasEitherOperand(InnerMatcher: SelectedPointerExpr));
61
62 const auto UnaryOperators = unaryOperator(
63 hasAnyOperatorName("++", "--"), hasUnaryOperand(InnerMatcher: SelectedPointerExpr));
64
65 Finder->addMatcher(NodeMatch: ArraySubscript, Action: this);
66 Finder->addMatcher(NodeMatch: BinaryOperators, Action: this);
67 Finder->addMatcher(NodeMatch: UnaryOperators, Action: this);
68}
69
70void PointerArithmeticOnPolymorphicObjectCheck::check(
71 const MatchFinder::MatchResult &Result) {
72 const auto *PointerExpr = Result.Nodes.getNodeAs<Expr>(ID: "pointer");
73 const auto *PointeeDecl = Result.Nodes.getNodeAs<CXXRecordDecl>(ID: "pointee");
74
75 diag(PointerExpr->getBeginLoc(),
76 "pointer arithmetic on polymorphic object of type %0 can result in "
77 "undefined behavior if the dynamic type differs from the pointer type")
78 << PointeeDecl << PointerExpr->getSourceRange();
79}
80
81} // namespace clang::tidy::bugprone
82

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp