Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 1 | //=== NoReturnFunctionChecker.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 defines NoReturnFunctionChecker, which evaluates functions that do not |
| 11 | // return to the caller. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "GRExprEngineInternalChecks.h" |
Ted Kremenek | cfd8ea9 | 2010-03-26 22:57:13 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | |
Ted Kremenek | cfd8ea9 | 2010-03-26 22:57:13 +0000 | [diff] [blame] | 23 | class NoReturnFunctionChecker : public CheckerVisitor<NoReturnFunctionChecker> { |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 24 | public: |
| 25 | static void *getTag() { static int tag = 0; return &tag; } |
Ted Kremenek | cfd8ea9 | 2010-03-26 22:57:13 +0000 | [diff] [blame] | 26 | void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | } |
| 30 | |
| 31 | void clang::RegisterNoReturnFunctionChecker(GRExprEngine &Eng) { |
| 32 | Eng.registerCheck(new NoReturnFunctionChecker()); |
| 33 | } |
| 34 | |
Ted Kremenek | cfd8ea9 | 2010-03-26 22:57:13 +0000 | [diff] [blame] | 35 | void NoReturnFunctionChecker::PostVisitCallExpr(CheckerContext &C, |
| 36 | const CallExpr *CE) { |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 37 | const GRState *state = C.getState(); |
| 38 | const Expr *Callee = CE->getCallee(); |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 39 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 40 | bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn(); |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | cfd8ea9 | 2010-03-26 22:57:13 +0000 | [diff] [blame] | 42 | if (!BuildSinks) { |
| 43 | SVal L = state->getSVal(Callee); |
| 44 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
| 45 | if (!FD) |
| 46 | return; |
| 47 | |
| 48 | if (FD->getAttr<AnalyzerNoReturnAttr>()) |
| 49 | BuildSinks = true; |
| 50 | else if (const IdentifierInfo *II = FD->getIdentifier()) { |
| 51 | // HACK: Some functions are not marked noreturn, and don't return. |
| 52 | // Here are a few hardwired ones. If this takes too long, we can |
| 53 | // potentially cache these results. |
| 54 | BuildSinks |
| 55 | = llvm::StringSwitch<bool>(llvm::StringRef(II->getName())) |
| 56 | .Case("exit", true) |
| 57 | .Case("panic", true) |
| 58 | .Case("error", true) |
| 59 | .Case("Assert", true) |
| 60 | // FIXME: This is just a wrapper around throwing an exception. |
| 61 | // Eventually inter-procedural analysis should handle this easily. |
| 62 | .Case("ziperr", true) |
| 63 | .Case("assfail", true) |
| 64 | .Case("db_error", true) |
| 65 | .Case("__assert", true) |
| 66 | .Case("__assert_rtn", true) |
| 67 | .Case("__assert_fail", true) |
| 68 | .Case("dtrace_assfail", true) |
| 69 | .Case("yy_fatal_error", true) |
| 70 | .Case("_XCAssertionFailureHandler", true) |
| 71 | .Case("_DTAssertionFailureHandler", true) |
| 72 | .Case("_TSAssertionFailureHandler", true) |
| 73 | .Default(false); |
| 74 | } |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 75 | } |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | cfd8ea9 | 2010-03-26 22:57:13 +0000 | [diff] [blame] | 77 | if (BuildSinks) |
| 78 | C.GenerateSink(CE); |
Zhongxing Xu | 935ef90 | 2009-12-07 09:17:35 +0000 | [diff] [blame] | 79 | } |