Index: clang/include/clang/StaticAnalyzer/Checkers/Checkers.td =================================================================== --- clang/include/clang/StaticAnalyzer/Checkers/Checkers.td +++ clang/include/clang/StaticAnalyzer/Checkers/Checkers.td @@ -1635,6 +1635,10 @@ HelpText<"Check for no uncounted member variables.">, Documentation; +def UncountedLambdaCapturesChecker : Checker<"UncountedLambdaCapturesChecker">, + HelpText<"Check uncounted lambda captures.">, + Documentation; + } // end webkit let ParentPackage = WebKitAlpha in { Index: clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt =================================================================== --- clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt +++ clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt @@ -126,6 +126,7 @@ WebKit/PtrTypesSemantics.cpp WebKit/RefCntblBaseVirtualDtorChecker.cpp WebKit/UncountedCallArgsChecker.cpp + WebKit/UncountedLambdaCapturesChecker.cpp LINK_LIBS clangAST Index: clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp =================================================================== --- /dev/null +++ clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp @@ -0,0 +1,100 @@ +//=======- RefCntblBaseVirtualDtor.cpp ---------------------------*- C++ -*-==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "DiagOutputUtils.h" +#include "PtrTypesSemantics.h" +#include "clang/AST/CXXInheritance.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" +#include "clang/StaticAnalyzer/Core/Checker.h" + +using namespace clang; +using namespace ento; + +namespace { +class UncountedLambdaCapturesChecker + : public Checker> { +private: + BugType Bug{this, + "Lambda capture of uncounted variable", + "WebKit coding guidelines"}; + mutable BugReporter *BR; + +public: + void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR, + BugReporter &BRArg) const { + BR = &BRArg; + + // The calls to checkAST* from AnalysisConsumer don't + // visit template instantiations or lambda classes. We + // want to visit those, so we make our own RecursiveASTVisitor. + struct LocalVisitor : public RecursiveASTVisitor { + const UncountedLambdaCapturesChecker *Checker; + explicit LocalVisitor(const UncountedLambdaCapturesChecker *Checker) + : Checker(Checker) { + assert(Checker); + } + + bool shouldVisitTemplateInstantiations() const { return true; } + bool shouldVisitImplicitCode() const { return false; } + + bool VisitLambdaExpr(LambdaExpr *L) { + Checker->visitLambdaExpr(L); + return true; + } + }; + + LocalVisitor visitor(this); + visitor.TraverseDecl(const_cast(TUD)); + } + + void visitLambdaExpr(LambdaExpr *L) const { + for (const LambdaCapture& C : L->captures()) { + if (C.capturesVariable()) { + VarDecl* CapturedVar = C.getCapturedVar(); + if (auto* CapturedVarType = CapturedVar->getType().getTypePtrOrNull() ) { + if (isUncountedPtr(CapturedVarType)) { + reportBug(C, CapturedVar); + } + } + } + } + } + + void reportBug(const LambdaCapture& Capture, VarDecl* CapturedVar) const { + assert(CapturedVar); + + SmallString<100> Buf; + llvm::raw_svector_ostream Os(Buf); + + if (Capture.isExplicit()) { + Os << "Captured raw-pointer/reference to uncounted type is unsafe."; + } else { + Os << "Implicitly captured raw-pointer/reference "; + printQuotedQualifiedName(Os, Capture.getCapturedVar()); + Os << " to uncounted type is unsafe."; + } + + PathDiagnosticLocation BSLoc(Capture.getLocation(), + BR->getSourceManager()); + auto Report = std::make_unique(Bug, Os.str(), BSLoc); + BR->emitReport(std::move(Report)); + } +}; +} // namespace + +void ento::registerUncountedLambdaCapturesChecker(CheckerManager &Mgr) { + Mgr.registerChecker(); +} + +bool ento::shouldRegisterUncountedLambdaCapturesChecker( + const CheckerManager &mgr) { + return true; +} Index: clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp @@ -0,0 +1,41 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=webkit.UncountedLambdaCapturesChecker -verify %s + +#include "mock-types.h" + +void simple() { + RefCountable* ref_countable = nullptr; + auto foo1 = [ref_countable](){}; + // expected-warning@-1{{Captured}} + auto foo2 = [&ref_countable](){}; + // expected-warning@-1{{Captured}} + auto foo3 = [&](){ ref_countable = nullptr; }; + // expected-warning@-1{{Implicitly captured}} + auto foo4 = [=](){ (void) ref_countable; }; + // expected-warning@-1{{Implicitly captured}} +} + +void refernces() { + RefCountable automatic; + RefCountable& ref_countable_ref = automatic; + + auto foo1 = [ref_countable_ref](){}; + // expected-warning@-1{{Captured}} + auto foo2 = [&ref_countable_ref](){}; + // expected-warning@-1{{Captured}} + auto foo3 = [&](){ (void) ref_countable_ref; }; + // expected-warning@-1{{Implicitly captured}} + auto foo4 = [=](){ (void) ref_countable_ref; }; + // expected-warning@-1{{Implicitly captured}} +} + +// This code is not expected to trigger any warnings. +void quiet() { + { + RefCountable automatic; + RefCountable& ref_countable_ref = automatic; + } + + auto foo3 = [&](){}; + auto foo4 = [=](){}; + RefCountable* ref_countable = nullptr; +}