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