1 //===--- UndefinedAssignmentChecker.h ---------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines UndefinedAssginmentChecker, a builtin check in ExprEngine that
11 // checks for assigning undefined values.
13 //===----------------------------------------------------------------------===//
15 #include "InternalChecks.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
19 using namespace clang
;
23 class UndefinedAssignmentChecker
24 : public CheckerVisitor
<UndefinedAssignmentChecker
> {
27 UndefinedAssignmentChecker() : BT(0) {}
28 static void *getTag();
29 virtual void PreVisitBind(CheckerContext
&C
, const Stmt
*StoreE
,
30 SVal location
, SVal val
);
34 void ento::RegisterUndefinedAssignmentChecker(ExprEngine
&Eng
){
35 Eng
.registerCheck(new UndefinedAssignmentChecker());
38 void *UndefinedAssignmentChecker::getTag() {
43 void UndefinedAssignmentChecker::PreVisitBind(CheckerContext
&C
,
50 ExplodedNode
*N
= C
.generateSink();
55 const char *str
= "Assigned value is garbage or undefined";
58 BT
= new BuiltinBug(str
);
60 // Generate a report for this bug.
64 if (const BinaryOperator
*B
= dyn_cast
<BinaryOperator
>(StoreE
)) {
65 if (B
->isCompoundAssignmentOp()) {
66 const GRState
*state
= C
.getState();
67 if (state
->getSVal(B
->getLHS()).isUndef()) {
68 str
= "The left expression of the compound assignment is an "
69 "uninitialized value. The computed value will also be garbage";
79 if (const DeclStmt
*DS
= dyn_cast
<DeclStmt
>(StoreE
)) {
80 const VarDecl
* VD
= dyn_cast
<VarDecl
>(DS
->getSingleDecl());
87 EnhancedBugReport
*R
= new EnhancedBugReport(*BT
, str
, N
);
89 R
->addRange(ex
->getSourceRange());
90 R
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
, ex
);