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