Index: tools/clang/test/CMakeLists.txt
===================================================================
--- tools/clang/test/CMakeLists.txt	(revision 160133)
+++ tools/clang/test/CMakeLists.txt	(working copy)
@@ -31,7 +31,7 @@
   set(CLANG_TEST_DEPS
     clang clang-headers
     c-index-test diagtool arcmt-test c-arcmt-test
-    clang-check
+    clang-check clang-ast-dump
     llvm-dis llc opt FileCheck count not
     )
   set(CLANG_TEST_PARAMS
@@ -80,7 +80,7 @@
       COMMENT "Running Clang regression tests"
       DEPENDS clang clang-headers
               c-index-test diagtool arcmt-test c-arcmt-test
-              clang-check
+              clang-check clang-ast-dump
       )
     set_target_properties(check-clang PROPERTIES FOLDER "Clang tests")
   endif()
Index: tools/clang/test/Tooling/clang-ast-dump.cpp
===================================================================
--- tools/clang/test/Tooling/clang-ast-dump.cpp	(revision 0)
+++ tools/clang/test/Tooling/clang-ast-dump.cpp	(revision 0)
@@ -0,0 +1,43 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: echo '[{"directory":".","command":"clang++ -c %t/test.cpp","file":"%t/test.cpp"}]' | sed -e 's/\\/\//g' > %t/compile_commands.json
+// RUN: cp "%s" "%t/test.cpp"
+// RUN: clang-ast-dump "%t/test.cpp" -f test_namespace::TheClass::theMethod 2>&1|FileCheck %s
+// FIXME: Make the above easier.
+
+// CHECK: Dumping test_namespace::TheClass::theMethod:
+// CHECK-NEXT: <CXXMethod ptr="0x{{[0-9a-f]+}}" name="theMethod" prototype="true">
+// CHECK-NEXT:  <FunctionProtoType ptr="0x{{[0-9a-f]+}}" canonical="0x{{[0-9a-f]+}}">
+// CHECK-NEXT:   <BuiltinType ptr="0x{{[0-9a-f]+}}" canonical="0x{{[0-9a-f]+}}"/>
+// CHECK-NEXT:   <parameters>
+// CHECK-NEXT:    <BuiltinType ptr="0x{{[0-9a-f]+}}" canonical="0x{{[0-9a-f]+}}"/>
+// CHECK-NEXT:   </parameters>
+// CHECK-NEXT:  </FunctionProtoType>
+// CHECK-NEXT:  <ParmVar ptr="0x{{[0-9a-f]+}}" name="x" initstyle="c">
+// CHECK-NEXT:   <BuiltinType ptr="0x{{[0-9a-f]+}}" canonical="0x{{[0-9a-f]+}}"/>
+// CHECK-NEXT:  </ParmVar>
+// CHECK-NEXT:  <Stmt>
+// CHECK-NEXT: (CompoundStmt 0x{{[0-9a-f]+}} <{{.+}}test.cpp:35:24, line:37:3>
+// CHECK-NEXT:   (ReturnStmt 0x{{[0-9a-f]+}} <line:36:5, col:16>
+// CHECK-NEXT:     (BinaryOperator 0x{{[0-9a-f]+}} <col:12, col:16> 'int' '+'
+// CHECK-NEXT:       (ImplicitCastExpr 0x{{[0-9a-f]+}} <col:12> 'int' <LValueToRValue>
+// CHECK-NEXT:         (DeclRefExpr 0x{{[0-9a-f]+}} <col:12> 'int' lvalue ParmVar 0x{{[0-9a-f]+}} 'x' 'int'))
+// CHECK-NEXT:       (ImplicitCastExpr 0x{{[0-9a-f]+}} <col:16> 'int' <LValueToRValue>
+// CHECK-NEXT:         (DeclRefExpr 0x{{[0-9a-f]+}} <col:16> 'int' lvalue ParmVar 0x{{[0-9a-f]+}} 'x' 'int')))))
+//
+// CHECK:  </Stmt>
+// CHECK-NEXT: </CXXMethod>
+
+namespace test_namespace {
+
+class TheClass {
+public:
+  int theMethod(int x) {
+    return x + x;
+  }
+};
+
+}
+
+// FIXME: This is incompatible to -fms-compatibility.
+// XFAIL: win32
Index: tools/clang/include/clang/Tooling/CommandLineClangTool.h
===================================================================
--- tools/clang/include/clang/Tooling/CommandLineClangTool.h	(revision 0)
+++ tools/clang/include/clang/Tooling/CommandLineClangTool.h	(revision 0)
@@ -0,0 +1,53 @@
+//===- CommandLineClangTool.h - command-line clang tools driver -*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements the CommandLineClangTool class used to run clang
+//  tools as separate command-line applications with a consistent common
+//  interface for handling compilation database and input files.
+//
+//  It provides a common subset of command-line options, common algorithm
+//  for locating a compilation database and source files, and help messages
+//  for the basic command-line interface.
+//
+//  It creates a CompilationDatabase, initializes a ClangTool and runs a
+//  user-specified FrontendAction over all TUs in which the given files are
+//  compiled.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMANDLINECLANGTOOL_H
+#define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMANDLINECLANGTOOL_H
+
+#include "llvm/Support/CommandLine.h"
+
+namespace clang {
+
+namespace tooling {
+
+class CompilationDatabase;
+class FrontendActionFactory;
+
+class CommandLineClangTool {
+  CompilationDatabase *Compilations;
+  llvm::cl::opt<std::string> BuildPath;
+  llvm::cl::list<std::string> SourcePaths;
+  llvm::cl::extrahelp MoreHelp;
+
+public:
+  CommandLineClangTool();
+  ~CommandLineClangTool();
+  void initialize(int argc, const char **argv);
+  int run(FrontendActionFactory *ActionFactory);
+};
+
+} // namespace tooling
+
+} // namespace clang
+
+#endif  // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMANDLINECLANGTOOL_H
Index: tools/clang/tools/clang-check/ClangCheck.cpp
===================================================================
--- tools/clang/tools/clang-check/ClangCheck.cpp	(revision 160133)
+++ tools/clang/tools/clang-check/ClangCheck.cpp	(working copy)
@@ -1,4 +1,4 @@
-//===- examples/Tooling/ClangCheck.cpp - Clang check tool -----------------===//
+//===- tools/clang-check/ClangCheck.cpp - Clang check tool ----------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -10,75 +10,37 @@
 //  This file implements a clang-check tool that runs the
 //  clang::SyntaxOnlyAction over a number of translation units.
 //
+//  This tool uses the Clang Tooling infrastructure, see
+//    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
+//  for details on setting it up with LLVM source tree.
+//
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/CommandLine.h"
 #include "clang/Frontend/FrontendActions.h"
-#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/CommandLineClangTool.h"
 #include "clang/Tooling/Tooling.h"
 
 using namespace clang::tooling;
 using namespace llvm;
 
-cl::opt<std::string> BuildPath(
-  "p",
-  cl::desc("<build-path>"),
-  cl::Optional);
-
-cl::list<std::string> SourcePaths(
-  cl::Positional,
-  cl::desc("<source0> [... <sourceN>]"),
-  cl::OneOrMore);
-
-static cl::extrahelp MoreHelp(
+static const char *MoreHelpText =
+    "\tFor example, to run clang-check on all files in a subtree of the\n"
+    "\tsource tree, use:\n"
     "\n"
-    "<build-path> is used to read a compile command database.\n"
+    "\t  find path/in/subtree -name '*.cpp'|xargs clang-check\n"
     "\n"
-    "For example, it can be a CMake build directory in which a file named\n"
-    "compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n"
-    "CMake option to get this output). When no build path is specified,\n"
-    "clang-check will attempt to locate it automatically using all parent\n"
-    "paths of the first input file.\n"
+    "\tor using a specific build path:\n"
     "\n"
-    "<source0> ... specify the paths of source files. These paths are looked\n"
-    "up in the compile command database. If the path of a file is absolute,\n"
-    "it needs to point into CMake's source tree. If the path is relative,\n"
-    "the current working directory needs to be in the CMake source tree and\n"
-    "the file must be in a subdirectory of the current working directory.\n"
-    "\"./\" prefixes in the relative files will be automatically removed,\n"
-    "but the rest of a relative path must be a suffix of a path in the\n"
-    "compile command database.\n"
+    "\t  find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
     "\n"
-    "For example, to use clang-check on all files in a subtree of the source\n"
-    "tree, use:\n"
-    "\n"
-    "  find path/in/subtree -name '*.cpp'|xargs clang-check\n"
-    "\n"
-    "or using a specific build path:\n"
-    "\n"
-    "  find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
-    "\n"
-    "Note, that path/in/subtree and current directory should follow the\n"
-    "rules described above.\n"
-    "\n"
-);
+    "\tNote, that path/in/subtree and current directory should follow the\n"
+    "\trules described above.\n"
+    "\n";
 
 int main(int argc, const char **argv) {
-  llvm::OwningPtr<CompilationDatabase> Compilations(
-    FixedCompilationDatabase::loadFromCommandLine(argc, argv));
-  cl::ParseCommandLineOptions(argc, argv);
-  if (!Compilations) {
-    std::string ErrorMessage;
-    if (!BuildPath.empty()) {
-      Compilations.reset(
-         CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
-    } else {
-      Compilations.reset(CompilationDatabase::autoDetectFromSource(
-          SourcePaths[0], ErrorMessage));
-    }
-    if (!Compilations)
-      llvm::report_fatal_error(ErrorMessage);
-  }
-  ClangTool Tool(*Compilations, SourcePaths);
+  CommandLineClangTool Tool;
+  cl::extrahelp MoreHelp(MoreHelpText);
+  Tool.initialize(argc, argv);
   return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
 }
Index: tools/clang/tools/clang-ast-dump/ClangASTDump.cpp
===================================================================
--- tools/clang/tools/clang-ast-dump/ClangASTDump.cpp	(revision 0)
+++ tools/clang/tools/clang-ast-dump/ClangASTDump.cpp	(revision 0)
@@ -0,0 +1,141 @@
+//===- tools/clang-ast-dump/ClangASTDump.cpp - Clang AST Dump tool --------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements a clang-ast-dump tool that dumps specified parts
+//  of an AST of a number of translation units.
+//
+//  Run with '-help' for details.
+//
+//  This tool uses the Clang Tooling infrastructure, see
+//    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
+//  for details on setting it up with LLVM source tree.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CommandLine.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Tooling/CommandLineClangTool.h"
+#include "clang/Tooling/Tooling.h"
+
+using namespace clang::tooling;
+using namespace llvm;
+
+cl::opt<std::string> FilterString(
+  "f",
+  cl::desc("Filter string"),
+  cl::Optional);
+
+cl::opt<bool> ListAll(
+  "l",
+  cl::desc("List all filterable nodes"),
+  cl::init(false),
+  cl::Optional);
+
+static const char *MoreHelpText =
+    "-f <filter-string> can be used to dump only AST declaration nodes having\n"
+    "\ta certain substring in a qualified name.\n"
+    "\n"
+    "-l \tlists qualified names of all filterable declaration nodes.\n"
+    "\n";
+
+namespace {
+
+using namespace clang;
+
+class SelectiveDumpVisitor :
+     public RecursiveASTVisitor<SelectiveDumpVisitor> {
+  typedef RecursiveASTVisitor<SelectiveDumpVisitor> base;
+public:
+  SelectiveDumpVisitor(const std::string &FilterString, bool ListAll)
+      : FilterString(FilterString), ListAll(ListAll) {}
+
+  ASTConsumer* newASTConsumer() {
+    return new DumpConsumer(this);
+  }
+
+  bool shouldWalkTypesOfTypeLocs() const { return false; }
+
+  void Run(TranslationUnitDecl *D) {
+    if (ListAll) {
+      llvm::outs().changeColor(llvm::raw_ostream::BLUE) <<
+          "Listing all filterable nodes:\n";
+      llvm::outs().resetColor();
+      TraverseDecl(D);
+      return;
+    }
+
+    if (FilterString.empty()) {
+      llvm::outs().changeColor(llvm::raw_ostream::BLUE) <<
+          "Dumping translation unit:\n";
+      llvm::outs().resetColor();
+      D->dumpXML(llvm::outs());
+      return;
+    }
+
+    TraverseDecl(D);
+  }
+
+  bool TraverseDecl(Decl *D) {
+    if (ListAll) {
+      std::string Name = getName(D);
+      if (!Name.empty())
+        llvm::outs() << Name << "\n";
+      return base::TraverseDecl(D);
+    }
+
+    if (filterMatches(D)) {
+      llvm::outs().changeColor(llvm::raw_ostream::BLUE) <<
+          "Dumping " << getName(D) << ":\n";
+      llvm::outs().resetColor();
+      D->dumpXML(llvm::outs());
+      return true;
+    }
+    return base::TraverseDecl(D);
+  }
+
+private:
+  std::string getName(Decl *D) {
+    if (isa<NamedDecl>(D))
+      return cast<NamedDecl>(D)->getQualifiedNameAsString();
+    return "";
+  }
+  bool filterMatches(Decl *D) {
+    return getName(D).find(FilterString) != std::string::npos;
+  }
+
+  class DumpConsumer : public ASTConsumer {
+  public:
+    DumpConsumer(SelectiveDumpVisitor *Visitor) : Visitor(Visitor) {}
+
+    virtual void HandleTranslationUnit(ASTContext &Context) {
+      Visitor->Context = &Context;
+      Visitor->Run(Context.getTranslationUnitDecl());
+    }
+
+  private:
+    SelectiveDumpVisitor *Visitor;
+  };
+
+  ASTContext *Context;
+  std::string FilterString;
+  bool ListAll;
+};
+
+} // namespace
+
+int main(int argc, const char **argv) {
+  CommandLineClangTool Tool;
+  cl::extrahelp MoreHelp(MoreHelpText);
+  Tool.initialize(argc, argv);
+  SelectiveDumpVisitor Dumper(FilterString, ListAll);
+  return Tool.run(newFrontendActionFactory(&Dumper));
+}
Index: tools/clang/tools/clang-ast-dump/CMakeLists.txt
===================================================================
--- tools/clang/tools/clang-ast-dump/CMakeLists.txt	(revision 0)
+++ tools/clang/tools/clang-ast-dump/CMakeLists.txt	(revision 0)
@@ -0,0 +1,8 @@
+add_clang_executable(clang-ast-dump
+  ClangASTDump.cpp
+  )
+
+target_link_libraries(clang-ast-dump
+  clangTooling
+  clangBasic
+  )
Index: tools/clang/tools/clang-ast-dump/Makefile
===================================================================
--- tools/clang/tools/clang-ast-dump/Makefile	(revision 0)
+++ tools/clang/tools/clang-ast-dump/Makefile	(revision 0)
@@ -0,0 +1,23 @@
+##===- tools/clang-check/Makefile --------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../..
+
+TOOLNAME = clang-ast-dump
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+LINK_COMPONENTS := support mc
+USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a \
+           clangTooling.a clangParse.a clangSema.a clangAnalysis.a \
+           clangEdit.a clangAST.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile
Index: tools/clang/tools/CMakeLists.txt
===================================================================
--- tools/clang/tools/CMakeLists.txt	(revision 160133)
+++ tools/clang/tools/CMakeLists.txt	(working copy)
@@ -5,3 +5,4 @@
 add_subdirectory(diagtool)
 add_subdirectory(driver)
 add_subdirectory(clang-check)
+add_subdirectory(clang-ast-dump)
Index: tools/clang/tools/Makefile
===================================================================
--- tools/clang/tools/Makefile	(revision 160133)
+++ tools/clang/tools/Makefile	(working copy)
@@ -9,7 +9,7 @@
 
 CLANG_LEVEL := ..
 DIRS := driver libclang c-index-test arcmt-test c-arcmt-test diagtool \
-        clang-check
+        clang-check clang-ast-dump
 
 include $(CLANG_LEVEL)/../../Makefile.config
 
Index: tools/clang/lib/Tooling/CommandLineClangTool.cpp
===================================================================
--- tools/clang/lib/Tooling/CommandLineClangTool.cpp	(revision 0)
+++ tools/clang/lib/Tooling/CommandLineClangTool.cpp	(revision 0)
@@ -0,0 +1,85 @@
+//===--- CommandLineClangTool.cpp - command-line clang tools driver -------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements the CommandLineClangTool class used to run clang
+//  tools as separate command-line applications with a consistent common
+//  interface for handling compilation database and input files.
+//
+//  It provides a common subset of command-line options, common algorithm
+//  for locating a compilation database and source files, and help messages
+//  for the basic command-line interface.
+//
+//  It creates a CompilationDatabase, initializes a ClangTool and runs a
+//  user-specified FrontendAction over all TUs in which the given files are
+//  compiled.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Tooling/CommandLineClangTool.h"
+#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/Tooling.h"
+
+using namespace clang::tooling;
+using namespace llvm;
+
+static const char *MoreHelpText =
+    "\n"
+    "-p <build-path> is used to read a compile command database.\n"
+    "\n"
+    "\tFor example, it can be a CMake build directory in which a file named\n"
+    "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n"
+    "\tCMake option to get this output). When no build path is specified,\n"
+    "\tclang-check will attempt to locate it automatically using all parent\n"
+    "\tpaths of the first input file.\n"
+    "\n"
+    "<source0> ... specify the paths of source files. These paths are looked\n"
+    "\tup in the compile command database. If the path of a file is absolute,\n"
+    "\tit needs to point into CMake's source tree. If the path is relative,\n"
+    "\tthe current working directory needs to be in the CMake source tree and\n"
+    "\tthe file must be in a subdirectory of the current working directory.\n"
+    "\t\"./\" prefixes in the relative files will be automatically removed,\n"
+    "\tbut the rest of a relative path must be a suffix of a path in the\n"
+    "\tcompile command database.\n"
+    "\n";
+
+CommandLineClangTool::CommandLineClangTool() :
+    Compilations(NULL),
+    BuildPath("p", cl::desc("Build path"), cl::Optional),
+    SourcePaths(cl::Positional, cl::desc("<source0> [... <sourceN>]"),
+                cl::OneOrMore),
+    MoreHelp(MoreHelpText) {
+}
+
+void CommandLineClangTool::initialize(int argc, const char **argv) {
+  Compilations = FixedCompilationDatabase::loadFromCommandLine(argc, argv);
+  cl::ParseCommandLineOptions(argc, argv);
+  if (!Compilations) {
+    std::string ErrorMessage;
+    if (!BuildPath.empty()) {
+      Compilations = CompilationDatabase::autoDetectFromDirectory(BuildPath,
+                                                                  ErrorMessage);
+    } else {
+      Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0],
+                                                               ErrorMessage);
+    }
+    if (!Compilations)
+      llvm::report_fatal_error(ErrorMessage);
+  }
+}
+
+CommandLineClangTool::~CommandLineClangTool() {
+  delete Compilations;
+  Compilations = NULL;
+}
+
+int CommandLineClangTool::run(FrontendActionFactory *ActionFactory) {
+  ClangTool Tool(*Compilations, SourcePaths);
+  return Tool.run(ActionFactory);
+}
Index: tools/clang/lib/Tooling/CMakeLists.txt
===================================================================
--- tools/clang/lib/Tooling/CMakeLists.txt	(revision 160133)
+++ tools/clang/lib/Tooling/CMakeLists.txt	(working copy)
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTooling
+  CommandLineClangTool.cpp
   CompilationDatabase.cpp
   Refactoring.cpp
   Tooling.cpp
