aboutsummaryrefslogtreecommitdiff
path: root/clang-tidy/fuchsia/TrailingReturnCheck.cpp
blob: 8494da8269c36eeffe1c211d4c769e39a1e0675c (plain)
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
//===--- TrailingReturnCheck.cpp - clang-tidy------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "TrailingReturnCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchersInternal.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace fuchsia {

namespace {
const internal::VariadicDynCastAllOfMatcher<Type, DecltypeType> decltypeType;

AST_MATCHER(FunctionDecl, hasTrailingReturn) {
  return Node.getType()->castAs<FunctionProtoType>()->hasTrailingReturn();
}
} // namespace

void TrailingReturnCheck::registerMatchers(MatchFinder *Finder) {

  // Requires C++11 or later.
  if (!getLangOpts().CPlusPlus11)
    return;

  // Functions that have trailing returns are disallowed, except for those
  // using decltype specifiers and lambda with otherwise unutterable
  // return types.
  Finder->addMatcher(
      functionDecl(allOf(hasTrailingReturn(),
                         unless(anyOf(returns(decltypeType()),
                                      hasParent(cxxRecordDecl(isLambda()))))))
          .bind("decl"),
      this);
}

void TrailingReturnCheck::check(const MatchFinder::MatchResult &Result) {
  if (const auto *D = Result.Nodes.getNodeAs<Decl>("decl"))
    diag(D->getLocStart(),
         "a trailing return type is disallowed for this type of declaration");
}

} // namespace fuchsia
} // namespace tidy
} // namespace clang