clang: lib/StaticAnalyzer/Checkers/RunLoopAutoreleaseLeakChecker.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

36

37using namespace clang;

38using namespace ento;

39using namespace ast_matchers;

40

41namespace {

42

43const char * RunLoopBind = "NSRunLoopM";

44const char * RunLoopRunBind = "RunLoopRunM";

45const char * OtherMsgBind = "OtherMessageSentM";

46const char * AutoreleasePoolBind = "AutoreleasePoolM";

47const char * OtherStmtAutoreleasePoolBind = "OtherAutoreleasePoolM";

48

49class RunLoopAutoreleaseLeakChecker : public Checkercheck::ASTCodeBody {

50

51public:

52 void checkASTCodeBody(const Decl *D,

55

56};

57

58}

59

60

61

62

63

64

66 for (const Stmt *C : Parent->children()) {

67 if (C) continue;

68

69 if (C == A)

70 return true;

71

72 if (C == B)

73 return false;

74

76 }

77 return false;

78}

79

84 const RunLoopAutoreleaseLeakChecker *Checker) {

85

88

90

92 assert(ME);

93

94 const auto *AP =

96 const auto *OAP =

98 bool HasAutoreleasePool = (AP != nullptr);

99

101 const auto *RLR = Match.getNodeAs<Stmt>(RunLoopRunBind);

102 assert(RLR && "Run loop launch not found");

103 assert(ME != RLR);

104

105

107 return;

108

109 if (HasAutoreleasePool && (OAP != AP))

110 return;

111

115

117 "Memory leak inside autorelease pool",

118 "Memory",

119

120 (Twine("Temporary objects allocated in the") +

121 " autorelease pool " +

122 (HasAutoreleasePool ? "" : "of last resort ") +

123 "followed by the launch of " +

124 (RL ? "main run loop " : "xpc_main ") +

125 "may never get released; consider moving them to a "

126 "separate autorelease pool")

127 .str(),

128 Location, Range);

129}

130

134 hasReceiverType(asString("NSRunLoop")),

135 Extra)

136 .bind(RunLoopBind);

137

139 hasReceiver(MainRunLoopM),

140 Extra).bind(RunLoopRunBind);

141

144 return anyOf(MainRunLoopRunM, XPCRunM);

145}

146

149 equalsBoundNode(RunLoopRunBind))),

150 Extra)

151 .bind(OtherMsgBind);

152}

153

154static void

156 const RunLoopAutoreleaseLeakChecker *Chkr) {

160

164 hasDescendant(OtherMessageSentM)).bind(AutoreleasePoolBind);

165

167

171}

172

173static void

175 const RunLoopAutoreleaseLeakChecker *Chkr) {

176

178

181

183 isMain(),

186 );

187

189

192

193}

194

195void RunLoopAutoreleaseLeakChecker::checkASTCodeBody(const Decl *D,

200}

201

202void ento::registerRunLoopAutoreleaseLeakChecker(CheckerManager &mgr) {

204}

205

206bool ento::shouldRegisterRunLoopAutoreleaseLeakChecker(const CheckerManager &mgr) {

207 return true;

208}

static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR, AnalysisManager &AM, const RunLoopAutoreleaseLeakChecker *Checker)

static bool seenBefore(const Stmt *Parent, const Stmt *A, const Stmt *B)

static void checkTempObjectsInSamePool(const Decl *D, AnalysisManager &AM, BugReporter &BR, const RunLoopAutoreleaseLeakChecker *Chkr)

static StatementMatcher getRunLoopRunM(StatementMatcher Extra=anything())

static StatementMatcher getOtherMessageSentM(StatementMatcher Extra=anything())

static void checkTempObjectsInNoPool(const Decl *D, AnalysisManager &AM, BugReporter &BR, const RunLoopAutoreleaseLeakChecker *Chkr)

AnalysisDeclContext contains the context data for the function, method or block under analysis.

const Decl * getDecl() const

Decl - This represents one declaration (or definition), e.g.

virtual Stmt * getBody() const

getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...

virtual bool hasBody() const

Returns true if this Decl represents a declaration for a body of code, such as a function or method d...

Represents Objective-C's @autoreleasepool Statement.

An expression that sends a message to the given Objective-C object or class.

A trivial tuple used to represent a source range.

Stmt - This represents one statement.

Maps string IDs to AST nodes matched by parts of a matcher.

const T * getNodeAs(StringRef ID) const

Returns the AST node bound to ID.

ASTContext & getASTContext() override

AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)

BugReporter is a utility class for generating PathDiagnostics for analysis.

const SourceManager & getSourceManager()

void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges={}, ArrayRef< FixItHint > Fixits={})

CHECKER * registerChecker(AT &&... Args)

Used to register checkers.

static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)

Create a location for the beginning of the declaration.

A Range represents the closed range [from, to].

const internal::VariadicOperatorMatcherFunc< 1, 1 > unless

Matches if the provided matcher does not match.

internal::Matcher< Decl > DeclarationMatcher

Types of matchers for the top-level classes in the AST class hierarchy.

const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant

Matches AST nodes that have descendant AST nodes that match the provided matcher.

internal::Matcher< NamedDecl > hasName(StringRef Name)

Matches NamedDecl nodes that have the specified name.

const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr

Matches call expressions.

const internal::VariadicDynCastAllOfMatcher< Stmt, ObjCAutoreleasePoolStmt > autoreleasePoolStmt

Matches an Objective-C autorelease pool statement.

SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)

Returns the results of matching Matcher on Node.

internal::TrueMatcher anything()

Matches any node.

const internal::VariadicDynCastAllOfMatcher< Stmt, ObjCMessageExpr > objcMessageExpr

Matches ObjectiveC Message invocation expressions.

internal::Matcher< Stmt > StatementMatcher

const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl

Matches function declarations.

const internal::VariadicAllOfMatcher< Decl > decl

Matches declarations.

const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf

Matches if any of the given matchers matches.

const internal::ArgumentAdaptingMatcherFunc< internal::HasAncestorMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr > > hasAncestor

Matches AST nodes that have an ancestor that matches the provided matcher.

The JSON file list parser is used to communicate input to InstallAPI.