diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 13:58:24 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 14:11:53 +0300 |
commit | 11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch) | |
tree | fabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/include/clang/Tooling/Transformer/MatchConsumer.h | |
parent | 9685917341315774aad5733b1793b1e533a88bbb (diff) | |
download | ydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz |
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/include/clang/Tooling/Transformer/MatchConsumer.h')
-rw-r--r-- | contrib/libs/clang16/include/clang/Tooling/Transformer/MatchConsumer.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/contrib/libs/clang16/include/clang/Tooling/Transformer/MatchConsumer.h b/contrib/libs/clang16/include/clang/Tooling/Transformer/MatchConsumer.h new file mode 100644 index 0000000000..ead75d578e --- /dev/null +++ b/contrib/libs/clang16/include/clang/Tooling/Transformer/MatchConsumer.h @@ -0,0 +1,114 @@ +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===--- 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_MATCHCONSUMER_H +#define LLVM_CLANG_TOOLING_TRANSFORMER_MATCHCONSUMER_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); + }; +} + +/// A failable computation over nodes bound by AST matchers, with (limited) +/// reflection via the `toString` method. +/// +/// 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. It is a +/// generalization of `MatchConsumer` and intended to replace it. +template <typename T> class MatchComputation { +public: + virtual ~MatchComputation() = default; + + /// Evaluates the computation and (potentially) updates the accumulator \c + /// Result. \c Result is undefined in the case of an error. `Result` is an + /// out parameter to optimize case where the computation involves composing + /// the result of sub-computation evaluations. + virtual llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &Match, + T *Result) const = 0; + + /// Convenience version of `eval`, for the case where the computation is being + /// evaluated on its own. + llvm::Expected<T> eval(const ast_matchers::MatchFinder::MatchResult &R) const; + + /// Constructs a string representation of the computation, for informational + /// purposes. The representation must be deterministic, but is not required to + /// be unique. + virtual std::string toString() const = 0; + +protected: + MatchComputation() = default; + + // Since this is an abstract class, copying/assigning only make sense for + // derived classes implementing `clone()`. + MatchComputation(const MatchComputation &) = default; + MatchComputation &operator=(const MatchComputation &) = default; +}; + +template <typename T> +llvm::Expected<T> MatchComputation<T>::eval( + const ast_matchers::MatchFinder::MatchResult &R) const { + T Output; + if (auto Err = eval(R, &Output)) + return std::move(Err); + return Output; +} +} // namespace transformer +} // namespace clang +#endif // LLVM_CLANG_TOOLING_TRANSFORMER_MATCHCONSUMER_H + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif |