blob: 844d672f121fbad1544d58a29387e6369245ff7a [file] [log] [blame]
abelkocsis0f4c70d2020-03-21 12:02:00 +01001//===--- 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
13using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
17namespace bugprone {
18
19void SpuriouslyWakeUpFunctionsCheck::registerMatchers(MatchFinder *Finder) {
20
21 auto hasUniqueLock = hasDescendant(declRefExpr(
22 hasDeclaration(varDecl(hasType(recordDecl(classTemplateSpecializationDecl(
23 hasName("::std::unique_lock"),
24 hasTemplateArgument(
25 0, templateArgument(refersToType(qualType(hasDeclaration(
26 cxxRecordDecl(hasName("::std::mutex"))))))))))))));
27
28 auto hasWaitDescendantCPP = hasDescendant(
29 cxxMemberCallExpr(
30 anyOf(
31 allOf(hasDescendant(memberExpr(hasDeclaration(functionDecl(
32 allOf(hasName("::std::condition_variable::wait"),
33 parameterCountIs(1)))))),
34 onImplicitObjectArgument(
35 declRefExpr(to(varDecl(hasType(references(recordDecl(
36 hasName("::std::condition_variable")))))))),
37 hasUniqueLock),
38 allOf(hasDescendant(memberExpr(hasDeclaration(functionDecl(
39 allOf(hasName("::std::condition_variable::wait_for"),
40 parameterCountIs(2)))))),
41 onImplicitObjectArgument(
42 declRefExpr(to(varDecl(hasType(references(recordDecl(
43 hasName("::std::condition_variable")))))))),
44 hasUniqueLock),
45 allOf(hasDescendant(memberExpr(hasDeclaration(functionDecl(
46 allOf(hasName("::std::condition_variable::wait_until"),
47 parameterCountIs(2)))))),
48 onImplicitObjectArgument(
49 declRefExpr(to(varDecl(hasType(references(recordDecl(
50 hasName("::std::condition_variable")))))))),
51 hasUniqueLock)
52
53 ))
54 .bind("wait"));
55
56 auto hasWaitDescendantC = hasDescendant(
57 callExpr(callee(functionDecl(
58 anyOf(hasName("cnd_wait"), hasName("cnd_timedwait")))))
59 .bind("wait"));
60 if (getLangOpts().CPlusPlus) {
61 // Check for `CON54-CPP`
62 Finder->addMatcher(
63 ifStmt(
64
65 allOf(hasWaitDescendantCPP,
66 unless(anyOf(hasDescendant(ifStmt(hasWaitDescendantCPP)),
67 hasDescendant(whileStmt(hasWaitDescendantCPP)),
68 hasDescendant(forStmt(hasWaitDescendantCPP)),
69 hasDescendant(doStmt(hasWaitDescendantCPP)))))
70
71 ),
72 this);
73 } else {
74 // Check for `CON36-C`
75 Finder->addMatcher(
76
77 ifStmt(
78 allOf(hasWaitDescendantC,
79 unless(anyOf(hasDescendant(ifStmt(hasWaitDescendantC)),
80 hasDescendant(whileStmt(hasWaitDescendantC)),
81 hasDescendant(forStmt(hasWaitDescendantC)),
82 hasDescendant(doStmt(hasWaitDescendantC)),
83 hasParent(whileStmt()),
84 hasParent(compoundStmt(hasParent(whileStmt()))),
85 hasParent(forStmt()),
86 hasParent(compoundStmt(hasParent(forStmt()))),
87 hasParent(doStmt()),
88 hasParent(compoundStmt(hasParent(doStmt())))))
89
90 ))
91
92 ,
93 this);
94 }
95}
96
97void SpuriouslyWakeUpFunctionsCheck::check(
98 const MatchFinder::MatchResult &Result) {
99 const auto *MatchedWait = Result.Nodes.getNodeAs<CallExpr>("wait");
100 StringRef WaitName = MatchedWait->getDirectCallee()->getName();
101 diag(MatchedWait->getExprLoc(),
102 "'%0' should be placed inside a while statement %select{|or used with a "
103 "conditional parameter}1")
104 << WaitName << (WaitName != "cnd_wait" && WaitName != "cnd_timedwait");
105}
106} // namespace bugprone
107} // namespace tidy
108} // namespace clang