summaryrefslogtreecommitdiffstats
path: root/clang/lib/Checker/AdjustedReturnValueChecker.cpp
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2010-02-04 00:47:48 +0000
committerTed Kremenek <kremenek@apple.com>2010-02-04 00:47:48 +0000
commit1e3b95580c3e2327377a0ea846bc63fc9b845a69 (patch)
treeac98ca97ce44628334b9fde68b3cbda0c350a29b /clang/lib/Checker/AdjustedReturnValueChecker.cpp
parentb8355e3b9f4419f7cd0c85bcddc58feb4b26d07a (diff)
downloadbcm5719-llvm-1e3b95580c3e2327377a0ea846bc63fc9b845a69.tar.gz
bcm5719-llvm-1e3b95580c3e2327377a0ea846bc63fc9b845a69.zip
static analyzer: handle casts of a function to a function pointer with
a different return type. While we don't emit any errors (yet), at least we avoid cases where we might crash because of an assertion failure later on (when the return type differs from what is expected). llvm-svn: 95268
Diffstat (limited to 'clang/lib/Checker/AdjustedReturnValueChecker.cpp')
-rw-r--r--clang/lib/Checker/AdjustedReturnValueChecker.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/clang/lib/Checker/AdjustedReturnValueChecker.cpp b/clang/lib/Checker/AdjustedReturnValueChecker.cpp
new file mode 100644
index 00000000000..898ce76a991
--- /dev/null
+++ b/clang/lib/Checker/AdjustedReturnValueChecker.cpp
@@ -0,0 +1,90 @@
+//== AdjustedReturnValueChecker.cpp -----------------------------*- C++ -*--==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines AdjustedReturnValueChecker, a simple check to see if the
+// return value of a function call is different than the one the caller thinks
+// it is.
+//
+//===----------------------------------------------------------------------===//
+
+#include "GRExprEngineInternalChecks.h"
+#include "clang/Checker/PathSensitive/GRExprEngine.h"
+#include "clang/Checker/BugReporter/BugReporter.h"
+#include "clang/Checker/PathSensitive/CheckerVisitor.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang;
+
+namespace {
+class AdjustedReturnValueChecker :
+ public CheckerVisitor<AdjustedReturnValueChecker> {
+public:
+ AdjustedReturnValueChecker() {}
+
+ void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE);
+
+ static void *getTag() {
+ static int x = 0; return &x;
+ }
+};
+}
+
+void clang::RegisterAdjustedReturnValueChecker(GRExprEngine &Eng) {
+ Eng.registerCheck(new AdjustedReturnValueChecker());
+}
+
+void AdjustedReturnValueChecker::PostVisitCallExpr(CheckerContext &C,
+ const CallExpr *CE) {
+
+ // Fetch the signature of the called function.
+ const GRState *state = C.getState();
+
+ SVal V = state->getSVal(CE);
+ if (V.isUnknown())
+ return;
+
+ const MemRegion *callee = state->getSVal(CE->getCallee()).getAsRegion();
+ if (!callee)
+ return;
+
+ QualType actualResultTy;
+
+ if (const FunctionTextRegion *FT = dyn_cast<FunctionTextRegion>(callee)) {
+ const FunctionDecl *FD = FT->getDecl();
+ actualResultTy = FD->getResultType();
+ }
+ else if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(callee)) {
+ const BlockTextRegion *BR = BD->getCodeRegion();
+ const BlockPointerType *BT =
+ BR->getLocationType(C.getASTContext())->getAs<BlockPointerType>();
+ const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>();
+ actualResultTy = FT->getResultType();
+ }
+
+ // Can this happen?
+ if (actualResultTy.isNull())
+ return;
+
+ // For now, ignore references.
+ if (actualResultTy->getAs<ReferenceType>())
+ return;
+
+ // Get the result type of the call.
+ QualType expectedResultTy = CE->getType();
+
+ // Are they the same?
+ if (expectedResultTy != actualResultTy) {
+ // FIXME: Do more checking and actual emit an error. At least performing
+ // the cast avoids some assertion failures elsewhere.
+ SValuator &SVator = C.getSValuator();
+ const SValuator::CastResult &R = SVator.EvalCast(V, state, expectedResultTy, actualResultTy);
+ C.GenerateNode(R.getState()->BindExpr(CE, R.getSVal()));
+ }
+}
OpenPOWER on IntegriCloud