clang 10.0.0git

ObjCNoReturn.cpp
Go to the documentation of this file.
1 //= ObjCNoReturn.cpp - Handling of Cocoa APIs known not to return --*- C++ -*---
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements special handling of recognizing ObjC API hooks that
10 // do not return but aren't marked as such in API headers.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ExprObjC.h"
16 #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
17
18 using namespace clang;
19
20 static bool isSubclass(const ObjCInterfaceDecl *Class, IdentifierInfo *II) {
21 if (!Class)
22 return false;
23 if (Class->getIdentifier() == II)
24 return true;
25 return isSubclass(Class->getSuperClass(), II);
26 }
27
28 ObjCNoReturn::ObjCNoReturn(ASTContext &C)
29 : RaiseSel(GetNullarySelector("raise", C)),
30 NSExceptionII(&C.Idents.get("NSException"))
31 {
32 // Generate selectors.
33 SmallVector<IdentifierInfo*, 3> II;
34
35 // raise:format:
36 II.push_back(&C.Idents.get("raise"));
37 II.push_back(&C.Idents.get("format"));
38 NSExceptionInstanceRaiseSelectors[0] =
39 C.Selectors.getSelector(II.size(), &II[0]);
40
41 // raise:format:arguments:
42 II.push_back(&C.Idents.get("arguments"));
43 NSExceptionInstanceRaiseSelectors[1] =
44 C.Selectors.getSelector(II.size(), &II[0]);
45 }
46
47
48 bool ObjCNoReturn::isImplicitNoReturn(const ObjCMessageExpr *ME) {
49 Selector S = ME->getSelector();
50
51 if (ME->isInstanceMessage()) {
52 // Check for the "raise" message.
53 return S == RaiseSel;
54 }
55
56 if (const ObjCInterfaceDecl *ID = ME->getReceiverInterface()) {
57 if (isSubclass(ID, NSExceptionII)) {
58 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) {
59 if (S == NSExceptionInstanceRaiseSelectors[i])
60 return true;
61 }
62 }
63 }
64
65 return false;
66 }
clang::Builtin::ID
ID
Definition: Builtins.h:46
ASTContext.h
Defines the clang::ASTContext interface.
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:685
clang::ObjCMessageExpr::getSelector
Selector getSelector() const
Definition: ExprObjC.cpp:337
isSubclass
static bool isSubclass(const ObjCInterfaceDecl *Class, IdentifierInfo *II)
Definition: ObjCNoReturn.cpp:20
clang::ObjCMessageExpr::getReceiverInterface
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition: ExprObjC.cpp:358
clang::NamedDecl::getIdentifier
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:244
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:57
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::ASTContext::Idents
IdentifierTable & Idents
Definition: ASTContext.h:580
clang::GetNullarySelector
Selector GetNullarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing a nullary selector.
Definition: ASTContext.h:3063
ExprObjC.h
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1186
clang::ObjCInterfaceDecl::getSuperClass
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:337
clang::ObjCMessageExpr
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:950
clang::ASTContext::Selectors
SelectorTable & Selectors
Definition: ASTContext.h:581
clang::IdentifierTable::get
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Definition: IdentifierTable.h:529
llvm::SmallVector
Definition: LLVM.h:35
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
ObjCNoReturn.h
clang::ObjCNoReturn::isImplicitNoReturn
bool isImplicitNoReturn(const ObjCMessageExpr *ME)
Return true if the given message expression is known to never return.
Definition: ObjCNoReturn.cpp:48
clang::ObjCNoReturn::ObjCNoReturn
ObjCNoReturn(ASTContext &C)
Definition: ObjCNoReturn.cpp:28
clang::SelectorTable::getSelector
Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV)
Can create any sort of selector.
Definition: IdentifierTable.cpp:651
clang::ObjCMessageExpr::isInstanceMessage
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
Definition: ExprObjC.h:1248