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