clang  14.0.0git
UncountedLambdaCapturesChecker.cpp
Go to the documentation of this file.
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"
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  Optional<bool> IsUncountedPtr = isUncountedPtr(CapturedVarType);
63  if (IsUncountedPtr && *IsUncountedPtr) {
64  reportBug(C, CapturedVar, CapturedVarType);
65  }
66  }
67  }
68  }
69  }
70 
71  void reportBug(const LambdaCapture &Capture, VarDecl *CapturedVar,
72  const Type *T) const {
73  assert(CapturedVar);
74 
75  SmallString<100> Buf;
76  llvm::raw_svector_ostream Os(Buf);
77 
78  if (Capture.isExplicit()) {
79  Os << "Captured ";
80  } else {
81  Os << "Implicitly captured ";
82  }
83  if (T->isPointerType()) {
84  Os << "raw-pointer ";
85  } else {
86  assert(T->isReferenceType());
87  Os << "reference ";
88  }
89 
90  printQuotedQualifiedName(Os, Capture.getCapturedVar());
91  Os << " to uncounted type is unsafe.";
92 
93  PathDiagnosticLocation BSLoc(Capture.getLocation(), BR->getSourceManager());
94  auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
95  BR->emitReport(std::move(Report));
96  }
97 };
98 } // namespace
99 
100 void ento::registerUncountedLambdaCapturesChecker(CheckerManager &Mgr) {
101  Mgr.registerChecker<UncountedLambdaCapturesChecker>();
102 }
103 
104 bool ento::shouldRegisterUncountedLambdaCapturesChecker(
105  const CheckerManager &mgr) {
106  return true;
107 }
clang::sema::Capture::getLocation
SourceLocation getLocation() const
Retrieve the location at which this variable was captured.
Definition: ScopeInfo.h:649
clang::LambdaCapture
Describes the capture of a variable or of this, or of a C++1y init-capture.
Definition: LambdaCapture.h:25
CXXInheritance.h
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:54
clang::isUncountedPtr
llvm::Optional< bool > isUncountedPtr(const Type *T)
Definition: PtrTypesSemantics.cpp:127
llvm::Optional< bool >
clang::sema::Capture
Definition: ScopeInfo.h:529
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6684
clang::TranslationUnitDecl
The top declaration context.
Definition: Decl.h:82
BuiltinCheckerRegistration.h
clang::LambdaExpr
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1865
DiagOutputUtils.h
BugReporter.h
clang::RecursiveASTVisitor
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Definition: RecursiveASTVisitor.h:164
llvm::SmallString
Definition: LLVM.h:37
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
PtrTypesSemantics.h
clang::Type::isPointerType
bool isPointerType() const
Definition: Type.h:6672
BugType.h
clang::printQuotedQualifiedName
void printQuotedQualifiedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
Definition: DiagOutputUtils.h:18
clang::LambdaExpr::captures
capture_range captures() const
Retrieve this lambda's captures.
Definition: ExprCXX.cpp:1222
Checker.h
clang
Definition: CalledOnceCheck.h:17
RecursiveASTVisitor.h
clang::QualType::getTypePtrOrNull
const Type * getTypePtrOrNull() const
Definition: Type.h:6428
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:687