2019-05-20 21:15:14 +08:00
|
|
|
//===- unittest/Tooling/RangeSelectorTest.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/RangeSelector.h"
|
2019-05-20 21:15:14 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
2019-05-20 22:44:40 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2019-05-20 21:15:14 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
2019-10-16 09:06:46 +08:00
|
|
|
#include "clang/Tooling/Transformer/SourceCode.h"
|
2019-05-20 21:15:14 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Testing/Support/Error.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2019-10-16 09:06:46 +08:00
|
|
|
using namespace transformer;
|
2019-05-20 21:15:14 +08:00
|
|
|
using namespace ast_matchers;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
using ::llvm::Expected;
|
|
|
|
using ::llvm::Failed;
|
|
|
|
using ::llvm::HasValue;
|
|
|
|
using ::llvm::StringError;
|
2019-05-29 20:40:36 +08:00
|
|
|
using ::testing::AllOf;
|
|
|
|
using ::testing::HasSubstr;
|
|
|
|
using ::testing::Property;
|
|
|
|
|
|
|
|
using MatchResult = MatchFinder::MatchResult;
|
2019-05-20 21:15:14 +08:00
|
|
|
|
|
|
|
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.
|
2019-05-20 22:44:40 +08:00
|
|
|
std::unique_ptr<clang::ASTUnit> ASTUnit;
|
2019-05-20 21:15:14 +08:00
|
|
|
// The result to use in the test. References `ast_unit`.
|
|
|
|
MatchResult Result;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename M> TestMatch matchCode(StringRef Code, M Matcher) {
|
2019-10-16 09:06:46 +08:00
|
|
|
auto ASTUnit = tooling::buildASTFromCode(Code);
|
2019-05-20 21:15:14 +08:00
|
|
|
assert(ASTUnit != nullptr && "AST construction failed");
|
|
|
|
|
|
|
|
ASTContext &Context = ASTUnit->getASTContext();
|
|
|
|
assert(!Context.getDiagnostics().hasErrorOccurred() && "Compilation error");
|
|
|
|
|
|
|
|
auto Matches = ast_matchers::match(Matcher, Context);
|
|
|
|
// We expect a single, exact match.
|
|
|
|
assert(Matches.size() != 0 && "no matches found");
|
|
|
|
assert(Matches.size() == 1 && "too many matches");
|
|
|
|
|
|
|
|
return TestMatch{std::move(ASTUnit), MatchResult(Matches[0], &Context)};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applies \p Selector to \p Match and, on success, returns the selected source.
|
2019-05-21 00:30:49 +08:00
|
|
|
Expected<StringRef> select(RangeSelector Selector, const TestMatch &Match) {
|
2019-05-20 21:15:14 +08:00
|
|
|
Expected<CharSourceRange> Range = Selector(Match.Result);
|
|
|
|
if (!Range)
|
|
|
|
return Range.takeError();
|
2019-10-16 09:06:46 +08:00
|
|
|
return tooling::getText(*Range, *Match.Result.Context);
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Applies \p Selector to a trivial match with only a single bound node with id
|
|
|
|
// "bound_node_id". For use in testing unbound-node errors.
|
2019-05-21 00:30:49 +08:00
|
|
|
Expected<CharSourceRange> selectFromTrivial(const RangeSelector &Selector) {
|
2019-05-20 21:15:14 +08:00
|
|
|
// We need to bind the result to something, or the match will fail. Use a
|
|
|
|
// binding that is not used in the unbound node tests.
|
|
|
|
TestMatch Match =
|
|
|
|
matchCode("static int x = 0;", varDecl().bind("bound_node_id"));
|
|
|
|
return Selector(Match.Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches the message expected for unbound-node failures.
|
|
|
|
testing::Matcher<StringError> withUnboundNodeMessage() {
|
|
|
|
return testing::Property(
|
|
|
|
&StringError::getMessage,
|
|
|
|
AllOf(HasSubstr("unbound_id"), HasSubstr("not bound")));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applies \p Selector to code containing assorted node types, where the match
|
|
|
|
// binds each one: a statement ("stmt"), a (non-member) ctor-initializer
|
|
|
|
// ("init"), an expression ("expr") and a (nameless) declaration ("decl"). Used
|
|
|
|
// to test failures caused by applying selectors to nodes of the wrong type.
|
2019-05-21 00:30:49 +08:00
|
|
|
Expected<CharSourceRange> selectFromAssorted(RangeSelector Selector) {
|
2019-05-20 21:15:14 +08:00
|
|
|
StringRef Code = R"cc(
|
|
|
|
struct A {};
|
|
|
|
class F : public A {
|
|
|
|
public:
|
|
|
|
F(int) {}
|
|
|
|
};
|
|
|
|
void g() { F f(1); }
|
|
|
|
)cc";
|
|
|
|
|
|
|
|
auto Matcher =
|
|
|
|
compoundStmt(
|
|
|
|
hasDescendant(
|
|
|
|
cxxConstructExpr(
|
|
|
|
hasDeclaration(
|
|
|
|
decl(hasDescendant(cxxCtorInitializer(isBaseInitializer())
|
|
|
|
.bind("init")))
|
|
|
|
.bind("decl")))
|
|
|
|
.bind("expr")))
|
|
|
|
.bind("stmt");
|
|
|
|
|
|
|
|
return Selector(matchCode(Code, Matcher).Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches the message expected for type-error failures.
|
2020-01-29 03:23:46 +08:00
|
|
|
testing::Matcher<StringError> withTypeErrorMessage(const std::string &NodeID) {
|
2019-05-20 21:15:14 +08:00
|
|
|
return testing::Property(
|
|
|
|
&StringError::getMessage,
|
|
|
|
AllOf(HasSubstr(NodeID), HasSubstr("mismatched type")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, UnboundNode) {
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromTrivial(node("unbound_id")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withUnboundNodeMessage()));
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:40:36 +08:00
|
|
|
MATCHER_P(EqualsCharSourceRange, Range, "") {
|
|
|
|
return Range.getAsRange() == arg.getAsRange() &&
|
|
|
|
Range.isTokenRange() == arg.isTokenRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: here and elsewhere: use llvm::Annotations library to explicitly mark
|
|
|
|
// points and ranges of interest, enabling more readable tests.
|
|
|
|
TEST(RangeSelectorTest, BeforeOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f(int x, int y, int z) { return 3; }
|
|
|
|
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Call = "call";
|
2019-05-29 20:40:36 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(Call));
|
|
|
|
const auto* E = Match.Result.Nodes.getNodeAs<Expr>(Call);
|
|
|
|
assert(E != nullptr);
|
|
|
|
auto ExprBegin = E->getSourceRange().getBegin();
|
|
|
|
EXPECT_THAT_EXPECTED(
|
|
|
|
before(node(Call))(Match.Result),
|
|
|
|
HasValue(EqualsCharSourceRange(
|
|
|
|
CharSourceRange::getCharRange(ExprBegin, ExprBegin))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, AfterOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f(int x, int y, int z) { return 3; }
|
|
|
|
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
|
|
|
|
)cc";
|
|
|
|
StringRef Call = "call";
|
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(Call));
|
|
|
|
const auto* E = Match.Result.Nodes.getNodeAs<Expr>(Call);
|
|
|
|
assert(E != nullptr);
|
|
|
|
const SourceRange Range = E->getSourceRange();
|
|
|
|
// The end token, a right paren, is one character wide, so advance by one,
|
|
|
|
// bringing us to the semicolon.
|
|
|
|
const SourceLocation SemiLoc = Range.getEnd().getLocWithOffset(1);
|
|
|
|
const auto ExpectedAfter = CharSourceRange::getCharRange(SemiLoc, SemiLoc);
|
|
|
|
|
|
|
|
// Test with a char range.
|
|
|
|
auto CharRange = CharSourceRange::getCharRange(Range.getBegin(), SemiLoc);
|
|
|
|
EXPECT_THAT_EXPECTED(after(charRange(CharRange))(Match.Result),
|
|
|
|
HasValue(EqualsCharSourceRange(ExpectedAfter)));
|
|
|
|
|
|
|
|
// Test with a token range.
|
|
|
|
auto TokenRange = CharSourceRange::getTokenRange(Range);
|
|
|
|
EXPECT_THAT_EXPECTED(after(charRange(TokenRange))(Match.Result),
|
|
|
|
HasValue(EqualsCharSourceRange(ExpectedAfter)));
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:15:14 +08:00
|
|
|
TEST(RangeSelectorTest, RangeOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f(int x, int y, int z) { return 3; }
|
|
|
|
int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Arg0 = "a0";
|
|
|
|
const char *Arg1 = "a1";
|
2019-05-20 21:15:14 +08:00
|
|
|
StringRef Call = "call";
|
|
|
|
auto Matcher = callExpr(hasArgument(0, expr().bind(Arg0)),
|
|
|
|
hasArgument(1, expr().bind(Arg1)))
|
|
|
|
.bind(Call);
|
|
|
|
TestMatch Match = matchCode(Code, Matcher);
|
|
|
|
|
|
|
|
// Node-id specific version:
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(range(Arg0, Arg1), Match), HasValue("3, 7"));
|
2019-05-20 21:15:14 +08:00
|
|
|
// General version:
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(range(node(Arg0), node(Arg1)), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue("3, 7"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NodeOpStatement) {
|
|
|
|
StringRef Code = "int f() { return 3; }";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, returnStmt().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(node(ID), Match), HasValue("return 3;"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NodeOpExpression) {
|
|
|
|
StringRef Code = "int f() { return 3; }";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, expr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(node(ID), Match), HasValue("3"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, StatementOp) {
|
|
|
|
StringRef Code = "int f() { return 3; }";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, expr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(statement(ID), Match), HasValue("3;"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, MemberOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
struct S {
|
|
|
|
int member;
|
|
|
|
};
|
|
|
|
int g() {
|
|
|
|
S s;
|
|
|
|
return s.member;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, memberExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("member"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that member does not select any qualifiers on the member name.
|
|
|
|
TEST(RangeSelectorTest, MemberOpQualified) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
struct S {
|
|
|
|
int member;
|
|
|
|
};
|
|
|
|
struct T : public S {
|
|
|
|
int field;
|
|
|
|
};
|
|
|
|
int g() {
|
|
|
|
T t;
|
|
|
|
return t.S::member;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, memberExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("member"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, MemberOpTemplate) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
struct S {
|
|
|
|
template <typename T> T foo(T t);
|
|
|
|
};
|
|
|
|
int f(int x) {
|
|
|
|
S s;
|
|
|
|
return s.template foo<int>(3);
|
|
|
|
}
|
|
|
|
)cc";
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, memberExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("foo"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, MemberOpOperator) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
struct S {
|
|
|
|
int operator*();
|
|
|
|
};
|
|
|
|
int f(int x) {
|
|
|
|
S s;
|
|
|
|
return s.operator *();
|
|
|
|
}
|
|
|
|
)cc";
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, memberExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("operator *"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NameOpNamedDecl) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int myfun() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, functionDecl().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(name(ID), Match), HasValue("myfun"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NameOpDeclRef) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int foo(int x) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
int g(int x) { return foo(x) * x; }
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Ref = "ref";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, declRefExpr(to(functionDecl())).bind(Ref));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(name(Ref), Match), HasValue("foo"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NameOpCtorInitializer) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
class C {
|
|
|
|
public:
|
|
|
|
C() : field(3) {}
|
|
|
|
int field;
|
|
|
|
};
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Init = "init";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, cxxCtorInitializer().bind(Init));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(name(Init), Match), HasValue("field"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NameOpErrors) {
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromTrivial(name("unbound_id")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withUnboundNodeMessage()));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromAssorted(name("stmt")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withTypeErrorMessage("stmt")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, NameOpDeclRefError) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
struct S {
|
|
|
|
int operator*();
|
|
|
|
};
|
|
|
|
int f(int x) {
|
|
|
|
S s;
|
|
|
|
return *s + x;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Ref = "ref";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, declRefExpr(to(functionDecl())).bind(Ref));
|
|
|
|
EXPECT_THAT_EXPECTED(
|
|
|
|
name(Ref)(Match.Result),
|
|
|
|
Failed<StringError>(testing::Property(
|
|
|
|
&StringError::getMessage,
|
|
|
|
AllOf(HasSubstr(Ref), HasSubstr("requires property 'identifier'")))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsOp) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar(int, int);
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return x.bar(3, 4);
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue("3, 4"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsOpNoArgs) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar();
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return x.bar();
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue(""));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsOpNoArgsWithComments) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar();
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return x.bar(/*empty*/);
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue("/*empty*/"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that arguments are extracted correctly when a temporary (with parens)
|
|
|
|
// is used.
|
|
|
|
TEST(RangeSelectorTest, CallArgsOpWithParens) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar(int, int) { return 3; }
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return C().bar(3, 4);
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match =
|
|
|
|
matchCode(Code, callExpr(callee(functionDecl(hasName("bar")))).bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue("3, 4"));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsOpLeadingComments) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar(int, int) { return 3; }
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return x.bar(/*leading*/ 3, 4);
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue("/*leading*/ 3, 4"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsOpTrailingComments) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar(int, int) { return 3; }
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return x.bar(3 /*trailing*/, 4);
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue("3 /*trailing*/, 4"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsOpEolComments) {
|
|
|
|
const StringRef Code = R"cc(
|
|
|
|
struct C {
|
|
|
|
int bar(int, int) { return 3; }
|
|
|
|
};
|
|
|
|
int f() {
|
|
|
|
C x;
|
|
|
|
return x.bar( // Header
|
|
|
|
1, // foo
|
|
|
|
2 // bar
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, callExpr().bind(ID));
|
2019-05-21 08:27:26 +08:00
|
|
|
std::string ExpectedString = R"( // Header
|
2019-05-20 21:15:14 +08:00
|
|
|
1, // foo
|
|
|
|
2 // bar
|
2019-05-21 08:27:26 +08:00
|
|
|
)";
|
|
|
|
EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue(ExpectedString));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, CallArgsErrors) {
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromTrivial(callArgs("unbound_id")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withUnboundNodeMessage()));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromAssorted(callArgs("stmt")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withTypeErrorMessage("stmt")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, StatementsOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
void g();
|
|
|
|
void f() { /* comment */ g(); /* comment */ g(); /* comment */ }
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, compoundStmt().bind(ID));
|
|
|
|
EXPECT_THAT_EXPECTED(
|
2019-05-21 00:30:49 +08:00
|
|
|
select(statements(ID), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue(" /* comment */ g(); /* comment */ g(); /* comment */ "));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, StatementsOpEmptyList) {
|
|
|
|
StringRef Code = "void f() {}";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, compoundStmt().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(statements(ID), Match), HasValue(""));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, StatementsOpErrors) {
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromTrivial(statements("unbound_id")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withUnboundNodeMessage()));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromAssorted(statements("decl")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withTypeErrorMessage("decl")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, ElementsOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
void f() {
|
|
|
|
int v[] = {/* comment */ 3, /* comment*/ 4 /* comment */};
|
|
|
|
(void)v;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, initListExpr().bind(ID));
|
|
|
|
EXPECT_THAT_EXPECTED(
|
2019-05-21 00:30:49 +08:00
|
|
|
select(initListElements(ID), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue("/* comment */ 3, /* comment*/ 4 /* comment */"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, ElementsOpEmptyList) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
void f() {
|
|
|
|
int v[] = {};
|
|
|
|
(void)v;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, initListExpr().bind(ID));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(initListElements(ID), Match), HasValue(""));
|
2019-05-20 21:15:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, ElementsOpErrors) {
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromTrivial(initListElements("unbound_id")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withUnboundNodeMessage()));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(selectFromAssorted(initListElements("stmt")),
|
2019-05-20 21:15:14 +08:00
|
|
|
Failed<StringError>(withTypeErrorMessage("stmt")));
|
|
|
|
}
|
|
|
|
|
2019-09-21 01:11:03 +08:00
|
|
|
TEST(RangeSelectorTest, ElseBranchOpSingleStatement) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f() {
|
|
|
|
int x = 0;
|
|
|
|
if (true) x = 3;
|
|
|
|
else x = 4;
|
|
|
|
return x + 5;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-09-21 01:11:03 +08:00
|
|
|
TestMatch Match = matchCode(Code, ifStmt().bind(ID));
|
|
|
|
EXPECT_THAT_EXPECTED(select(elseBranch(ID), Match), HasValue("else x = 4;"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, ElseBranchOpCompoundStatement) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f() {
|
|
|
|
int x = 0;
|
|
|
|
if (true) x = 3;
|
|
|
|
else { x = 4; }
|
|
|
|
return x + 5;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id";
|
2019-09-21 01:11:03 +08:00
|
|
|
TestMatch Match = matchCode(Code, ifStmt().bind(ID));
|
|
|
|
EXPECT_THAT_EXPECTED(select(elseBranch(ID), Match),
|
|
|
|
HasValue("else { x = 4; }"));
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:15:14 +08:00
|
|
|
// Tests case where the matched node is the complete expanded text.
|
|
|
|
TEST(RangeSelectorTest, ExpansionOp) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
#define BADDECL(E) int bad(int x) { return E; }
|
|
|
|
BADDECL(x * x)
|
|
|
|
)cc";
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Fun = "Fun";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, functionDecl(hasName("bad")).bind(Fun));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(expansion(node(Fun)), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue("BADDECL(x * x)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests case where the matched node is (only) part of the expanded text.
|
|
|
|
TEST(RangeSelectorTest, ExpansionOpPartial) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
#define BADDECL(E) int bad(int x) { return E; }
|
|
|
|
BADDECL(x * x)
|
|
|
|
)cc";
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *Ret = "Ret";
|
2019-05-20 21:15:14 +08:00
|
|
|
TestMatch Match = matchCode(Code, returnStmt().bind(Ret));
|
2019-05-21 00:30:49 +08:00
|
|
|
EXPECT_THAT_EXPECTED(select(expansion(node(Ret)), Match),
|
2019-05-20 21:15:14 +08:00
|
|
|
HasValue("BADDECL(x * x)"));
|
|
|
|
}
|
|
|
|
|
2019-09-21 01:11:03 +08:00
|
|
|
TEST(RangeSelectorTest, IfBoundOpBound) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f() {
|
|
|
|
return 3 + 5;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id", *Op = "op";
|
2019-09-21 01:11:03 +08:00
|
|
|
TestMatch Match =
|
|
|
|
matchCode(Code, binaryOperator(hasLHS(expr().bind(ID))).bind(Op));
|
|
|
|
EXPECT_THAT_EXPECTED(select(ifBound(ID, node(ID), node(Op)), Match),
|
|
|
|
HasValue("3"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(RangeSelectorTest, IfBoundOpUnbound) {
|
|
|
|
StringRef Code = R"cc(
|
|
|
|
int f() {
|
|
|
|
return 3 + 5;
|
|
|
|
}
|
|
|
|
)cc";
|
2020-01-29 03:23:46 +08:00
|
|
|
const char *ID = "id", *Op = "op";
|
2019-09-21 01:11:03 +08:00
|
|
|
TestMatch Match = matchCode(Code, binaryOperator().bind(Op));
|
|
|
|
EXPECT_THAT_EXPECTED(select(ifBound(ID, node(ID), node(Op)), Match),
|
|
|
|
HasValue("3 + 5"));
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:15:14 +08:00
|
|
|
} // namespace
|