diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2013-11-08 00:08:23 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2013-11-08 00:08:23 +0000 |
commit | 8b1265b353b027d90252f1a23c5c3121ee70f3a1 (patch) | |
tree | 4d104d38960d01338209c32d106c9b6a764976e7 /clang-tools-extra/unittests/clang-query/QueryEngineTest.cpp | |
parent | 640830142100ea828ba54b42c68ccc4b3dd1d8ea (diff) | |
download | bcm5719-llvm-8b1265b353b027d90252f1a23c5c3121ee70f3a1.tar.gz bcm5719-llvm-8b1265b353b027d90252f1a23c5c3121ee70f3a1.zip |
Introduce clang-query tool.
This tool is for interactive exploration of the Clang AST using AST matchers.
It currently allows the user to enter a matcher at an interactive prompt
and view the resulting bindings as diagnostics, AST pretty prints or AST
dumps. Example session:
$ cat foo.c
void foo(void) {}
$ clang-query foo.c --
clang-query> match functionDecl()
Match #1:
foo.c:1:1: note: "root" binds here
void foo(void) {}
^~~~~~~~~~~~~~~~~
1 match.
Differential Revision: http://llvm-reviews.chandlerc.com/D2098
llvm-svn: 194227
Diffstat (limited to 'clang-tools-extra/unittests/clang-query/QueryEngineTest.cpp')
-rw-r--r-- | clang-tools-extra/unittests/clang-query/QueryEngineTest.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/clang-tools-extra/unittests/clang-query/QueryEngineTest.cpp b/clang-tools-extra/unittests/clang-query/QueryEngineTest.cpp new file mode 100644 index 00000000000..266743ddf70 --- /dev/null +++ b/clang-tools-extra/unittests/clang-query/QueryEngineTest.cpp @@ -0,0 +1,110 @@ +//===---- QueryTest.cpp - clang-query test --------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "Query.h" +#include "QuerySession.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/Dynamic/VariantValue.h" +#include "clang/Frontend/ASTUnit.h" +#include "clang/Tooling/Tooling.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" +#include <string> + +using namespace clang; +using namespace clang::ast_matchers; +using namespace clang::ast_matchers::dynamic; +using namespace clang::query; +using namespace clang::tooling; + +TEST(Query, Basic) { + OwningPtr<ASTUnit> FooAST( + buildASTFromCode("void foo1(void) {}\nvoid foo2(void) {}", "foo.cc")); + ASSERT_TRUE(FooAST.get()); + OwningPtr<ASTUnit> BarAST( + buildASTFromCode("void bar1(void) {}\nvoid bar2(void) {}", "bar.cc")); + ASSERT_TRUE(BarAST.get()); + + ASTUnit *ASTs[] = { FooAST.get(), BarAST.get() }; + + std::string Str; + llvm::raw_string_ostream OS(Str); + QuerySession S(ASTs); + + DynTypedMatcher FnMatcher = functionDecl(); + DynTypedMatcher FooMatcher = functionDecl(hasName("foo1")); + + EXPECT_TRUE(NoOpQuery().run(OS, S)); + + EXPECT_EQ("", OS.str()); + + Str.clear(); + + EXPECT_FALSE(InvalidQuery("Parse error").run(OS, S)); + + EXPECT_EQ("Parse error\n", OS.str()); + + Str.clear(); + + EXPECT_TRUE(HelpQuery().run(OS, S)); + + EXPECT_TRUE(OS.str().find("Available commands:") != std::string::npos); + + Str.clear(); + + EXPECT_TRUE(MatchQuery(FnMatcher).run(OS, S)); + + EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") != + std::string::npos); + EXPECT_TRUE(OS.str().find("foo.cc:2:1: note: \"root\" binds here") != + std::string::npos); + EXPECT_TRUE(OS.str().find("bar.cc:1:1: note: \"root\" binds here") != + std::string::npos); + EXPECT_TRUE(OS.str().find("bar.cc:2:1: note: \"root\" binds here") != + std::string::npos); + EXPECT_TRUE(OS.str().find("4 matches.") != std::string::npos); + + Str.clear(); + + EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); + + EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") != + std::string::npos); + EXPECT_TRUE(OS.str().find("1 match.") != std::string::npos); + + Str.clear(); + + EXPECT_TRUE( + SetQuery<OutputKind>(&QuerySession::OutKind, OK_Print).run(OS, S)); + EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); + + EXPECT_TRUE(OS.str().find("Binding for \"root\":\nvoid foo1()") != + std::string::npos); + + Str.clear(); + + EXPECT_TRUE(SetQuery<OutputKind>(&QuerySession::OutKind, OK_Dump).run(OS, S)); + EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); + + EXPECT_TRUE(OS.str().find("FunctionDecl") != std::string::npos); + + Str.clear(); + + EXPECT_TRUE(SetQuery<bool>(&QuerySession::BindRoot, false).run(OS, S)); + EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S)); + + EXPECT_TRUE(OS.str().find("No bindings.") != std::string::npos); + + Str.clear(); + + EXPECT_FALSE(MatchQuery(isArrow()).run(OS, S)); + + EXPECT_EQ("Not a valid top-level matcher.\n", OS.str()); +} |