1 //===--- VirtualNearMissCheck.h - clang-tidy---------------------*- C++ -*-===//
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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_VIRTUAL_NEAR_MISS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_VIRTUAL_NEAR_MISS_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "llvm/ADT/DenseMap.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace bugprone {
18 
19 /// Checks for near miss of virtual methods.
20 ///
21 /// For a method in a derived class, this check looks for virtual method with a
22 /// very similar name and an identical signature defined in a base class.
23 ///
24 /// For the user-facing documentation see:
25 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-virtual-near-miss.html
26 class VirtualNearMissCheck : public ClangTidyCheck {
27 public:
VirtualNearMissCheck(StringRef Name,ClangTidyContext * Context)28   VirtualNearMissCheck(StringRef Name, ClangTidyContext *Context)
29       : ClangTidyCheck(Name, Context) {}
isLanguageVersionSupported(const LangOptions & LangOpts)30   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
31     return LangOpts.CPlusPlus;
32   }
33   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
34   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
35 
36 private:
37   /// Check if the given method is possible to be overridden by some other
38   /// method. Operators and destructors are excluded.
39   ///
40   /// Results are memoized in PossibleMap.
41   bool isPossibleToBeOverridden(const CXXMethodDecl *BaseMD);
42 
43   /// Check if the given base method is overridden by some methods in the given
44   /// derived class.
45   ///
46   /// Results are memoized in OverriddenMap.
47   bool isOverriddenByDerivedClass(const CXXMethodDecl *BaseMD,
48                                   const CXXRecordDecl *DerivedRD);
49 
50   /// Key: the unique ID of a method.
51   /// Value: whether the method is possible to be overridden.
52   llvm::DenseMap<const CXXMethodDecl *, bool> PossibleMap;
53 
54   /// Key: <unique ID of base method, name of derived class>
55   /// Value: whether the base method is overridden by some method in the derived
56   /// class.
57   llvm::DenseMap<std::pair<const CXXMethodDecl *, const CXXRecordDecl *>, bool>
58       OverriddenMap;
59 
60   const unsigned EditDistanceThreshold = 1;
61 };
62 
63 } // namespace bugprone
64 } // namespace tidy
65 } // namespace clang
66 
67 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_VIRTUAL_NEAR_MISS_H
68