diff options
author | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-11-04 08:30:18 -0500 |
---|---|---|
committer | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-11-11 12:44:15 -0500 |
commit | 489449c28aaa45086d507fbad96826420adf409d (patch) | |
tree | aee53aafb71c762cbbb82c30688dd4ea7be00655 /clang/lib | |
parent | 14df08f0580cea8f8bec5814e3b895b373001b56 (diff) | |
download | bcm5719-llvm-489449c28aaa45086d507fbad96826420adf409d.tar.gz bcm5719-llvm-489449c28aaa45086d507fbad96826420adf409d.zip |
[libTooling] Further simplify `Stencil` type and introduce `MatchComputation`.
Summary:
This revision introduces a new interface `MatchComputation` which generalizes
the `Stencil` interface and replaces the `std::function` interface of
`MatchConsumer`. With this revision, `Stencil` (as an abstraction) becomes just
one collection of implementations of
`MatchComputation<std::string>`. Correspondingly, we remove the `Stencil` class
entirely in favor of a simple type alias, deprecate `MatchConsumer` and change
all functions that accepted `MatchConsumer<std::string>` to use
`MatchComputation<std::string>` instead.
Reviewers: gribozavr
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69802
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Tooling/Transformer/RewriteRule.cpp | 28 | ||||
-rw-r--r-- | clang/lib/Tooling/Transformer/Stencil.cpp | 40 |
2 files changed, 42 insertions, 26 deletions
diff --git a/clang/lib/Tooling/Transformer/RewriteRule.cpp b/clang/lib/Tooling/Transformer/RewriteRule.cpp index b96b5eeabad..20d3a371950 100644 --- a/clang/lib/Tooling/Transformer/RewriteRule.cpp +++ b/clang/lib/Tooling/Transformer/RewriteRule.cpp @@ -43,7 +43,7 @@ transformer::detail::translateEdits(const MatchResult &Result, // it as is currently done. if (!EditRange) return SmallVector<Transformation, 0>(); - auto Replacement = Edit.Replacement(Result); + auto Replacement = Edit.Replacement->eval(Result); if (!Replacement) return Replacement.takeError(); transformer::detail::Transformation T; @@ -61,6 +61,28 @@ ASTEdit transformer::changeTo(RangeSelector S, TextGenerator Replacement) { return E; } +namespace { +/// A \c TextGenerator that always returns a fixed string. +class SimpleTextGenerator : public MatchComputation<std::string> { + std::string S; + +public: + SimpleTextGenerator(std::string S) : S(std::move(S)) {} + llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &, + std::string *Result) const override { + Result->append(S); + return llvm::Error::success(); + } + std::string toString() const override { + return (llvm::Twine("text(\"") + S + "\")").str(); + } +}; +} // namespace + +ASTEdit transformer::remove(RangeSelector S) { + return change(std::move(S), std::make_shared<SimpleTextGenerator>("")); +} + RewriteRule transformer::makeRule(DynTypedMatcher M, SmallVector<ASTEdit, 1> Edits, TextGenerator Explanation) { return RewriteRule{{RewriteRule::Case{ @@ -176,3 +198,7 @@ transformer::detail::findSelectedCase(const MatchResult &Result, } constexpr llvm::StringLiteral RewriteRule::RootID; + +TextGenerator tooling::text(std::string M) { + return std::make_shared<SimpleTextGenerator>(std::move(M)); +} diff --git a/clang/lib/Tooling/Transformer/Stencil.cpp b/clang/lib/Tooling/Transformer/Stencil.cpp index d994fdcf870..486e18b341f 100644 --- a/clang/lib/Tooling/Transformer/Stencil.cpp +++ b/clang/lib/Tooling/Transformer/Stencil.cpp @@ -272,14 +272,6 @@ public: }; } // namespace -llvm::Expected<std::string> -StencilInterface::eval(const MatchFinder::MatchResult &R) const { - std::string Output; - if (auto Err = eval(R, &Output)) - return std::move(Err); - return Output; -} - Stencil transformer::detail::makeStencil(StringRef Text) { return text(Text); } Stencil transformer::detail::makeStencil(RangeSelector Selector) { @@ -287,52 +279,50 @@ Stencil transformer::detail::makeStencil(RangeSelector Selector) { } Stencil transformer::text(StringRef Text) { - return Stencil(std::make_shared<StencilImpl<RawTextData>>(Text)); + return std::make_shared<StencilImpl<RawTextData>>(Text); } Stencil transformer::selection(RangeSelector Selector) { - return Stencil( - std::make_shared<StencilImpl<SelectorData>>(std::move(Selector))); + return std::make_shared<StencilImpl<SelectorData>>(std::move(Selector)); } Stencil transformer::dPrint(StringRef Id) { - return Stencil(std::make_shared<StencilImpl<DebugPrintNodeData>>(Id)); + return std::make_shared<StencilImpl<DebugPrintNodeData>>(Id); } Stencil transformer::expression(llvm::StringRef Id) { - return Stencil(std::make_shared<StencilImpl<UnaryOperationData>>( - UnaryNodeOperator::Parens, Id)); + return std::make_shared<StencilImpl<UnaryOperationData>>( + UnaryNodeOperator::Parens, Id); } Stencil transformer::deref(llvm::StringRef ExprId) { - return Stencil(std::make_shared<StencilImpl<UnaryOperationData>>( - UnaryNodeOperator::Deref, ExprId)); + return std::make_shared<StencilImpl<UnaryOperationData>>( + UnaryNodeOperator::Deref, ExprId); } Stencil transformer::addressOf(llvm::StringRef ExprId) { - return Stencil(std::make_shared<StencilImpl<UnaryOperationData>>( - UnaryNodeOperator::Address, ExprId)); + return std::make_shared<StencilImpl<UnaryOperationData>>( + UnaryNodeOperator::Address, ExprId); } Stencil transformer::access(StringRef BaseId, Stencil Member) { - return Stencil( - std::make_shared<StencilImpl<AccessData>>(BaseId, std::move(Member))); + return std::make_shared<StencilImpl<AccessData>>(BaseId, std::move(Member)); } Stencil transformer::ifBound(StringRef Id, Stencil TrueStencil, Stencil FalseStencil) { - return Stencil(std::make_shared<StencilImpl<IfBoundData>>( - Id, std::move(TrueStencil), std::move(FalseStencil))); + return std::make_shared<StencilImpl<IfBoundData>>(Id, std::move(TrueStencil), + std::move(FalseStencil)); } Stencil transformer::run(MatchConsumer<std::string> Fn) { - return Stencil( - std::make_shared<StencilImpl<MatchConsumer<std::string>>>(std::move(Fn))); + return std::make_shared<StencilImpl<MatchConsumer<std::string>>>( + std::move(Fn)); } Stencil transformer::catVector(std::vector<Stencil> Parts) { // Only one argument, so don't wrap in sequence. if (Parts.size() == 1) return std::move(Parts[0]); - return Stencil(std::make_shared<StencilImpl<SequenceData>>(std::move(Parts))); + return std::make_shared<StencilImpl<SequenceData>>(std::move(Parts)); } |