clang-tools  7.0.0
TrailingReturnCheck.cpp
Go to the documentation of this file.
1 //===--- TrailingReturnCheck.cpp - clang-tidy------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "TrailingReturnCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchersInternal.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace fuchsia {
20 
21 namespace {
22 AST_MATCHER(FunctionDecl, hasTrailingReturn) {
23  return Node.getType()->castAs<FunctionProtoType>()->hasTrailingReturn();
24 }
25 } // namespace
26 
27 void TrailingReturnCheck::registerMatchers(MatchFinder *Finder) {
28 
29  // Requires C++11 or later.
30  if (!getLangOpts().CPlusPlus11)
31  return;
32 
33  // Functions that have trailing returns are disallowed, except for those
34  // using decltype specifiers and lambda with otherwise unutterable
35  // return types.
36  Finder->addMatcher(
37  functionDecl(allOf(hasTrailingReturn(),
38  unless(anyOf(returns(decltypeType()),
39  hasParent(cxxRecordDecl(isLambda()))))))
40  .bind("decl"),
41  this);
42 }
43 
44 void TrailingReturnCheck::check(const MatchFinder::MatchResult &Result) {
45  if (const auto *D = Result.Nodes.getNodeAs<Decl>("decl"))
46  diag(D->getLocStart(),
47  "a trailing return type is disallowed for this type of declaration");
48 }
49 
50 } // namespace fuchsia
51 } // namespace tidy
52 } // namespace clang
AST_MATCHER(BinaryOperator, isAssignmentOperator)
Definition: Matchers.h:20
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//