2017-01-17 03:11:41 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MustReturnFromCallerChecker.h"
|
|
|
|
#include "CustomMatchers.h"
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void MustReturnFromCallerChecker::registerMatchers(MatchFinder *AstMatcher) {
|
2019-09-20 05:17:01 +03:00
|
|
|
// Look for a call to a MOZ_MUST_RETURN_FROM_CALLER member
|
2017-10-20 20:11:50 +03:00
|
|
|
AstMatcher->addMatcher(
|
2019-09-20 05:17:01 +03:00
|
|
|
cxxMemberCallExpr(
|
|
|
|
on(declRefExpr(to(parmVarDecl()))),
|
|
|
|
callee(functionDecl(isMozMustReturnFromCaller())),
|
|
|
|
anyOf(hasAncestor(lambdaExpr().bind("containing-lambda")),
|
|
|
|
hasAncestor(functionDecl().bind("containing-func"))))
|
2017-10-20 20:11:50 +03:00
|
|
|
.bind("call"),
|
|
|
|
this);
|
2017-01-17 03:11:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MustReturnFromCallerChecker::check(
|
2017-10-20 20:11:50 +03:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
2017-01-17 03:11:41 +03:00
|
|
|
const auto *ContainingLambda =
|
2017-10-20 20:11:50 +03:00
|
|
|
Result.Nodes.getNodeAs<LambdaExpr>("containing-lambda");
|
2017-01-17 03:11:41 +03:00
|
|
|
const auto *ContainingFunc =
|
2017-10-20 20:11:50 +03:00
|
|
|
Result.Nodes.getNodeAs<FunctionDecl>("containing-func");
|
2017-01-17 03:11:41 +03:00
|
|
|
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
|
|
|
|
|
|
|
|
Stmt *Body = nullptr;
|
|
|
|
if (ContainingLambda) {
|
|
|
|
Body = ContainingLambda->getBody();
|
|
|
|
} else if (ContainingFunc) {
|
|
|
|
Body = ContainingFunc->getBody();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(Body && "Should have a body by this point");
|
|
|
|
|
|
|
|
// Generate the CFG for the enclosing function or decl.
|
|
|
|
CFG::BuildOptions Options;
|
|
|
|
std::unique_ptr<CFG> TheCFG =
|
2017-10-20 20:11:50 +03:00
|
|
|
CFG::buildCFG(nullptr, Body, Result.Context, Options);
|
2017-01-17 03:11:41 +03:00
|
|
|
if (!TheCFG) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine which block in the CFG we want to look at the successors of.
|
|
|
|
StmtToBlockMap BlockMap(TheCFG.get(), Result.Context);
|
|
|
|
size_t CallIndex;
|
|
|
|
const auto *Block = BlockMap.blockContainingStmt(Call, &CallIndex);
|
|
|
|
assert(Block && "This statement should be within the CFG!");
|
|
|
|
|
|
|
|
if (!immediatelyReturns(Block, Result.Context, CallIndex + 1)) {
|
2018-11-03 16:41:36 +03:00
|
|
|
diag(Call->getBeginLoc(),
|
2017-01-17 03:11:41 +03:00
|
|
|
"You must immediately return after calling this function",
|
|
|
|
DiagnosticIDs::Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
bool MustReturnFromCallerChecker::immediatelyReturns(
|
|
|
|
RecurseGuard<const CFGBlock *> Block, ASTContext *TheContext,
|
|
|
|
size_t FromIdx) {
|
2017-01-17 03:11:41 +03:00
|
|
|
if (Block.isRepeat()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t I = FromIdx; I < Block->size(); ++I) {
|
|
|
|
Optional<CFGStmt> S = (*Block)[I].getAs<CFGStmt>();
|
|
|
|
if (!S) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto AfterTrivials = IgnoreTrivials(S->getStmt());
|
|
|
|
|
|
|
|
// If we are looking at a ConstructExpr, a DeclRefExpr or a MemberExpr it's
|
|
|
|
// OK to use them after a call to a MOZ_MUST_RETURN_FROM_CALLER function.
|
|
|
|
// It is also, of course, OK to look at a ReturnStmt.
|
|
|
|
if (isa<ReturnStmt>(AfterTrivials) ||
|
|
|
|
isa<CXXConstructExpr>(AfterTrivials) ||
|
2017-10-20 20:11:50 +03:00
|
|
|
isa<DeclRefExpr>(AfterTrivials) || isa<MemberExpr>(AfterTrivials)) {
|
2017-01-17 03:11:41 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's also OK to call any function or method which is annotated with
|
|
|
|
// MOZ_MAY_CALL_AFTER_MUST_RETURN. We consider all CXXConversionDecls
|
|
|
|
// to be MOZ_MAY_CALL_AFTER_MUST_RETURN (like operator T*()).
|
|
|
|
if (auto CE = dyn_cast<CallExpr>(AfterTrivials)) {
|
|
|
|
auto Callee = CE->getDirectCallee();
|
2017-10-20 20:11:50 +03:00
|
|
|
if (Callee &&
|
2018-09-18 16:03:33 +03:00
|
|
|
hasCustomAttribute<moz_may_call_after_must_return>(Callee)) {
|
2017-01-17 03:11:41 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Callee && isa<CXXConversionDecl>(Callee)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this expression is problematic.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto Succ = Block->succ_begin(); Succ != Block->succ_end(); ++Succ) {
|
|
|
|
if (!immediatelyReturns(Block.recurse(*Succ), TheContext, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|