1 //===--- AvoidReferenceCoroutineParametersCheck.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 "AvoidReferenceCoroutineParametersCheck.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::cppcoreguidelines {
16 
17 void AvoidReferenceCoroutineParametersCheck::registerMatchers(
18     MatchFinder *Finder) {
19   auto IsCoroMatcher =
20       hasDescendant(expr(anyOf(coyieldExpr(), coreturnStmt(), coawaitExpr())));
21   Finder->addMatcher(parmVarDecl(hasType(type(referenceType())),
22                                  hasAncestor(functionDecl(IsCoroMatcher)))
23                          .bind("param"),
24                      this);
25 }
26 
27 void AvoidReferenceCoroutineParametersCheck::check(
28     const MatchFinder::MatchResult &Result) {
29   if (const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param")) {
30     diag(Param->getBeginLoc(), "coroutine parameters should not be references");
31   }
32 }
33 
34 } // namespace clang::tidy::cppcoreguidelines
35