Introduce tools/index-test.

This tool will be the test bed for indexing related operations. It basically reads PCH files passed by the command line and performs various operations.

Currently it can accept a file:line:column which resolves to a declaration/statement and displays some information about them.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74198 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/Index/resolve-loc.c b/test/Index/resolve-loc.c
new file mode 100644
index 0000000..d04b79b
--- /dev/null
+++ b/test/Index/resolve-loc.c
@@ -0,0 +1,24 @@
+// RUN: clang-cc -emit-pch %s -o %t.ast &&
+// RUN: index-test %t.ast -point-at %s:15:8 | grep top_var &&
+// RUN: index-test %t.ast -point-at %s:17:15 | grep top_func_decl &&
+// RUN: index-test %t.ast -point-at %s:17:25 | grep param1 &&
+// RUN: index-test %t.ast -point-at %s:19:17 | grep top_func_def &&
+// RUN: index-test %t.ast -point-at %s:19:23 | grep param2 &&
+// RUN: index-test %t.ast -point-at %s:20:10 | grep local_var1 &&
+// RUN: index-test %t.ast -point-at %s:21:15 | grep for_var &&
+// RUN: index-test %t.ast -point-at %s:21:43 | grep top_func_def &&
+// RUN: index-test %t.ast -point-at %s:21:43 | grep '++for_var' &&
+// RUN: index-test %t.ast -point-at %s:22:9 | grep local_var2 &&
+// RUN: index-test %t.ast -point-at %s:22:30 | grep local_var2 &&
+// RUN: index-test %t.ast -point-at %s:22:30 | grep 'for_var + 1'
+
+int top_var;
+
+void top_func_decl(int param1);
+
+void top_func_def(int param2) {
+  int local_var1;
+  for (int for_var = 100; for_var < 500; ++for_var) {
+    int local_var2 = for_var + 1;
+  }
+}
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 6c66dea..fc30fb8 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -1,2 +1,3 @@
 add_subdirectory(clang-cc)
 add_subdirectory(driver)
+add_subdirectory(index-test)
diff --git a/tools/Makefile b/tools/Makefile
index 43124ba..5b49cc8 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -8,6 +8,6 @@
 ##===----------------------------------------------------------------------===##
 
 LEVEL := ../../..
-DIRS := clang-cc driver
+DIRS := clang-cc driver index-test
 
 include $(LEVEL)/Makefile.common
diff --git a/tools/index-test/CMakeLists.txt b/tools/index-test/CMakeLists.txt
new file mode 100644
index 0000000..09f4fc1
--- /dev/null
+++ b/tools/index-test/CMakeLists.txt
@@ -0,0 +1,17 @@
+set(LLVM_NO_RTTI 1)
+
+set( LLVM_USED_LIBS
+  clangFrontend
+  clangSema
+  clangAST
+  clangLex
+  clangBasic
+  )
+
+set( LLVM_LINK_COMPONENTS
+  bitreader
+  )
+
+add_clang_executable(index-test
+  index-test.cpp
+  )
diff --git a/tools/index-test/Makefile b/tools/index-test/Makefile
new file mode 100644
index 0000000..4fbde29
--- /dev/null
+++ b/tools/index-test/Makefile
@@ -0,0 +1,23 @@
+##===- tools/index-test/Makefile ---------------------------*- Makefile -*-===##
+# 
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+# 
+##===----------------------------------------------------------------------===##
+LEVEL = ../../../..
+
+TOOLNAME = index-test
+CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include
+CXXFLAGS = -fno-rtti
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+include $(LEVEL)/Makefile.config
+
+LINK_COMPONENTS := bitreader
+USEDLIBS = clangFrontend.a clangSema.a clangAST.a clangLex.a clangBasic.a 
+
+include $(LLVM_SRC_ROOT)/Makefile.rules
diff --git a/tools/index-test/index-test.cpp b/tools/index-test/index-test.cpp
new file mode 100644
index 0000000..0acaf78
--- /dev/null
+++ b/tools/index-test/index-test.cpp
@@ -0,0 +1,143 @@
+//===--- index-test.cpp - Indexing test bed -------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This utility may be invoked in the following manner:
+//   index-test --help                - Output help info.
+//   index-test [options]             - Read from stdin.
+//   index-test [options] file        - Read from "file".
+//   index-test [options] file1 file2 - Read these files.
+//
+//  Files must be AST files.
+//
+//===----------------------------------------------------------------------===//
+//
+//   -Wfatal-errors
+//   -ftabstop=width
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Frontend/ASTUnit.h"
+#include "clang/Frontend/Utils.h"
+#include "clang/Frontend/CommandLineSourceLoc.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/Stmt.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/System/Signals.h"
+using namespace clang;
+
+
+static llvm::cl::list<ParsedSourceLocation>
+PointAtLocation("point-at", llvm::cl::Optional,
+                 llvm::cl::value_desc("source-location"),
+   llvm::cl::desc("Point at the given source location of the first AST file"));
+
+static llvm::cl::opt<bool>
+DisableFree("disable-free",
+           llvm::cl::desc("Disable freeing of memory on exit"),
+           llvm::cl::init(false));
+
+static llvm::cl::list<std::string>
+InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
+
+int main(int argc, char **argv) {
+  llvm::sys::PrintStackTraceOnErrorSignal();
+  llvm::PrettyStackTraceProgram X(argc, argv);
+  llvm::cl::ParseCommandLineOptions(argc, argv,
+                     "LLVM 'Clang' Indexing Test Bed: http://clang.llvm.org\n");
+  
+  FileManager FileMgr;
+  
+  // If no input was specified, read from stdin.
+  if (InputFilenames.empty())
+    InputFilenames.push_back("-");
+
+  // FIXME: Only the first AST file is used for now.
+
+  const std::string &InFile = InputFilenames[0];
+  
+  std::string ErrMsg;
+  llvm::OwningPtr<ASTUnit> AST;
+
+  AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg));
+  if (!AST) {
+    llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n';
+    return 1;
+  }
+
+  struct ASTPoint {
+    Decl *D;
+    Stmt *Node;
+    ASTPoint() : D(0), Node(0) {}
+  };
+  
+  ASTPoint Point;
+
+  if (!PointAtLocation.empty()) {
+    const std::string &Filename = PointAtLocation[0].FileName;
+    const FileEntry *File = FileMgr.getFile(Filename);
+    if (File == 0) {
+      llvm::errs() << "File '" << Filename << "' does not exist\n";
+      return 1;
+    }
+    unsigned Line = PointAtLocation[0].Line;
+    unsigned Col = PointAtLocation[0].Column;
+
+    SourceLocation Loc = AST->getSourceManager().getLocation(File, Line, Col);
+    if (Loc.isInvalid()) {
+      llvm::errs() << "[" << InFile << "] Error: " <<
+        "Couldn't resolve source location (invalid location)\n";
+      return 1;
+    }
+    
+    llvm::tie(Point.D, Point.Node) =
+      ResolveLocationInAST(AST->getASTContext(), Loc);
+    if (Point.D == 0) {
+      llvm::errs() << "[" << InFile << "] Error: " <<
+        "Couldn't resolve source location (no declaration found)\n";
+      return 1;
+    }
+  }
+  
+  if (Point.D) {
+    if (PointAtLocation.empty()) {
+      llvm::errs() << "'-print-point-info' should be used together "
+                      "with '-point-at'\n";
+      return 1;
+    }
+    
+    llvm::raw_ostream &OS = llvm::outs();
+    assert(Point.D && "If no node was found we should have exited with error");
+    OS << "Declaration node at point: " << Point.D->getDeclKindName() << " ";
+    if (NamedDecl *ND = dyn_cast<NamedDecl>(Point.D))
+      OS << ND->getNameAsString();
+    OS << "\n";
+    
+    if (Point.Node) {
+      OS << "Statement node at point: " << Point.Node->getStmtClassName()
+         << " ";
+      Point.Node->printPretty(OS, AST->getASTContext());
+      OS << "\n";
+    }
+  }
+
+  if (DisableFree)
+    AST.take();
+
+  // Managed static deconstruction. Useful for making things like
+  // -time-passes usable.
+  llvm::llvm_shutdown();
+  
+  return 0;
+}