1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "MustReturnFromCallerChecker.h"
6 #include "CustomMatchers.h"
8 void MustReturnFromCallerChecker::registerMatchers(MatchFinder
*AstMatcher
) {
9 // Look for a call to a MOZ_MUST_RETURN_FROM_CALLER function
10 AstMatcher
->addMatcher(
11 callExpr(callee(functionDecl(isMozMustReturnFromCaller())),
12 anyOf(hasAncestor(lambdaExpr().bind("containing-lambda")),
13 hasAncestor(functionDecl().bind("containing-func"))))
18 void MustReturnFromCallerChecker::check(
19 const MatchFinder::MatchResult
&Result
) {
20 const auto *ContainingLambda
=
21 Result
.Nodes
.getNodeAs
<LambdaExpr
>("containing-lambda");
22 const auto *ContainingFunc
=
23 Result
.Nodes
.getNodeAs
<FunctionDecl
>("containing-func");
24 const auto *Call
= Result
.Nodes
.getNodeAs
<CallExpr
>("call");
27 if (ContainingLambda
) {
28 Body
= ContainingLambda
->getBody();
29 } else if (ContainingFunc
) {
30 Body
= ContainingFunc
->getBody();
34 assert(Body
&& "Should have a body by this point");
36 // Generate the CFG for the enclosing function or decl.
37 CFG::BuildOptions Options
;
38 std::unique_ptr
<CFG
> TheCFG
=
39 CFG::buildCFG(nullptr, Body
, Result
.Context
, Options
);
44 // Determine which block in the CFG we want to look at the successors of.
45 StmtToBlockMap
BlockMap(TheCFG
.get(), Result
.Context
);
47 const auto *Block
= BlockMap
.blockContainingStmt(Call
, &CallIndex
);
48 assert(Block
&& "This statement should be within the CFG!");
50 if (!immediatelyReturns(Block
, Result
.Context
, CallIndex
+ 1)) {
51 diag(Call
->getBeginLoc(),
52 "You must immediately return after calling this function",
53 DiagnosticIDs::Error
);
57 bool MustReturnFromCallerChecker::immediatelyReturns(
58 RecurseGuard
<const CFGBlock
*> Block
, ASTContext
*TheContext
,
60 if (Block
.isRepeat()) {
64 for (size_t I
= FromIdx
; I
< Block
->size(); ++I
) {
65 Optional
<CFGStmt
> S
= (*Block
)[I
].getAs
<CFGStmt
>();
70 auto AfterTrivials
= IgnoreTrivials(S
->getStmt());
72 // If we are looking at a ConstructExpr, a DeclRefExpr or a MemberExpr it's
73 // OK to use them after a call to a MOZ_MUST_RETURN_FROM_CALLER function.
74 // It is also, of course, OK to look at a ReturnStmt.
75 if (isa
<ReturnStmt
>(AfterTrivials
) ||
76 isa
<CXXConstructExpr
>(AfterTrivials
) ||
77 isa
<DeclRefExpr
>(AfterTrivials
) || isa
<MemberExpr
>(AfterTrivials
)) {
81 // It's also OK to call any function or method which is annotated with
82 // MOZ_MAY_CALL_AFTER_MUST_RETURN. We consider all CXXConversionDecls
83 // to be MOZ_MAY_CALL_AFTER_MUST_RETURN (like operator T*()).
84 if (auto CE
= dyn_cast
<CallExpr
>(AfterTrivials
)) {
85 auto Callee
= CE
->getDirectCallee();
87 hasCustomAttribute
<moz_may_call_after_must_return
>(Callee
)) {
91 if (Callee
&& isa
<CXXConversionDecl
>(Callee
)) {
96 // Otherwise, this expression is problematic.
100 for (auto Succ
= Block
->succ_begin(); Succ
!= Block
->succ_end(); ++Succ
) {
101 if (!immediatelyReturns(Block
.recurse(*Succ
), TheContext
, 0)) {