| 1 |
//===--- SpuriouslyWakeUpFunctionsCheck.cpp - clang-tidy ------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "SpuriouslyWakeUpFunctionsCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 |
|
| 13 |
using namespace clang::ast_matchers; |
| 14 |
|
| 15 |
namespace clang::tidy::bugprone { |
| 16 |
|
| 17 |
void SpuriouslyWakeUpFunctionsCheck::registerMatchers(MatchFinder *Finder) { |
| 18 |
|
| 19 |
auto HasUniqueLock = hasDescendant(declRefExpr( |
| 20 |
hasDeclaration(varDecl(hasType(recordDecl(classTemplateSpecializationDecl( |
| 21 |
hasName("::std::unique_lock"), |
| 22 |
hasTemplateArgument( |
| 23 |
0, templateArgument(refersToType(qualType(hasDeclaration( |
| 24 |
cxxRecordDecl(hasName("::std::mutex")))))))))))))); |
| 25 |
|
| 26 |
auto HasWaitDescendantCpp = hasDescendant( |
| 27 |
cxxMemberCallExpr( |
| 28 |
anyOf( |
| 29 |
allOf(hasDescendant(memberExpr(hasDeclaration(functionDecl( |
| 30 |
allOf(hasName("::std::condition_variable::wait"), |
| 31 |
parameterCountIs(1)))))), |
| 32 |
onImplicitObjectArgument( |
| 33 |
declRefExpr(to(varDecl(hasType(references(recordDecl( |
| 34 |
hasName("::std::condition_variable")))))))), |
| 35 |
HasUniqueLock), |
| 36 |
allOf(hasDescendant(memberExpr(hasDeclaration(functionDecl( |
| 37 |
allOf(hasName("::std::condition_variable::wait_for"), |
| 38 |
parameterCountIs(2)))))), |
| 39 |
onImplicitObjectArgument( |
| 40 |
declRefExpr(to(varDecl(hasType(references(recordDecl( |
| 41 |
hasName("::std::condition_variable")))))))), |
| 42 |
HasUniqueLock), |
| 43 |
allOf(hasDescendant(memberExpr(hasDeclaration(functionDecl( |
| 44 |
allOf(hasName("::std::condition_variable::wait_until"), |
| 45 |
parameterCountIs(2)))))), |
| 46 |
onImplicitObjectArgument( |
| 47 |
declRefExpr(to(varDecl(hasType(references(recordDecl( |
| 48 |
hasName("::std::condition_variable")))))))), |
| 49 |
HasUniqueLock) |
| 50 |
|
| 51 |
)) |
| 52 |
.bind("wait")); |
| 53 |
|
| 54 |
auto HasWaitDescendantC = hasDescendant( |
| 55 |
callExpr(callee(functionDecl(hasAnyName("cnd_wait", "cnd_timedwait")))) |
| 56 |
.bind("wait")); |
| 57 |
if (getLangOpts().CPlusPlus) { |
| 58 |
// Check for `CON54-CPP` |
| 59 |
Finder->addMatcher( |
| 60 |
ifStmt(HasWaitDescendantCpp, |
| 61 |
unless(hasDescendant(mapAnyOf(ifStmt, whileStmt, forStmt, doStmt) |
| 62 |
.with(HasWaitDescendantCpp)))), |
| 63 |
this); |
| 64 |
} else { |
| 65 |
// Check for `CON36-C` |
| 66 |
Finder->addMatcher( |
| 67 |
ifStmt(HasWaitDescendantC, |
| 68 |
unless(anyOf( |
| 69 |
hasDescendant(mapAnyOf(ifStmt, whileStmt, forStmt, doStmt) |
| 70 |
.with(HasWaitDescendantC)), |
| 71 |
hasParent(mapAnyOf(whileStmt, forStmt, doStmt)), |
| 72 |
hasParent(compoundStmt( |
| 73 |
hasParent(mapAnyOf(whileStmt, forStmt, doStmt))))))), |
| 74 |
this); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void SpuriouslyWakeUpFunctionsCheck::check( |
| 79 |
const MatchFinder::MatchResult &Result) { |
| 80 |
const auto *MatchedWait = Result.Nodes.getNodeAs<CallExpr>("wait"); |
| 81 |
StringRef WaitName = MatchedWait->getDirectCallee()->getName(); |
| 82 |
diag(MatchedWait->getExprLoc(), |
| 83 |
"'%0' should be placed inside a while statement %select{|or used with a " |
| 84 |
"conditional parameter}1") |
| 85 |
<< WaitName << (WaitName != "cnd_wait" && WaitName != "cnd_timedwait"); |
| 86 |
} |
| 87 |
} // namespace clang::tidy::bugprone |