clang API Documentation

UndefCapturedBlockVarChecker.cpp
Go to the documentation of this file.
00001 // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This checker detects blocks that capture uninitialized values.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ClangSACheckers.h"
00015 #include "clang/StaticAnalyzer/Core/Checker.h"
00016 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00017 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
00019 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00020 #include "llvm/ADT/SmallString.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 
00023 using namespace clang;
00024 using namespace ento;
00025 
00026 namespace {
00027 class UndefCapturedBlockVarChecker
00028   : public Checker< check::PostStmt<BlockExpr> > {
00029  mutable OwningPtr<BugType> BT;
00030 
00031 public:
00032   void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
00033 };
00034 } // end anonymous namespace
00035 
00036 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
00037                                                const VarDecl *VD) {
00038   if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
00039     if (BR->getDecl() == VD)
00040       return BR;
00041 
00042   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
00043        I!=E; ++I)
00044     if (const Stmt *child = *I) {
00045       const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
00046       if (BR)
00047         return BR;
00048     }
00049 
00050   return NULL;
00051 }
00052 
00053 void
00054 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
00055                                             CheckerContext &C) const {
00056   if (!BE->getBlockDecl()->hasCaptures())
00057     return;
00058 
00059   ProgramStateRef state = C.getState();
00060   const BlockDataRegion *R =
00061     cast<BlockDataRegion>(state->getSVal(BE,
00062                                          C.getLocationContext()).getAsRegion());
00063 
00064   BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
00065                                             E = R->referenced_vars_end();
00066 
00067   for (; I != E; ++I) {
00068     // This VarRegion is the region associated with the block; we need
00069     // the one associated with the encompassing context.
00070     const VarRegion *VR = *I;
00071     const VarDecl *VD = VR->getDecl();
00072 
00073     if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
00074       continue;
00075 
00076     // Get the VarRegion associated with VD in the local stack frame.
00077     const LocationContext *LC = C.getLocationContext();
00078     VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
00079     SVal VRVal = state->getSVal(VR);
00080 
00081     if (VRVal.isUndef())
00082       if (ExplodedNode *N = C.generateSink()) {
00083         if (!BT)
00084           BT.reset(new BuiltinBug("uninitialized variable captured by block"));
00085 
00086         // Generate a bug report.
00087         SmallString<128> buf;
00088         llvm::raw_svector_ostream os(buf);
00089 
00090         os << "Variable '" << VD->getName() 
00091            << "' is uninitialized when captured by block";
00092 
00093         BugReport *R = new BugReport(*BT, os.str(), N);
00094         if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
00095           R->addRange(Ex->getSourceRange());
00096         R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
00097         // need location of block
00098         C.EmitReport(R);
00099       }
00100   }
00101 }
00102 
00103 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
00104   mgr.registerChecker<UndefCapturedBlockVarChecker>();
00105 }