clang-tools 24.0.0git
PointerArithmeticOnPolymorphicObjectCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
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) {
19 return Node.hasDefinition() && Node.isAbstract();
20}
21AST_MATCHER(CXXRecordDecl, isPolymorphic) {
22 return Node.hasDefinition() && Node.isPolymorphic();
23}
24} // namespace
25
28 ClangTidyContext *Context)
29 : ClangTidyCheck(Name, Context),
30 IgnoreInheritedVirtualFunctions(
31 Options.get("IgnoreInheritedVirtualFunctions", false)) {}
32
35 Options.store(Opts, "IgnoreInheritedVirtualFunctions",
36 IgnoreInheritedVirtualFunctions);
37}
38
40 MatchFinder *Finder) {
41 const auto PolymorphicPointerExpr =
42 expr(hasType(hasCanonicalType(pointerType(pointee(hasCanonicalType(
43 hasDeclaration(cxxRecordDecl(unless(isFinal()), isPolymorphic())
44 .bind("pointee"))))))))
45 .bind("pointer");
46
47 const auto PointerExprWithVirtualMethod =
48 expr(hasType(hasCanonicalType(
49 pointerType(pointee(hasCanonicalType(hasDeclaration(
50 cxxRecordDecl(
51 unless(isFinal()),
52 anyOf(hasMethod(isVirtualAsWritten()), isAbstract()))
53 .bind("pointee"))))))))
54 .bind("pointer");
55
56 const auto SelectedPointerExpr = IgnoreInheritedVirtualFunctions
57 ? PointerExprWithVirtualMethod
58 : PolymorphicPointerExpr;
59
60 const auto ArraySubscript =
61 expr(arraySubscriptExpr(hasBase(SelectedPointerExpr)),
62 unless(isInstantiationDependent()));
63
64 const auto BinaryOperators =
65 binaryOperator(hasAnyOperatorName("+", "-", "+=", "-="),
66 hasEitherOperand(SelectedPointerExpr));
67
68 const auto UnaryOperators = unaryOperator(
69 hasAnyOperatorName("++", "--"), hasUnaryOperand(SelectedPointerExpr));
70
71 Finder->addMatcher(ArraySubscript, this);
72 Finder->addMatcher(BinaryOperators, this);
73 Finder->addMatcher(UnaryOperators, this);
74}
75
77 const MatchFinder::MatchResult &Result) {
78 const auto *PointerExpr = Result.Nodes.getNodeAs<Expr>("pointer");
79 const auto *PointeeDecl = Result.Nodes.getNodeAs<CXXRecordDecl>("pointee");
80
81 diag(PointerExpr->getBeginLoc(),
82 "pointer arithmetic on polymorphic object of type %0 can result in "
83 "undefined behavior if the dynamic type differs from the pointer type")
84 << PointeeDecl << PointerExpr->getSourceRange();
85}
86
87} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap