UncountedLambdaCapturesChecker.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //=======- UncountedLambdaCapturesChecker.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. using namespace clang;
  17. using namespace ento;
  18. namespace {
  19. class UncountedLambdaCapturesChecker
  20. : public Checker<check::ASTDecl<TranslationUnitDecl>> {
  21. private:
  22. BugType Bug{this, "Lambda capture of uncounted variable",
  23. "WebKit coding guidelines"};
  24. mutable BugReporter *BR;
  25. public:
  26. void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR,
  27. BugReporter &BRArg) const {
  28. BR = &BRArg;
  29. // The calls to checkAST* from AnalysisConsumer don't
  30. // visit template instantiations or lambda classes. We
  31. // want to visit those, so we make our own RecursiveASTVisitor.
  32. struct LocalVisitor : public RecursiveASTVisitor<LocalVisitor> {
  33. const UncountedLambdaCapturesChecker *Checker;
  34. explicit LocalVisitor(const UncountedLambdaCapturesChecker *Checker)
  35. : Checker(Checker) {
  36. assert(Checker);
  37. }
  38. bool shouldVisitTemplateInstantiations() const { return true; }
  39. bool shouldVisitImplicitCode() const { return false; }
  40. bool VisitLambdaExpr(LambdaExpr *L) {
  41. Checker->visitLambdaExpr(L);
  42. return true;
  43. }
  44. };
  45. LocalVisitor visitor(this);
  46. visitor.TraverseDecl(const_cast<TranslationUnitDecl *>(TUD));
  47. }
  48. void visitLambdaExpr(LambdaExpr *L) const {
  49. for (const LambdaCapture &C : L->captures()) {
  50. if (C.capturesVariable()) {
  51. VarDecl *CapturedVar = C.getCapturedVar();
  52. if (auto *CapturedVarType = CapturedVar->getType().getTypePtrOrNull()) {
  53. Optional<bool> IsUncountedPtr = isUncountedPtr(CapturedVarType);
  54. if (IsUncountedPtr && *IsUncountedPtr) {
  55. reportBug(C, CapturedVar, CapturedVarType);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. void reportBug(const LambdaCapture &Capture, VarDecl *CapturedVar,
  62. const Type *T) const {
  63. assert(CapturedVar);
  64. SmallString<100> Buf;
  65. llvm::raw_svector_ostream Os(Buf);
  66. if (Capture.isExplicit()) {
  67. Os << "Captured ";
  68. } else {
  69. Os << "Implicitly captured ";
  70. }
  71. if (T->isPointerType()) {
  72. Os << "raw-pointer ";
  73. } else {
  74. assert(T->isReferenceType());
  75. Os << "reference ";
  76. }
  77. printQuotedQualifiedName(Os, Capture.getCapturedVar());
  78. Os << " to uncounted type is unsafe.";
  79. PathDiagnosticLocation BSLoc(Capture.getLocation(), BR->getSourceManager());
  80. auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
  81. BR->emitReport(std::move(Report));
  82. }
  83. };
  84. } // namespace
  85. void ento::registerUncountedLambdaCapturesChecker(CheckerManager &Mgr) {
  86. Mgr.registerChecker<UncountedLambdaCapturesChecker>();
  87. }
  88. bool ento::shouldRegisterUncountedLambdaCapturesChecker(
  89. const CheckerManager &mgr) {
  90. return true;
  91. }