zcov: / lib/Checker/ReturnUndefChecker.cpp


Files: 1 Branches Taken: 58.3% 7 / 12
Generated: 2010-02-10 01:31 Branches Executed: 83.3% 10 / 12
Line Coverage: 90.9% 20 / 22


Programs: 1 Runs 2897


       1                 : //== ReturnUndefChecker.cpp -------------------------------------*- 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 file defines ReturnUndefChecker, which is a path-sensitive
      11                 : // check which looks for undefined or garbage values being returned to the
      12                 : // caller.
      13                 : //
      14                 : //===----------------------------------------------------------------------===//
      15                 : 
      16                 : #include "GRExprEngineInternalChecks.h"
      17                 : #include "clang/Checker/PathSensitive/GRExprEngine.h"
      18                 : #include "clang/Checker/BugReporter/BugReporter.h"
      19                 : #include "clang/Checker/PathSensitive/CheckerVisitor.h"
      20                 : #include "llvm/ADT/SmallString.h"
      21                 : 
      22                 : using namespace clang;
      23                 : 
      24                 : namespace {
      25                 : class ReturnUndefChecker : 
                     2138: branch 1 taken
                        0: branch 2 not taken
                        0: branch 5 not taken
                        0: branch 6 not taken
      26             2138:     public CheckerVisitor<ReturnUndefChecker> {      
      27                 :   BuiltinBug *BT;
      28                 : public:
      29             2138:     ReturnUndefChecker() : BT(0) {}
      30                 :     static void *getTag();
      31                 :     void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS);
      32                 : };
      33                 : }
      34                 : 
      35             2138: void clang::RegisterReturnUndefChecker(GRExprEngine &Eng) {
      36             2138:   Eng.registerCheck(new ReturnUndefChecker());
      37             2138: }
      38                 : 
      39             2138: void *ReturnUndefChecker::getTag() {
      40             2138:   static int x = 0; return &x;
      41                 : }
      42                 : 
      43                 : void ReturnUndefChecker::PreVisitReturnStmt(CheckerContext &C,
      44             1315:                                             const ReturnStmt *RS) {
      45                 :  
      46             1315:   const Expr *RetE = RS->getRetValue();
                      236: branch 0 taken
                     1079: branch 1 taken
      47             1315:   if (!RetE)
      48              236:     return;
      49                 :   
                     1071: branch 4 taken
                        8: branch 5 taken
      50             1079:   if (!C.getState()->getSVal(RetE).isUndef())
      51             1071:     return;
      52                 :   
      53                8:   ExplodedNode *N = C.GenerateSink();
      54                 : 
                        0: branch 0 not taken
                        8: branch 1 taken
      55                8:   if (!N)
      56                0:     return;
      57                 :   
                        8: branch 0 taken
                        0: branch 1 not taken
      58                8:   if (!BT)
      59                 :     BT = new BuiltinBug("Garbage return value",
      60                8:                         "Undefined or garbage value returned to caller");
      61                 :     
      62                 :   EnhancedBugReport *report = 
      63                8:     new EnhancedBugReport(*BT, BT->getDescription(), N);
      64                 : 
      65                8:   report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, RetE);
      66                 : 
      67                8:   C.EmitReport(report);
      68                0: }

Generated: 2010-02-10 01:31 by zcov