2009-12-07 17:17:35 +08:00
|
|
|
//=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines NoReturnFunctionChecker, which evaluates functions that do not
|
|
|
|
// return to the caller.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/Checker.h"
|
2009-12-07 17:17:35 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class NoReturnFunctionChecker : public Checker {
|
|
|
|
public:
|
|
|
|
static void *getTag() { static int tag = 0; return &tag; }
|
|
|
|
virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::RegisterNoReturnFunctionChecker(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new NoReturnFunctionChecker());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NoReturnFunctionChecker::EvalCallExpr(CheckerContext &C,
|
|
|
|
const CallExpr *CE) {
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
const Expr *Callee = CE->getCallee();
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal L = state->getSVal(Callee);
|
2009-12-07 17:17:35 +08:00
|
|
|
const FunctionDecl *FD = L.getAsFunctionDecl();
|
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool BuildSinks = false;
|
|
|
|
|
|
|
|
if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
|
|
|
|
BuildSinks = true;
|
2009-12-16 13:58:28 +08:00
|
|
|
else if (const IdentifierInfo *II = FD->getIdentifier()) {
|
2009-12-07 17:17:35 +08:00
|
|
|
// HACK: Some functions are not marked noreturn, and don't return.
|
|
|
|
// Here are a few hardwired ones. If this takes too long, we can
|
|
|
|
// potentially cache these results.
|
|
|
|
BuildSinks
|
2009-12-16 13:58:28 +08:00
|
|
|
= llvm::StringSwitch<bool>(llvm::StringRef(II->getName()))
|
2009-12-07 17:17:35 +08:00
|
|
|
.Case("exit", true)
|
|
|
|
.Case("panic", true)
|
|
|
|
.Case("error", true)
|
|
|
|
.Case("Assert", true)
|
|
|
|
// FIXME: This is just a wrapper around throwing an exception.
|
|
|
|
// Eventually inter-procedural analysis should handle this easily.
|
|
|
|
.Case("ziperr", true)
|
|
|
|
.Case("assfail", true)
|
|
|
|
.Case("db_error", true)
|
|
|
|
.Case("__assert", true)
|
|
|
|
.Case("__assert_rtn", true)
|
|
|
|
.Case("__assert_fail", true)
|
|
|
|
.Case("dtrace_assfail", true)
|
|
|
|
.Case("yy_fatal_error", true)
|
|
|
|
.Case("_XCAssertionFailureHandler", true)
|
|
|
|
.Case("_DTAssertionFailureHandler", true)
|
|
|
|
.Case("_TSAssertionFailureHandler", true)
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BuildSinks)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
C.GenerateSink(CE);
|
|
|
|
return true;
|
|
|
|
}
|