blob: 9df58844dc3130a66416f89bf5f4f889ee5eca09 [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
25void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C,
26 const Stmt *S,
27 SVal location,
28 SVal val) {
29 if (!val.isUndef())
30 return;
31
32 ExplodedNode *N = C.GenerateNode(S, true);
33
34 if (!N)
35 return;
36
37 if (!BT)
38 BT = new BugType("Assigned value is garbage or undefined",
39 "Logic error");
40
41 // Generate a report for this bug.
42 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName().c_str(), N);
43 const Expr *ex = 0;
44
45 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
46 ex = B->getRHS();
47 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
48 const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
49 ex = VD->getInit();
50 }
51
52 if (ex) {
53 R->addRange(ex->getSourceRange());
54 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
55 }
56
57 C.EmitReport(R);
58}
59