aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/bugprone/SpuriouslyWakeUpFunctionsCheck.cpp
blob: ff1d7da7ff402af257ab6438c1523ad51f7aa156 (plain) (blame)
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
//===--- 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::tidy::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(HasWaitDescendantCpp,
               unless(hasDescendant(mapAnyOf(ifStmt, whileStmt, forStmt, doStmt)
                                        .with(HasWaitDescendantCpp)))),
        this);
  } else {
    // Check for `CON36-C`
    Finder->addMatcher(
        ifStmt(HasWaitDescendantC,
               unless(anyOf(
                   hasDescendant(mapAnyOf(ifStmt, whileStmt, forStmt, doStmt)
                                     .with(HasWaitDescendantC)),
                   hasParent(mapAnyOf(whileStmt, forStmt, doStmt)),
                   hasParent(compoundStmt(
                       hasParent(mapAnyOf(whileStmt, forStmt, 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 clang::tidy::bugprone