37#include "llvm/Support/Debug.h"
46const char *WarnAtNode =
"waitcall";
48static auto findGCDAntiPatternWithSemaphore() ->
decltype(
compoundStmt());
49static auto findGCDAntiPatternWithGroup() ->
decltype(
compoundStmt());
51class GCDAntipatternChecker :
public Checker<check::ASTCodeBody> {
54 const StatementMatcher SemaphoreMatcherM = findGCDAntiPatternWithSemaphore();
58 void checkASTCodeBody(
const Decl *D,
60 BugReporter &BR)
const;
63decltype(
auto) callsName(
const char *FunctionName) {
67decltype(
auto) equalsBoundArgDecl(
int ArgIdx,
const char *DeclName) {
68 return hasArgument(ArgIdx, ignoringParenCasts(
declRefExpr(
69 to(
varDecl(equalsBoundNode(DeclName))))));
72decltype(
auto) bindAssignmentToDecl(
const char *DeclName) {
73 return hasLHS(ignoringParenImpCasts(
81static bool isTest(
const Decl *D) {
82 if (
const auto* ND = dyn_cast<NamedDecl>(D)) {
83 std::string DeclName = ND->getNameAsString();
84 if (StringRef(DeclName).starts_with(
"test"))
87 if (
const auto *OD = dyn_cast<ObjCMethodDecl>(D)) {
88 if (
const auto *CD = dyn_cast<ObjCContainerDecl>(OD->getParent())) {
89 std::string ContainerName = CD->getNameAsString();
90 StringRef CN(ContainerName);
91 if (CN.contains_insensitive(
"test") || CN.contains_insensitive(
"mock"))
98static auto findGCDAntiPatternWithSemaphore() ->
decltype(
compoundStmt()) {
100 const char *SemaphoreBinding =
"semaphore_name";
102 callsName(
"dispatch_semaphore_create"),
105 auto SemaphoreBindingM =
anyOf(
109 hasRHS(SemaphoreCreateM))));
111 auto HasBlockArgumentM = hasAnyArgument(hasType(
117 callsName(
"dispatch_semaphore_signal"),
118 equalsBoundArgDecl(0, SemaphoreBinding)
121 auto HasBlockAndCallsSignalM =
allOf(HasBlockArgumentM, ArgCallsSignalM);
123 auto HasBlockCallingSignalM =
133 callsName(
"dispatch_semaphore_wait"),
134 equalsBoundArgDecl(0, SemaphoreBinding)
139 SemaphoreBindingM, HasBlockCallingSignalM, SemaphoreWaitM);
142static auto findGCDAntiPatternWithGroup() ->
decltype(
compoundStmt()) {
144 const char *GroupBinding =
"group_name";
145 auto DispatchGroupCreateM =
callExpr(callsName(
"dispatch_group_create"));
147 auto GroupBindingM =
anyOf(
151 hasRHS(DispatchGroupCreateM))));
155 equalsBoundArgDecl(0, GroupBinding)))));
157 auto HasBlockArgumentM = hasAnyArgument(hasType(
163 callsName(
"dispatch_group_leave"),
164 equalsBoundArgDecl(0, GroupBinding)
167 auto HasBlockAndCallsLeaveM =
allOf(HasBlockArgumentM, ArgCallsSignalM);
179 callsName(
"dispatch_group_wait"),
180 equalsBoundArgDecl(0, GroupBinding)
184 return compoundStmt(GroupBindingM, GroupEnterM, AcceptsBlockM, GroupWaitM);
191 const GCDAntipatternChecker *
Checker) {
195 std::string Diagnostics;
196 llvm::raw_string_ostream
OS(Diagnostics);
197 OS <<
"Waiting on a callback using a " <<
Type <<
" creates useless threads "
198 <<
"and is subject to priority inversion; consider "
199 <<
"using a synchronous API or changing the caller to be asynchronous";
204 "GCD performance anti-pattern",
208 SW->getSourceRange());
211void GCDAntipatternChecker::checkASTCodeBody(
const Decl *D,
220 for (BoundNodes
Match : Matches)
224 for (BoundNodes
Match : Matches)
230void ento::registerGCDAntipattern(CheckerManager &Mgr) {
234bool ento::shouldRegisterGCDAntipattern(
const CheckerManager &mgr) {
static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR, AnalysisManager &AM, const ObjCAutoreleaseWriteChecker *Checker)
AnalysisDeclContext contains the context data for the function, method or block under analysis.
const Decl * getDecl() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Decl - This represents one declaration (or definition), e.g.
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
The base class of the type hierarchy.
Maps string IDs to AST nodes matched by parts of a matcher.
const T * getNodeAs(StringRef ID) const
Returns the AST node bound to ID.
ASTContext & getASTContext() override
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
BugReporter is a utility class for generating PathDiagnostics for analysis.
const SourceManager & getSourceManager()
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerFrontend *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges={}, ArrayRef< FixItHint > Fixits={})
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, CompoundStmt > compoundStmt
Matches compound statements.
const AstTypeMatcher< BlockPointerType > blockPointerType
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const internal::VariadicDynCastAllOfMatcher< Stmt, ObjCMessageExpr > objcMessageExpr
Matches ObjectiveC Message invocation expressions.
internal::Matcher< Stmt > StatementMatcher
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, IntegerLiteral > integerLiteral
Matches integer literals of all sizes / encodings, e.g.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool equals(const til::SExpr *E1, const til::SExpr *E2)
Top level wrappers for InstallAPI frontend operations.
@ Match
This is not an overload because the signature exactly matches an existing declaration.