1 //===---- QueryTest.cpp - clang-query test --------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Query.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" 19 #include <string> 20 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; 26 27 TEST(Query, Basic) { 28 std::unique_ptr<ASTUnit> FooAST( 29 buildASTFromCode("void foo1(void) {}\nvoid foo2(void) {}", "foo.cc")); 30 ASSERT_TRUE(FooAST.get()); 31 std::unique_ptr<ASTUnit> BarAST( 32 buildASTFromCode("void bar1(void) {}\nvoid bar2(void) {}", "bar.cc")); 33 ASSERT_TRUE(BarAST.get()); 34 35 ASTUnit *ASTs[] = { FooAST.get(), BarAST.get() }; 36 37 std::string Str; 38 llvm::raw_string_ostream OS(Str); 39 QuerySession S(ASTs); 40 41 DynTypedMatcher FnMatcher = functionDecl(); 42 DynTypedMatcher FooMatcher = functionDecl(hasName("foo1")); 43 44 EXPECT_TRUE(NoOpQuery().run(OS, S)); 45 46 EXPECT_EQ("", OS.str()); 47 48 Str.clear(); 49 50 EXPECT_FALSE(InvalidQuery("Parse error").run(OS, S)); 51 52 EXPECT_EQ("Parse error\n", OS.str()); 53 54 Str.clear(); 55 56 EXPECT_TRUE(HelpQuery().run(OS, S)); 57 58 EXPECT_TRUE(OS.str().find("Available commands:") != std::string::npos); 59 60 Str.clear(); 61 62 EXPECT_TRUE(MatchQuery(FnMatcher).run(OS, S)); 63 64 EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") != 65 std::string::npos); 66 EXPECT_TRUE(OS.str().find("foo.cc:2:1: note: \"root\" binds here") != 67 std::string::npos); 68 EXPECT_TRUE(OS.str().find("bar.cc:1:1: note: \"root\" binds here") != 69 std::string::npos); 70 EXPECT_TRUE(OS.str().find("bar.cc:2:1: note: \"root\" binds here") != 71 std::string::npos); 72 EXPECT_TRUE(OS.str().find("4 matches.") != std::string::npos); 73 74 Str.clear(); 75 76 EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); 77 78 EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") != 79 std::string::npos); 80 EXPECT_TRUE(OS.str().find("1 match.") != std::string::npos); 81 82 Str.clear(); 83 84 EXPECT_TRUE( 85 SetQuery<OutputKind>(&QuerySession::OutKind, OK_Print).run(OS, S)); 86 EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); 87 88 EXPECT_TRUE(OS.str().find("Binding for \"root\":\nvoid foo1()") != 89 std::string::npos); 90 91 Str.clear(); 92 93 EXPECT_TRUE(SetQuery<OutputKind>(&QuerySession::OutKind, OK_Dump).run(OS, S)); 94 EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); 95 96 EXPECT_TRUE(OS.str().find("FunctionDecl") != std::string::npos); 97 98 Str.clear(); 99 100 EXPECT_TRUE(SetQuery<bool>(&QuerySession::BindRoot, false).run(OS, S)); 101 EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); 102 103 EXPECT_TRUE(OS.str().find("No bindings.") != std::string::npos); 104 105 Str.clear(); 106 107 EXPECT_FALSE(MatchQuery(isArrow()).run(OS, S)); 108 109 EXPECT_EQ("Not a valid top-level matcher.\n", OS.str()); 110 } 111