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/AST/Attr.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21 #include "llvm/ADT/StringSwitch.h"
24 using namespace clang;
29 class NoReturnFunctionChecker : public Checker< check::PostStmt<CallExpr>,
30 check::PostObjCMessage > {
32 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
33 void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
38 void NoReturnFunctionChecker::checkPostStmt(const CallExpr *CE,
39 CheckerContext &C) const {
40 ProgramStateRef state = C.getState();
41 const Expr *Callee = CE->getCallee();
43 bool BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
46 SVal L = state->getSVal(Callee, C.getLocationContext());
47 const FunctionDecl *FD = L.getAsFunctionDecl();
51 if (FD->getAttr<AnalyzerNoReturnAttr>() || FD->isNoReturn())
53 else if (const IdentifierInfo *II = FD->getIdentifier()) {
54 // HACK: Some functions are not marked noreturn, and don't return.
55 // Here are a few hardwired ones. If this takes too long, we can
56 // potentially cache these results.
58 = llvm::StringSwitch<bool>(StringRef(II->getName()))
63 // FIXME: This is just a wrapper around throwing an exception.
64 // Eventually inter-procedural analysis should handle this easily.
66 .Case("assfail", true)
67 .Case("db_error", true)
68 .Case("__assert", true)
69 .Case("__assert_rtn", true)
70 .Case("__assert_fail", true)
71 .Case("dtrace_assfail", true)
72 .Case("yy_fatal_error", true)
73 .Case("_XCAssertionFailureHandler", true)
74 .Case("_DTAssertionFailureHandler", true)
75 .Case("_TSAssertionFailureHandler", true)
84 static bool END_WITH_NULL isMultiArgSelector(const Selector *Sel, ...) {
90 while ((Arg = va_arg(argp, const char *))) {
91 if (!Sel->getNameForSlot(Slot).equals(Arg))
92 break; // still need to va_end!
98 // We only succeeded if we made it to the end of the argument list.
102 void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
103 CheckerContext &C) const {
104 // Check if the method is annotated with analyzer_noreturn.
105 if (const ObjCMethodDecl *MD = Msg.getDecl()) {
106 MD = MD->getCanonicalDecl();
107 if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
113 // HACK: This entire check is to handle two messages in the Cocoa frameworks:
114 // -[NSAssertionHandler
115 // handleFailureInMethod:object:file:lineNumber:description:]
116 // -[NSAssertionHandler
117 // handleFailureInFunction:file:lineNumber:description:]
118 // Eventually these should be annotated with __attribute__((noreturn)).
119 // Because ObjC messages use dynamic dispatch, it is not generally safe to
120 // assume certain methods can't return. In cases where it is definitely valid,
121 // see if you can mark the methods noreturn or analyzer_noreturn instead of
122 // adding more explicit checks to this method.
124 if (!Msg.isInstanceMessage())
127 const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
130 if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
133 Selector Sel = Msg.getSelector();
134 switch (Sel.getNumArgs()) {
138 if (!isMultiArgSelector(&Sel, "handleFailureInFunction", "file",
139 "lineNumber", "description", NULL))
143 if (!isMultiArgSelector(&Sel, "handleFailureInMethod", "object", "file",
144 "lineNumber", "description", NULL))
149 // If we got here, it's one of the messages we care about.
154 void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
155 mgr.registerChecker<NoReturnFunctionChecker>();