1 //=== NoReturnFunctionChecker.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines NoReturnFunctionChecker, which evaluates functions that do not
11 // return to the caller.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
20 #include "llvm/ADT/StringSwitch.h"
23 using namespace clang;
28 class NoReturnFunctionChecker : public Checker< check::PostStmt<CallExpr>,
29 check::PostObjCMessage > {
31 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
32 void checkPostObjCMessage(const ObjCMessage &msg, CheckerContext &C) const;
37 void NoReturnFunctionChecker::checkPostStmt(const CallExpr *CE,
38 CheckerContext &C) const {
39 const ProgramState *state = C.getState();
40 const Expr *Callee = CE->getCallee();
42 bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
45 SVal L = state->getSVal(Callee);
46 const FunctionDecl *FD = L.getAsFunctionDecl();
50 if (FD->getAttr<AnalyzerNoReturnAttr>())
52 else if (const IdentifierInfo *II = FD->getIdentifier()) {
53 // HACK: Some functions are not marked noreturn, and don't return.
54 // Here are a few hardwired ones. If this takes too long, we can
55 // potentially cache these results.
57 = llvm::StringSwitch<bool>(StringRef(II->getName()))
62 // FIXME: This is just a wrapper around throwing an exception.
63 // Eventually inter-procedural analysis should handle this easily.
65 .Case("assfail", true)
66 .Case("db_error", true)
67 .Case("__assert", true)
68 .Case("__assert_rtn", true)
69 .Case("__assert_fail", true)
70 .Case("dtrace_assfail", true)
71 .Case("yy_fatal_error", true)
72 .Case("_XCAssertionFailureHandler", true)
73 .Case("_DTAssertionFailureHandler", true)
74 .Case("_TSAssertionFailureHandler", true)
83 static bool END_WITH_NULL isMultiArgSelector(const Selector *Sel, ...) {
89 while ((Arg = va_arg(argp, const char *))) {
90 if (!Sel->getNameForSlot(Slot).equals(Arg))
91 break; // still need to va_end!
97 // We only succeeded if we made it to the end of the argument list.
101 void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMessage &Msg,
102 CheckerContext &C) const {
103 // HACK: This entire check is to handle two messages in the Cocoa frameworks:
104 // -[NSAssertionHandler
105 // handleFailureInMethod:object:file:lineNumber:description:]
106 // -[NSAssertionHandler
107 // handleFailureInFunction:file:lineNumber:description:]
108 // Eventually these should be annotated with __attribute__((noreturn)).
109 // Because ObjC messages use dynamic dispatch, it is not generally safe to
110 // assume certain methods can't return. In cases where it is definitely valid,
111 // see if you can mark the methods noreturn or analyzer_noreturn instead of
112 // adding more explicit checks to this method.
114 if (!Msg.isInstanceMessage())
117 const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
120 if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
123 Selector Sel = Msg.getSelector();
124 switch (Sel.getNumArgs()) {
128 if (!isMultiArgSelector(&Sel, "handleFailureInFunction", "file",
129 "lineNumber", "description", NULL))
133 if (!isMultiArgSelector(&Sel, "handleFailureInMethod", "object", "file",
134 "lineNumber", "description", NULL))
139 // If we got here, it's one of the messages we care about.
144 void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
145 mgr.registerChecker<NoReturnFunctionChecker>();