]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Tooling/Transformer/MatchConsumer.h
Vendor import of stripped clang trunk r375505, the last commit before
[FreeBSD/FreeBSD.git] / include / clang / Tooling / Transformer / MatchConsumer.h
1 //===--- MatchConsumer.h - MatchConsumer abstraction ------------*- C++ -*-===//
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 /// \file This file defines the *MatchConsumer* abstraction: a computation over
10 /// match results, specifically the `ast_matchers::MatchFinder::MatchResult`
11 /// class.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_MATCH_CONSUMER_H_
16 #define LLVM_CLANG_TOOLING_TRANSFORMER_MATCH_CONSUMER_H_
17
18 #include "clang/AST/ASTTypeTraits.h"
19 #include "clang/ASTMatchers/ASTMatchFinder.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/Error.h"
23
24 namespace clang {
25 namespace transformer {
26 /// A failable computation over nodes bound by AST matchers.
27 ///
28 /// The computation should report any errors though its return value (rather
29 /// than terminating the program) to enable usage in interactive scenarios like
30 /// clang-query.
31 ///
32 /// This is a central abstraction of the Transformer framework.
33 template <typename T>
34 using MatchConsumer =
35     std::function<Expected<T>(const ast_matchers::MatchFinder::MatchResult &)>;
36
37 /// Creates an error that signals that a `MatchConsumer` expected a certain node
38 /// to be bound by AST matchers, but it was not actually bound.
39 inline llvm::Error notBoundError(llvm::StringRef Id) {
40   return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
41                                              "Id not bound: " + Id);
42 }
43
44 /// Chooses between the two consumers, based on whether \p ID is bound in the
45 /// match.
46 template <typename T>
47 MatchConsumer<T> ifBound(std::string ID, MatchConsumer<T> TrueC,
48                          MatchConsumer<T> FalseC) {
49   return [=](const ast_matchers::MatchFinder::MatchResult &Result) {
50     auto &Map = Result.Nodes.getMap();
51     return (Map.find(ID) != Map.end() ? TrueC : FalseC)(Result);
52   };
53 }
54 } // namespace transformer
55
56 namespace tooling {
57 // DEPRECATED: Temporary alias supporting client migration to the `transformer`
58 // namespace.
59 using transformer::ifBound;
60 } // namespace tooling
61 } // namespace clang
62 #endif // LLVM_CLANG_TOOLING_TRANSFORMER_MATCH_CONSUMER_H_