1 //===--- ThreadCanceltypeAsynchronousCheck.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 "ThreadCanceltypeAsynchronousCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Preprocessor.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace concurrency {
19 
registerMatchers(MatchFinder * Finder)20 void ThreadCanceltypeAsynchronousCheck::registerMatchers(MatchFinder *Finder) {
21   Finder->addMatcher(
22       callExpr(
23           allOf(callee(functionDecl(hasName("::pthread_setcanceltype"))),
24                 argumentCountIs(2)),
25           hasArgument(0, isExpandedFromMacro("PTHREAD_CANCEL_ASYNCHRONOUS")))
26           .bind("setcanceltype"),
27       this);
28 }
29 
check(const MatchFinder::MatchResult & Result)30 void ThreadCanceltypeAsynchronousCheck::check(
31     const MatchFinder::MatchResult &Result) {
32   const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>("setcanceltype");
33   diag(MatchedExpr->getBeginLoc(), "the cancel type for a pthread should not "
34                                    "be 'PTHREAD_CANCEL_ASYNCHRONOUS'");
35 }
36 
37 } // namespace concurrency
38 } // namespace tidy
39 } // namespace clang
40