aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests
diff options
context:
space:
mode:
authorYitzhak Mandelbaum <yitzhakm@google.com>2019-11-04 08:30:18 -0500
committerYitzhak Mandelbaum <yitzhakm@google.com>2019-11-11 12:44:15 -0500
commit489449c28aaa45086d507fbad96826420adf409d (patch)
treeaee53aafb71c762cbbb82c30688dd4ea7be00655 /clang/unittests
parent14df08f0580cea8f8bec5814e3b895b373001b56 (diff)
downloadllvm-489449c28aaa45086d507fbad96826420adf409d.zip
llvm-489449c28aaa45086d507fbad96826420adf409d.tar.gz
llvm-489449c28aaa45086d507fbad96826420adf409d.tar.bz2
[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/unittests')
-rw-r--r--clang/unittests/Tooling/StencilTest.cpp21
-rw-r--r--clang/unittests/Tooling/TransformerTest.cpp15
2 files changed, 9 insertions, 27 deletions
diff --git a/clang/unittests/Tooling/StencilTest.cpp b/clang/unittests/Tooling/StencilTest.cpp
index 5bbdfea3..7f530fe 100644
--- a/clang/unittests/Tooling/StencilTest.cpp
+++ b/clang/unittests/Tooling/StencilTest.cpp
@@ -24,7 +24,6 @@ using ::llvm::Failed;
using ::llvm::HasValue;
using ::llvm::StringError;
using ::testing::AllOf;
-using ::testing::Eq;
using ::testing::HasSubstr;
using MatchResult = MatchFinder::MatchResult;
@@ -135,26 +134,6 @@ TEST_F(StencilTest, SingleStatement) {
HasValue("if (!true) return 0; else return 1;"));
}
-// Tests `stencil`.
-TEST_F(StencilTest, StencilFactoryFunction) {
- StringRef Condition("C"), Then("T"), Else("E");
- const std::string Snippet = R"cc(
- if (true)
- return 1;
- else
- return 0;
- )cc";
- auto StmtMatch = matchStmt(
- Snippet, ifStmt(hasCondition(expr().bind(Condition)),
- hasThen(stmt().bind(Then)), hasElse(stmt().bind(Else))));
- ASSERT_TRUE(StmtMatch);
- // Invert the if-then-else.
- auto Consumer = cat("if (!", node(Condition), ") ", statement(Else), " else ",
- statement(Then));
- EXPECT_THAT_EXPECTED(Consumer(StmtMatch->Result),
- HasValue("if (!true) return 0; else return 1;"));
-}
-
TEST_F(StencilTest, UnboundNode) {
const std::string Snippet = R"cc(
if (true)
diff --git a/clang/unittests/Tooling/TransformerTest.cpp b/clang/unittests/Tooling/TransformerTest.cpp
index c99bf97..c382783 100644
--- a/clang/unittests/Tooling/TransformerTest.cpp
+++ b/clang/unittests/Tooling/TransformerTest.cpp
@@ -575,13 +575,16 @@ TEST_F(TransformerTest, TextGeneratorFailure) {
std::string Input = "int conflictOneRule() { return 3 + 7; }";
// Try to change the whole binary-operator expression AND one its operands:
StringRef O = "O";
- auto AlwaysFail = [](const ast_matchers::MatchFinder::MatchResult &)
- -> llvm::Expected<std::string> {
- return llvm::createStringError(llvm::errc::invalid_argument, "ERROR");
+ class AlwaysFail : public transformer::MatchComputation<std::string> {
+ llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &,
+ std::string *) const override {
+ return llvm::createStringError(llvm::errc::invalid_argument, "ERROR");
+ }
+ std::string toString() const override { return "AlwaysFail"; }
};
- Transformer T(
- makeRule(binaryOperator().bind(O), changeTo(node(O), AlwaysFail)),
- consumer());
+ Transformer T(makeRule(binaryOperator().bind(O),
+ changeTo(node(O), std::make_shared<AlwaysFail>())),
+ consumer());
T.registerMatchers(&MatchFinder);
EXPECT_FALSE(rewrite(Input));
EXPECT_THAT(Changes, IsEmpty());