blob: 4273f0cf3157c713b81c2a14a75a809c9417f80f [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
Zhongxing Xuc3372e02009-11-22 12:29:52 +000015#include "GRExprEngineInternalChecks.h"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000016#include "clang/GR/BugReporter/BugType.h"
17#include "clang/GR/PathSensitive/CheckerVisitor.h"
Ted Kremenekb107c4b2009-11-04 04:24:16 +000018
19using namespace clang;
20
Zhongxing Xuc3372e02009-11-22 12:29:52 +000021namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000022class UndefinedAssignmentChecker
Zhongxing Xuc3372e02009-11-22 12:29:52 +000023 : public CheckerVisitor<UndefinedAssignmentChecker> {
24 BugType *BT;
25public:
26 UndefinedAssignmentChecker() : BT(0) {}
27 static void *getTag();
Ted Kremenek79d73042010-09-02 00:56:20 +000028 virtual void PreVisitBind(CheckerContext &C, const Stmt *StoreE,
29 SVal location, SVal val);
Zhongxing Xuc3372e02009-11-22 12:29:52 +000030};
31}
32
33void clang::RegisterUndefinedAssignmentChecker(GRExprEngine &Eng){
34 Eng.registerCheck(new UndefinedAssignmentChecker());
35}
36
Ted Kremenekb107c4b2009-11-04 04:24:16 +000037void *UndefinedAssignmentChecker::getTag() {
38 static int x = 0;
39 return &x;
40}
41
Ted Kremenek50ecd152009-11-05 00:42:23 +000042void UndefinedAssignmentChecker::PreVisitBind(CheckerContext &C,
Ted Kremenek50ecd152009-11-05 00:42:23 +000043 const Stmt *StoreE,
Ted Kremenekb107c4b2009-11-04 04:24:16 +000044 SVal location,
45 SVal val) {
46 if (!val.isUndef())
47 return;
48
Ted Kremenekd048c6e2010-12-20 21:19:09 +000049 ExplodedNode *N = C.generateSink();
Ted Kremenekb107c4b2009-11-04 04:24:16 +000050
51 if (!N)
52 return;
53
Ted Kremenek12182a02010-03-22 22:16:26 +000054 const char *str = "Assigned value is garbage or undefined";
55
Ted Kremenekb107c4b2009-11-04 04:24:16 +000056 if (!BT)
Ted Kremenek12182a02010-03-22 22:16:26 +000057 BT = new BuiltinBug(str);
Ted Kremenekb107c4b2009-11-04 04:24:16 +000058
59 // Generate a report for this bug.
Ted Kremenek12182a02010-03-22 22:16:26 +000060 const Expr *ex = 0;
Ted Kremenekb107c4b2009-11-04 04:24:16 +000061
Ted Kremenek79d73042010-09-02 00:56:20 +000062 while (StoreE) {
63 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
Ted Kremenek12182a02010-03-22 22:16:26 +000064 if (B->isCompoundAssignmentOp()) {
65 const GRState *state = C.getState();
66 if (state->getSVal(B->getLHS()).isUndef()) {
67 str = "The left expression of the compound assignment is an "
68 "uninitialized value. The computed value will also be garbage";
69 ex = B->getLHS();
70 break;
71 }
72 }
Ted Kremenekb107c4b2009-11-04 04:24:16 +000073
Ted Kremenek50ecd152009-11-05 00:42:23 +000074 ex = B->getRHS();
Ted Kremenek12182a02010-03-22 22:16:26 +000075 break;
76 }
77
Ted Kremenek79d73042010-09-02 00:56:20 +000078 if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
Ted Kremenek50ecd152009-11-05 00:42:23 +000079 const VarDecl* VD = dyn_cast<VarDecl>(DS->getSingleDecl());
80 ex = VD->getInit();
81 }
Ted Kremenek12182a02010-03-22 22:16:26 +000082
83 break;
Ted Kremenekb107c4b2009-11-04 04:24:16 +000084 }
85
Ted Kremenek12182a02010-03-22 22:16:26 +000086 EnhancedBugReport *R = new EnhancedBugReport(*BT, str, N);
87 if (ex) {
88 R->addRange(ex->getSourceRange());
89 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, ex);
90 }
Ted Kremenekb107c4b2009-11-04 04:24:16 +000091 C.EmitReport(R);
Ted Kremenek12182a02010-03-22 22:16:26 +000092}
Ted Kremenekb107c4b2009-11-04 04:24:16 +000093