blob: 2e3ac34913ab805a562856ba70f2fda644134125 [file] [log] [blame]
Ted Kremenekb107c4b2009-11-04 04:24:16 +00001//===--- UndefinedAssignmentChecker.h ---------------------------*- 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 defines UndefinedAssginmentChecker, a builtin check in GRExprEngine that
11// checks for assigning undefined values.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/Checkers/UndefinedAssignmentChecker.h"
16#include "clang/Analysis/PathSensitive/BugReporter.h"
17
18using namespace clang;
19
20void *UndefinedAssignmentChecker::getTag() {
21 static int x = 0;
22 return &x;
23}
24
Ted Kremenek50ecd152009-11-05 00:42:23 +000025void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C,
26 const Stmt *AssignE,
27 const Stmt *StoreE,
Ted Kremenekb107c4b2009-11-04 04:24:16 +000028 SVal location,
29 SVal val) {
30 if (!val.isUndef())
31 return;
32
Ted Kremenek50ecd152009-11-05 00:42:23 +000033 ExplodedNode *N = C.GenerateNode(StoreE, true);
Ted Kremenekb107c4b2009-11-04 04:24:16 +000034
35 if (!N)
36 return;
37
38 if (!BT)
39 BT = new BugType("Assigned value is garbage or undefined",
40 "Logic error");
41
42 // Generate a report for this bug.
43 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName().c_str(), N);
Ted Kremenekb107c4b2009-11-04 04:24:16 +000044
Ted Kremenek50ecd152009-11-05 00:42:23 +000045 if (AssignE) {
46 const Expr *ex = 0;
Ted Kremenekb107c4b2009-11-04 04:24:16 +000047
Ted Kremenek50ecd152009-11-05 00:42:23 +000048 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(AssignE))
49 ex = B->getRHS();
50 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(AssignE)) {
51 const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
52 ex = VD->getInit();
53 }
54 if (ex) {
55 R->addRange(ex->getSourceRange());
56 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
57 }
Ted Kremenekb107c4b2009-11-04 04:24:16 +000058 }
59
60 C.EmitReport(R);
61}
62