Path: blob/main/contrib/llvm-project/clang/lib/Analysis/CalledOnceCheck.cpp
35234 views
//===- CalledOnceCheck.cpp - Check 'called once' parameters ---------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Analysis/Analyses/CalledOnceCheck.h"9#include "clang/AST/ASTContext.h"10#include "clang/AST/Attr.h"11#include "clang/AST/Decl.h"12#include "clang/AST/DeclBase.h"13#include "clang/AST/Expr.h"14#include "clang/AST/ExprObjC.h"15#include "clang/AST/OperationKinds.h"16#include "clang/AST/ParentMap.h"17#include "clang/AST/RecursiveASTVisitor.h"18#include "clang/AST/Stmt.h"19#include "clang/AST/StmtObjC.h"20#include "clang/AST/StmtVisitor.h"21#include "clang/AST/Type.h"22#include "clang/Analysis/AnalysisDeclContext.h"23#include "clang/Analysis/CFG.h"24#include "clang/Analysis/FlowSensitive/DataflowWorklist.h"25#include "clang/Basic/Builtins.h"26#include "clang/Basic/IdentifierTable.h"27#include "clang/Basic/LLVM.h"28#include "llvm/ADT/BitVector.h"29#include "llvm/ADT/BitmaskEnum.h"30#include "llvm/ADT/PointerIntPair.h"31#include "llvm/ADT/STLExtras.h"32#include "llvm/ADT/Sequence.h"33#include "llvm/ADT/SmallVector.h"34#include "llvm/ADT/StringRef.h"35#include "llvm/Support/Casting.h"36#include "llvm/Support/Compiler.h"37#include "llvm/Support/ErrorHandling.h"38#include <memory>39#include <optional>4041using namespace clang;4243namespace {44static constexpr unsigned EXPECTED_MAX_NUMBER_OF_PARAMS = 2;45template <class T>46using ParamSizedVector = llvm::SmallVector<T, EXPECTED_MAX_NUMBER_OF_PARAMS>;47static constexpr unsigned EXPECTED_NUMBER_OF_BASIC_BLOCKS = 8;48template <class T>49using CFGSizedVector = llvm::SmallVector<T, EXPECTED_NUMBER_OF_BASIC_BLOCKS>;50constexpr llvm::StringLiteral CONVENTIONAL_NAMES[] = {51"completionHandler", "completion", "withCompletionHandler",52"withCompletion", "completionBlock", "withCompletionBlock",53"replyTo", "reply", "withReplyTo"};54constexpr llvm::StringLiteral CONVENTIONAL_SUFFIXES[] = {55"WithCompletionHandler", "WithCompletion", "WithCompletionBlock",56"WithReplyTo", "WithReply"};57constexpr llvm::StringLiteral CONVENTIONAL_CONDITIONS[] = {58"error", "cancel", "shouldCall", "done", "OK", "success"};5960struct KnownCalledOnceParameter {61llvm::StringLiteral FunctionName;62unsigned ParamIndex;63};64constexpr KnownCalledOnceParameter KNOWN_CALLED_ONCE_PARAMETERS[] = {65{llvm::StringLiteral{"dispatch_async"}, 1},66{llvm::StringLiteral{"dispatch_async_and_wait"}, 1},67{llvm::StringLiteral{"dispatch_after"}, 2},68{llvm::StringLiteral{"dispatch_sync"}, 1},69{llvm::StringLiteral{"dispatch_once"}, 1},70{llvm::StringLiteral{"dispatch_barrier_async"}, 1},71{llvm::StringLiteral{"dispatch_barrier_async_and_wait"}, 1},72{llvm::StringLiteral{"dispatch_barrier_sync"}, 1}};7374class ParameterStatus {75public:76// Status kind is basically the main part of parameter's status.77// The kind represents our knowledge (so far) about a tracked parameter78// in the context of this analysis.79//80// Since we want to report on missing and extraneous calls, we need to81// track the fact whether paramater was called or not. This automatically82// decides two kinds: `NotCalled` and `Called`.83//84// One of the erroneous situations is the case when parameter is called only85// on some of the paths. We could've considered it `NotCalled`, but we want86// to report double call warnings even if these two calls are not guaranteed87// to happen in every execution. We also don't want to have it as `Called`88// because not calling tracked parameter on all of the paths is an error89// on its own. For these reasons, we need to have a separate kind,90// `MaybeCalled`, and change `Called` to `DefinitelyCalled` to avoid91// confusion.92//93// Two violations of calling parameter more than once and not calling it on94// every path are not, however, mutually exclusive. In situations where both95// violations take place, we prefer to report ONLY double call. It's always96// harder to pinpoint a bug that has arisen when a user neglects to take the97// right action (and therefore, no action is taken), than when a user takes98// the wrong action. And, in order to remember that we already reported99// a double call, we need another kind: `Reported`.100//101// Our analysis is intra-procedural and, while in the perfect world,102// developers only use tracked parameters to call them, in the real world,103// the picture might be different. Parameters can be stored in global104// variables or leaked into other functions that we know nothing about.105// We try to be lenient and trust users. Another kind `Escaped` reflects106// such situations. We don't know if it gets called there or not, but we107// should always think of `Escaped` as the best possible option.108//109// Some of the paths in the analyzed functions might end with a call110// to noreturn functions. Such paths are not required to have parameter111// calls and we want to track that. For the purposes of better diagnostics,112// we don't want to reuse `Escaped` and, thus, have another kind `NoReturn`.113//114// Additionally, we have `NotVisited` kind that tells us nothing about115// a tracked parameter, but is used for tracking analyzed (aka visited)116// basic blocks.117//118// If we consider `|` to be a JOIN operation of two kinds coming from119// two different paths, the following properties must hold:120//121// 1. for any Kind K: K | K == K122// Joining two identical kinds should result in the same kind.123//124// 2. for any Kind K: Reported | K == Reported125// Doesn't matter on which path it was reported, it still is.126//127// 3. for any Kind K: NoReturn | K == K128// We can totally ignore noreturn paths during merges.129//130// 4. DefinitelyCalled | NotCalled == MaybeCalled131// Called on one path, not called on another - that's simply132// a definition for MaybeCalled.133//134// 5. for any Kind K in [DefinitelyCalled, NotCalled, MaybeCalled]:135// Escaped | K == K136// Escaped mirrors other statuses after joins.137// Every situation, when we join any of the listed kinds K,138// is a violation. For this reason, in order to assume the139// best outcome for this escape, we consider it to be the140// same as the other path.141//142// 6. for any Kind K in [DefinitelyCalled, NotCalled]:143// MaybeCalled | K == MaybeCalled144// MaybeCalled should basically stay after almost every join.145enum Kind {146// No-return paths should be absolutely transparent for the analysis.147// 0x0 is the identity element for selected join operation (binary or).148NoReturn = 0x0, /* 0000 */149// Escaped marks situations when marked parameter escaped into150// another function (so we can assume that it was possibly called there).151Escaped = 0x1, /* 0001 */152// Parameter was definitely called once at this point.153DefinitelyCalled = 0x3, /* 0011 */154// Kinds less or equal to NON_ERROR_STATUS are not considered errors.155NON_ERROR_STATUS = DefinitelyCalled,156// Parameter was not yet called.157NotCalled = 0x5, /* 0101 */158// Parameter was not called at least on one path leading to this point,159// while there is also at least one path that it gets called.160MaybeCalled = 0x7, /* 0111 */161// Parameter was not yet analyzed.162NotVisited = 0x8, /* 1000 */163// We already reported a violation and stopped tracking calls for this164// parameter.165Reported = 0xF, /* 1111 */166LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Reported)167};168169constexpr ParameterStatus() = default;170/* implicit */ ParameterStatus(Kind K) : StatusKind(K) {171assert(!seenAnyCalls(K) && "Can't initialize status without a call");172}173ParameterStatus(Kind K, const Expr *Call) : StatusKind(K), Call(Call) {174assert(seenAnyCalls(K) && "This kind is not supposed to have a call");175}176177const Expr &getCall() const {178assert(seenAnyCalls(getKind()) && "ParameterStatus doesn't have a call");179return *Call;180}181static bool seenAnyCalls(Kind K) {182return (K & DefinitelyCalled) == DefinitelyCalled && K != Reported;183}184bool seenAnyCalls() const { return seenAnyCalls(getKind()); }185186static bool isErrorStatus(Kind K) { return K > NON_ERROR_STATUS; }187bool isErrorStatus() const { return isErrorStatus(getKind()); }188189Kind getKind() const { return StatusKind; }190191void join(const ParameterStatus &Other) {192// If we have a pointer already, let's keep it.193// For the purposes of the analysis, it doesn't really matter194// which call we report.195//196// If we don't have a pointer, let's take whatever gets joined.197if (!Call) {198Call = Other.Call;199}200// Join kinds.201StatusKind |= Other.getKind();202}203204bool operator==(const ParameterStatus &Other) const {205// We compare only kinds, pointers on their own is only additional206// information.207return getKind() == Other.getKind();208}209210private:211// It would've been a perfect place to use llvm::PointerIntPair, but212// unfortunately NumLowBitsAvailable for clang::Expr had been reduced to 2.213Kind StatusKind = NotVisited;214const Expr *Call = nullptr;215};216217/// State aggregates statuses of all tracked parameters.218class State {219public:220State(unsigned Size, ParameterStatus::Kind K = ParameterStatus::NotVisited)221: ParamData(Size, K) {}222223/// Return status of a parameter with the given index.224/// \{225ParameterStatus &getStatusFor(unsigned Index) { return ParamData[Index]; }226const ParameterStatus &getStatusFor(unsigned Index) const {227return ParamData[Index];228}229/// \}230231/// Return true if parameter with the given index can be called.232bool seenAnyCalls(unsigned Index) const {233return getStatusFor(Index).seenAnyCalls();234}235/// Return a reference that we consider a call.236///237/// Should only be used for parameters that can be called.238const Expr &getCallFor(unsigned Index) const {239return getStatusFor(Index).getCall();240}241/// Return status kind of parameter with the given index.242ParameterStatus::Kind getKindFor(unsigned Index) const {243return getStatusFor(Index).getKind();244}245246bool isVisited() const {247return llvm::all_of(ParamData, [](const ParameterStatus &S) {248return S.getKind() != ParameterStatus::NotVisited;249});250}251252// Join other state into the current state.253void join(const State &Other) {254assert(ParamData.size() == Other.ParamData.size() &&255"Couldn't join statuses with different sizes");256for (auto Pair : llvm::zip(ParamData, Other.ParamData)) {257std::get<0>(Pair).join(std::get<1>(Pair));258}259}260261using iterator = ParamSizedVector<ParameterStatus>::iterator;262using const_iterator = ParamSizedVector<ParameterStatus>::const_iterator;263264iterator begin() { return ParamData.begin(); }265iterator end() { return ParamData.end(); }266267const_iterator begin() const { return ParamData.begin(); }268const_iterator end() const { return ParamData.end(); }269270bool operator==(const State &Other) const {271return ParamData == Other.ParamData;272}273274private:275ParamSizedVector<ParameterStatus> ParamData;276};277278/// A simple class that finds DeclRefExpr in the given expression.279///280/// However, we don't want to find ANY nested DeclRefExpr skipping whatever281/// expressions on our way. Only certain expressions considered "no-op"282/// for our task are indeed skipped.283class DeclRefFinder284: public ConstStmtVisitor<DeclRefFinder, const DeclRefExpr *> {285public:286/// Find a DeclRefExpr in the given expression.287///288/// In its most basic form (ShouldRetrieveFromComparisons == false),289/// this function can be simply reduced to the following question:290///291/// - If expression E is used as a function argument, could we say292/// that DeclRefExpr nested in E is used as an argument?293///294/// According to this rule, we can say that parens, casts and dereferencing295/// (dereferencing only applied to function pointers, but this is our case)296/// can be skipped.297///298/// When we should look into comparisons the question changes to:299///300/// - If expression E is used as a condition, could we say that301/// DeclRefExpr is being checked?302///303/// And even though, these are two different questions, they have quite a lot304/// in common. Actually, we can say that whatever expression answers305/// positively the first question also fits the second question as well.306///307/// In addition, we skip binary operators == and !=, and unary opeartor !.308static const DeclRefExpr *find(const Expr *E,309bool ShouldRetrieveFromComparisons = false) {310return DeclRefFinder(ShouldRetrieveFromComparisons).Visit(E);311}312313const DeclRefExpr *VisitDeclRefExpr(const DeclRefExpr *DR) { return DR; }314315const DeclRefExpr *VisitUnaryOperator(const UnaryOperator *UO) {316switch (UO->getOpcode()) {317case UO_LNot:318// We care about logical not only if we care about comparisons.319if (!ShouldRetrieveFromComparisons)320return nullptr;321[[fallthrough]];322// Function pointer/references can be dereferenced before a call.323// That doesn't make it, however, any different from a regular call.324// For this reason, dereference operation is a "no-op".325case UO_Deref:326return Visit(UO->getSubExpr());327default:328return nullptr;329}330}331332const DeclRefExpr *VisitBinaryOperator(const BinaryOperator *BO) {333if (!ShouldRetrieveFromComparisons)334return nullptr;335336switch (BO->getOpcode()) {337case BO_EQ:338case BO_NE: {339const DeclRefExpr *LHS = Visit(BO->getLHS());340return LHS ? LHS : Visit(BO->getRHS());341}342default:343return nullptr;344}345}346347const DeclRefExpr *VisitOpaqueValueExpr(const OpaqueValueExpr *OVE) {348return Visit(OVE->getSourceExpr());349}350351const DeclRefExpr *VisitCallExpr(const CallExpr *CE) {352if (!ShouldRetrieveFromComparisons)353return nullptr;354355// We want to see through some of the boolean builtin functions356// that we are likely to see in conditions.357switch (CE->getBuiltinCallee()) {358case Builtin::BI__builtin_expect:359case Builtin::BI__builtin_expect_with_probability: {360assert(CE->getNumArgs() >= 2);361362const DeclRefExpr *Candidate = Visit(CE->getArg(0));363return Candidate != nullptr ? Candidate : Visit(CE->getArg(1));364}365366case Builtin::BI__builtin_unpredictable:367return Visit(CE->getArg(0));368369default:370return nullptr;371}372}373374const DeclRefExpr *VisitExpr(const Expr *E) {375// It is a fallback method that gets called whenever the actual type376// of the given expression is not covered.377//378// We first check if we have anything to skip. And then repeat the whole379// procedure for a nested expression instead.380const Expr *DeclutteredExpr = E->IgnoreParenCasts();381return E != DeclutteredExpr ? Visit(DeclutteredExpr) : nullptr;382}383384private:385DeclRefFinder(bool ShouldRetrieveFromComparisons)386: ShouldRetrieveFromComparisons(ShouldRetrieveFromComparisons) {}387388bool ShouldRetrieveFromComparisons;389};390391const DeclRefExpr *findDeclRefExpr(const Expr *In,392bool ShouldRetrieveFromComparisons = false) {393return DeclRefFinder::find(In, ShouldRetrieveFromComparisons);394}395396const ParmVarDecl *397findReferencedParmVarDecl(const Expr *In,398bool ShouldRetrieveFromComparisons = false) {399if (const DeclRefExpr *DR =400findDeclRefExpr(In, ShouldRetrieveFromComparisons)) {401return dyn_cast<ParmVarDecl>(DR->getDecl());402}403404return nullptr;405}406407/// Return conditions expression of a statement if it has one.408const Expr *getCondition(const Stmt *S) {409if (!S) {410return nullptr;411}412413if (const auto *If = dyn_cast<IfStmt>(S)) {414return If->getCond();415}416if (const auto *Ternary = dyn_cast<AbstractConditionalOperator>(S)) {417return Ternary->getCond();418}419420return nullptr;421}422423/// A small helper class that collects all named identifiers in the given424/// expression. It traverses it recursively, so names from deeper levels425/// of the AST will end up in the results.426/// Results might have duplicate names, if this is a problem, convert to427/// string sets afterwards.428class NamesCollector : public RecursiveASTVisitor<NamesCollector> {429public:430static constexpr unsigned EXPECTED_NUMBER_OF_NAMES = 5;431using NameCollection =432llvm::SmallVector<llvm::StringRef, EXPECTED_NUMBER_OF_NAMES>;433434static NameCollection collect(const Expr *From) {435NamesCollector Impl;436Impl.TraverseStmt(const_cast<Expr *>(From));437return Impl.Result;438}439440bool VisitDeclRefExpr(const DeclRefExpr *E) {441Result.push_back(E->getDecl()->getName());442return true;443}444445bool VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) {446llvm::StringRef Name;447448if (E->isImplicitProperty()) {449ObjCMethodDecl *PropertyMethodDecl = nullptr;450if (E->isMessagingGetter()) {451PropertyMethodDecl = E->getImplicitPropertyGetter();452} else {453PropertyMethodDecl = E->getImplicitPropertySetter();454}455assert(PropertyMethodDecl &&456"Implicit property must have associated declaration");457Name = PropertyMethodDecl->getSelector().getNameForSlot(0);458} else {459assert(E->isExplicitProperty());460Name = E->getExplicitProperty()->getName();461}462463Result.push_back(Name);464return true;465}466467private:468NamesCollector() = default;469NameCollection Result;470};471472/// Check whether the given expression mentions any of conventional names.473bool mentionsAnyOfConventionalNames(const Expr *E) {474NamesCollector::NameCollection MentionedNames = NamesCollector::collect(E);475476return llvm::any_of(MentionedNames, [](llvm::StringRef ConditionName) {477return llvm::any_of(478CONVENTIONAL_CONDITIONS,479[ConditionName](const llvm::StringLiteral &Conventional) {480return ConditionName.contains_insensitive(Conventional);481});482});483}484485/// Clarification is a simple pair of a reason why parameter is not called486/// on every path and a statement to blame.487struct Clarification {488NeverCalledReason Reason;489const Stmt *Location;490};491492/// A helper class that can produce a clarification based on the given pair493/// of basic blocks.494class NotCalledClarifier495: public ConstStmtVisitor<NotCalledClarifier,496std::optional<Clarification>> {497public:498/// The main entrypoint for the class, the function that tries to find the499/// clarification of how to explain which sub-path starts with a CFG edge500/// from Conditional to SuccWithoutCall.501///502/// This means that this function has one precondition:503/// SuccWithoutCall should be a successor block for Conditional.504///505/// Because clarification is not needed for non-trivial pairs of blocks506/// (i.e. SuccWithoutCall is not the only successor), it returns meaningful507/// results only for such cases. For this very reason, the parent basic508/// block, Conditional, is named that way, so it is clear what kind of509/// block is expected.510static std::optional<Clarification> clarify(const CFGBlock *Conditional,511const CFGBlock *SuccWithoutCall) {512if (const Stmt *Terminator = Conditional->getTerminatorStmt()) {513return NotCalledClarifier{Conditional, SuccWithoutCall}.Visit(Terminator);514}515return std::nullopt;516}517518std::optional<Clarification> VisitIfStmt(const IfStmt *If) {519return VisitBranchingBlock(If, NeverCalledReason::IfThen);520}521522std::optional<Clarification>523VisitAbstractConditionalOperator(const AbstractConditionalOperator *Ternary) {524return VisitBranchingBlock(Ternary, NeverCalledReason::IfThen);525}526527std::optional<Clarification> VisitSwitchStmt(const SwitchStmt *Switch) {528const Stmt *CaseToBlame = SuccInQuestion->getLabel();529if (!CaseToBlame) {530// If interesting basic block is not labeled, it means that this531// basic block does not represent any of the cases.532return Clarification{NeverCalledReason::SwitchSkipped, Switch};533}534535for (const SwitchCase *Case = Switch->getSwitchCaseList(); Case;536Case = Case->getNextSwitchCase()) {537if (Case == CaseToBlame) {538return Clarification{NeverCalledReason::Switch, Case};539}540}541542llvm_unreachable("Found unexpected switch structure");543}544545std::optional<Clarification> VisitForStmt(const ForStmt *For) {546return VisitBranchingBlock(For, NeverCalledReason::LoopEntered);547}548549std::optional<Clarification> VisitWhileStmt(const WhileStmt *While) {550return VisitBranchingBlock(While, NeverCalledReason::LoopEntered);551}552553std::optional<Clarification>554VisitBranchingBlock(const Stmt *Terminator, NeverCalledReason DefaultReason) {555assert(Parent->succ_size() == 2 &&556"Branching block should have exactly two successors");557unsigned SuccessorIndex = getSuccessorIndex(Parent, SuccInQuestion);558NeverCalledReason ActualReason =559updateForSuccessor(DefaultReason, SuccessorIndex);560return Clarification{ActualReason, Terminator};561}562563std::optional<Clarification> VisitBinaryOperator(const BinaryOperator *) {564// We don't want to report on short-curcuit logical operations.565return std::nullopt;566}567568std::optional<Clarification> VisitStmt(const Stmt *Terminator) {569// If we got here, we didn't have a visit function for more derived570// classes of statement that this terminator actually belongs to.571//572// This is not a good scenario and should not happen in practice, but573// at least we'll warn the user.574return Clarification{NeverCalledReason::FallbackReason, Terminator};575}576577static unsigned getSuccessorIndex(const CFGBlock *Parent,578const CFGBlock *Child) {579CFGBlock::const_succ_iterator It = llvm::find(Parent->succs(), Child);580assert(It != Parent->succ_end() &&581"Given blocks should be in parent-child relationship");582return It - Parent->succ_begin();583}584585static NeverCalledReason586updateForSuccessor(NeverCalledReason ReasonForTrueBranch,587unsigned SuccessorIndex) {588assert(SuccessorIndex <= 1);589unsigned RawReason =590static_cast<unsigned>(ReasonForTrueBranch) + SuccessorIndex;591assert(RawReason <=592static_cast<unsigned>(NeverCalledReason::LARGEST_VALUE));593return static_cast<NeverCalledReason>(RawReason);594}595596private:597NotCalledClarifier(const CFGBlock *Parent, const CFGBlock *SuccInQuestion)598: Parent(Parent), SuccInQuestion(SuccInQuestion) {}599600const CFGBlock *Parent, *SuccInQuestion;601};602603class CalledOnceChecker : public ConstStmtVisitor<CalledOnceChecker> {604public:605static void check(AnalysisDeclContext &AC, CalledOnceCheckHandler &Handler,606bool CheckConventionalParameters) {607CalledOnceChecker(AC, Handler, CheckConventionalParameters).check();608}609610private:611CalledOnceChecker(AnalysisDeclContext &AC, CalledOnceCheckHandler &Handler,612bool CheckConventionalParameters)613: FunctionCFG(*AC.getCFG()), AC(AC), Handler(Handler),614CheckConventionalParameters(CheckConventionalParameters),615CurrentState(0) {616initDataStructures();617assert((size() == 0 || !States.empty()) &&618"Data structures are inconsistent");619}620621//===----------------------------------------------------------------------===//622// Initializing functions623//===----------------------------------------------------------------------===//624625void initDataStructures() {626const Decl *AnalyzedDecl = AC.getDecl();627628if (const auto *Function = dyn_cast<FunctionDecl>(AnalyzedDecl)) {629findParamsToTrack(Function);630} else if (const auto *Method = dyn_cast<ObjCMethodDecl>(AnalyzedDecl)) {631findParamsToTrack(Method);632} else if (const auto *Block = dyn_cast<BlockDecl>(AnalyzedDecl)) {633findCapturesToTrack(Block);634findParamsToTrack(Block);635}636637// Have something to track, let's init states for every block from the CFG.638if (size() != 0) {639States =640CFGSizedVector<State>(FunctionCFG.getNumBlockIDs(), State(size()));641}642}643644void findCapturesToTrack(const BlockDecl *Block) {645for (const auto &Capture : Block->captures()) {646if (const auto *P = dyn_cast<ParmVarDecl>(Capture.getVariable())) {647// Parameter DeclContext is its owning function or method.648const DeclContext *ParamContext = P->getDeclContext();649if (shouldBeCalledOnce(ParamContext, P)) {650TrackedParams.push_back(P);651}652}653}654}655656template <class FunctionLikeDecl>657void findParamsToTrack(const FunctionLikeDecl *Function) {658for (unsigned Index : llvm::seq<unsigned>(0u, Function->param_size())) {659if (shouldBeCalledOnce(Function, Index)) {660TrackedParams.push_back(Function->getParamDecl(Index));661}662}663}664665//===----------------------------------------------------------------------===//666// Main logic 'check' functions667//===----------------------------------------------------------------------===//668669void check() {670// Nothing to check here: we don't have marked parameters.671if (size() == 0 || isPossiblyEmptyImpl())672return;673674assert(675llvm::none_of(States, [](const State &S) { return S.isVisited(); }) &&676"None of the blocks should be 'visited' before the analysis");677678// For our task, both backward and forward approaches suite well.679// However, in order to report better diagnostics, we decided to go with680// backward analysis.681//682// Let's consider the following CFG and how forward and backward analyses683// will work for it.684//685// FORWARD: | BACKWARD:686// #1 | #1687// +---------+ | +-----------+688// | if | | |MaybeCalled|689// +---------+ | +-----------+690// |NotCalled| | | if |691// +---------+ | +-----------+692// / \ | / \693// #2 / \ #3 | #2 / \ #3694// +----------------+ +---------+ | +----------------+ +---------+695// | foo() | | ... | | |DefinitelyCalled| |NotCalled|696// +----------------+ +---------+ | +----------------+ +---------+697// |DefinitelyCalled| |NotCalled| | | foo() | | ... |698// +----------------+ +---------+ | +----------------+ +---------+699// \ / | \ /700// \ #4 / | \ #4 /701// +-----------+ | +---------+702// | ... | | |NotCalled|703// +-----------+ | +---------+704// |MaybeCalled| | | ... |705// +-----------+ | +---------+706//707// The most natural way to report lacking call in the block #3 would be to708// message that the false branch of the if statement in the block #1 doesn't709// have a call. And while with the forward approach we'll need to find a710// least common ancestor or something like that to find the 'if' to blame,711// backward analysis gives it to us out of the box.712BackwardDataflowWorklist Worklist(FunctionCFG, AC);713714// Let's visit EXIT.715const CFGBlock *Exit = &FunctionCFG.getExit();716assignState(Exit, State(size(), ParameterStatus::NotCalled));717Worklist.enqueuePredecessors(Exit);718719while (const CFGBlock *BB = Worklist.dequeue()) {720assert(BB && "Worklist should filter out null blocks");721check(BB);722assert(CurrentState.isVisited() &&723"After the check, basic block should be visited");724725// Traverse successor basic blocks if the status of this block726// has changed.727if (assignState(BB, CurrentState)) {728Worklist.enqueuePredecessors(BB);729}730}731732// Check that we have all tracked parameters at the last block.733// As we are performing a backward version of the analysis,734// it should be the ENTRY block.735checkEntry(&FunctionCFG.getEntry());736}737738void check(const CFGBlock *BB) {739// We start with a state 'inherited' from all the successors.740CurrentState = joinSuccessors(BB);741assert(CurrentState.isVisited() &&742"Shouldn't start with a 'not visited' state");743744// This is the 'exit' situation, broken promises are probably OK745// in such scenarios.746if (BB->hasNoReturnElement()) {747markNoReturn();748// This block still can have calls (even multiple calls) and749// for this reason there is no early return here.750}751752// We use a backward dataflow propagation and for this reason we753// should traverse basic blocks bottom-up.754for (const CFGElement &Element : llvm::reverse(*BB)) {755if (std::optional<CFGStmt> S = Element.getAs<CFGStmt>()) {756check(S->getStmt());757}758}759}760void check(const Stmt *S) { Visit(S); }761762void checkEntry(const CFGBlock *Entry) {763// We finalize this algorithm with the ENTRY block because764// we use a backward version of the analysis. This is where765// we can judge that some of the tracked parameters are not called on766// every path from ENTRY to EXIT.767768const State &EntryStatus = getState(Entry);769llvm::BitVector NotCalledOnEveryPath(size(), false);770llvm::BitVector NotUsedOnEveryPath(size(), false);771772// Check if there are no calls of the marked parameter at all773for (const auto &IndexedStatus : llvm::enumerate(EntryStatus)) {774const ParmVarDecl *Parameter = getParameter(IndexedStatus.index());775776switch (IndexedStatus.value().getKind()) {777case ParameterStatus::NotCalled:778// If there were places where this parameter escapes (aka being used),779// we can provide a more useful diagnostic by pointing at the exact780// branches where it is not even mentioned.781if (!hasEverEscaped(IndexedStatus.index())) {782// This parameter is was not used at all, so we should report the783// most generic version of the warning.784if (isCaptured(Parameter)) {785// We want to specify that it was captured by the block.786Handler.handleCapturedNeverCalled(Parameter, AC.getDecl(),787!isExplicitlyMarked(Parameter));788} else {789Handler.handleNeverCalled(Parameter,790!isExplicitlyMarked(Parameter));791}792} else {793// Mark it as 'interesting' to figure out which paths don't even794// have escapes.795NotUsedOnEveryPath[IndexedStatus.index()] = true;796}797798break;799case ParameterStatus::MaybeCalled:800// If we have 'maybe called' at this point, we have an error801// that there is at least one path where this parameter802// is not called.803//804// However, reporting the warning with only that information can be805// too vague for the users. For this reason, we mark such parameters806// as "interesting" for further analysis.807NotCalledOnEveryPath[IndexedStatus.index()] = true;808break;809default:810break;811}812}813814// Early exit if we don't have parameters for extra analysis...815if (NotCalledOnEveryPath.none() && NotUsedOnEveryPath.none() &&816// ... or if we've seen variables with cleanup functions.817// We can't reason that we've seen every path in this case,818// and thus abandon reporting any warnings that imply that.819!FunctionHasCleanupVars)820return;821822// We are looking for a pair of blocks A, B so that the following is true:823// * A is a predecessor of B824// * B is marked as NotCalled825// * A has at least one successor marked as either826// Escaped or DefinitelyCalled827//828// In that situation, it is guaranteed that B is the first block of the path829// where the user doesn't call or use parameter in question.830//831// For this reason, branch A -> B can be used for reporting.832//833// This part of the algorithm is guarded by a condition that the function834// does indeed have a violation of contract. For this reason, we can835// spend more time to find a good spot to place the warning.836//837// The following algorithm has the worst case complexity of O(V + E),838// where V is the number of basic blocks in FunctionCFG,839// E is the number of edges between blocks in FunctionCFG.840for (const CFGBlock *BB : FunctionCFG) {841if (!BB)842continue;843844const State &BlockState = getState(BB);845846for (unsigned Index : llvm::seq(0u, size())) {847// We don't want to use 'isLosingCall' here because we want to report848// the following situation as well:849//850// MaybeCalled851// | ... |852// MaybeCalled NotCalled853//854// Even though successor is not 'DefinitelyCalled', it is still useful855// to report it, it is still a path without a call.856if (NotCalledOnEveryPath[Index] &&857BlockState.getKindFor(Index) == ParameterStatus::MaybeCalled) {858859findAndReportNotCalledBranches(BB, Index);860} else if (NotUsedOnEveryPath[Index] &&861isLosingEscape(BlockState, BB, Index)) {862863findAndReportNotCalledBranches(BB, Index, /* IsEscape = */ true);864}865}866}867}868869/// Check potential call of a tracked parameter.870void checkDirectCall(const CallExpr *Call) {871if (auto Index = getIndexOfCallee(Call)) {872processCallFor(*Index, Call);873}874}875876/// Check the call expression for being an indirect call of one of the tracked877/// parameters. It is indirect in the sense that this particular call is not878/// calling the parameter itself, but rather uses it as the argument.879template <class CallLikeExpr>880void checkIndirectCall(const CallLikeExpr *CallOrMessage) {881// CallExpr::arguments does not interact nicely with llvm::enumerate.882llvm::ArrayRef<const Expr *> Arguments =883llvm::ArrayRef(CallOrMessage->getArgs(), CallOrMessage->getNumArgs());884885// Let's check if any of the call arguments is a point of interest.886for (const auto &Argument : llvm::enumerate(Arguments)) {887if (auto Index = getIndexOfExpression(Argument.value())) {888if (shouldBeCalledOnce(CallOrMessage, Argument.index())) {889// If the corresponding parameter is marked as 'called_once' we should890// consider it as a call.891processCallFor(*Index, CallOrMessage);892} else {893// Otherwise, we mark this parameter as escaped, which can be894// interpreted both as called or not called depending on the context.895processEscapeFor(*Index);896}897// Otherwise, let's keep the state as it is.898}899}900}901902/// Process call of the parameter with the given index903void processCallFor(unsigned Index, const Expr *Call) {904ParameterStatus &CurrentParamStatus = CurrentState.getStatusFor(Index);905906if (CurrentParamStatus.seenAnyCalls()) {907908// At this point, this parameter was called, so this is a second call.909const ParmVarDecl *Parameter = getParameter(Index);910Handler.handleDoubleCall(911Parameter, &CurrentState.getCallFor(Index), Call,912!isExplicitlyMarked(Parameter),913// We are sure that the second call is definitely914// going to happen if the status is 'DefinitelyCalled'.915CurrentParamStatus.getKind() == ParameterStatus::DefinitelyCalled);916917// Mark this parameter as already reported on, so we don't repeat918// warnings.919CurrentParamStatus = ParameterStatus::Reported;920921} else if (CurrentParamStatus.getKind() != ParameterStatus::Reported) {922// If we didn't report anything yet, let's mark this parameter923// as called.924ParameterStatus Called(ParameterStatus::DefinitelyCalled, Call);925CurrentParamStatus = Called;926}927}928929/// Process escape of the parameter with the given index930void processEscapeFor(unsigned Index) {931ParameterStatus &CurrentParamStatus = CurrentState.getStatusFor(Index);932933// Escape overrides whatever error we think happened.934if (CurrentParamStatus.isErrorStatus() &&935CurrentParamStatus.getKind() != ParameterStatus::Kind::Reported) {936CurrentParamStatus = ParameterStatus::Escaped;937}938}939940void findAndReportNotCalledBranches(const CFGBlock *Parent, unsigned Index,941bool IsEscape = false) {942for (const CFGBlock *Succ : Parent->succs()) {943if (!Succ)944continue;945946if (getState(Succ).getKindFor(Index) == ParameterStatus::NotCalled) {947assert(Parent->succ_size() >= 2 &&948"Block should have at least two successors at this point");949if (auto Clarification = NotCalledClarifier::clarify(Parent, Succ)) {950const ParmVarDecl *Parameter = getParameter(Index);951Handler.handleNeverCalled(952Parameter, AC.getDecl(), Clarification->Location,953Clarification->Reason, !IsEscape, !isExplicitlyMarked(Parameter));954}955}956}957}958959//===----------------------------------------------------------------------===//960// Predicate functions to check parameters961//===----------------------------------------------------------------------===//962963/// Return true if parameter is explicitly marked as 'called_once'.964static bool isExplicitlyMarked(const ParmVarDecl *Parameter) {965return Parameter->hasAttr<CalledOnceAttr>();966}967968/// Return true if the given name matches conventional pattens.969static bool isConventional(llvm::StringRef Name) {970return llvm::count(CONVENTIONAL_NAMES, Name) != 0;971}972973/// Return true if the given name has conventional suffixes.974static bool hasConventionalSuffix(llvm::StringRef Name) {975return llvm::any_of(CONVENTIONAL_SUFFIXES, [Name](llvm::StringRef Suffix) {976return Name.ends_with(Suffix);977});978}979980/// Return true if the given type can be used for conventional parameters.981static bool isConventional(QualType Ty) {982if (!Ty->isBlockPointerType()) {983return false;984}985986QualType BlockType = Ty->castAs<BlockPointerType>()->getPointeeType();987// Completion handlers should have a block type with void return type.988return BlockType->castAs<FunctionType>()->getReturnType()->isVoidType();989}990991/// Return true if the only parameter of the function is conventional.992static bool isOnlyParameterConventional(const FunctionDecl *Function) {993IdentifierInfo *II = Function->getIdentifier();994return Function->getNumParams() == 1 && II &&995hasConventionalSuffix(II->getName());996}997998/// Return true/false if 'swift_async' attribute states that the given999/// parameter is conventionally called once.1000/// Return std::nullopt if the given declaration doesn't have 'swift_async'1001/// attribute.1002static std::optional<bool> isConventionalSwiftAsync(const Decl *D,1003unsigned ParamIndex) {1004if (const SwiftAsyncAttr *A = D->getAttr<SwiftAsyncAttr>()) {1005if (A->getKind() == SwiftAsyncAttr::None) {1006return false;1007}10081009return A->getCompletionHandlerIndex().getASTIndex() == ParamIndex;1010}1011return std::nullopt;1012}10131014/// Return true if the specified selector represents init method.1015static bool isInitMethod(Selector MethodSelector) {1016return MethodSelector.getMethodFamily() == OMF_init;1017}10181019/// Return true if the specified selector piece matches conventions.1020static bool isConventionalSelectorPiece(Selector MethodSelector,1021unsigned PieceIndex,1022QualType PieceType) {1023if (!isConventional(PieceType) || isInitMethod(MethodSelector)) {1024return false;1025}10261027if (MethodSelector.getNumArgs() == 1) {1028assert(PieceIndex == 0);1029return hasConventionalSuffix(MethodSelector.getNameForSlot(0));1030}10311032llvm::StringRef PieceName = MethodSelector.getNameForSlot(PieceIndex);1033return isConventional(PieceName) || hasConventionalSuffix(PieceName);1034}10351036bool shouldBeCalledOnce(const ParmVarDecl *Parameter) const {1037return isExplicitlyMarked(Parameter) ||1038(CheckConventionalParameters &&1039(isConventional(Parameter->getName()) ||1040hasConventionalSuffix(Parameter->getName())) &&1041isConventional(Parameter->getType()));1042}10431044bool shouldBeCalledOnce(const DeclContext *ParamContext,1045const ParmVarDecl *Param) {1046unsigned ParamIndex = Param->getFunctionScopeIndex();1047if (const auto *Function = dyn_cast<FunctionDecl>(ParamContext)) {1048return shouldBeCalledOnce(Function, ParamIndex);1049}1050if (const auto *Method = dyn_cast<ObjCMethodDecl>(ParamContext)) {1051return shouldBeCalledOnce(Method, ParamIndex);1052}1053return shouldBeCalledOnce(Param);1054}10551056bool shouldBeCalledOnce(const BlockDecl *Block, unsigned ParamIndex) const {1057return shouldBeCalledOnce(Block->getParamDecl(ParamIndex));1058}10591060bool shouldBeCalledOnce(const FunctionDecl *Function,1061unsigned ParamIndex) const {1062if (ParamIndex >= Function->getNumParams()) {1063return false;1064}1065// 'swift_async' goes first and overrides anything else.1066if (auto ConventionalAsync =1067isConventionalSwiftAsync(Function, ParamIndex)) {1068return *ConventionalAsync;1069}10701071return shouldBeCalledOnce(Function->getParamDecl(ParamIndex)) ||1072(CheckConventionalParameters &&1073isOnlyParameterConventional(Function));1074}10751076bool shouldBeCalledOnce(const ObjCMethodDecl *Method,1077unsigned ParamIndex) const {1078Selector MethodSelector = Method->getSelector();1079if (ParamIndex >= MethodSelector.getNumArgs()) {1080return false;1081}10821083// 'swift_async' goes first and overrides anything else.1084if (auto ConventionalAsync = isConventionalSwiftAsync(Method, ParamIndex)) {1085return *ConventionalAsync;1086}10871088const ParmVarDecl *Parameter = Method->getParamDecl(ParamIndex);1089return shouldBeCalledOnce(Parameter) ||1090(CheckConventionalParameters &&1091isConventionalSelectorPiece(MethodSelector, ParamIndex,1092Parameter->getType()));1093}10941095bool shouldBeCalledOnce(const CallExpr *Call, unsigned ParamIndex) const {1096const FunctionDecl *Function = Call->getDirectCallee();1097return Function && shouldBeCalledOnce(Function, ParamIndex);1098}10991100bool shouldBeCalledOnce(const ObjCMessageExpr *Message,1101unsigned ParamIndex) const {1102const ObjCMethodDecl *Method = Message->getMethodDecl();1103return Method && ParamIndex < Method->param_size() &&1104shouldBeCalledOnce(Method, ParamIndex);1105}11061107//===----------------------------------------------------------------------===//1108// Utility methods1109//===----------------------------------------------------------------------===//11101111bool isCaptured(const ParmVarDecl *Parameter) const {1112if (const BlockDecl *Block = dyn_cast<BlockDecl>(AC.getDecl())) {1113return Block->capturesVariable(Parameter);1114}1115return false;1116}11171118// Return a call site where the block is called exactly once or null otherwise1119const Expr *getBlockGuaraneedCallSite(const BlockExpr *Block) const {1120ParentMap &PM = AC.getParentMap();11211122// We don't want to track the block through assignments and so on, instead1123// we simply see how the block used and if it's used directly in a call,1124// we decide based on call to what it is.1125//1126// In order to do this, we go up the parents of the block looking for1127// a call or a message expressions. These might not be immediate parents1128// of the actual block expression due to casts and parens, so we skip them.1129for (const Stmt *Prev = Block, *Current = PM.getParent(Block);1130Current != nullptr; Prev = Current, Current = PM.getParent(Current)) {1131// Skip no-op (for our case) operations.1132if (isa<CastExpr>(Current) || isa<ParenExpr>(Current))1133continue;11341135// At this point, Prev represents our block as an immediate child of the1136// call.1137if (const auto *Call = dyn_cast<CallExpr>(Current)) {1138// It might be the call of the Block itself...1139if (Call->getCallee() == Prev)1140return Call;11411142// ...or it can be an indirect call of the block.1143return shouldBlockArgumentBeCalledOnce(Call, Prev) ? Call : nullptr;1144}1145if (const auto *Message = dyn_cast<ObjCMessageExpr>(Current)) {1146return shouldBlockArgumentBeCalledOnce(Message, Prev) ? Message1147: nullptr;1148}11491150break;1151}11521153return nullptr;1154}11551156template <class CallLikeExpr>1157bool shouldBlockArgumentBeCalledOnce(const CallLikeExpr *CallOrMessage,1158const Stmt *BlockArgument) const {1159// CallExpr::arguments does not interact nicely with llvm::enumerate.1160llvm::ArrayRef<const Expr *> Arguments =1161llvm::ArrayRef(CallOrMessage->getArgs(), CallOrMessage->getNumArgs());11621163for (const auto &Argument : llvm::enumerate(Arguments)) {1164if (Argument.value() == BlockArgument) {1165return shouldBlockArgumentBeCalledOnce(CallOrMessage, Argument.index());1166}1167}11681169return false;1170}11711172bool shouldBlockArgumentBeCalledOnce(const CallExpr *Call,1173unsigned ParamIndex) const {1174const FunctionDecl *Function = Call->getDirectCallee();1175return shouldBlockArgumentBeCalledOnce(Function, ParamIndex) ||1176shouldBeCalledOnce(Call, ParamIndex);1177}11781179bool shouldBlockArgumentBeCalledOnce(const ObjCMessageExpr *Message,1180unsigned ParamIndex) const {1181// At the moment, we don't have any Obj-C methods we want to specifically1182// check in here.1183return shouldBeCalledOnce(Message, ParamIndex);1184}11851186static bool shouldBlockArgumentBeCalledOnce(const FunctionDecl *Function,1187unsigned ParamIndex) {1188// There is a list of important API functions that while not following1189// conventions nor being directly annotated, still guarantee that the1190// callback parameter will be called exactly once.1191//1192// Here we check if this is the case.1193return Function &&1194llvm::any_of(KNOWN_CALLED_ONCE_PARAMETERS,1195[Function, ParamIndex](1196const KnownCalledOnceParameter &Reference) {1197return Reference.FunctionName ==1198Function->getName() &&1199Reference.ParamIndex == ParamIndex;1200});1201}12021203/// Return true if the analyzed function is actually a default implementation1204/// of the method that has to be overriden.1205///1206/// These functions can have tracked parameters, but wouldn't call them1207/// because they are not designed to perform any meaningful actions.1208///1209/// There are a couple of flavors of such default implementations:1210/// 1. Empty methods or methods with a single return statement1211/// 2. Methods that have one block with a call to no return function1212/// 3. Methods with only assertion-like operations1213bool isPossiblyEmptyImpl() const {1214if (!isa<ObjCMethodDecl>(AC.getDecl())) {1215// We care only about functions that are not supposed to be called.1216// Only methods can be overriden.1217return false;1218}12191220// Case #1 (without return statements)1221if (FunctionCFG.size() == 2) {1222// Method has only two blocks: ENTRY and EXIT.1223// This is equivalent to empty function.1224return true;1225}12261227// Case #21228if (FunctionCFG.size() == 3) {1229const CFGBlock &Entry = FunctionCFG.getEntry();1230if (Entry.succ_empty()) {1231return false;1232}12331234const CFGBlock *OnlyBlock = *Entry.succ_begin();1235// Method has only one block, let's see if it has a no-return1236// element.1237if (OnlyBlock && OnlyBlock->hasNoReturnElement()) {1238return true;1239}1240// Fallthrough, CFGs with only one block can fall into #1 and #3 as well.1241}12421243// Cases #1 (return statements) and #3.1244//1245// It is hard to detect that something is an assertion or came1246// from assertion. Here we use a simple heuristic:1247//1248// - If it came from a macro, it can be an assertion.1249//1250// Additionally, we can't assume a number of basic blocks or the CFG's1251// structure because assertions might include loops and conditions.1252return llvm::all_of(FunctionCFG, [](const CFGBlock *BB) {1253if (!BB) {1254// Unreachable blocks are totally fine.1255return true;1256}12571258// Return statements can have sub-expressions that are represented as1259// separate statements of a basic block. We should allow this.1260// This parent map will be initialized with a parent tree for all1261// subexpressions of the block's return statement (if it has one).1262std::unique_ptr<ParentMap> ReturnChildren;12631264return llvm::all_of(1265llvm::reverse(*BB), // we should start with return statements, if we1266// have any, i.e. from the bottom of the block1267[&ReturnChildren](const CFGElement &Element) {1268if (std::optional<CFGStmt> S = Element.getAs<CFGStmt>()) {1269const Stmt *SuspiciousStmt = S->getStmt();12701271if (isa<ReturnStmt>(SuspiciousStmt)) {1272// Let's initialize this structure to test whether1273// some further statement is a part of this return.1274ReturnChildren = std::make_unique<ParentMap>(1275const_cast<Stmt *>(SuspiciousStmt));1276// Return statements are allowed as part of #1.1277return true;1278}12791280return SuspiciousStmt->getBeginLoc().isMacroID() ||1281(ReturnChildren &&1282ReturnChildren->hasParent(SuspiciousStmt));1283}1284return true;1285});1286});1287}12881289/// Check if parameter with the given index has ever escaped.1290bool hasEverEscaped(unsigned Index) const {1291return llvm::any_of(States, [Index](const State &StateForOneBB) {1292return StateForOneBB.getKindFor(Index) == ParameterStatus::Escaped;1293});1294}12951296/// Return status stored for the given basic block.1297/// \{1298State &getState(const CFGBlock *BB) {1299assert(BB);1300return States[BB->getBlockID()];1301}1302const State &getState(const CFGBlock *BB) const {1303assert(BB);1304return States[BB->getBlockID()];1305}1306/// \}13071308/// Assign status to the given basic block.1309///1310/// Returns true when the stored status changed.1311bool assignState(const CFGBlock *BB, const State &ToAssign) {1312State &Current = getState(BB);1313if (Current == ToAssign) {1314return false;1315}13161317Current = ToAssign;1318return true;1319}13201321/// Join all incoming statuses for the given basic block.1322State joinSuccessors(const CFGBlock *BB) const {1323auto Succs =1324llvm::make_filter_range(BB->succs(), [this](const CFGBlock *Succ) {1325return Succ && this->getState(Succ).isVisited();1326});1327// We came to this block from somewhere after all.1328assert(!Succs.empty() &&1329"Basic block should have at least one visited successor");13301331State Result = getState(*Succs.begin());13321333for (const CFGBlock *Succ : llvm::drop_begin(Succs, 1)) {1334Result.join(getState(Succ));1335}13361337if (const Expr *Condition = getCondition(BB->getTerminatorStmt())) {1338handleConditional(BB, Condition, Result);1339}13401341return Result;1342}13431344void handleConditional(const CFGBlock *BB, const Expr *Condition,1345State &ToAlter) const {1346handleParameterCheck(BB, Condition, ToAlter);1347if (SuppressOnConventionalErrorPaths) {1348handleConventionalCheck(BB, Condition, ToAlter);1349}1350}13511352void handleParameterCheck(const CFGBlock *BB, const Expr *Condition,1353State &ToAlter) const {1354// In this function, we try to deal with the following pattern:1355//1356// if (parameter)1357// parameter(...);1358//1359// It's not good to show a warning here because clearly 'parameter'1360// couldn't and shouldn't be called on the 'else' path.1361//1362// Let's check if this if statement has a check involving one of1363// the tracked parameters.1364if (const ParmVarDecl *Parameter = findReferencedParmVarDecl(1365Condition,1366/* ShouldRetrieveFromComparisons = */ true)) {1367if (const auto Index = getIndex(*Parameter)) {1368ParameterStatus &CurrentStatus = ToAlter.getStatusFor(*Index);13691370// We don't want to deep dive into semantics of the check and1371// figure out if that check was for null or something else.1372// We simply trust the user that they know what they are doing.1373//1374// For this reason, in the following loop we look for the1375// best-looking option.1376for (const CFGBlock *Succ : BB->succs()) {1377if (!Succ)1378continue;13791380const ParameterStatus &StatusInSucc =1381getState(Succ).getStatusFor(*Index);13821383if (StatusInSucc.isErrorStatus()) {1384continue;1385}13861387// Let's use this status instead.1388CurrentStatus = StatusInSucc;13891390if (StatusInSucc.getKind() == ParameterStatus::DefinitelyCalled) {1391// This is the best option to have and we already found it.1392break;1393}13941395// If we found 'Escaped' first, we still might find 'DefinitelyCalled'1396// on the other branch. And we prefer the latter.1397}1398}1399}1400}14011402void handleConventionalCheck(const CFGBlock *BB, const Expr *Condition,1403State &ToAlter) const {1404// Even when the analysis is technically correct, it is a widespread pattern1405// not to call completion handlers in some scenarios. These usually have1406// typical conditional names, such as 'error' or 'cancel'.1407if (!mentionsAnyOfConventionalNames(Condition)) {1408return;1409}14101411for (const auto &IndexedStatus : llvm::enumerate(ToAlter)) {1412const ParmVarDecl *Parameter = getParameter(IndexedStatus.index());1413// Conventions do not apply to explicitly marked parameters.1414if (isExplicitlyMarked(Parameter)) {1415continue;1416}14171418ParameterStatus &CurrentStatus = IndexedStatus.value();1419// If we did find that on one of the branches the user uses the callback1420// and doesn't on the other path, we believe that they know what they are1421// doing and trust them.1422//1423// There are two possible scenarios for that:1424// 1. Current status is 'MaybeCalled' and one of the branches is1425// 'DefinitelyCalled'1426// 2. Current status is 'NotCalled' and one of the branches is 'Escaped'1427if (isLosingCall(ToAlter, BB, IndexedStatus.index()) ||1428isLosingEscape(ToAlter, BB, IndexedStatus.index())) {1429CurrentStatus = ParameterStatus::Escaped;1430}1431}1432}14331434bool isLosingCall(const State &StateAfterJoin, const CFGBlock *JoinBlock,1435unsigned ParameterIndex) const {1436// Let's check if the block represents DefinitelyCalled -> MaybeCalled1437// transition.1438return isLosingJoin(StateAfterJoin, JoinBlock, ParameterIndex,1439ParameterStatus::MaybeCalled,1440ParameterStatus::DefinitelyCalled);1441}14421443bool isLosingEscape(const State &StateAfterJoin, const CFGBlock *JoinBlock,1444unsigned ParameterIndex) const {1445// Let's check if the block represents Escaped -> NotCalled transition.1446return isLosingJoin(StateAfterJoin, JoinBlock, ParameterIndex,1447ParameterStatus::NotCalled, ParameterStatus::Escaped);1448}14491450bool isLosingJoin(const State &StateAfterJoin, const CFGBlock *JoinBlock,1451unsigned ParameterIndex, ParameterStatus::Kind AfterJoin,1452ParameterStatus::Kind BeforeJoin) const {1453assert(!ParameterStatus::isErrorStatus(BeforeJoin) &&1454ParameterStatus::isErrorStatus(AfterJoin) &&1455"It's not a losing join if statuses do not represent "1456"correct-to-error transition");14571458const ParameterStatus &CurrentStatus =1459StateAfterJoin.getStatusFor(ParameterIndex);14601461return CurrentStatus.getKind() == AfterJoin &&1462anySuccessorHasStatus(JoinBlock, ParameterIndex, BeforeJoin);1463}14641465/// Return true if any of the successors of the given basic block has1466/// a specified status for the given parameter.1467bool anySuccessorHasStatus(const CFGBlock *Parent, unsigned ParameterIndex,1468ParameterStatus::Kind ToFind) const {1469return llvm::any_of(1470Parent->succs(), [this, ParameterIndex, ToFind](const CFGBlock *Succ) {1471return Succ && getState(Succ).getKindFor(ParameterIndex) == ToFind;1472});1473}14741475/// Check given expression that was discovered to escape.1476void checkEscapee(const Expr *E) {1477if (const ParmVarDecl *Parameter = findReferencedParmVarDecl(E)) {1478checkEscapee(*Parameter);1479}1480}14811482/// Check given parameter that was discovered to escape.1483void checkEscapee(const ParmVarDecl &Parameter) {1484if (auto Index = getIndex(Parameter)) {1485processEscapeFor(*Index);1486}1487}14881489/// Mark all parameters in the current state as 'no-return'.1490void markNoReturn() {1491for (ParameterStatus &PS : CurrentState) {1492PS = ParameterStatus::NoReturn;1493}1494}14951496/// Check if the given assignment represents suppression and act on it.1497void checkSuppression(const BinaryOperator *Assignment) {1498// Suppression has the following form:1499// parameter = 0;1500// 0 can be of any form (NULL, nil, etc.)1501if (auto Index = getIndexOfExpression(Assignment->getLHS())) {15021503// We don't care what is written in the RHS, it could be whatever1504// we can interpret as 0.1505if (auto Constant =1506Assignment->getRHS()->IgnoreParenCasts()->getIntegerConstantExpr(1507AC.getASTContext())) {15081509ParameterStatus &CurrentParamStatus = CurrentState.getStatusFor(*Index);15101511if (0 == *Constant && CurrentParamStatus.seenAnyCalls()) {1512// Even though this suppression mechanism is introduced to tackle1513// false positives for multiple calls, the fact that the user has1514// to use suppression can also tell us that we couldn't figure out1515// how different paths cancel each other out. And if that is true,1516// we will most certainly have false positives about parameters not1517// being called on certain paths.1518//1519// For this reason, we abandon tracking this parameter altogether.1520CurrentParamStatus = ParameterStatus::Reported;1521}1522}1523}1524}15251526public:1527//===----------------------------------------------------------------------===//1528// Tree traversal methods1529//===----------------------------------------------------------------------===//15301531void VisitCallExpr(const CallExpr *Call) {1532// This call might be a direct call, i.e. a parameter call...1533checkDirectCall(Call);1534// ... or an indirect call, i.e. when parameter is an argument.1535checkIndirectCall(Call);1536}15371538void VisitObjCMessageExpr(const ObjCMessageExpr *Message) {1539// The most common situation that we are defending against here is1540// copying a tracked parameter.1541if (const Expr *Receiver = Message->getInstanceReceiver()) {1542checkEscapee(Receiver);1543}1544// Message expressions unlike calls, could not be direct.1545checkIndirectCall(Message);1546}15471548void VisitBlockExpr(const BlockExpr *Block) {1549// Block expressions are tricky. It is a very common practice to capture1550// completion handlers by blocks and use them there.1551// For this reason, it is important to analyze blocks and report warnings1552// for completion handler misuse in blocks.1553//1554// However, it can be quite difficult to track how the block itself is being1555// used. The full precise anlysis of that will be similar to alias analysis1556// for completion handlers and can be too heavyweight for a compile-time1557// diagnostic. Instead, we judge about the immediate use of the block.1558//1559// Here, we try to find a call expression where we know due to conventions,1560// annotations, or other reasons that the block is called once and only1561// once.1562const Expr *CalledOnceCallSite = getBlockGuaraneedCallSite(Block);15631564// We need to report this information to the handler because in the1565// situation when we know that the block is called exactly once, we can be1566// stricter in terms of reported diagnostics.1567if (CalledOnceCallSite) {1568Handler.handleBlockThatIsGuaranteedToBeCalledOnce(Block->getBlockDecl());1569} else {1570Handler.handleBlockWithNoGuarantees(Block->getBlockDecl());1571}15721573for (const auto &Capture : Block->getBlockDecl()->captures()) {1574if (const auto *Param = dyn_cast<ParmVarDecl>(Capture.getVariable())) {1575if (auto Index = getIndex(*Param)) {1576if (CalledOnceCallSite) {1577// The call site of a block can be considered a call site of the1578// captured parameter we track.1579processCallFor(*Index, CalledOnceCallSite);1580} else {1581// We still should consider this block as an escape for parameter,1582// if we don't know about its call site or the number of time it1583// can be invoked.1584processEscapeFor(*Index);1585}1586}1587}1588}1589}15901591void VisitBinaryOperator(const BinaryOperator *Op) {1592if (Op->getOpcode() == clang::BO_Assign) {1593// Let's check if one of the tracked parameters is assigned into1594// something, and if it is we don't want to track extra variables, so we1595// consider it as an escapee.1596checkEscapee(Op->getRHS());15971598// Let's check whether this assignment is a suppression.1599checkSuppression(Op);1600}1601}16021603void VisitDeclStmt(const DeclStmt *DS) {1604// Variable initialization is not assignment and should be handled1605// separately.1606//1607// Multiple declarations can be a part of declaration statement.1608for (const auto *Declaration : DS->getDeclGroup()) {1609if (const auto *Var = dyn_cast<VarDecl>(Declaration)) {1610if (Var->getInit()) {1611checkEscapee(Var->getInit());1612}16131614if (Var->hasAttr<CleanupAttr>()) {1615FunctionHasCleanupVars = true;1616}1617}1618}1619}16201621void VisitCStyleCastExpr(const CStyleCastExpr *Cast) {1622// We consider '(void)parameter' as a manual no-op escape.1623// It should be used to explicitly tell the analysis that this parameter1624// is intentionally not called on this path.1625if (Cast->getType().getCanonicalType()->isVoidType()) {1626checkEscapee(Cast->getSubExpr());1627}1628}16291630void VisitObjCAtThrowStmt(const ObjCAtThrowStmt *) {1631// It is OK not to call marked parameters on exceptional paths.1632markNoReturn();1633}16341635private:1636unsigned size() const { return TrackedParams.size(); }16371638std::optional<unsigned> getIndexOfCallee(const CallExpr *Call) const {1639return getIndexOfExpression(Call->getCallee());1640}16411642std::optional<unsigned> getIndexOfExpression(const Expr *E) const {1643if (const ParmVarDecl *Parameter = findReferencedParmVarDecl(E)) {1644return getIndex(*Parameter);1645}16461647return std::nullopt;1648}16491650std::optional<unsigned> getIndex(const ParmVarDecl &Parameter) const {1651// Expected number of parameters that we actually track is 1.1652//1653// Also, the maximum number of declared parameters could not be on a scale1654// of hundreds of thousands.1655//1656// In this setting, linear search seems reasonable and even performs better1657// than bisection.1658ParamSizedVector<const ParmVarDecl *>::const_iterator It =1659llvm::find(TrackedParams, &Parameter);16601661if (It != TrackedParams.end()) {1662return It - TrackedParams.begin();1663}16641665return std::nullopt;1666}16671668const ParmVarDecl *getParameter(unsigned Index) const {1669assert(Index < TrackedParams.size());1670return TrackedParams[Index];1671}16721673const CFG &FunctionCFG;1674AnalysisDeclContext &AC;1675CalledOnceCheckHandler &Handler;1676bool CheckConventionalParameters;1677// As of now, we turn this behavior off. So, we still are going to report1678// missing calls on paths that look like it was intentional.1679// Technically such reports are true positives, but they can make some users1680// grumpy because of the sheer number of warnings.1681// It can be turned back on if we decide that we want to have the other way1682// around.1683bool SuppressOnConventionalErrorPaths = false;16841685// The user can annotate variable declarations with cleanup functions, which1686// essentially imposes a custom destructor logic on that variable.1687// It is possible to use it, however, to call tracked parameters on all exits1688// from the function. For this reason, we track the fact that the function1689// actually has these.1690bool FunctionHasCleanupVars = false;16911692State CurrentState;1693ParamSizedVector<const ParmVarDecl *> TrackedParams;1694CFGSizedVector<State> States;1695};16961697} // end anonymous namespace16981699namespace clang {1700void checkCalledOnceParameters(AnalysisDeclContext &AC,1701CalledOnceCheckHandler &Handler,1702bool CheckConventionalParameters) {1703CalledOnceChecker::check(AC, Handler, CheckConventionalParameters);1704}1705} // end namespace clang170617071708