diff options
| author | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-09-26 00:53:56 +0000 |
|---|---|---|
| committer | Yitzhak Mandelbaum <yitzhakm@google.com> | 2019-09-26 00:53:56 +0000 |
| commit | ea966c1bc05bca8353db0c28751b4f804a62bd42 (patch) | |
| tree | 1813ad52745b87f6aea56c5bc1f30d81ab383280 /clang | |
| parent | 3c8c6672358aa13788a1026ac89336d8dee2821d (diff) | |
| download | bcm5719-llvm-ea966c1bc05bca8353db0c28751b4f804a62bd42.tar.gz bcm5719-llvm-ea966c1bc05bca8353db0c28751b4f804a62bd42.zip | |
[libTooling] Add `run` combinator to Stencils.
Summary:
This revision adds `run`, a StencilPart that runs a user-defined function that
computes a result over `MatchFinder::MatchResult`.
Reviewers: gribozavr
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67969
llvm-svn: 372936
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/include/clang/Tooling/Refactoring/Stencil.h | 5 | ||||
| -rw-r--r-- | clang/lib/Tooling/Refactoring/Stencil.cpp | 22 | ||||
| -rw-r--r-- | clang/unittests/Tooling/StencilTest.cpp | 18 |
3 files changed, 45 insertions, 0 deletions
diff --git a/clang/include/clang/Tooling/Refactoring/Stencil.h b/clang/include/clang/Tooling/Refactoring/Stencil.h index 43b0429d4ea..3bd66e578b8 100644 --- a/clang/include/clang/Tooling/Refactoring/Stencil.h +++ b/clang/include/clang/Tooling/Refactoring/Stencil.h @@ -23,6 +23,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/ASTTypeTraits.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Tooling/Refactoring/MatchConsumer.h" #include "clang/Tooling/Refactoring/RangeSelector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" @@ -175,6 +176,10 @@ inline StencilPart ifBound(llvm::StringRef Id, llvm::StringRef TrueText, return ifBound(Id, text(TrueText), text(FalseText)); } +/// Wraps a MatchConsumer in a StencilPart, so that it can be used in a Stencil. +/// This supports user-defined extensions to the Stencil language. +StencilPart run(MatchConsumer<std::string> C); + /// For debug use only; semantics are not guaranteed. /// /// \returns the string resulting from calling the node's print() method. diff --git a/clang/lib/Tooling/Refactoring/Stencil.cpp b/clang/lib/Tooling/Refactoring/Stencil.cpp index 0e5eaccc7b7..78239b96d3c 100644 --- a/clang/lib/Tooling/Refactoring/Stencil.cpp +++ b/clang/lib/Tooling/Refactoring/Stencil.cpp @@ -26,6 +26,7 @@ using namespace tooling; using ast_matchers::MatchFinder; using llvm::errc; using llvm::Error; +using llvm::Expected; using llvm::StringError; // A down_cast function to safely down cast a StencilPartInterface to a subclass @@ -102,6 +103,12 @@ bool isEqualData(const IfBoundData &A, const IfBoundData &B) { return A.Id == B.Id && A.TruePart == B.TruePart && A.FalsePart == B.FalsePart; } +// Equality is not defined over MatchConsumers, which are opaque. +bool isEqualData(const MatchConsumer<std::string> &A, + const MatchConsumer<std::string> &B) { + return false; +} + // The `evalData()` overloads evaluate the given stencil data to a string, given // the match result, and append it to `Result`. We define an overload for each // type of stencil data. @@ -159,6 +166,15 @@ Error evalData(const IfBoundData &Data, const MatchFinder::MatchResult &Match, .eval(Match, Result); } +Error evalData(const MatchConsumer<std::string> &Fn, + const MatchFinder::MatchResult &Match, std::string *Result) { + Expected<std::string> Value = Fn(Match); + if (!Value) + return Value.takeError(); + *Result += *Value; + return Error::success(); +} + template <typename T> class StencilPartImpl : public StencilPartInterface { T Data; @@ -233,3 +249,9 @@ StencilPart stencil::ifBound(StringRef Id, StencilPart TruePart, return StencilPart(std::make_shared<StencilPartImpl<IfBoundData>>( Id, std::move(TruePart), std::move(FalsePart))); } + +StencilPart stencil::run(MatchConsumer<std::string> Fn) { + return StencilPart( + std::make_shared<StencilPartImpl<MatchConsumer<std::string>>>( + std::move(Fn))); +} diff --git a/clang/unittests/Tooling/StencilTest.cpp b/clang/unittests/Tooling/StencilTest.cpp index cc0e8facca8..2202693f023 100644 --- a/clang/unittests/Tooling/StencilTest.cpp +++ b/clang/unittests/Tooling/StencilTest.cpp @@ -29,6 +29,7 @@ using stencil::access; using stencil::cat; using stencil::dPrint; using stencil::ifBound; +using stencil::run; using stencil::text; // Create a valid translation-unit from a statement. @@ -283,6 +284,15 @@ TEST_F(StencilTest, AccessOpImplicitThis) { EXPECT_THAT_EXPECTED(Stencil.eval(StmtMatch->Result), HasValue("field")); } +TEST_F(StencilTest, RunOp) { + StringRef Id = "id"; + auto SimpleFn = [Id](const MatchResult &R) { + return std::string(R.Nodes.getNodeAs<Stmt>(Id) != nullptr ? "Bound" + : "Unbound"); + }; + testExpr(Id, "3;", cat(run(SimpleFn)), "Bound"); +} + TEST(StencilEqualityTest, Equality) { auto Lhs = cat("foo", dPrint("dprint_id")); auto Rhs = cat("foo", dPrint("dprint_id")); @@ -307,4 +317,12 @@ TEST(StencilEqualityTest, InEqualitySelection) { auto S2 = cat(node("node")); EXPECT_NE(S1, S2); } + +// `run` is opaque. +TEST(StencilEqualityTest, InEqualityRun) { + auto F = [](const MatchResult &R) { return "foo"; }; + auto S1 = cat(run(F)); + auto S2 = cat(run(F)); + EXPECT_NE(S1, S2); +} } // namespace |

