|
|
|||
File indexing completed on 2026-05-10 08:37:12
0001 //===--- MatchConsumer.h - MatchConsumer abstraction ------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 /// 0009 /// \file This file defines the *MatchConsumer* abstraction: a computation over 0010 /// match results, specifically the `ast_matchers::MatchFinder::MatchResult` 0011 /// class. 0012 /// 0013 //===----------------------------------------------------------------------===// 0014 0015 #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_MATCHCONSUMER_H 0016 #define LLVM_CLANG_TOOLING_TRANSFORMER_MATCHCONSUMER_H 0017 0018 #include "clang/AST/ASTTypeTraits.h" 0019 #include "clang/ASTMatchers/ASTMatchFinder.h" 0020 #include "llvm/ADT/StringRef.h" 0021 #include "llvm/Support/Errc.h" 0022 #include "llvm/Support/Error.h" 0023 0024 namespace clang { 0025 namespace transformer { 0026 /// A failable computation over nodes bound by AST matchers. 0027 /// 0028 /// The computation should report any errors though its return value (rather 0029 /// than terminating the program) to enable usage in interactive scenarios like 0030 /// clang-query. 0031 /// 0032 /// This is a central abstraction of the Transformer framework. 0033 template <typename T> 0034 using MatchConsumer = 0035 std::function<Expected<T>(const ast_matchers::MatchFinder::MatchResult &)>; 0036 0037 /// Creates an error that signals that a `MatchConsumer` expected a certain node 0038 /// to be bound by AST matchers, but it was not actually bound. 0039 inline llvm::Error notBoundError(llvm::StringRef Id) { 0040 return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument, 0041 "Id not bound: " + Id); 0042 } 0043 0044 /// Chooses between the two consumers, based on whether \p ID is bound in the 0045 /// match. 0046 template <typename T> 0047 MatchConsumer<T> ifBound(std::string ID, MatchConsumer<T> TrueC, 0048 MatchConsumer<T> FalseC) { 0049 return [=](const ast_matchers::MatchFinder::MatchResult &Result) { 0050 auto &Map = Result.Nodes.getMap(); 0051 return (Map.find(ID) != Map.end() ? TrueC : FalseC)(Result); 0052 }; 0053 } 0054 0055 /// A failable computation over nodes bound by AST matchers, with (limited) 0056 /// reflection via the `toString` method. 0057 /// 0058 /// The computation should report any errors though its return value (rather 0059 /// than terminating the program) to enable usage in interactive scenarios like 0060 /// clang-query. 0061 /// 0062 /// This is a central abstraction of the Transformer framework. It is a 0063 /// generalization of `MatchConsumer` and intended to replace it. 0064 template <typename T> class MatchComputation { 0065 public: 0066 virtual ~MatchComputation() = default; 0067 0068 /// Evaluates the computation and (potentially) updates the accumulator \c 0069 /// Result. \c Result is undefined in the case of an error. `Result` is an 0070 /// out parameter to optimize case where the computation involves composing 0071 /// the result of sub-computation evaluations. 0072 virtual llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &Match, 0073 T *Result) const = 0; 0074 0075 /// Convenience version of `eval`, for the case where the computation is being 0076 /// evaluated on its own. 0077 llvm::Expected<T> eval(const ast_matchers::MatchFinder::MatchResult &R) const; 0078 0079 /// Constructs a string representation of the computation, for informational 0080 /// purposes. The representation must be deterministic, but is not required to 0081 /// be unique. 0082 virtual std::string toString() const = 0; 0083 0084 protected: 0085 MatchComputation() = default; 0086 0087 // Since this is an abstract class, copying/assigning only make sense for 0088 // derived classes implementing `clone()`. 0089 MatchComputation(const MatchComputation &) = default; 0090 MatchComputation &operator=(const MatchComputation &) = default; 0091 }; 0092 0093 template <typename T> 0094 llvm::Expected<T> MatchComputation<T>::eval( 0095 const ast_matchers::MatchFinder::MatchResult &R) const { 0096 T Output; 0097 if (auto Err = eval(R, &Output)) 0098 return std::move(Err); 0099 return Output; 0100 } 0101 } // namespace transformer 0102 } // namespace clang 0103 #endif // LLVM_CLANG_TOOLING_TRANSFORMER_MATCHCONSUMER_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|