Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 1 | //== AdjustedReturnValueChecker.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 AdjustedReturnValueChecker, a simple check to see if the |
| 11 | // return value of a function call is different than the one the caller thinks |
| 12 | // it is. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "GRExprEngineInternalChecks.h" |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 17 | #include "clang/Checker/BugReporter/BugReporter.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 18 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 19 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | namespace { |
| 24 | class AdjustedReturnValueChecker : |
| 25 | public CheckerVisitor<AdjustedReturnValueChecker> { |
| 26 | public: |
| 27 | AdjustedReturnValueChecker() {} |
| 28 | |
| 29 | void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
| 30 | |
| 31 | static void *getTag() { |
| 32 | static int x = 0; return &x; |
| 33 | } |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | void clang::RegisterAdjustedReturnValueChecker(GRExprEngine &Eng) { |
| 38 | Eng.registerCheck(new AdjustedReturnValueChecker()); |
| 39 | } |
| 40 | |
| 41 | void AdjustedReturnValueChecker::PostVisitCallExpr(CheckerContext &C, |
| 42 | const CallExpr *CE) { |
| 43 | |
Ted Kremenek | 95a0112 | 2010-02-04 04:18:55 +0000 | [diff] [blame] | 44 | // Get the result type of the call. |
| 45 | QualType expectedResultTy = CE->getType(); |
| 46 | |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 47 | // Fetch the signature of the called function. |
| 48 | const GRState *state = C.getState(); |
| 49 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 50 | SVal V = state->getSVal(CE); |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 52 | if (V.isUnknown()) |
| 53 | return; |
Ted Kremenek | 95a0112 | 2010-02-04 04:18:55 +0000 | [diff] [blame] | 54 | |
| 55 | // Casting to void? Discard the value. |
| 56 | if (expectedResultTy->isVoidType()) { |
| 57 | C.GenerateNode(state->BindExpr(CE, UnknownVal())); |
| 58 | return; |
| 59 | } |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 61 | const MemRegion *callee = state->getSVal(CE->getCallee()).getAsRegion(); |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 62 | if (!callee) |
| 63 | return; |
| 64 | |
| 65 | QualType actualResultTy; |
| 66 | |
| 67 | if (const FunctionTextRegion *FT = dyn_cast<FunctionTextRegion>(callee)) { |
| 68 | const FunctionDecl *FD = FT->getDecl(); |
| 69 | actualResultTy = FD->getResultType(); |
| 70 | } |
| 71 | else if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(callee)) { |
| 72 | const BlockTextRegion *BR = BD->getCodeRegion(); |
| 73 | const BlockPointerType *BT = |
| 74 | BR->getLocationType(C.getASTContext())->getAs<BlockPointerType>(); |
| 75 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 76 | actualResultTy = FT->getResultType(); |
| 77 | } |
| 78 | |
| 79 | // Can this happen? |
| 80 | if (actualResultTy.isNull()) |
| 81 | return; |
| 82 | |
| 83 | // For now, ignore references. |
| 84 | if (actualResultTy->getAs<ReferenceType>()) |
| 85 | return; |
| 86 | |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 87 | |
| 88 | // Are they the same? |
| 89 | if (expectedResultTy != actualResultTy) { |
| 90 | // FIXME: Do more checking and actual emit an error. At least performing |
| 91 | // the cast avoids some assertion failures elsewhere. |
| 92 | SValuator &SVator = C.getSValuator(); |
Zhongxing Xu | 814e6b9 | 2010-02-04 04:56:43 +0000 | [diff] [blame] | 93 | V = SVator.EvalCast(V, expectedResultTy, actualResultTy); |
| 94 | C.GenerateNode(state->BindExpr(CE, V)); |
Ted Kremenek | 40c37e1 | 2010-02-04 00:47:48 +0000 | [diff] [blame] | 95 | } |
| 96 | } |