2019-04-19 01:23:01 +08:00
|
|
|
//===- unittest/Tooling/StencilTest.cpp -----------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-10 10:34:47 +08:00
|
|
|
#include "clang/Tooling/Transformer/Stencil.h"
|
2020-06-19 04:53:03 +08:00
|
|
|
#include "clang/AST/ASTTypeTraits.h"
|
2019-04-19 01:23:01 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Tooling/FixIt.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
2019-09-23 21:21:42 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Testing/Support/Error.h"
|
2019-04-19 01:23:01 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2019-10-16 09:06:46 +08:00
|
|
|
using namespace transformer;
|
2019-04-19 01:23:01 +08:00
|
|
|
using namespace ast_matchers;
|
|
|
|
|
|
|
|
namespace {
|
2019-10-03 21:01:00 +08:00
|
|
|
using ::llvm::Failed;
|
2019-09-23 21:21:42 +08:00
|
|
|
using ::llvm::HasValue;
|
2019-10-03 21:01:00 +08:00
|
|
|
using ::llvm::StringError;
|
2019-04-19 01:23:01 +08:00
|
|
|
using ::testing::AllOf;
|
|
|
|
using ::testing::HasSubstr;
|
|
|
|
using MatchResult = MatchFinder::MatchResult;
|
|
|
|
|
|
|
|
// Create a valid translation-unit from a statement.
|
2019-09-23 21:21:42 +08:00
|
|
|
static std::string wrapSnippet(StringRef StatementCode) {
|
|
|
|
return ("struct S { int field; }; auto stencil_test_snippet = []{" +
|
|
|
|
StatementCode + "};")
|
|
|
|
.str();
|
2019-04-19 01:23:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DeclarationMatcher wrapMatcher(const StatementMatcher &Matcher) {
|
|
|
|
return varDecl(hasName("stencil_test_snippet"),
|
|
|
|
hasDescendant(compoundStmt(hasAnySubstatement(Matcher))));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestMatch {
|
|
|
|
// The AST unit from which `result` is built. We bundle it because it backs
|
|
|
|
// the result. Users are not expected to access it.
|
|
|
|
std::unique_ptr<ASTUnit> AstUnit;
|
|
|
|
// The result to use in the test. References `ast_unit`.
|
|
|
|
MatchResult Result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Matches `Matcher` against the statement `StatementCode` and returns the
|
|
|
|
// result. Handles putting the statement inside a function and modifying the
|
2019-09-23 21:21:42 +08:00
|
|
|
// matcher correspondingly. `Matcher` should match one of the statements in
|
|
|
|
// `StatementCode` exactly -- that is, produce exactly one match. However,
|
|
|
|
// `StatementCode` may contain other statements not described by `Matcher`.
|
|
|
|
static llvm::Optional<TestMatch> matchStmt(StringRef StatementCode,
|
2019-04-19 01:23:01 +08:00
|
|
|
StatementMatcher Matcher) {
|
2019-10-16 09:06:46 +08:00
|
|
|
auto AstUnit = tooling::buildASTFromCode(wrapSnippet(StatementCode));
|
2019-04-19 01:23:01 +08:00
|
|
|
if (AstUnit == nullptr) {
|
|
|
|
ADD_FAILURE() << "AST construction failed";
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
ASTContext &Context = AstUnit->getASTContext();
|
2019-11-12 21:29:40 +08:00
|
|
|
auto Matches = ast_matchers::match(
|
|
|
|
traverse(ast_type_traits::TK_AsIs, wrapMatcher(Matcher)), Context);
|
2019-04-19 01:23:01 +08:00
|
|
|
// We expect a single, exact match for the statement.
|
|
|
|
if (Matches.size() != 1) {
|
|
|
|
ADD_FAILURE() << "Wrong number of matches: " << Matches.size();
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
return TestMatch{std::move(AstUnit), MatchResult(Matches[0], &Context)};
|
|
|
|
}
|
|
|
|
|
|
|
|
class StencilTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
// Verifies that the given stencil fails when evaluated on a valid match
|
|
|
|
// result. Binds a statement to "stmt", a (non-member) ctor-initializer to
|
|
|
|
// "init", an expression to "expr" and a (nameless) declaration to "decl".
|
|
|
|
void testError(const Stencil &Stencil,
|
|
|
|
::testing::Matcher<std::string> Matcher) {
|
|
|
|
const std::string Snippet = R"cc(
|
|
|
|
struct A {};
|
|
|
|
class F : public A {
|
|
|
|
public:
|
|
|
|
F(int) {}
|
|
|
|
};
|
|
|
|
F(1);
|
|
|
|
)cc";
|
|
|
|
auto StmtMatch = matchStmt(
|
|
|
|
Snippet,
|
|
|
|
stmt(hasDescendant(
|
|
|
|
cxxConstructExpr(
|
|
|
|
hasDeclaration(decl(hasDescendant(cxxCtorInitializer(
|
|
|
|
isBaseInitializer())
|
|
|
|
.bind("init")))
|
|
|
|
.bind("decl")))
|
|
|
|
.bind("expr")))
|
|
|
|
.bind("stmt"));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
2019-10-30 08:26:34 +08:00
|
|
|
if (auto ResultOrErr = Stencil->eval(StmtMatch->Result)) {
|
2019-04-19 01:23:01 +08:00
|
|
|
ADD_FAILURE() << "Expected failure but succeeded: " << *ResultOrErr;
|
|
|
|
} else {
|
|
|
|
auto Err = llvm::handleErrors(ResultOrErr.takeError(),
|
2019-10-03 21:01:00 +08:00
|
|
|
[&Matcher](const StringError &Err) {
|
2019-04-19 01:23:01 +08:00
|
|
|
EXPECT_THAT(Err.getMessage(), Matcher);
|
|
|
|
});
|
|
|
|
if (Err) {
|
|
|
|
ADD_FAILURE() << "Unhandled error: " << llvm::toString(std::move(Err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests failures caused by references to unbound nodes. `unbound_id` is the
|
|
|
|
// id that will cause the failure.
|
2019-09-23 21:21:42 +08:00
|
|
|
void testUnboundNodeError(const Stencil &Stencil, StringRef UnboundId) {
|
2020-01-29 03:23:46 +08:00
|
|
|
testError(Stencil,
|
|
|
|
AllOf(HasSubstr(std::string(UnboundId)), HasSubstr("not bound")));
|
2019-04-19 01:23:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(StencilTest, SingleStatement) {
|
|
|
|
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.
|
2020-01-29 03:23:46 +08:00
|
|
|
auto Stencil =
|
|
|
|
cat("if (!", node(std::string(Condition)), ") ",
|
|
|
|
statement(std::string(Else)), " else ", statement(std::string(Then)));
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result),
|
2019-09-26 04:04:25 +08:00
|
|
|
HasValue("if (!true) return 0; else return 1;"));
|
2019-04-19 01:23:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, UnboundNode) {
|
|
|
|
const std::string Snippet = R"cc(
|
|
|
|
if (true)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
)cc";
|
|
|
|
auto StmtMatch = matchStmt(Snippet, ifStmt(hasCondition(stmt().bind("a1")),
|
|
|
|
hasThen(stmt().bind("a2"))));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
2019-05-23 02:03:00 +08:00
|
|
|
auto Stencil = cat("if(!", node("a1"), ") ", node("UNBOUND"), ";");
|
2019-10-30 08:26:34 +08:00
|
|
|
auto ResultOrErr = Stencil->eval(StmtMatch->Result);
|
2019-04-19 01:23:01 +08:00
|
|
|
EXPECT_TRUE(llvm::errorToBool(ResultOrErr.takeError()))
|
|
|
|
<< "Expected unbound node, got " << *ResultOrErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a stencil with a single parameter (`Id`) evaluates to the expected
|
|
|
|
// string, when `Id` is bound to the expression-statement in `Snippet`.
|
|
|
|
void testExpr(StringRef Id, StringRef Snippet, const Stencil &Stencil,
|
|
|
|
StringRef Expected) {
|
|
|
|
auto StmtMatch = matchStmt(Snippet, expr().bind(Id));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
2020-01-29 03:23:46 +08:00
|
|
|
EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result),
|
|
|
|
HasValue(std::string(Expected)));
|
2019-04-19 01:23:01 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:01:00 +08:00
|
|
|
void testFailure(StringRef Id, StringRef Snippet, const Stencil &Stencil,
|
|
|
|
testing::Matcher<std::string> MessageMatcher) {
|
|
|
|
auto StmtMatch = matchStmt(Snippet, expr().bind(Id));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result),
|
2019-10-03 21:01:00 +08:00
|
|
|
Failed<StringError>(testing::Property(
|
|
|
|
&StringError::getMessage, MessageMatcher)));
|
|
|
|
}
|
|
|
|
|
2019-05-23 02:03:00 +08:00
|
|
|
TEST_F(StencilTest, SelectionOp) {
|
2019-04-19 01:23:01 +08:00
|
|
|
StringRef Id = "id";
|
2020-01-29 03:23:46 +08:00
|
|
|
testExpr(Id, "3;", cat(node(std::string(Id))), "3");
|
2019-04-19 01:23:01 +08:00
|
|
|
}
|
|
|
|
|
2019-09-23 21:21:42 +08:00
|
|
|
TEST_F(StencilTest, IfBoundOpBound) {
|
|
|
|
StringRef Id = "id";
|
2020-06-20 00:47:12 +08:00
|
|
|
testExpr(Id, "3;", ifBound(Id, cat("5"), cat("7")), "5");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, IfBoundOpUnbound) {
|
|
|
|
StringRef Id = "id";
|
2020-06-20 00:47:12 +08:00
|
|
|
testExpr(Id, "3;", ifBound("other", cat("5"), cat("7")), "7");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:01:00 +08:00
|
|
|
TEST_F(StencilTest, ExpressionOpNoParens) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "3;", expression(Id), "3");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't parenthesize a parens expression.
|
|
|
|
TEST_F(StencilTest, ExpressionOpNoParensParens) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "(3);", expression(Id), "(3)");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, ExpressionOpBinaryOpParens) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "3+4;", expression(Id), "(3+4)");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// `expression` shares code with other ops, so we get sufficient coverage of the
|
|
|
|
// error handling code with this test. If that changes in the future, more error
|
|
|
|
// tests should be added.
|
|
|
|
TEST_F(StencilTest, ExpressionOpUnbound) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testFailure(Id, "3;", expression("ACACA"),
|
2019-10-03 21:01:00 +08:00
|
|
|
AllOf(HasSubstr("ACACA"), HasSubstr("not bound")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, DerefPointer) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "int *x; x;", deref(Id), "*x");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, DerefBinOp) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "int *x; x + 1;", deref(Id), "*(x + 1)");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, DerefAddressExpr) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "int x; &x;", deref(Id), "x");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AddressOfValue) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "int x; x;", addressOf(Id), "&x");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AddressOfDerefExpr) {
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "int *x; *x;", addressOf(Id), "x");
|
2019-10-03 21:01:00 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:35:22 +08:00
|
|
|
TEST_F(StencilTest, MaybeDerefValue) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int x; x;", maybeDeref(Id), "x");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeDerefPointer) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int *x; x;", maybeDeref(Id), "*x");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeDerefBinOp) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int *x; x + 1;", maybeDeref(Id), "*(x + 1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeDerefAddressExpr) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int x; &x;", maybeDeref(Id), "x");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeAddressOfPointer) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int *x; x;", maybeAddressOf(Id), "x");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeAddressOfValue) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int x; x;", addressOf(Id), "&x");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeAddressOfBinOp) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int x; x + 1;", maybeAddressOf(Id), "&(x + 1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, MaybeAddressOfDerefExpr) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
testExpr(Id, "int *x; *x;", addressOf(Id), "x");
|
|
|
|
}
|
|
|
|
|
2019-09-23 21:21:42 +08:00
|
|
|
TEST_F(StencilTest, AccessOpValue) {
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
S x;
|
|
|
|
x;
|
|
|
|
)cc";
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, Snippet, access(Id, "field"), "x.field");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AccessOpValueExplicitText) {
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
S x;
|
|
|
|
x;
|
|
|
|
)cc";
|
|
|
|
StringRef Id = "id";
|
2020-06-20 00:47:12 +08:00
|
|
|
testExpr(Id, Snippet, access(Id, cat("field")), "x.field");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AccessOpValueAddress) {
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
S x;
|
|
|
|
&x;
|
|
|
|
)cc";
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, Snippet, access(Id, "field"), "x.field");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AccessOpPointer) {
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
S *x;
|
|
|
|
x;
|
|
|
|
)cc";
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, Snippet, access(Id, "field"), "x->field");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AccessOpPointerDereference) {
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
S *x;
|
|
|
|
*x;
|
|
|
|
)cc";
|
|
|
|
StringRef Id = "id";
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, Snippet, access(Id, "field"), "x->field");
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AccessOpExplicitThis) {
|
|
|
|
using clang::ast_matchers::hasObjectExpression;
|
|
|
|
using clang::ast_matchers::memberExpr;
|
|
|
|
|
|
|
|
// Set up the code so we can bind to a use of this.
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
class C {
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
int foo() { return this->x; }
|
|
|
|
};
|
|
|
|
)cc";
|
2019-11-12 21:29:40 +08:00
|
|
|
auto StmtMatch = matchStmt(
|
|
|
|
Snippet, traverse(ast_type_traits::TK_AsIs,
|
|
|
|
returnStmt(hasReturnValue(ignoringImplicit(memberExpr(
|
|
|
|
hasObjectExpression(expr().bind("obj"))))))));
|
2019-09-23 21:21:42 +08:00
|
|
|
ASSERT_TRUE(StmtMatch);
|
2019-10-30 08:26:34 +08:00
|
|
|
const Stencil Stencil = access("obj", "field");
|
|
|
|
EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result),
|
2019-09-23 21:21:42 +08:00
|
|
|
HasValue("this->field"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, AccessOpImplicitThis) {
|
|
|
|
using clang::ast_matchers::hasObjectExpression;
|
|
|
|
using clang::ast_matchers::memberExpr;
|
|
|
|
|
|
|
|
// Set up the code so we can bind to a use of (implicit) this.
|
|
|
|
StringRef Snippet = R"cc(
|
|
|
|
class C {
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
int foo() { return x; }
|
|
|
|
};
|
|
|
|
)cc";
|
|
|
|
auto StmtMatch =
|
|
|
|
matchStmt(Snippet, returnStmt(hasReturnValue(ignoringImplicit(memberExpr(
|
|
|
|
hasObjectExpression(expr().bind("obj")))))));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
2019-10-30 08:26:34 +08:00
|
|
|
const Stencil Stencil = access("obj", "field");
|
|
|
|
EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), HasValue("field"));
|
2019-09-23 21:21:42 +08:00
|
|
|
}
|
|
|
|
|
2019-09-26 08:53:56 +08:00
|
|
|
TEST_F(StencilTest, RunOp) {
|
|
|
|
StringRef Id = "id";
|
|
|
|
auto SimpleFn = [Id](const MatchResult &R) {
|
|
|
|
return std::string(R.Nodes.getNodeAs<Stmt>(Id) != nullptr ? "Bound"
|
|
|
|
: "Unbound");
|
|
|
|
};
|
2019-10-30 08:26:34 +08:00
|
|
|
testExpr(Id, "3;", run(SimpleFn), "Bound");
|
2019-09-26 08:53:56 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 04:53:03 +08:00
|
|
|
TEST_F(StencilTest, CatOfMacroRangeSucceeds) {
|
|
|
|
StringRef Snippet = R"cpp(
|
|
|
|
#define MACRO 3.77
|
|
|
|
double foo(double d);
|
|
|
|
foo(MACRO);)cpp";
|
|
|
|
|
|
|
|
auto StmtMatch =
|
|
|
|
matchStmt(Snippet, callExpr(callee(functionDecl(hasName("foo"))),
|
|
|
|
argumentCountIs(1),
|
|
|
|
hasArgument(0, expr().bind("arg"))));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
|
|
|
Stencil S = cat(node("arg"));
|
|
|
|
EXPECT_THAT_EXPECTED(S->eval(StmtMatch->Result), HasValue("MACRO"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, CatOfMacroArgRangeSucceeds) {
|
|
|
|
StringRef Snippet = R"cpp(
|
|
|
|
#define MACRO(a, b) a + b
|
|
|
|
MACRO(2, 3);)cpp";
|
|
|
|
|
|
|
|
auto StmtMatch =
|
|
|
|
matchStmt(Snippet, binaryOperator(hasRHS(expr().bind("rhs"))));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
|
|
|
Stencil S = cat(node("rhs"));
|
|
|
|
EXPECT_THAT_EXPECTED(S->eval(StmtMatch->Result), HasValue("3"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StencilTest, CatOfMacroArgSubRangeSucceeds) {
|
|
|
|
StringRef Snippet = R"cpp(
|
|
|
|
#define MACRO(a, b) a + b
|
|
|
|
int foo(int);
|
|
|
|
MACRO(2, foo(3));)cpp";
|
|
|
|
|
|
|
|
auto StmtMatch = matchStmt(
|
|
|
|
Snippet, binaryOperator(hasRHS(callExpr(
|
|
|
|
callee(functionDecl(hasName("foo"))), argumentCountIs(1),
|
|
|
|
hasArgument(0, expr().bind("arg"))))));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
|
|
|
Stencil S = cat(node("arg"));
|
|
|
|
EXPECT_THAT_EXPECTED(S->eval(StmtMatch->Result), HasValue("3"));
|
|
|
|
}
|
|
|
|
|
2020-01-07 00:00:44 +08:00
|
|
|
TEST_F(StencilTest, CatOfInvalidRangeFails) {
|
|
|
|
StringRef Snippet = R"cpp(
|
|
|
|
#define MACRO (3.77)
|
|
|
|
double foo(double d);
|
|
|
|
foo(MACRO);)cpp";
|
|
|
|
|
|
|
|
auto StmtMatch =
|
|
|
|
matchStmt(Snippet, callExpr(callee(functionDecl(hasName("foo"))),
|
|
|
|
argumentCountIs(1),
|
|
|
|
hasArgument(0, expr().bind("arg"))));
|
|
|
|
ASSERT_TRUE(StmtMatch);
|
|
|
|
Stencil S = cat(node("arg"));
|
2020-06-27 00:17:28 +08:00
|
|
|
Expected<std::string> Result = S->eval(StmtMatch->Result);
|
|
|
|
ASSERT_THAT_EXPECTED(Result, Failed<StringError>());
|
|
|
|
llvm::handleAllErrors(Result.takeError(), [](const llvm::StringError &E) {
|
|
|
|
EXPECT_THAT(E.getMessage(), AllOf(HasSubstr("selected range"),
|
|
|
|
HasSubstr("macro expansion")));
|
|
|
|
});
|
2020-01-07 00:00:44 +08:00
|
|
|
}
|
|
|
|
|
2019-10-08 00:20:22 +08:00
|
|
|
TEST(StencilToStringTest, RawTextOp) {
|
|
|
|
auto S = cat("foo bar baz");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"("foo bar baz")";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, RawTextOpEscaping) {
|
|
|
|
auto S = cat("foo \"bar\" baz\\n");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"("foo \"bar\" baz\\n")";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, DebugPrintNodeOp) {
|
2019-10-30 08:26:34 +08:00
|
|
|
auto S = dPrint("Id");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"repr(dPrint("Id"))repr";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, ExpressionOp) {
|
2019-10-30 08:26:34 +08:00
|
|
|
auto S = expression("Id");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"repr(expression("Id"))repr";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, DerefOp) {
|
2019-10-30 08:26:34 +08:00
|
|
|
auto S = deref("Id");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"repr(deref("Id"))repr";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, AddressOfOp) {
|
2019-10-30 08:26:34 +08:00
|
|
|
auto S = addressOf("Id");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"repr(addressOf("Id"))repr";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 22:02:03 +08:00
|
|
|
TEST(StencilToStringTest, SelectionOp) {
|
|
|
|
auto S1 = cat(node("node1"));
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S1->toString(), "selection(...)");
|
2019-10-11 22:02:03 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 08:26:34 +08:00
|
|
|
TEST(StencilToStringTest, AccessOpText) {
|
|
|
|
auto S = access("Id", "memberData");
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected = R"repr(access("Id", "memberData"))repr";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 08:26:34 +08:00
|
|
|
TEST(StencilToStringTest, AccessOpSelector) {
|
2020-06-20 00:47:12 +08:00
|
|
|
auto S = access("Id", cat(name("otherId")));
|
2019-10-30 08:26:34 +08:00
|
|
|
StringRef Expected = R"repr(access("Id", selection(...)))repr";
|
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, AccessOpStencil) {
|
|
|
|
auto S = access("Id", cat("foo_", "bar"));
|
|
|
|
StringRef Expected = R"repr(access("Id", seq("foo_", "bar")))repr";
|
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, IfBoundOp) {
|
2020-06-20 00:47:12 +08:00
|
|
|
auto S = ifBound("Id", cat("trueText"), access("exprId", "memberData"));
|
2019-10-08 01:24:23 +08:00
|
|
|
StringRef Expected =
|
|
|
|
R"repr(ifBound("Id", "trueText", access("exprId", "memberData")))repr";
|
2019-10-30 08:26:34 +08:00
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 22:02:03 +08:00
|
|
|
TEST(StencilToStringTest, RunOp) {
|
|
|
|
auto F1 = [](const MatchResult &R) { return "foo"; };
|
2019-10-30 08:26:34 +08:00
|
|
|
auto S1 = run(F1);
|
|
|
|
EXPECT_EQ(S1->toString(), "run(...)");
|
2019-10-11 22:02:03 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 08:26:34 +08:00
|
|
|
TEST(StencilToStringTest, Sequence) {
|
2019-10-08 00:20:22 +08:00
|
|
|
auto S = cat("foo", access("x", "m()"), "bar",
|
2020-06-20 00:47:12 +08:00
|
|
|
ifBound("x", cat("t"), access("e", "f")));
|
2019-10-30 08:26:34 +08:00
|
|
|
StringRef Expected = R"repr(seq("foo", access("x", "m()"), "bar", )repr"
|
|
|
|
R"repr(ifBound("x", "t", access("e", "f"))))repr";
|
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, SequenceEmpty) {
|
|
|
|
auto S = cat();
|
|
|
|
StringRef Expected = "seq()";
|
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, SequenceSingle) {
|
|
|
|
auto S = cat("foo");
|
|
|
|
StringRef Expected = "\"foo\"";
|
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StencilToStringTest, SequenceFromVector) {
|
2020-06-20 00:47:12 +08:00
|
|
|
auto S = catVector({cat("foo"), access("x", "m()"), cat("bar"),
|
|
|
|
ifBound("x", cat("t"), access("e", "f"))});
|
2019-10-30 08:26:34 +08:00
|
|
|
StringRef Expected = R"repr(seq("foo", access("x", "m()"), "bar", )repr"
|
|
|
|
R"repr(ifBound("x", "t", access("e", "f"))))repr";
|
|
|
|
EXPECT_EQ(S->toString(), Expected);
|
2019-10-08 00:20:22 +08:00
|
|
|
}
|
2019-04-19 01:23:01 +08:00
|
|
|
} // namespace
|