1 //===-- QueryEngineTest.cpp - clang-query test ----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "QueryParser.h"
11 #include "QuerySession.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Tooling/Tooling.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "gtest/gtest.h"
21 using namespace clang
;
22 using namespace clang::ast_matchers
;
23 using namespace clang::ast_matchers::dynamic
;
24 using namespace clang::query
;
25 using namespace clang::tooling
;
27 class QueryEngineTest
: public ::testing::Test
{
28 ArrayRef
<std::unique_ptr
<ASTUnit
>> mkASTUnit2(std::unique_ptr
<ASTUnit
> a
,
29 std::unique_ptr
<ASTUnit
> b
) {
30 ASTs
[0] = std::move(a
);
31 ASTs
[1] = std::move(b
);
32 return ArrayRef
<std::unique_ptr
<ASTUnit
>>(ASTs
);
37 : S(mkASTUnit2(buildASTFromCode("void foo1(void) {}\nvoid foo2(void) {}",
39 buildASTFromCode("void bar1(void) {}\nvoid bar2(void) {}",
43 std::unique_ptr
<ASTUnit
> ASTs
[2];
47 llvm::raw_string_ostream OS
;
50 TEST_F(QueryEngineTest
, Basic
) {
51 DynTypedMatcher FnMatcher
= functionDecl();
52 DynTypedMatcher FooMatcher
= functionDecl(hasName("foo1"));
54 std::string FooMatcherString
= "functionDecl(hasName(\"foo1\"))";
56 EXPECT_TRUE(NoOpQuery().run(OS
, S
));
58 EXPECT_EQ("", OS
.str());
62 EXPECT_FALSE(InvalidQuery("Parse error").run(OS
, S
));
64 EXPECT_EQ("Parse error\n", OS
.str());
68 EXPECT_TRUE(HelpQuery().run(OS
, S
));
70 EXPECT_TRUE(OS
.str().find("Available commands:") != std::string::npos
);
74 EXPECT_TRUE(MatchQuery("functionDecl()", FnMatcher
).run(OS
, S
));
76 EXPECT_TRUE(OS
.str().find("foo.cc:1:1: note: \"root\" binds here") !=
78 EXPECT_TRUE(OS
.str().find("foo.cc:2:1: note: \"root\" binds here") !=
80 EXPECT_TRUE(OS
.str().find("bar.cc:1:1: note: \"root\" binds here") !=
82 EXPECT_TRUE(OS
.str().find("bar.cc:2:1: note: \"root\" binds here") !=
84 EXPECT_TRUE(OS
.str().find("4 matches.") != std::string::npos
);
88 EXPECT_TRUE(MatchQuery(FooMatcherString
, FooMatcher
).run(OS
, S
));
90 EXPECT_TRUE(OS
.str().find("foo.cc:1:1: note: \"root\" binds here") !=
92 EXPECT_TRUE(OS
.str().find("1 match.") != std::string::npos
);
97 SetExclusiveOutputQuery(&QuerySession::PrintOutput
).run(OS
, S
));
98 EXPECT_TRUE(MatchQuery(FooMatcherString
, FooMatcher
).run(OS
, S
));
100 EXPECT_TRUE(OS
.str().find("Binding for \"root\":\nvoid foo1()") !=
106 SetExclusiveOutputQuery(&QuerySession::DetailedASTOutput
).run(OS
, S
));
107 EXPECT_TRUE(MatchQuery(FooMatcherString
, FooMatcher
).run(OS
, S
));
109 EXPECT_TRUE(OS
.str().find("FunctionDecl") != std::string::npos
);
113 EXPECT_TRUE(EnableOutputQuery(&QuerySession::DiagOutput
).run(OS
, S
));
114 EXPECT_TRUE(EnableOutputQuery(&QuerySession::DetailedASTOutput
).run(OS
, S
));
115 EXPECT_TRUE(MatchQuery(FooMatcherString
, FooMatcher
).run(OS
, S
));
118 auto Output
= OS
.str();
119 EXPECT_TRUE(Output
.find("FunctionDecl") != std::string::npos
);
120 EXPECT_TRUE(Output
.find("foo.cc:1:1: note: \"root\" binds here") !=
126 EXPECT_TRUE(SetQuery
<bool>(&QuerySession::BindRoot
, false).run(OS
, S
));
127 EXPECT_TRUE(MatchQuery(FooMatcherString
, FooMatcher
).run(OS
, S
));
129 EXPECT_TRUE(OS
.str().find("No bindings.") != std::string::npos
);
133 EXPECT_FALSE(MatchQuery("isMain()", isMain()).run(OS
, S
));
135 EXPECT_EQ("Not a valid top-level matcher.\n", OS
.str());
138 TEST_F(QueryEngineTest
, LetAndMatch
) {
139 EXPECT_TRUE(QueryParser::parse("let x \"foo1\"", S
)->run(OS
, S
));
140 EXPECT_EQ("", OS
.str());
143 EXPECT_TRUE(QueryParser::parse("let y hasName(x)", S
)->run(OS
, S
));
144 EXPECT_EQ("", OS
.str());
147 EXPECT_TRUE(QueryParser::parse("match functionDecl(y)", S
)->run(OS
, S
));
148 EXPECT_TRUE(OS
.str().find("foo.cc:1:1: note: \"root\" binds here") !=
150 EXPECT_TRUE(OS
.str().find("1 match.") != std::string::npos
);
153 EXPECT_TRUE(QueryParser::parse("unlet x", S
)->run(OS
, S
));
154 EXPECT_EQ("", OS
.str());
157 EXPECT_FALSE(QueryParser::parse("let y hasName(x)", S
)->run(OS
, S
));
158 EXPECT_EQ("1:2: Error parsing argument 1 for matcher hasName.\n"
159 "1:10: Value not found: x\n", OS
.str());