RefCntblBaseVirtualDtorChecker.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //=======- RefCntblBaseVirtualDtor.cpp ---------------------------*- 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. #include "DiagOutputUtils.h"
  9. #include "PtrTypesSemantics.h"
  10. #include "clang/AST/CXXInheritance.h"
  11. #include "clang/AST/RecursiveASTVisitor.h"
  12. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  13. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include <optional>
  17. using namespace clang;
  18. using namespace ento;
  19. namespace {
  20. class RefCntblBaseVirtualDtorChecker
  21. : public Checker<check::ASTDecl<TranslationUnitDecl>> {
  22. private:
  23. BugType Bug;
  24. mutable BugReporter *BR;
  25. public:
  26. RefCntblBaseVirtualDtorChecker()
  27. : Bug(this,
  28. "Reference-countable base class doesn't have virtual destructor",
  29. "WebKit coding guidelines") {}
  30. void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR,
  31. BugReporter &BRArg) const {
  32. BR = &BRArg;
  33. // The calls to checkAST* from AnalysisConsumer don't
  34. // visit template instantiations or lambda classes. We
  35. // want to visit those, so we make our own RecursiveASTVisitor.
  36. struct LocalVisitor : public RecursiveASTVisitor<LocalVisitor> {
  37. const RefCntblBaseVirtualDtorChecker *Checker;
  38. explicit LocalVisitor(const RefCntblBaseVirtualDtorChecker *Checker)
  39. : Checker(Checker) {
  40. assert(Checker);
  41. }
  42. bool shouldVisitTemplateInstantiations() const { return true; }
  43. bool shouldVisitImplicitCode() const { return false; }
  44. bool VisitCXXRecordDecl(const CXXRecordDecl *RD) {
  45. Checker->visitCXXRecordDecl(RD);
  46. return true;
  47. }
  48. };
  49. LocalVisitor visitor(this);
  50. visitor.TraverseDecl(const_cast<TranslationUnitDecl *>(TUD));
  51. }
  52. void visitCXXRecordDecl(const CXXRecordDecl *RD) const {
  53. if (shouldSkipDecl(RD))
  54. return;
  55. CXXBasePaths Paths;
  56. Paths.setOrigin(RD);
  57. const CXXBaseSpecifier *ProblematicBaseSpecifier = nullptr;
  58. const CXXRecordDecl *ProblematicBaseClass = nullptr;
  59. const auto IsPublicBaseRefCntblWOVirtualDtor =
  60. [RD, &ProblematicBaseSpecifier,
  61. &ProblematicBaseClass](const CXXBaseSpecifier *Base, CXXBasePath &) {
  62. const auto AccSpec = Base->getAccessSpecifier();
  63. if (AccSpec == AS_protected || AccSpec == AS_private ||
  64. (AccSpec == AS_none && RD->isClass()))
  65. return false;
  66. std::optional<const CXXRecordDecl*> RefCntblBaseRD = isRefCountable(Base);
  67. if (!RefCntblBaseRD || !(*RefCntblBaseRD))
  68. return false;
  69. const auto *Dtor = (*RefCntblBaseRD)->getDestructor();
  70. if (!Dtor || !Dtor->isVirtual()) {
  71. ProblematicBaseSpecifier = Base;
  72. ProblematicBaseClass = *RefCntblBaseRD;
  73. return true;
  74. }
  75. return false;
  76. };
  77. if (RD->lookupInBases(IsPublicBaseRefCntblWOVirtualDtor, Paths,
  78. /*LookupInDependent =*/true)) {
  79. reportBug(RD, ProblematicBaseSpecifier, ProblematicBaseClass);
  80. }
  81. }
  82. bool shouldSkipDecl(const CXXRecordDecl *RD) const {
  83. if (!RD->isThisDeclarationADefinition())
  84. return true;
  85. if (RD->isImplicit())
  86. return true;
  87. if (RD->isLambda())
  88. return true;
  89. // If the construct doesn't have a source file, then it's not something
  90. // we want to diagnose.
  91. const auto RDLocation = RD->getLocation();
  92. if (!RDLocation.isValid())
  93. return true;
  94. const auto Kind = RD->getTagKind();
  95. if (Kind != TTK_Struct && Kind != TTK_Class)
  96. return true;
  97. // Ignore CXXRecords that come from system headers.
  98. if (BR->getSourceManager().getFileCharacteristic(RDLocation) !=
  99. SrcMgr::C_User)
  100. return true;
  101. return false;
  102. }
  103. void reportBug(const CXXRecordDecl *DerivedClass,
  104. const CXXBaseSpecifier *BaseSpec,
  105. const CXXRecordDecl *ProblematicBaseClass) const {
  106. assert(DerivedClass);
  107. assert(BaseSpec);
  108. assert(ProblematicBaseClass);
  109. SmallString<100> Buf;
  110. llvm::raw_svector_ostream Os(Buf);
  111. Os << (ProblematicBaseClass->isClass() ? "Class" : "Struct") << " ";
  112. printQuotedQualifiedName(Os, ProblematicBaseClass);
  113. Os << " is used as a base of "
  114. << (DerivedClass->isClass() ? "class" : "struct") << " ";
  115. printQuotedQualifiedName(Os, DerivedClass);
  116. Os << " but doesn't have virtual destructor";
  117. PathDiagnosticLocation BSLoc(BaseSpec->getSourceRange().getBegin(),
  118. BR->getSourceManager());
  119. auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
  120. Report->addRange(BaseSpec->getSourceRange());
  121. BR->emitReport(std::move(Report));
  122. }
  123. };
  124. } // namespace
  125. void ento::registerRefCntblBaseVirtualDtorChecker(CheckerManager &Mgr) {
  126. Mgr.registerChecker<RefCntblBaseVirtualDtorChecker>();
  127. }
  128. bool ento::shouldRegisterRefCntblBaseVirtualDtorChecker(
  129. const CheckerManager &mgr) {
  130. return true;
  131. }