clang-tools  9.0.0
TrailingReturnCheck.cpp
Go to the documentation of this file.
1 //===--- TrailingReturnCheck.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 "TrailingReturnCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchersInternal.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace fuchsia {
19 
20 namespace {
21 AST_MATCHER(FunctionDecl, hasTrailingReturn) {
22  return Node.getType()->castAs<FunctionProtoType>()->hasTrailingReturn();
23 }
24 } // namespace
25 
26 void TrailingReturnCheck::registerMatchers(MatchFinder *Finder) {
27 
28  // Requires C++11 or later.
29  if (!getLangOpts().CPlusPlus11)
30  return;
31 
32  // Functions that have trailing returns are disallowed, except for those
33  // using decltype specifiers and lambda with otherwise unutterable
34  // return types.
35  Finder->addMatcher(
36  functionDecl(hasTrailingReturn(),
37  unless(anyOf(returns(decltypeType()),
38  hasParent(cxxRecordDecl(isLambda())))))
39  .bind("decl"),
40  this);
41 }
42 
43 void TrailingReturnCheck::check(const MatchFinder::MatchResult &Result) {
44  if (const auto *D = Result.Nodes.getNodeAs<Decl>("decl"))
45  diag(D->getBeginLoc(),
46  "a trailing return type is disallowed for this type of declaration");
47 }
48 
49 } // namespace fuchsia
50 } // namespace tidy
51 } // namespace clang
const Decl * D
Definition: XRefs.cpp:868
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36