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 "SelectorExtras.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22 #include "llvm/ADT/StringSwitch.h"
25 using namespace clang;
30 class NoReturnFunctionChecker : public Checker< check::PostCall,
31 check::PostObjCMessage > {
32 mutable Selector HandleFailureInFunctionSel;
33 mutable Selector HandleFailureInMethodSel;
35 void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
36 void checkPostObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
41 void NoReturnFunctionChecker::checkPostCall(const CallEvent &CE,
42 CheckerContext &C) const {
43 bool BuildSinks = false;
45 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CE.getDecl()))
46 BuildSinks = FD->hasAttr<AnalyzerNoReturnAttr>() || FD->isNoReturn();
48 const Expr *Callee = CE.getOriginExpr();
49 if (!BuildSinks && Callee)
50 BuildSinks = getFunctionExtInfo(Callee->getType()).getNoReturn();
52 if (!BuildSinks && CE.isGlobalCFunction()) {
53 if (const IdentifierInfo *II = CE.getCalleeIdentifier()) {
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("__assert2", true)
70 // For the purpose of static analysis, we do not care that
71 // this MSVC function will return if the user decides to continue.
72 .Case("_wassert", true)
73 .Case("__assert_rtn", true)
74 .Case("__assert_fail", true)
75 .Case("dtrace_assfail", true)
76 .Case("yy_fatal_error", true)
77 .Case("_XCAssertionFailureHandler", true)
78 .Case("_DTAssertionFailureHandler", true)
79 .Case("_TSAssertionFailureHandler", true)
85 C.generateSink(C.getState(), C.getPredecessor());
88 void NoReturnFunctionChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
89 CheckerContext &C) const {
90 // Check if the method is annotated with analyzer_noreturn.
91 if (const ObjCMethodDecl *MD = Msg.getDecl()) {
92 MD = MD->getCanonicalDecl();
93 if (MD->hasAttr<AnalyzerNoReturnAttr>()) {
94 C.generateSink(C.getState(), C.getPredecessor());
99 // HACK: This entire check is to handle two messages in the Cocoa frameworks:
100 // -[NSAssertionHandler
101 // handleFailureInMethod:object:file:lineNumber:description:]
102 // -[NSAssertionHandler
103 // handleFailureInFunction:file:lineNumber:description:]
104 // Eventually these should be annotated with __attribute__((noreturn)).
105 // Because ObjC messages use dynamic dispatch, it is not generally safe to
106 // assume certain methods can't return. In cases where it is definitely valid,
107 // see if you can mark the methods noreturn or analyzer_noreturn instead of
108 // adding more explicit checks to this method.
110 if (!Msg.isInstanceMessage())
113 const ObjCInterfaceDecl *Receiver = Msg.getReceiverInterface();
116 if (!Receiver->getIdentifier()->isStr("NSAssertionHandler"))
119 Selector Sel = Msg.getSelector();
120 switch (Sel.getNumArgs()) {
124 lazyInitKeywordSelector(HandleFailureInFunctionSel, C.getASTContext(),
125 "handleFailureInFunction", "file", "lineNumber",
127 if (Sel != HandleFailureInFunctionSel)
131 lazyInitKeywordSelector(HandleFailureInMethodSel, C.getASTContext(),
132 "handleFailureInMethod", "object", "file",
133 "lineNumber", "description");
134 if (Sel != HandleFailureInMethodSel)
139 // If we got here, it's one of the messages we care about.
140 C.generateSink(C.getState(), C.getPredecessor());
143 void ento::registerNoReturnFunctionChecker(CheckerManager &mgr) {
144 mgr.registerChecker<NoReturnFunctionChecker>();