Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 1 | //===--- UndefinedArgChecker.h - Undefined arguments checker ----*- 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 BadCallChecker, a builtin check in GRExprEngine that performs |
| 11 | // checks for undefined arguments. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 17 | #include "GRExprEngineInternalChecks.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | class VISIBILITY_HIDDEN UndefinedArgChecker |
| 23 | : public CheckerVisitor<UndefinedArgChecker> { |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 24 | BugType *BT_call; |
| 25 | BugType *BT_msg; |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 26 | public: |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 27 | UndefinedArgChecker() : BT_call(0), BT_msg(0) {} |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 28 | static void *getTag() { |
| 29 | static int x = 0; |
| 30 | return &x; |
| 31 | } |
| 32 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 33 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 34 | }; |
| 35 | } // end anonymous namespace |
| 36 | |
| 37 | void clang::RegisterUndefinedArgChecker(GRExprEngine &Eng) { |
| 38 | Eng.registerCheck(new UndefinedArgChecker()); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void UndefinedArgChecker::PreVisitCallExpr(CheckerContext &C, |
| 42 | const CallExpr *CE){ |
| 43 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 44 | I != E; ++I) { |
| 45 | if (C.getState()->getSVal(*I).isUndef()) { |
| 46 | if (ExplodedNode *N = C.GenerateNode(CE, true)) { |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 47 | if (!BT_call) |
| 48 | BT_call = new BuiltinBug("Pass-by-value argument in function call is " |
| 49 | "undefined"); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 50 | // Generate a report for this bug. |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 51 | EnhancedBugReport *R = new EnhancedBugReport(*BT_call, |
| 52 | BT_call->getName(), N); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 53 | R->addRange((*I)->getSourceRange()); |
| 54 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I); |
| 55 | C.EmitReport(R); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void UndefinedArgChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 63 | const ObjCMessageExpr *ME) { |
| 64 | |
| 65 | // Check for any arguments that are uninitialized/undefined. |
| 66 | const GRState *state = C.getState(); |
| 67 | for (ObjCMessageExpr::const_arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 68 | I != E; ++I) { |
| 69 | if (state->getSVal(*I).isUndef()) { |
| 70 | if (ExplodedNode *N = C.GenerateNode(ME, true)) { |
| 71 | if (!BT_msg) |
| 72 | BT_msg = new BuiltinBug("Pass-by-value argument in message expression" |
| 73 | " is undefined"); |
| 74 | // Generate a report for this bug. |
| 75 | EnhancedBugReport *R = new EnhancedBugReport(*BT_msg, BT_msg->getName(), |
| 76 | N); |
| 77 | R->addRange((*I)->getSourceRange()); |
| 78 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I); |
| 79 | C.EmitReport(R); |
| 80 | return; |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |