UncountedLambdaCapturesChecker.cpp 3.5 KB

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