clang  6.0.0svn
UndefCapturedBlockVarChecker.cpp
Go to the documentation of this file.
1 // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This checker detects blocks that capture uninitialized values.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ClangSACheckers.h"
15 #include "clang/AST/Attr.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 namespace {
28 class UndefCapturedBlockVarChecker
29  : public Checker< check::PostStmt<BlockExpr> > {
30  mutable std::unique_ptr<BugType> BT;
31 
32 public:
33  void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
34 };
35 } // end anonymous namespace
36 
37 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
38  const VarDecl *VD) {
39  if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
40  if (BR->getDecl() == VD)
41  return BR;
42 
43  for (const Stmt *Child : S->children())
44  if (Child)
45  if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
46  return BR;
47 
48  return nullptr;
49 }
50 
51 void
52 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
53  CheckerContext &C) const {
54  if (!BE->getBlockDecl()->hasCaptures())
55  return;
56 
58  const BlockDataRegion *R =
59  cast<BlockDataRegion>(state->getSVal(BE,
60  C.getLocationContext()).getAsRegion());
61 
63  E = R->referenced_vars_end();
64 
65  for (; I != E; ++I) {
66  // This VarRegion is the region associated with the block; we need
67  // the one associated with the encompassing context.
68  const VarRegion *VR = I.getCapturedRegion();
69  const VarDecl *VD = VR->getDecl();
70 
71  if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
72  continue;
73 
74  // Get the VarRegion associated with VD in the local stack frame.
76  state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
77  if (ExplodedNode *N = C.generateErrorNode()) {
78  if (!BT)
79  BT.reset(
80  new BuiltinBug(this, "uninitialized variable captured by block"));
81 
82  // Generate a bug report.
83  SmallString<128> buf;
84  llvm::raw_svector_ostream os(buf);
85 
86  os << "Variable '" << VD->getName()
87  << "' is uninitialized when captured by block";
88 
89  auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
90  if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
91  R->addRange(Ex->getSourceRange());
92  R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
93  *V, VR, /*EnableNullFPSuppression*/ false));
94  R->disablePathPruning();
95  // need location of block
96  C.emitReport(std::move(R));
97  }
98  }
99  }
100 }
101 
102 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
103  mgr.registerChecker<UndefCapturedBlockVarChecker>();
104 }
const BlockDecl * getBlockDecl() const
Definition: Expr.h:4865
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
bool hasCaptures() const
hasCaptures - True if this block (or its nested blocks) captures anything of local storage from its e...
Definition: Decl.h:3807
static const DeclRefExpr * FindBlockDeclRefExpr(const Stmt *S, const VarDecl *VD)
Stmt - This represents one statement.
Definition: Stmt.h:66
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:807
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
BlockDataRegion - A region that represents a block instance.
Definition: MemRegion.h:656
child_range children()
Definition: Stmt.cpp:226
referenced_vars_iterator referenced_vars_end() const
Definition: MemRegion.cpp:1460
const Stmt * getBody() const
Definition: Expr.cpp:2012
bool hasAttr() const
Definition: DeclBase.h:535
Expr - This represents one expression.
Definition: Expr.h:106
bool hasLocalStorage() const
hasLocalStorage - Returns true if a variable with function scope is a non-static local variable...
Definition: Decl.h:1026
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4851
const VarDecl * getDecl() const
Definition: MemRegion.h:935
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
referenced_vars_iterator referenced_vars_begin() const
Definition: MemRegion.cpp:1443
Dataflow Directional Tag Classes.
const ProgramStateRef & getState() const
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:270
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:956
const LocationContext * getLocationContext() const