Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame^] | 1 | //=== UndefBranchChecker.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 UndefBranchChecker, which checks for undefined branch |
| 11 | // condition. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "GRExprEngineInternalChecks.h" |
| 16 | #include "clang/Analysis/PathSensitive/Checker.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class VISIBILITY_HIDDEN UndefBranchChecker : public Checker { |
| 23 | BuiltinBug *BT; |
| 24 | public: |
| 25 | UndefBranchChecker() : BT(0) {} |
| 26 | static void *getTag(); |
| 27 | void VisitBranchCondition(GRBranchNodeBuilder &Builder, GRExprEngine &Eng, |
| 28 | Stmt *Condition, void *tag); |
| 29 | }; |
| 30 | |
| 31 | } |
| 32 | |
| 33 | void clang::RegisterUndefBranchChecker(GRExprEngine &Eng) { |
| 34 | Eng.registerCheck(new UndefBranchChecker()); |
| 35 | } |
| 36 | |
| 37 | void *UndefBranchChecker::getTag() { |
| 38 | static int x; |
| 39 | return &x; |
| 40 | } |
| 41 | |
| 42 | void UndefBranchChecker::VisitBranchCondition(GRBranchNodeBuilder &Builder, |
| 43 | GRExprEngine &Eng, |
| 44 | Stmt *Condition, void *tag) { |
| 45 | const GRState *state = Builder.getState(); |
| 46 | SVal X = state->getSVal(Condition); |
| 47 | if (X.isUndef()) { |
| 48 | ExplodedNode *N = Builder.generateNode(state, true); |
| 49 | if (N) { |
| 50 | N->markAsSink(); |
| 51 | if (!BT) |
| 52 | BT = new BuiltinBug("Undefined branch", |
| 53 | "Branch condition evaluates to an undefined or garbage value"); |
| 54 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N); |
| 55 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 56 | Condition); |
| 57 | Eng.getBugReporter().EmitReport(R); |
| 58 | } |
| 59 | |
| 60 | Builder.markInfeasible(true); |
| 61 | Builder.markInfeasible(false); |
| 62 | } |
| 63 | } |