29 #include "llvm/ADT/BitVector.h"
30 #include "llvm/ADT/BitmaskEnum.h"
31 #include "llvm/ADT/Optional.h"
32 #include "llvm/ADT/PointerIntPair.h"
33 #include "llvm/ADT/STLExtras.h"
34 #include "llvm/ADT/Sequence.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/ADT/StringRef.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/Compiler.h"
39 #include "llvm/Support/ErrorHandling.h"
42 using namespace clang;
45 static constexpr
unsigned EXPECTED_MAX_NUMBER_OF_PARAMS = 2;
48 static constexpr
unsigned EXPECTED_NUMBER_OF_BASIC_BLOCKS = 8;
51 constexpr llvm::StringLiteral CONVENTIONAL_NAMES[] = {
52 "completionHandler",
"completion",
"withCompletionHandler",
53 "withCompletion",
"completionBlock",
"withCompletionBlock",
54 "replyTo",
"reply",
"withReplyTo"};
55 constexpr llvm::StringLiteral CONVENTIONAL_SUFFIXES[] = {
56 "WithCompletionHandler",
"WithCompletion",
"WithCompletionBlock",
57 "WithReplyTo",
"WithReply"};
58 constexpr llvm::StringLiteral CONVENTIONAL_CONDITIONS[] = {
59 "error",
"cancel",
"shouldCall",
"done",
"OK",
"success"};
61 struct KnownCalledOnceParameter {
62 llvm::StringLiteral FunctionName;
65 constexpr KnownCalledOnceParameter KNOWN_CALLED_ONCE_PARAMETERS[] = {
66 {llvm::StringLiteral{
"dispatch_async"}, 1},
67 {llvm::StringLiteral{
"dispatch_async_and_wait"}, 1},
68 {llvm::StringLiteral{
"dispatch_after"}, 2},
69 {llvm::StringLiteral{
"dispatch_sync"}, 1},
70 {llvm::StringLiteral{
"dispatch_once"}, 1},
71 {llvm::StringLiteral{
"dispatch_barrier_async"}, 1},
72 {llvm::StringLiteral{
"dispatch_barrier_async_and_wait"}, 1},
73 {llvm::StringLiteral{
"dispatch_barrier_sync"}, 1}};
75 class ParameterStatus {
154 DefinitelyCalled = 0x3,
156 NON_ERROR_STATUS = DefinitelyCalled,
170 constexpr ParameterStatus() =
default;
171 ParameterStatus(
Kind K) : StatusKind(K) {
172 assert(!seenAnyCalls(K) &&
"Can't initialize status without a call");
175 assert(seenAnyCalls(K) &&
"This kind is not supposed to have a call");
178 const Expr &getCall()
const {
179 assert(seenAnyCalls(
getKind()) &&
"ParameterStatus doesn't have a call");
182 static bool seenAnyCalls(
Kind K) {
183 return (K & DefinitelyCalled) == DefinitelyCalled && K != Reported;
185 bool seenAnyCalls()
const {
return seenAnyCalls(
getKind()); }
187 static bool isErrorStatus(
Kind K) {
return K > NON_ERROR_STATUS; }
188 bool isErrorStatus()
const {
return isErrorStatus(
getKind()); }
192 void join(
const ParameterStatus &Other) {
202 StatusKind |= Other.getKind();
205 bool operator==(
const ParameterStatus &Other)
const {
208 return getKind() == Other.getKind();
214 Kind StatusKind = NotVisited;
221 State(
unsigned Size, ParameterStatus::Kind K = ParameterStatus::NotVisited)
222 : ParamData(
Size, K) {}
226 ParameterStatus &getStatusFor(
unsigned Index) {
return ParamData[Index]; }
227 const ParameterStatus &getStatusFor(
unsigned Index)
const {
228 return ParamData[Index];
233 bool seenAnyCalls(
unsigned Index)
const {
234 return getStatusFor(Index).seenAnyCalls();
239 const Expr &getCallFor(
unsigned Index)
const {
240 return getStatusFor(Index).getCall();
243 ParameterStatus::Kind getKindFor(
unsigned Index)
const {
244 return getStatusFor(Index).getKind();
247 bool isVisited()
const {
248 return llvm::all_of(ParamData, [](
const ParameterStatus &S) {
249 return S.getKind() != ParameterStatus::NotVisited;
254 void join(
const State &Other) {
255 assert(ParamData.size() == Other.ParamData.size() &&
256 "Couldn't join statuses with different sizes");
257 for (
auto Pair : llvm::zip(ParamData, Other.ParamData)) {
258 std::get<0>(Pair).join(std::get<1>(Pair));
262 using iterator = ParamSizedVector<ParameterStatus>::iterator;
263 using const_iterator = ParamSizedVector<ParameterStatus>::const_iterator;
265 iterator begin() {
return ParamData.begin(); }
266 iterator end() {
return ParamData.end(); }
268 const_iterator begin()
const {
return ParamData.begin(); }
269 const_iterator end()
const {
return ParamData.end(); }
272 return ParamData == Other.ParamData;
276 ParamSizedVector<ParameterStatus> ParamData;
310 bool ShouldRetrieveFromComparisons =
false) {
311 return DeclRefFinder(ShouldRetrieveFromComparisons).Visit(E);
320 if (!ShouldRetrieveFromComparisons)
334 if (!ShouldRetrieveFromComparisons)
341 return LHS ? LHS : Visit(BO->
getRHS());
353 if (!ShouldRetrieveFromComparisons)
359 case Builtin::BI__builtin_expect:
360 case Builtin::BI__builtin_expect_with_probability: {
364 return Candidate !=
nullptr ? Candidate : Visit(CE->
getArg(1));
367 case Builtin::BI__builtin_unpredictable:
368 return Visit(CE->
getArg(0));
382 return E != DeclutteredExpr ? Visit(DeclutteredExpr) : nullptr;
386 DeclRefFinder(
bool ShouldRetrieveFromComparisons)
387 : ShouldRetrieveFromComparisons(ShouldRetrieveFromComparisons) {}
389 bool ShouldRetrieveFromComparisons;
393 bool ShouldRetrieveFromComparisons =
false) {
394 return DeclRefFinder::find(In, ShouldRetrieveFromComparisons);
398 findReferencedParmVarDecl(
const Expr *In,
399 bool ShouldRetrieveFromComparisons =
false) {
401 findDeclRefExpr(In, ShouldRetrieveFromComparisons)) {
402 return dyn_cast<ParmVarDecl>(DR->getDecl());
409 const Expr *getCondition(
const Stmt *S) {
414 if (
const auto *If = dyn_cast<IfStmt>(S)) {
415 return If->getCond();
417 if (
const auto *Ternary = dyn_cast<AbstractConditionalOperator>(S)) {
418 return Ternary->getCond();
431 static constexpr
unsigned EXPECTED_NUMBER_OF_NAMES = 5;
432 using NameCollection =
435 static NameCollection collect(
const Expr *From) {
437 Impl.TraverseStmt(
const_cast<Expr *
>(From));
447 llvm::StringRef Name;
456 assert(PropertyMethodDecl &&
457 "Implicit property must have associated declaration");
464 Result.push_back(Name);
469 NamesCollector() =
default;
470 NameCollection Result;
474 bool mentionsAnyOfConventionalNames(
const Expr *E) {
477 return llvm::any_of(MentionedNames, [](llvm::StringRef ConditionName) {
479 CONVENTIONAL_CONDITIONS,
480 [ConditionName](
const llvm::StringLiteral &Conventional) {
481 return ConditionName.contains_insensitive(Conventional);
488 struct Clarification {
490 const Stmt *Location;
495 class NotCalledClarifier
497 llvm::Optional<Clarification>> {
514 return NotCalledClarifier{
Conditional, SuccWithoutCall}.Visit(Terminator);
529 const Stmt *CaseToBlame = SuccInQuestion->getLabel();
537 Case = Case->getNextSwitchCase()) {
538 if (Case == CaseToBlame) {
543 llvm_unreachable(
"Found unexpected switch structure");
556 assert(
Parent->succ_size() == 2 &&
557 "Branching block should have exactly two successors");
558 unsigned SuccessorIndex = getSuccessorIndex(
Parent, SuccInQuestion);
560 updateForSuccessor(DefaultReason, SuccessorIndex);
561 return Clarification{ActualReason, Terminator};
581 assert(It !=
Parent->succ_end() &&
582 "Given blocks should be in parent-child relationship");
583 return It -
Parent->succ_begin();
588 unsigned SuccessorIndex) {
589 assert(SuccessorIndex <= 1);
591 static_cast<unsigned>(ReasonForTrueBranch) + SuccessorIndex;
607 bool CheckConventionalParameters) {
608 CalledOnceChecker(AC, Handler, CheckConventionalParameters).check();
613 bool CheckConventionalParameters)
614 : FunctionCFG(*AC.getCFG()), AC(AC), Handler(Handler),
615 CheckConventionalParameters(CheckConventionalParameters),
617 initDataStructures();
618 assert((size() == 0 || !States.empty()) &&
619 "Data structures are inconsistent");
626 void initDataStructures() {
629 if (
const auto *Function = dyn_cast<FunctionDecl>(AnalyzedDecl)) {
630 findParamsToTrack(Function);
631 }
else if (
const auto *Method = dyn_cast<ObjCMethodDecl>(AnalyzedDecl)) {
632 findParamsToTrack(Method);
633 }
else if (
const auto *Block = dyn_cast<BlockDecl>(AnalyzedDecl)) {
634 findCapturesToTrack(Block);
635 findParamsToTrack(Block);
641 CFGSizedVector<State>(FunctionCFG.getNumBlockIDs(),
State(size()));
645 void findCapturesToTrack(
const BlockDecl *Block) {
650 if (shouldBeCalledOnce(ParamContext,
P)) {
651 TrackedParams.push_back(
P);
657 template <
class FunctionLikeDecl>
658 void findParamsToTrack(
const FunctionLikeDecl *Function) {
659 for (
unsigned Index : llvm::seq<unsigned>(0u,
Function->param_size())) {
660 if (shouldBeCalledOnce(Function, Index)) {
661 TrackedParams.push_back(
Function->getParamDecl(Index));
672 if (size() == 0 || isPossiblyEmptyImpl())
676 llvm::none_of(States, [](
const State &S) {
return S.isVisited(); }) &&
677 "None of the blocks should be 'visited' before the analysis");
716 const CFGBlock *Exit = &FunctionCFG.getExit();
717 assignState(Exit,
State(size(), ParameterStatus::NotCalled));
718 Worklist.enqueuePredecessors(Exit);
720 while (
const CFGBlock *BB = Worklist.dequeue()) {
721 assert(BB &&
"Worklist should filter out null blocks");
723 assert(CurrentState.isVisited() &&
724 "After the check, basic block should be visited");
728 if (assignState(BB, CurrentState)) {
729 Worklist.enqueuePredecessors(BB);
736 checkEntry(&FunctionCFG.getEntry());
741 CurrentState = joinSuccessors(BB);
742 assert(CurrentState.isVisited() &&
743 "Shouldn't start with a 'not visited' state");
755 for (
const CFGElement &Element : llvm::reverse(*BB)) {
761 void check(
const Stmt *S) { Visit(S); }
763 void checkEntry(
const CFGBlock *Entry) {
769 const State &EntryStatus = getState(Entry);
770 llvm::BitVector NotCalledOnEveryPath(size(),
false);
771 llvm::BitVector NotUsedOnEveryPath(size(),
false);
774 for (
const auto &IndexedStatus : llvm::enumerate(EntryStatus)) {
777 switch (IndexedStatus.value().getKind()) {
778 case ParameterStatus::NotCalled:
782 if (!hasEverEscaped(IndexedStatus.index())) {
785 if (isCaptured(Parameter)) {
788 !isExplicitlyMarked(Parameter));
791 !isExplicitlyMarked(Parameter));
796 NotUsedOnEveryPath[IndexedStatus.index()] =
true;
800 case ParameterStatus::MaybeCalled:
808 NotCalledOnEveryPath[IndexedStatus.index()] =
true;
816 if (NotCalledOnEveryPath.none() && NotUsedOnEveryPath.none() &&
820 !FunctionHasCleanupVars)
841 for (
const CFGBlock *BB : FunctionCFG) {
845 const State &BlockState = getState(BB);
847 for (
unsigned Index : llvm::seq(0u, size())) {
857 if (NotCalledOnEveryPath[Index] &&
858 BlockState.getKindFor(Index) == ParameterStatus::MaybeCalled) {
860 findAndReportNotCalledBranches(BB, Index);
861 }
else if (NotUsedOnEveryPath[Index] &&
862 isLosingEscape(BlockState, BB, Index)) {
864 findAndReportNotCalledBranches(BB, Index,
true);
871 void checkDirectCall(
const CallExpr *Call) {
872 if (
auto Index = getIndexOfCallee(Call)) {
873 processCallFor(*Index, Call);
880 template <
class CallLikeExpr>
881 void checkIndirectCall(
const CallLikeExpr *CallOrMessage) {
884 CallOrMessage->getArgs(), CallOrMessage->getNumArgs());
887 for (
const auto &Argument : llvm::enumerate(Arguments)) {
888 if (
auto Index = getIndexOfExpression(Argument.value())) {
889 if (shouldBeCalledOnce(CallOrMessage, Argument.index())) {
892 processCallFor(*Index, CallOrMessage);
896 processEscapeFor(*Index);
904 void processCallFor(
unsigned Index,
const Expr *Call) {
905 ParameterStatus &CurrentParamStatus = CurrentState.getStatusFor(Index);
907 if (CurrentParamStatus.seenAnyCalls()) {
912 Parameter, &CurrentState.getCallFor(Index), Call,
913 !isExplicitlyMarked(Parameter),
916 CurrentParamStatus.getKind() == ParameterStatus::DefinitelyCalled);
920 CurrentParamStatus = ParameterStatus::Reported;
922 }
else if (CurrentParamStatus.getKind() != ParameterStatus::Reported) {
925 ParameterStatus Called(ParameterStatus::DefinitelyCalled, Call);
926 CurrentParamStatus = Called;
931 void processEscapeFor(
unsigned Index) {
932 ParameterStatus &CurrentParamStatus = CurrentState.getStatusFor(Index);
935 if (CurrentParamStatus.isErrorStatus()) {
936 CurrentParamStatus = ParameterStatus::Escaped;
940 void findAndReportNotCalledBranches(
const CFGBlock *
Parent,
unsigned Index,
941 bool IsEscape =
false) {
946 if (getState(Succ).getKindFor(Index) == ParameterStatus::NotCalled) {
947 assert(
Parent->succ_size() >= 2 &&
948 "Block should have at least two successors at this point");
949 if (
auto Clarification = NotCalledClarifier::clarify(
Parent, Succ)) {
952 Parameter, AC.
getDecl(), Clarification->Location,
953 Clarification->Reason, !IsEscape, !isExplicitlyMarked(Parameter));
964 static bool isExplicitlyMarked(
const ParmVarDecl *Parameter) {
965 return Parameter->hasAttr<CalledOnceAttr>();
969 static bool isConventional(llvm::StringRef Name) {
970 return llvm::count(CONVENTIONAL_NAMES, Name) != 0;
974 static bool hasConventionalSuffix(llvm::StringRef Name) {
975 return llvm::any_of(CONVENTIONAL_SUFFIXES, [Name](llvm::StringRef Suffix) {
976 return Name.endswith(Suffix);
981 static bool isConventional(
QualType Ty) {
992 static bool isOnlyParameterConventional(
const FunctionDecl *Function) {
994 return Function->getNumParams() == 1 && II &&
995 hasConventionalSuffix(II->
getName());
1003 unsigned ParamIndex) {
1004 if (
const SwiftAsyncAttr *A = D->
getAttr<SwiftAsyncAttr>()) {
1009 return A->getCompletionHandlerIndex().getASTIndex() == ParamIndex;
1015 static bool isInitMethod(
Selector MethodSelector) {
1020 static bool isConventionalSelectorPiece(
Selector MethodSelector,
1021 unsigned PieceIndex,
1023 if (!isConventional(PieceType) || isInitMethod(MethodSelector)) {
1028 assert(PieceIndex == 0);
1032 llvm::StringRef PieceName = MethodSelector.
getNameForSlot(PieceIndex);
1033 return isConventional(PieceName) || hasConventionalSuffix(PieceName);
1036 bool shouldBeCalledOnce(
const ParmVarDecl *Parameter)
const {
1037 return isExplicitlyMarked(Parameter) ||
1038 (CheckConventionalParameters &&
1039 (isConventional(
Parameter->getName()) ||
1040 hasConventionalSuffix(
Parameter->getName())) &&
1044 bool shouldBeCalledOnce(
const DeclContext *ParamContext,
1047 if (
const auto *Function = dyn_cast<FunctionDecl>(ParamContext)) {
1048 return shouldBeCalledOnce(Function, ParamIndex);
1050 if (
const auto *Method = dyn_cast<ObjCMethodDecl>(ParamContext)) {
1051 return shouldBeCalledOnce(Method, ParamIndex);
1053 return shouldBeCalledOnce(Param);
1056 bool shouldBeCalledOnce(
const BlockDecl *Block,
unsigned ParamIndex)
const {
1057 return shouldBeCalledOnce(
Block->getParamDecl(ParamIndex));
1061 unsigned ParamIndex)
const {
1062 if (ParamIndex >=
Function->getNumParams()) {
1066 if (
auto ConventionalAsync =
1067 isConventionalSwiftAsync(Function, ParamIndex)) {
1068 return *ConventionalAsync;
1071 return shouldBeCalledOnce(
Function->getParamDecl(ParamIndex)) ||
1072 (CheckConventionalParameters &&
1073 isOnlyParameterConventional(Function));
1077 unsigned ParamIndex)
const {
1079 if (ParamIndex >= MethodSelector.
getNumArgs()) {
1084 if (
auto ConventionalAsync = isConventionalSwiftAsync(Method, ParamIndex)) {
1085 return *ConventionalAsync;
1089 return shouldBeCalledOnce(Parameter) ||
1090 (CheckConventionalParameters &&
1091 isConventionalSelectorPiece(MethodSelector, ParamIndex,
1095 bool shouldBeCalledOnce(
const CallExpr *Call,
unsigned ParamIndex)
const {
1097 return Function && shouldBeCalledOnce(Function, ParamIndex);
1101 unsigned ParamIndex)
const {
1103 return Method && ParamIndex < Method->
param_size() &&
1104 shouldBeCalledOnce(Method, ParamIndex);
1111 bool isCaptured(
const ParmVarDecl *Parameter)
const {
1113 return Block->capturesVariable(Parameter);
1119 const Expr *getBlockGuaraneedCallSite(
const BlockExpr *Block)
const {
1129 for (
const Stmt *Prev = Block, *Current = PM.
getParent(Block);
1130 Current !=
nullptr; Prev = Current, Current = PM.
getParent(Current)) {
1132 if (isa<CastExpr>(Current) || isa<ParenExpr>(Current))
1137 if (
const auto *Call = dyn_cast<CallExpr>(Current)) {
1139 if (
Call->getCallee() == Prev)
1143 return shouldBlockArgumentBeCalledOnce(Call, Prev) ?
Call :
nullptr;
1145 if (
const auto *Message = dyn_cast<ObjCMessageExpr>(Current)) {
1146 return shouldBlockArgumentBeCalledOnce(Message, Prev) ?
Message
1156 template <
class CallLikeExpr>
1157 bool shouldBlockArgumentBeCalledOnce(
const CallLikeExpr *CallOrMessage,
1158 const Stmt *BlockArgument)
const {
1161 CallOrMessage->getArgs(), CallOrMessage->getNumArgs());
1163 for (
const auto &Argument : llvm::enumerate(Arguments)) {
1164 if (Argument.value() == BlockArgument) {
1165 return shouldBlockArgumentBeCalledOnce(CallOrMessage, Argument.index());
1172 bool shouldBlockArgumentBeCalledOnce(
const CallExpr *Call,
1173 unsigned ParamIndex)
const {
1175 return shouldBlockArgumentBeCalledOnce(Function, ParamIndex) ||
1176 shouldBeCalledOnce(Call, ParamIndex);
1180 unsigned ParamIndex)
const {
1183 return shouldBeCalledOnce(Message, ParamIndex);
1186 static bool shouldBlockArgumentBeCalledOnce(
const FunctionDecl *Function,
1187 unsigned ParamIndex) {
1194 llvm::any_of(KNOWN_CALLED_ONCE_PARAMETERS,
1195 [Function, ParamIndex](
1196 const KnownCalledOnceParameter &Reference) {
1213 bool isPossiblyEmptyImpl()
const {
1214 if (!isa<ObjCMethodDecl>(AC.
getDecl())) {
1221 if (FunctionCFG.size() == 2) {
1228 if (FunctionCFG.size() == 3) {
1229 const CFGBlock &Entry = FunctionCFG.getEntry();
1252 return llvm::all_of(FunctionCFG, [](
const CFGBlock *BB) {
1262 std::unique_ptr<ParentMap> ReturnChildren;
1264 return llvm::all_of(
1267 [&ReturnChildren](
const CFGElement &Element) {
1269 const Stmt *SuspiciousStmt = S->getStmt();
1271 if (isa<ReturnStmt>(SuspiciousStmt)) {
1274 ReturnChildren = std::make_unique<ParentMap>(
1275 const_cast<Stmt *>(SuspiciousStmt));
1280 return SuspiciousStmt->getBeginLoc().isMacroID() ||
1282 ReturnChildren->hasParent(SuspiciousStmt));
1290 bool hasEverEscaped(
unsigned Index)
const {
1291 return llvm::any_of(States, [Index](
const State &StateForOneBB) {
1292 return StateForOneBB.getKindFor(Index) == ParameterStatus::Escaped;
1311 bool assignState(
const CFGBlock *BB,
const State &ToAssign) {
1312 State &Current = getState(BB);
1313 if (Current == ToAssign) {
1324 llvm::make_filter_range(BB->
succs(), [
this](
const CFGBlock *Succ) {
1325 return Succ && this->getState(Succ).isVisited();
1328 assert(!Succs.empty() &&
1329 "Basic block should have at least one visited successor");
1331 State Result = getState(*Succs.begin());
1333 for (
const CFGBlock *Succ : llvm::drop_begin(Succs, 1)) {
1334 Result.join(getState(Succ));
1338 handleConditional(BB, Condition, Result);
1344 void handleConditional(
const CFGBlock *BB,
const Expr *Condition,
1345 State &ToAlter)
const {
1346 handleParameterCheck(BB, Condition, ToAlter);
1347 if (SuppressOnConventionalErrorPaths) {
1348 handleConventionalCheck(BB, Condition, ToAlter);
1352 void handleParameterCheck(
const CFGBlock *BB,
const Expr *Condition,
1353 State &ToAlter)
const {
1364 if (
const ParmVarDecl *Parameter = findReferencedParmVarDecl(
1367 if (
const auto Index = getIndex(*Parameter)) {
1368 ParameterStatus &CurrentStatus = ToAlter.getStatusFor(*Index);
1380 const ParameterStatus &StatusInSucc =
1381 getState(Succ).getStatusFor(*Index);
1383 if (StatusInSucc.isErrorStatus()) {
1388 CurrentStatus = StatusInSucc;
1390 if (StatusInSucc.getKind() == ParameterStatus::DefinitelyCalled) {
1402 void handleConventionalCheck(
const CFGBlock *BB,
const Expr *Condition,
1403 State &ToAlter)
const {
1407 if (!mentionsAnyOfConventionalNames(Condition)) {
1411 for (
const auto &IndexedStatus : llvm::enumerate(ToAlter)) {
1414 if (isExplicitlyMarked(Parameter)) {
1418 ParameterStatus &CurrentStatus = IndexedStatus.value();
1427 if (isLosingCall(ToAlter, BB, IndexedStatus.index()) ||
1428 isLosingEscape(ToAlter, BB, IndexedStatus.index())) {
1429 CurrentStatus = ParameterStatus::Escaped;
1434 bool isLosingCall(
const State &StateAfterJoin,
const CFGBlock *JoinBlock,
1435 unsigned ParameterIndex)
const {
1438 return isLosingJoin(StateAfterJoin, JoinBlock, ParameterIndex,
1439 ParameterStatus::MaybeCalled,
1440 ParameterStatus::DefinitelyCalled);
1443 bool isLosingEscape(
const State &StateAfterJoin,
const CFGBlock *JoinBlock,
1444 unsigned ParameterIndex)
const {
1446 return isLosingJoin(StateAfterJoin, JoinBlock, ParameterIndex,
1447 ParameterStatus::NotCalled, ParameterStatus::Escaped);
1450 bool isLosingJoin(
const State &StateAfterJoin,
const CFGBlock *JoinBlock,
1451 unsigned ParameterIndex, ParameterStatus::Kind AfterJoin,
1452 ParameterStatus::Kind BeforeJoin)
const {
1453 assert(!ParameterStatus::isErrorStatus(BeforeJoin) &&
1454 ParameterStatus::isErrorStatus(AfterJoin) &&
1455 "It's not a losing join if statuses do not represent "
1456 "correct-to-error transition");
1458 const ParameterStatus &CurrentStatus =
1459 StateAfterJoin.getStatusFor(ParameterIndex);
1461 return CurrentStatus.getKind() == AfterJoin &&
1462 anySuccessorHasStatus(JoinBlock, ParameterIndex, BeforeJoin);
1467 bool anySuccessorHasStatus(
const CFGBlock *
Parent,
unsigned ParameterIndex,
1468 ParameterStatus::Kind ToFind)
const {
1469 return llvm::any_of(
1470 Parent->succs(), [
this, ParameterIndex, ToFind](
const CFGBlock *Succ) {
1471 return Succ && getState(Succ).getKindFor(ParameterIndex) == ToFind;
1476 void checkEscapee(
const Expr *E) {
1477 if (
const ParmVarDecl *Parameter = findReferencedParmVarDecl(E)) {
1478 checkEscapee(*Parameter);
1484 if (
auto Index = getIndex(Parameter)) {
1485 processEscapeFor(*Index);
1490 void markNoReturn() {
1491 for (ParameterStatus &PS : CurrentState) {
1492 PS = ParameterStatus::NoReturn;
1501 if (
auto Index = getIndexOfExpression(
Assignment->getLHS())) {
1506 Assignment->getRHS()->IgnoreParenCasts()->getIntegerConstantExpr(
1509 ParameterStatus &CurrentParamStatus = CurrentState.getStatusFor(*Index);
1511 if (0 == *Constant && CurrentParamStatus.seenAnyCalls()) {
1520 CurrentParamStatus = ParameterStatus::Reported;
1531 void VisitCallExpr(
const CallExpr *Call) {
1533 checkDirectCall(Call);
1535 checkIndirectCall(Call);
1541 if (
const Expr *Receiver =
Message->getInstanceReceiver()) {
1542 checkEscapee(Receiver);
1545 checkIndirectCall(Message);
1548 void VisitBlockExpr(
const BlockExpr *Block) {
1562 const Expr *CalledOnceCallSite = getBlockGuaraneedCallSite(Block);
1567 if (CalledOnceCallSite) {
1573 for (
const auto &
Capture :
Block->getBlockDecl()->captures()) {
1575 if (
auto Index = getIndex(*Param)) {
1576 if (CalledOnceCallSite) {
1579 processCallFor(*Index, CalledOnceCallSite);
1584 processEscapeFor(*Index);
1592 if (Op->
getOpcode() == clang::BO_Assign) {
1596 checkEscapee(Op->
getRHS());
1599 checkSuppression(Op);
1603 void VisitDeclStmt(
const DeclStmt *DS) {
1609 if (
const auto *Var = dyn_cast<VarDecl>(Declaration)) {
1610 if (Var->getInit()) {
1611 checkEscapee(Var->getInit());
1614 if (Var->hasAttr<CleanupAttr>()) {
1615 FunctionHasCleanupVars =
true;
1625 if (
Cast->getType().getCanonicalType()->isVoidType()) {
1626 checkEscapee(
Cast->getSubExpr());
1636 unsigned size()
const {
return TrackedParams.size(); }
1639 return getIndexOfExpression(
Call->getCallee());
1643 if (
const ParmVarDecl *Parameter = findReferencedParmVarDecl(E)) {
1644 return getIndex(*Parameter);
1658 ParamSizedVector<const ParmVarDecl *>::const_iterator It =
1659 llvm::find(TrackedParams, &Parameter);
1661 if (It != TrackedParams.end()) {
1662 return It - TrackedParams.begin();
1668 const ParmVarDecl *getParameter(
unsigned Index)
const {
1669 assert(Index < TrackedParams.size());
1670 return TrackedParams[Index];
1673 const CFG &FunctionCFG;
1676 bool CheckConventionalParameters;
1683 bool SuppressOnConventionalErrorPaths =
false;
1690 bool FunctionHasCleanupVars =
false;
1693 ParamSizedVector<const ParmVarDecl *> TrackedParams;
1694 CFGSizedVector<State> States;
1702 bool CheckConventionalParameters) {
1703 CalledOnceChecker::check(AC, Handler, CheckConventionalParameters);