VirtualNearMissCheck.h 2.4 KB

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