summaryrefslogtreecommitdiff
path: root/include/clang/Tooling/Transformer/MatchConsumer.h
blob: 0a1dbe13ea1e41b4e0e4e75959aacf40fe830b58 (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
55
56
57
58
59
60
61
62
//===--- MatchConsumer.h - MatchConsumer abstraction ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file This file defines the *MatchConsumer* abstraction: a computation over
/// match results, specifically the `ast_matchers::MatchFinder::MatchResult`
/// class.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLING_TRANSFORMER_MATCH_CONSUMER_H_
#define LLVM_CLANG_TOOLING_TRANSFORMER_MATCH_CONSUMER_H_

#include "clang/AST/ASTTypeTraits.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"

namespace clang {
namespace transformer {
/// A failable computation over nodes bound by AST matchers.
///
/// The computation should report any errors though its return value (rather
/// than terminating the program) to enable usage in interactive scenarios like
/// clang-query.
///
/// This is a central abstraction of the Transformer framework.
template <typename T>
using MatchConsumer =
    std::function<Expected<T>(const ast_matchers::MatchFinder::MatchResult &)>;

/// Creates an error that signals that a `MatchConsumer` expected a certain node
/// to be bound by AST matchers, but it was not actually bound.
inline llvm::Error notBoundError(llvm::StringRef Id) {
  return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
                                             "Id not bound: " + Id);
}

/// Chooses between the two consumers, based on whether \p ID is bound in the
/// match.
template <typename T>
MatchConsumer<T> ifBound(std::string ID, MatchConsumer<T> TrueC,
                         MatchConsumer<T> FalseC) {
  return [=](const ast_matchers::MatchFinder::MatchResult &Result) {
    auto &Map = Result.Nodes.getMap();
    return (Map.find(ID) != Map.end() ? TrueC : FalseC)(Result);
  };
}
} // namespace transformer

namespace tooling {
// DEPRECATED: Temporary alias supporting client migration to the `transformer`
// namespace.
using transformer::ifBound;
} // namespace tooling
} // namespace clang
#endif // LLVM_CLANG_TOOLING_TRANSFORMER_MATCH_CONSUMER_H_