2013-05-14 17:13:00 +08:00
|
|
|
//===- unittest/ASTMatchers/Dynamic/RegistryTest.cpp - Registry unit tests -===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2013-05-14 17:13:00 +08:00
|
|
|
//
|
|
|
|
//===-----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "../ASTMatchersTest.h"
|
|
|
|
#include "clang/ASTMatchers/Dynamic/Registry.h"
|
|
|
|
#include "gtest/gtest.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include <vector>
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
namespace dynamic {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ast_matchers::internal::Matcher;
|
|
|
|
|
2013-06-20 22:28:32 +08:00
|
|
|
class RegistryTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
std::vector<ParserValue> Args() { return std::vector<ParserValue>(); }
|
|
|
|
std::vector<ParserValue> Args(const VariantValue &Arg1) {
|
|
|
|
std::vector<ParserValue> Out(1);
|
|
|
|
Out[0].Value = Arg1;
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
std::vector<ParserValue> Args(const VariantValue &Arg1,
|
|
|
|
const VariantValue &Arg2) {
|
|
|
|
std::vector<ParserValue> Out(2);
|
|
|
|
Out[0].Value = Arg1;
|
|
|
|
Out[1].Value = Arg2;
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:51:21 +08:00
|
|
|
llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName) {
|
|
|
|
return Registry::lookupMatcherCtor(MatcherName);
|
2013-11-23 09:13:16 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 22:54:51 +08:00
|
|
|
VariantMatcher constructMatcher(StringRef MatcherName,
|
2014-06-08 16:38:12 +08:00
|
|
|
Diagnostics *Error = nullptr) {
|
2013-07-16 03:25:06 +08:00
|
|
|
Diagnostics DummyError;
|
|
|
|
if (!Error) Error = &DummyError;
|
2014-04-14 21:51:21 +08:00
|
|
|
llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName);
|
2013-11-23 09:13:16 +08:00
|
|
|
VariantMatcher Out;
|
|
|
|
if (Ctor)
|
|
|
|
Out = Registry::constructMatcher(*Ctor, SourceRange(), Args(), Error);
|
2013-07-20 04:02:35 +08:00
|
|
|
EXPECT_EQ("", DummyError.toStringFull());
|
2013-07-16 03:25:06 +08:00
|
|
|
return Out;
|
2013-06-20 22:28:32 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 22:54:51 +08:00
|
|
|
VariantMatcher constructMatcher(StringRef MatcherName,
|
|
|
|
const VariantValue &Arg1,
|
2014-06-08 16:38:12 +08:00
|
|
|
Diagnostics *Error = nullptr) {
|
2013-07-16 03:25:06 +08:00
|
|
|
Diagnostics DummyError;
|
|
|
|
if (!Error) Error = &DummyError;
|
2014-04-14 21:51:21 +08:00
|
|
|
llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName);
|
2013-11-23 09:13:16 +08:00
|
|
|
VariantMatcher Out;
|
|
|
|
if (Ctor)
|
|
|
|
Out = Registry::constructMatcher(*Ctor, SourceRange(), Args(Arg1), Error);
|
2014-03-10 23:40:23 +08:00
|
|
|
EXPECT_EQ("", DummyError.toStringFull()) << MatcherName;
|
2013-07-16 03:25:06 +08:00
|
|
|
return Out;
|
2013-06-20 22:28:32 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 22:54:51 +08:00
|
|
|
VariantMatcher constructMatcher(StringRef MatcherName,
|
|
|
|
const VariantValue &Arg1,
|
|
|
|
const VariantValue &Arg2,
|
2014-06-08 16:38:12 +08:00
|
|
|
Diagnostics *Error = nullptr) {
|
2013-07-16 03:25:06 +08:00
|
|
|
Diagnostics DummyError;
|
|
|
|
if (!Error) Error = &DummyError;
|
2014-04-14 21:51:21 +08:00
|
|
|
llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName);
|
2013-11-23 09:13:16 +08:00
|
|
|
VariantMatcher Out;
|
|
|
|
if (Ctor)
|
|
|
|
Out = Registry::constructMatcher(*Ctor, SourceRange(), Args(Arg1, Arg2),
|
|
|
|
Error);
|
2013-07-20 04:02:35 +08:00
|
|
|
EXPECT_EQ("", DummyError.toStringFull());
|
2013-07-16 03:25:06 +08:00
|
|
|
return Out;
|
2013-06-20 22:28:32 +08:00
|
|
|
}
|
2014-01-24 06:48:38 +08:00
|
|
|
|
|
|
|
typedef std::vector<MatcherCompletion> CompVector;
|
|
|
|
|
|
|
|
CompVector getCompletions() {
|
2014-08-13 05:11:37 +08:00
|
|
|
std::vector<std::pair<MatcherCtor, unsigned> > Context;
|
|
|
|
return Registry::getMatcherCompletions(
|
|
|
|
Registry::getAcceptedCompletionTypes(Context));
|
2014-01-24 06:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CompVector getCompletions(StringRef MatcherName1, unsigned ArgNo1) {
|
|
|
|
std::vector<std::pair<MatcherCtor, unsigned> > Context;
|
|
|
|
llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName1);
|
|
|
|
if (!Ctor)
|
|
|
|
return CompVector();
|
|
|
|
Context.push_back(std::make_pair(*Ctor, ArgNo1));
|
2014-08-13 05:11:37 +08:00
|
|
|
return Registry::getMatcherCompletions(
|
|
|
|
Registry::getAcceptedCompletionTypes(Context));
|
2014-01-24 06:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CompVector getCompletions(StringRef MatcherName1, unsigned ArgNo1,
|
|
|
|
StringRef MatcherName2, unsigned ArgNo2) {
|
|
|
|
std::vector<std::pair<MatcherCtor, unsigned> > Context;
|
|
|
|
llvm::Optional<MatcherCtor> Ctor = lookupMatcherCtor(MatcherName1);
|
|
|
|
if (!Ctor)
|
|
|
|
return CompVector();
|
|
|
|
Context.push_back(std::make_pair(*Ctor, ArgNo1));
|
|
|
|
Ctor = lookupMatcherCtor(MatcherName2);
|
|
|
|
if (!Ctor)
|
|
|
|
return CompVector();
|
|
|
|
Context.push_back(std::make_pair(*Ctor, ArgNo2));
|
2014-08-13 05:11:37 +08:00
|
|
|
return Registry::getMatcherCompletions(
|
|
|
|
Registry::getAcceptedCompletionTypes(Context));
|
2014-01-24 06:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasCompletion(const CompVector &Comps, StringRef TypedText,
|
2014-08-13 05:11:37 +08:00
|
|
|
StringRef MatcherDecl = StringRef()) {
|
2014-01-24 06:48:38 +08:00
|
|
|
for (CompVector::const_iterator I = Comps.begin(), E = Comps.end(); I != E;
|
|
|
|
++I) {
|
|
|
|
if (I->TypedText == TypedText &&
|
|
|
|
(MatcherDecl.empty() || I->MatcherDecl == MatcherDecl)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-20 22:28:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(RegistryTest, CanConstructNoArgs) {
|
2013-07-16 03:25:06 +08:00
|
|
|
Matcher<Stmt> IsArrowValue = constructMatcher(
|
|
|
|
"memberExpr", constructMatcher("isArrow")).getTypedMatcher<Stmt>();
|
|
|
|
Matcher<Stmt> BoolValue =
|
2015-09-17 21:30:52 +08:00
|
|
|
constructMatcher("cxxBoolLiteral").getTypedMatcher<Stmt>();
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
const std::string ClassSnippet = "struct Foo { int x; };\n"
|
|
|
|
"Foo *foo = new Foo;\n"
|
|
|
|
"int i = foo->x;\n";
|
|
|
|
const std::string BoolSnippet = "bool Foo = true;\n";
|
|
|
|
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_TRUE(matches(ClassSnippet, IsArrowValue));
|
|
|
|
EXPECT_TRUE(matches(BoolSnippet, BoolValue));
|
|
|
|
EXPECT_FALSE(matches(ClassSnippet, BoolValue));
|
|
|
|
EXPECT_FALSE(matches(BoolSnippet, IsArrowValue));
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 22:28:32 +08:00
|
|
|
TEST_F(RegistryTest, ConstructWithSimpleArgs) {
|
2013-07-16 03:25:06 +08:00
|
|
|
Matcher<Decl> Value = constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"namedDecl", constructMatcher("hasName", StringRef("X")))
|
2013-07-16 03:25:06 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_TRUE(matches("class X {};", Value));
|
|
|
|
EXPECT_FALSE(matches("int x;", Value));
|
|
|
|
|
2013-07-16 03:25:06 +08:00
|
|
|
Value = functionDecl(constructMatcher("parameterCountIs", 2)
|
|
|
|
.getTypedMatcher<FunctionDecl>());
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_TRUE(matches("void foo(int,int);", Value));
|
|
|
|
EXPECT_FALSE(matches("void foo(int);", Value));
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 22:28:32 +08:00
|
|
|
TEST_F(RegistryTest, ConstructWithMatcherArgs) {
|
2013-08-13 22:54:51 +08:00
|
|
|
Matcher<Decl> HasInitializerSimple = constructMatcher(
|
|
|
|
"varDecl", constructMatcher("hasInitializer", constructMatcher("stmt")))
|
|
|
|
.getTypedMatcher<Decl>();
|
2013-07-16 03:25:06 +08:00
|
|
|
Matcher<Decl> HasInitializerComplex = constructMatcher(
|
2013-08-13 22:54:51 +08:00
|
|
|
"varDecl",
|
|
|
|
constructMatcher("hasInitializer", constructMatcher("callExpr")))
|
2013-07-16 03:25:06 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
std::string code = "int i;";
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_FALSE(matches(code, HasInitializerSimple));
|
|
|
|
EXPECT_FALSE(matches(code, HasInitializerComplex));
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
code = "int i = 1;";
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_TRUE(matches(code, HasInitializerSimple));
|
|
|
|
EXPECT_FALSE(matches(code, HasInitializerComplex));
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
code = "int y(); int i = y();";
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_TRUE(matches(code, HasInitializerSimple));
|
|
|
|
EXPECT_TRUE(matches(code, HasInitializerComplex));
|
2013-06-04 23:46:22 +08:00
|
|
|
|
2013-08-13 22:54:51 +08:00
|
|
|
Matcher<Decl> HasParameter =
|
|
|
|
functionDecl(constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"hasParameter", 1, constructMatcher("hasName", StringRef("x")))
|
2013-08-13 22:54:51 +08:00
|
|
|
.getTypedMatcher<FunctionDecl>());
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_TRUE(matches("void f(int a, int x);", HasParameter));
|
|
|
|
EXPECT_FALSE(matches("void f(int x, int a);", HasParameter));
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 00:13:57 +08:00
|
|
|
TEST_F(RegistryTest, OverloadedMatchers) {
|
|
|
|
Matcher<Stmt> CallExpr0 = constructMatcher(
|
|
|
|
"callExpr",
|
|
|
|
constructMatcher("callee", constructMatcher("memberExpr",
|
|
|
|
constructMatcher("isArrow"))))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
|
|
|
|
Matcher<Stmt> CallExpr1 = constructMatcher(
|
|
|
|
"callExpr",
|
|
|
|
constructMatcher(
|
|
|
|
"callee",
|
2015-09-17 21:30:52 +08:00
|
|
|
constructMatcher("cxxMethodDecl",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("x")))))
|
2013-07-23 00:13:57 +08:00
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
|
|
|
|
std::string Code = "class Y { public: void x(); }; void z() { Y y; y.x(); }";
|
|
|
|
EXPECT_FALSE(matches(Code, CallExpr0));
|
|
|
|
EXPECT_TRUE(matches(Code, CallExpr1));
|
|
|
|
|
|
|
|
Code = "class Z { public: void z() { this->z(); } };";
|
|
|
|
EXPECT_TRUE(matches(Code, CallExpr0));
|
|
|
|
EXPECT_FALSE(matches(Code, CallExpr1));
|
2014-03-10 23:40:23 +08:00
|
|
|
|
|
|
|
Matcher<Decl> DeclDecl = declaratorDecl(hasTypeLoc(
|
|
|
|
constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"loc", constructMatcher("asString", StringRef("const double *")))
|
2014-03-10 23:40:23 +08:00
|
|
|
.getTypedMatcher<TypeLoc>()));
|
|
|
|
|
|
|
|
Matcher<NestedNameSpecifierLoc> NNSL =
|
|
|
|
constructMatcher(
|
|
|
|
"loc", VariantMatcher::SingleMatcher(nestedNameSpecifier(
|
|
|
|
specifiesType(hasDeclaration(recordDecl(hasName("A")))))))
|
|
|
|
.getTypedMatcher<NestedNameSpecifierLoc>();
|
|
|
|
|
|
|
|
Code = "const double * x = 0;";
|
|
|
|
EXPECT_TRUE(matches(Code, DeclDecl));
|
|
|
|
EXPECT_FALSE(matches(Code, NNSL));
|
|
|
|
|
|
|
|
Code = "struct A { struct B {}; }; A::B a_b;";
|
|
|
|
EXPECT_FALSE(matches(Code, DeclDecl));
|
|
|
|
EXPECT_TRUE(matches(Code, NNSL));
|
2013-07-23 00:13:57 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
TEST_F(RegistryTest, PolymorphicMatchers) {
|
2013-08-13 22:54:51 +08:00
|
|
|
const VariantMatcher IsDefinition = constructMatcher("isDefinition");
|
2013-07-16 03:25:06 +08:00
|
|
|
Matcher<Decl> Var =
|
|
|
|
constructMatcher("varDecl", IsDefinition).getTypedMatcher<Decl>();
|
2013-06-21 23:51:31 +08:00
|
|
|
Matcher<Decl> Class =
|
2013-07-16 03:25:06 +08:00
|
|
|
constructMatcher("recordDecl", IsDefinition).getTypedMatcher<Decl>();
|
2013-06-21 23:51:31 +08:00
|
|
|
Matcher<Decl> Func =
|
2013-07-16 03:25:06 +08:00
|
|
|
constructMatcher("functionDecl", IsDefinition).getTypedMatcher<Decl>();
|
2013-06-21 23:51:31 +08:00
|
|
|
EXPECT_TRUE(matches("int a;", Var));
|
|
|
|
EXPECT_FALSE(matches("extern int a;", Var));
|
|
|
|
EXPECT_TRUE(matches("class A {};", Class));
|
|
|
|
EXPECT_FALSE(matches("class A;", Class));
|
|
|
|
EXPECT_TRUE(matches("void f(){};", Func));
|
|
|
|
EXPECT_FALSE(matches("void f();", Func));
|
|
|
|
|
2013-07-16 03:25:06 +08:00
|
|
|
Matcher<Decl> Anything = constructMatcher("anything").getTypedMatcher<Decl>();
|
2013-08-29 23:39:26 +08:00
|
|
|
Matcher<Decl> RecordDecl = constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"recordDecl", constructMatcher("hasName", StringRef("Foo")),
|
2013-08-29 23:39:26 +08:00
|
|
|
VariantMatcher::SingleMatcher(Anything)).getTypedMatcher<Decl>();
|
|
|
|
|
|
|
|
EXPECT_TRUE(matches("int Foo;", Anything));
|
|
|
|
EXPECT_TRUE(matches("class Foo {};", Anything));
|
|
|
|
EXPECT_TRUE(matches("void Foo(){};", Anything));
|
|
|
|
EXPECT_FALSE(matches("int Foo;", RecordDecl));
|
|
|
|
EXPECT_TRUE(matches("class Foo {};", RecordDecl));
|
|
|
|
EXPECT_FALSE(matches("void Foo(){};", RecordDecl));
|
2013-11-18 22:53:42 +08:00
|
|
|
|
|
|
|
Matcher<Stmt> ConstructExpr = constructMatcher(
|
2015-09-17 21:30:52 +08:00
|
|
|
"cxxConstructExpr",
|
2013-11-18 22:53:42 +08:00
|
|
|
constructMatcher(
|
|
|
|
"hasDeclaration",
|
|
|
|
constructMatcher(
|
2015-09-17 21:30:52 +08:00
|
|
|
"cxxMethodDecl",
|
2013-11-18 22:53:42 +08:00
|
|
|
constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"ofClass", constructMatcher("hasName", StringRef("Foo"))))))
|
2013-11-18 22:53:42 +08:00
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_FALSE(matches("class Foo { public: Foo(); };", ConstructExpr));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
matches("class Foo { public: Foo(); }; Foo foo = Foo();", ConstructExpr));
|
2013-06-21 23:51:31 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 23:11:30 +08:00
|
|
|
TEST_F(RegistryTest, TemplateArgument) {
|
|
|
|
Matcher<Decl> HasTemplateArgument = constructMatcher(
|
|
|
|
"classTemplateSpecializationDecl",
|
|
|
|
constructMatcher(
|
|
|
|
"hasAnyTemplateArgument",
|
|
|
|
constructMatcher("refersToType",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("asString", StringRef("int")))))
|
2013-07-17 23:11:30 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
EXPECT_TRUE(matches("template<typename T> class A {}; A<int> a;",
|
|
|
|
HasTemplateArgument));
|
|
|
|
EXPECT_FALSE(matches("template<typename T> class A {}; A<char> a;",
|
|
|
|
HasTemplateArgument));
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:25:06 +08:00
|
|
|
TEST_F(RegistryTest, TypeTraversal) {
|
|
|
|
Matcher<Type> M = constructMatcher(
|
|
|
|
"pointerType",
|
|
|
|
constructMatcher("pointee", constructMatcher("isConstQualified"),
|
|
|
|
constructMatcher("isInteger"))).getTypedMatcher<Type>();
|
|
|
|
EXPECT_FALSE(matches("int *a;", M));
|
|
|
|
EXPECT_TRUE(matches("int const *b;", M));
|
|
|
|
|
|
|
|
M = constructMatcher(
|
|
|
|
"arrayType",
|
|
|
|
constructMatcher("hasElementType", constructMatcher("builtinType")))
|
|
|
|
.getTypedMatcher<Type>();
|
|
|
|
EXPECT_FALSE(matches("struct A{}; A a[7];;", M));
|
|
|
|
EXPECT_TRUE(matches("int b[7];", M));
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:55:22 +08:00
|
|
|
TEST_F(RegistryTest, CXXBaseSpecifier) {
|
|
|
|
// TODO: rewrite with top-level cxxBaseSpecifier matcher when available
|
|
|
|
DeclarationMatcher ClassHasAnyDirectBase =
|
|
|
|
constructMatcher("cxxRecordDecl",
|
|
|
|
constructMatcher("hasDirectBase",
|
|
|
|
constructMatcher("cxxBaseSpecifier")))
|
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
EXPECT_TRUE(matches("class X {}; class Y : X {};", ClassHasAnyDirectBase));
|
|
|
|
EXPECT_TRUE(notMatches("class X {};", ClassHasAnyDirectBase));
|
|
|
|
}
|
|
|
|
|
2013-07-17 22:28:00 +08:00
|
|
|
TEST_F(RegistryTest, CXXCtorInitializer) {
|
|
|
|
Matcher<Decl> CtorDecl = constructMatcher(
|
2015-09-17 21:30:52 +08:00
|
|
|
"cxxConstructorDecl",
|
2013-08-13 22:54:51 +08:00
|
|
|
constructMatcher(
|
|
|
|
"hasAnyConstructorInitializer",
|
|
|
|
constructMatcher("forField",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("foo")))))
|
2013-07-17 22:28:00 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
EXPECT_TRUE(matches("struct Foo { Foo() : foo(1) {} int foo; };", CtorDecl));
|
|
|
|
EXPECT_FALSE(matches("struct Foo { Foo() {} int foo; };", CtorDecl));
|
|
|
|
EXPECT_FALSE(matches("struct Foo { Foo() : bar(1) {} int bar; };", CtorDecl));
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:48:01 +08:00
|
|
|
TEST_F(RegistryTest, Adaptative) {
|
|
|
|
Matcher<Decl> D = constructMatcher(
|
|
|
|
"recordDecl",
|
|
|
|
constructMatcher(
|
|
|
|
"has",
|
|
|
|
constructMatcher("recordDecl",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("X")))))
|
2013-07-24 22:48:01 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
EXPECT_TRUE(matches("class X {};", D));
|
|
|
|
EXPECT_TRUE(matches("class Y { class X {}; };", D));
|
|
|
|
EXPECT_FALSE(matches("class Y { class Z {}; };", D));
|
|
|
|
|
|
|
|
Matcher<Stmt> S = constructMatcher(
|
|
|
|
"forStmt",
|
|
|
|
constructMatcher(
|
|
|
|
"hasDescendant",
|
|
|
|
constructMatcher("varDecl",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("X")))))
|
2013-07-24 22:48:01 +08:00
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("void foo() { for(int X;;); }", S));
|
|
|
|
EXPECT_TRUE(matches("void foo() { for(;;) { int X; } }", S));
|
|
|
|
EXPECT_FALSE(matches("void foo() { for(;;); }", S));
|
|
|
|
EXPECT_FALSE(matches("void foo() { if (int X = 0){} }", S));
|
|
|
|
|
|
|
|
S = constructMatcher(
|
|
|
|
"compoundStmt", constructMatcher("hasParent", constructMatcher("ifStmt")))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("void foo() { if (true) { int x = 42; } }", S));
|
|
|
|
EXPECT_FALSE(matches("void foo() { if (true) return; }", S));
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:42:04 +08:00
|
|
|
TEST_F(RegistryTest, VariadicOp) {
|
|
|
|
Matcher<Decl> D = constructMatcher(
|
2013-08-29 23:39:26 +08:00
|
|
|
"anyOf",
|
|
|
|
constructMatcher("recordDecl",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("Foo"))),
|
2014-10-06 21:14:30 +08:00
|
|
|
constructMatcher("functionDecl",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("foo"))))
|
2013-08-29 02:42:04 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
|
|
|
|
EXPECT_TRUE(matches("void foo(){}", D));
|
|
|
|
EXPECT_TRUE(matches("struct Foo{};", D));
|
|
|
|
EXPECT_FALSE(matches("int i = 0;", D));
|
|
|
|
|
|
|
|
D = constructMatcher(
|
|
|
|
"allOf", constructMatcher("recordDecl"),
|
|
|
|
constructMatcher(
|
|
|
|
"namedDecl",
|
|
|
|
constructMatcher("anyOf",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("Foo")),
|
|
|
|
constructMatcher("hasName", StringRef("Bar")))))
|
2013-08-29 02:42:04 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
|
|
|
|
EXPECT_FALSE(matches("void foo(){}", D));
|
|
|
|
EXPECT_TRUE(matches("struct Foo{};", D));
|
|
|
|
EXPECT_FALSE(matches("int i = 0;", D));
|
|
|
|
EXPECT_TRUE(matches("class Bar{};", D));
|
|
|
|
EXPECT_FALSE(matches("class OtherBar{};", D));
|
2013-11-22 22:41:48 +08:00
|
|
|
|
2013-11-23 07:05:57 +08:00
|
|
|
D = recordDecl(
|
|
|
|
has(fieldDecl(hasName("Foo"))),
|
2013-11-22 22:41:48 +08:00
|
|
|
constructMatcher(
|
|
|
|
"unless",
|
|
|
|
constructMatcher("namedDecl",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("hasName", StringRef("Bar"))))
|
2013-11-23 07:05:57 +08:00
|
|
|
.getTypedMatcher<Decl>());
|
2013-11-22 22:41:48 +08:00
|
|
|
|
2013-11-23 07:05:57 +08:00
|
|
|
EXPECT_FALSE(matches("class Bar{ int Foo; };", D));
|
|
|
|
EXPECT_TRUE(matches("class OtherBar{ int Foo; };", D));
|
2014-10-14 01:38:12 +08:00
|
|
|
|
|
|
|
D = constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"namedDecl", constructMatcher("hasName", StringRef("Foo")),
|
2014-10-14 01:38:12 +08:00
|
|
|
constructMatcher("unless", constructMatcher("recordDecl")))
|
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
EXPECT_TRUE(matches("void Foo(){}", D));
|
|
|
|
EXPECT_TRUE(notMatches("struct Foo {};", D));
|
2013-08-29 02:42:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 22:28:32 +08:00
|
|
|
TEST_F(RegistryTest, Errors) {
|
2013-05-14 17:13:00 +08:00
|
|
|
// Incorrect argument count.
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<Diagnostics> Error(new Diagnostics());
|
2013-08-13 22:54:51 +08:00
|
|
|
EXPECT_TRUE(constructMatcher("hasInitializer", Error.get()).isNull());
|
2013-05-14 17:13:00 +08:00
|
|
|
EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)",
|
2013-07-20 04:02:35 +08:00
|
|
|
Error->toString());
|
2013-05-14 17:13:00 +08:00
|
|
|
Error.reset(new Diagnostics());
|
2015-07-06 16:47:15 +08:00
|
|
|
EXPECT_TRUE(constructMatcher("isArrow", StringRef(), Error.get()).isNull());
|
2013-05-14 17:13:00 +08:00
|
|
|
EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)",
|
2013-07-20 04:02:35 +08:00
|
|
|
Error->toString());
|
2013-11-22 22:41:48 +08:00
|
|
|
Error.reset(new Diagnostics());
|
|
|
|
EXPECT_TRUE(constructMatcher("anyOf", Error.get()).isNull());
|
|
|
|
EXPECT_EQ("Incorrect argument count. (Expected = (2, )) != (Actual = 0)",
|
|
|
|
Error->toString());
|
|
|
|
Error.reset(new Diagnostics());
|
2015-07-06 16:47:15 +08:00
|
|
|
EXPECT_TRUE(constructMatcher("unless", StringRef(), StringRef(),
|
2013-11-22 22:41:48 +08:00
|
|
|
Error.get()).isNull());
|
|
|
|
EXPECT_EQ("Incorrect argument count. (Expected = (1, 1)) != (Actual = 2)",
|
|
|
|
Error->toString());
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
// Bad argument type
|
|
|
|
Error.reset(new Diagnostics());
|
2015-07-06 16:47:15 +08:00
|
|
|
EXPECT_TRUE(constructMatcher("ofClass", StringRef(), Error.get()).isNull());
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_EQ("Incorrect type for arg 1. (Expected = Matcher<CXXRecordDecl>) != "
|
|
|
|
"(Actual = String)",
|
2013-07-20 04:02:35 +08:00
|
|
|
Error->toString());
|
2013-05-14 17:13:00 +08:00
|
|
|
Error.reset(new Diagnostics());
|
2015-09-17 21:30:52 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
constructMatcher("cxxRecordDecl", constructMatcher("cxxRecordDecl"),
|
|
|
|
constructMatcher("parameterCountIs", 3), Error.get())
|
|
|
|
.isNull());
|
2013-06-20 22:28:32 +08:00
|
|
|
EXPECT_EQ("Incorrect type for arg 2. (Expected = Matcher<CXXRecordDecl>) != "
|
2016-02-01 22:11:47 +08:00
|
|
|
"(Actual = Matcher<FunctionDecl|FunctionProtoType>)",
|
2013-07-20 04:02:35 +08:00
|
|
|
Error->toString());
|
2013-08-29 02:42:04 +08:00
|
|
|
|
|
|
|
// Bad argument type with variadic.
|
|
|
|
Error.reset(new Diagnostics());
|
2015-07-06 16:47:15 +08:00
|
|
|
EXPECT_TRUE(constructMatcher("anyOf", StringRef(), StringRef(),
|
2013-11-22 22:41:48 +08:00
|
|
|
Error.get()).isNull());
|
2013-08-29 02:42:04 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
"Incorrect type for arg 1. (Expected = Matcher<>) != (Actual = String)",
|
|
|
|
Error->toString());
|
|
|
|
Error.reset(new Diagnostics());
|
|
|
|
EXPECT_TRUE(constructMatcher(
|
2015-09-17 21:30:52 +08:00
|
|
|
"cxxRecordDecl",
|
2013-08-29 02:42:04 +08:00
|
|
|
constructMatcher("allOf",
|
2015-07-06 16:47:15 +08:00
|
|
|
constructMatcher("isDerivedFrom", StringRef("FOO")),
|
2013-08-29 02:42:04 +08:00
|
|
|
constructMatcher("isArrow")),
|
|
|
|
Error.get()).isNull());
|
|
|
|
EXPECT_EQ("Incorrect type for arg 1. "
|
|
|
|
"(Expected = Matcher<CXXRecordDecl>) != "
|
2019-08-13 07:23:35 +08:00
|
|
|
"(Actual = Matcher<CXXRecordDecl|ObjCInterfaceDecl>&Matcher"
|
2018-09-18 02:48:43 +08:00
|
|
|
"<MemberExpr|UnresolvedMemberExpr|CXXDependentScopeMemberExpr>)",
|
2013-08-29 02:42:04 +08:00
|
|
|
Error->toString());
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2014-01-24 06:48:38 +08:00
|
|
|
TEST_F(RegistryTest, Completion) {
|
|
|
|
CompVector Comps = getCompletions();
|
2014-10-10 06:08:52 +08:00
|
|
|
// Overloaded
|
2015-10-21 18:07:26 +08:00
|
|
|
EXPECT_TRUE(hasCompletion(
|
2015-10-23 17:04:55 +08:00
|
|
|
Comps, "hasParent(",
|
2016-04-14 08:47:40 +08:00
|
|
|
"Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...> "
|
|
|
|
"hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)"));
|
2014-10-10 06:08:52 +08:00
|
|
|
// Variadic.
|
2014-01-24 06:48:38 +08:00
|
|
|
EXPECT_TRUE(hasCompletion(Comps, "whileStmt(",
|
|
|
|
"Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)"));
|
2014-10-10 06:08:52 +08:00
|
|
|
// Polymorphic.
|
|
|
|
EXPECT_TRUE(hasCompletion(
|
|
|
|
Comps, "hasDescendant(",
|
2016-04-14 08:47:40 +08:00
|
|
|
"Matcher<NestedNameSpecifierLoc|QualType|TypeLoc|...> "
|
|
|
|
"hasDescendant(Matcher<NestedNameSpecifierLoc|QualType|TypeLoc|...>)"));
|
2014-01-24 06:48:38 +08:00
|
|
|
|
|
|
|
CompVector WhileComps = getCompletions("whileStmt", 0);
|
|
|
|
|
|
|
|
EXPECT_TRUE(hasCompletion(WhileComps, "hasBody(",
|
2014-08-13 05:11:37 +08:00
|
|
|
"Matcher<WhileStmt> hasBody(Matcher<Stmt>)"));
|
2016-04-14 08:47:40 +08:00
|
|
|
EXPECT_TRUE(hasCompletion(
|
|
|
|
WhileComps, "hasParent(",
|
|
|
|
"Matcher<Stmt> "
|
|
|
|
"hasParent(Matcher<NestedNameSpecifierLoc|TypeLoc|Decl|...>)"));
|
2014-08-13 05:11:37 +08:00
|
|
|
EXPECT_TRUE(
|
|
|
|
hasCompletion(WhileComps, "allOf(", "Matcher<T> allOf(Matcher<T>...)"));
|
2014-01-24 06:48:38 +08:00
|
|
|
|
|
|
|
EXPECT_FALSE(hasCompletion(WhileComps, "whileStmt("));
|
|
|
|
EXPECT_FALSE(hasCompletion(WhileComps, "ifStmt("));
|
|
|
|
|
|
|
|
CompVector AllOfWhileComps =
|
|
|
|
getCompletions("allOf", 0, "whileStmt", 0);
|
|
|
|
ASSERT_EQ(AllOfWhileComps.size(), WhileComps.size());
|
|
|
|
EXPECT_TRUE(std::equal(WhileComps.begin(), WhileComps.end(),
|
|
|
|
AllOfWhileComps.begin()));
|
|
|
|
|
|
|
|
CompVector DeclWhileComps =
|
|
|
|
getCompletions("decl", 0, "whileStmt", 0);
|
|
|
|
EXPECT_EQ(0u, DeclWhileComps.size());
|
|
|
|
|
|
|
|
CompVector NamedDeclComps = getCompletions("namedDecl", 0);
|
|
|
|
EXPECT_TRUE(
|
|
|
|
hasCompletion(NamedDeclComps, "isPublic()", "Matcher<Decl> isPublic()"));
|
|
|
|
EXPECT_TRUE(hasCompletion(NamedDeclComps, "hasName(\"",
|
|
|
|
"Matcher<NamedDecl> hasName(string)"));
|
2014-10-10 06:08:52 +08:00
|
|
|
|
|
|
|
// Heterogeneous overloads.
|
|
|
|
Comps = getCompletions("classTemplateSpecializationDecl", 0);
|
|
|
|
EXPECT_TRUE(hasCompletion(
|
|
|
|
Comps, "isSameOrDerivedFrom(",
|
|
|
|
"Matcher<CXXRecordDecl> isSameOrDerivedFrom(string|Matcher<NamedDecl>)"));
|
2014-01-24 06:48:38 +08:00
|
|
|
}
|
|
|
|
|
2021-02-07 08:09:41 +08:00
|
|
|
TEST_F(RegistryTest, MatcherBuilder) {
|
|
|
|
auto Ctor = *lookupMatcherCtor("mapAnyOf");
|
|
|
|
EXPECT_TRUE(Registry::isBuilderMatcher(Ctor));
|
|
|
|
auto BuiltCtor = Registry::buildMatcherCtor(Ctor, {}, Args(ASTNodeKind::getFromNodeKind<WhileStmt>(), ASTNodeKind::getFromNodeKind<ForStmt>()), nullptr);
|
|
|
|
EXPECT_TRUE(BuiltCtor.get());
|
|
|
|
auto LoopMatcher = Registry::constructMatcher(BuiltCtor.get(), SourceRange(), Args(), nullptr).getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("void f() { for (;;) {} }", LoopMatcher));
|
|
|
|
EXPECT_TRUE(matches("void f() { while (true) {} }", LoopMatcher));
|
|
|
|
EXPECT_FALSE(matches("void f() { if (true) {} }", LoopMatcher));
|
|
|
|
|
|
|
|
auto NotBuiltCtor = Registry::buildMatcherCtor(Ctor, {}, Args(ASTNodeKind::getFromNodeKind<FunctionDecl>(), ASTNodeKind::getFromNodeKind<ForStmt>()), nullptr);
|
|
|
|
EXPECT_FALSE(NotBuiltCtor.get());
|
|
|
|
}
|
|
|
|
|
2021-02-07 07:48:14 +08:00
|
|
|
TEST_F(RegistryTest, NodeType) {
|
|
|
|
EXPECT_TRUE(Registry::nodeMatcherType(*lookupMatcherCtor("callExpr")).isSame(ASTNodeKind::getFromNodeKind<CallExpr>()));
|
|
|
|
EXPECT_TRUE(Registry::nodeMatcherType(*lookupMatcherCtor("has")).isNone());
|
|
|
|
EXPECT_TRUE(Registry::nodeMatcherType(*lookupMatcherCtor("allOf")).isNone());
|
|
|
|
}
|
|
|
|
|
2014-08-25 19:23:50 +08:00
|
|
|
TEST_F(RegistryTest, HasArgs) {
|
|
|
|
Matcher<Decl> Value = constructMatcher(
|
2015-07-06 16:47:15 +08:00
|
|
|
"decl", constructMatcher("hasAttr", StringRef("attr::WarnUnused")))
|
2014-08-25 19:23:50 +08:00
|
|
|
.getTypedMatcher<Decl>();
|
|
|
|
EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};", Value));
|
|
|
|
EXPECT_FALSE(matches("struct X {};", Value));
|
|
|
|
}
|
|
|
|
|
2016-01-21 00:17:39 +08:00
|
|
|
TEST_F(RegistryTest, ParenExpr) {
|
|
|
|
Matcher<Stmt> Value = constructMatcher("parenExpr").getTypedMatcher<Stmt>();
|
2020-12-11 07:52:35 +08:00
|
|
|
EXPECT_TRUE(matches("int i = (1);", traverse(TK_AsIs, Value)));
|
|
|
|
EXPECT_FALSE(matches("int i = 1;", traverse(TK_AsIs, Value)));
|
2016-01-21 00:17:39 +08:00
|
|
|
}
|
|
|
|
|
2017-06-09 06:00:58 +08:00
|
|
|
TEST_F(RegistryTest, EqualsMatcher) {
|
|
|
|
Matcher<Stmt> BooleanStmt = constructMatcher(
|
|
|
|
"cxxBoolLiteral", constructMatcher("equals", VariantValue(true)))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("bool x = true;", BooleanStmt));
|
|
|
|
EXPECT_FALSE(matches("bool x = false;", BooleanStmt));
|
|
|
|
EXPECT_FALSE(matches("bool x = 0;", BooleanStmt));
|
|
|
|
|
|
|
|
BooleanStmt = constructMatcher(
|
|
|
|
"cxxBoolLiteral", constructMatcher("equals", VariantValue(0)))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("bool x = false;", BooleanStmt));
|
|
|
|
EXPECT_FALSE(matches("bool x = true;", BooleanStmt));
|
|
|
|
EXPECT_FALSE(matches("bool x = 0;", BooleanStmt));
|
|
|
|
|
|
|
|
Matcher<Stmt> DoubleStmt = constructMatcher(
|
|
|
|
"floatLiteral", constructMatcher("equals", VariantValue(1.2)))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("double x = 1.2;", DoubleStmt));
|
2017-06-09 06:58:12 +08:00
|
|
|
#if 0
|
|
|
|
// FIXME floatLiteral matching should work regardless of suffix.
|
2017-06-09 06:00:58 +08:00
|
|
|
EXPECT_TRUE(matches("double x = 1.2f;", DoubleStmt));
|
|
|
|
EXPECT_TRUE(matches("double x = 1.2l;", DoubleStmt));
|
2017-06-09 06:58:12 +08:00
|
|
|
#endif
|
2017-06-09 06:00:58 +08:00
|
|
|
EXPECT_TRUE(matches("double x = 12e-1;", DoubleStmt));
|
|
|
|
EXPECT_FALSE(matches("double x = 1.23;", DoubleStmt));
|
|
|
|
|
|
|
|
Matcher<Stmt> IntegerStmt = constructMatcher(
|
|
|
|
"integerLiteral", constructMatcher("equals", VariantValue(42)))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("int x = 42;", IntegerStmt));
|
|
|
|
EXPECT_FALSE(matches("int x = 1;", IntegerStmt));
|
|
|
|
|
|
|
|
Matcher<Stmt> CharStmt = constructMatcher(
|
|
|
|
"characterLiteral", constructMatcher("equals", VariantValue('x')))
|
|
|
|
.getTypedMatcher<Stmt>();
|
|
|
|
EXPECT_TRUE(matches("int x = 'x';", CharStmt));
|
|
|
|
EXPECT_TRUE(matches("int x = L'x';", CharStmt));
|
|
|
|
EXPECT_TRUE(matches("int x = u'x';", CharStmt));
|
|
|
|
EXPECT_TRUE(matches("int x = U'x';", CharStmt));
|
|
|
|
EXPECT_FALSE(matches("int x = 120;", CharStmt));
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:13:00 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
} // end namespace dynamic
|
|
|
|
} // end namespace ast_matchers
|
|
|
|
} // end namespace clang
|