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