[cfe-commits] r74198 - in /cfe/trunk: test/Index/ test/Index/resolve-loc.c tools/CMakeLists.txt tools/Makefile tools/index-test/ tools/index-test/CMakeLists.txt tools/index-test/Makefile tools/index-test/index-test.cpp

Douglas Gregor dgregor at apple.com
Thu Jun 25 15:43:38 PDT 2009


On Jun 25, 2009, at 11:22 AM, Argiris Kirtzidis wrote:

> Author: akirtzidis
> Date: Thu Jun 25 13:22:52 2009
> New Revision: 74198
>
> URL: http://llvm.org/viewvc/llvm-project?rev=74198&view=rev
> Log:
> 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.

Very cool.


> Added:
>    cfe/trunk/test/Index/
>    cfe/trunk/test/Index/resolve-loc.c
>    cfe/trunk/tools/index-test/
>    cfe/trunk/tools/index-test/CMakeLists.txt
>    cfe/trunk/tools/index-test/Makefile
>    cfe/trunk/tools/index-test/index-test.cpp
> Modified:
>    cfe/trunk/tools/CMakeLists.txt
>    cfe/trunk/tools/Makefile
>
> Added: cfe/trunk/test/Index/resolve-loc.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/resolve-loc.c?rev=74198&view=auto
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/test/Index/resolve-loc.c (added)
> +++ cfe/trunk/test/Index/resolve-loc.c Thu Jun 25 13:22:52 2009
> @@ -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;
> +  }
> +}
>
> Modified: cfe/trunk/tools/CMakeLists.txt
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CMakeLists.txt?rev=74198&r1=74197&r2=74198&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/tools/CMakeLists.txt (original)
> +++ cfe/trunk/tools/CMakeLists.txt Thu Jun 25 13:22:52 2009
> @@ -1,2 +1,3 @@
> add_subdirectory(clang-cc)
> add_subdirectory(driver)
> +add_subdirectory(index-test)
>
> Modified: cfe/trunk/tools/Makefile
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/Makefile?rev=74198&r1=74197&r2=74198&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/tools/Makefile (original)
> +++ cfe/trunk/tools/Makefile Thu Jun 25 13:22:52 2009
> @@ -8,6 +8,6 @@
> ##= 
> = 
> = 
> ----------------------------------------------------------------------= 
> ==##
>
> LEVEL := ../../..
> -DIRS := clang-cc driver
> +DIRS := clang-cc driver index-test
>
> include $(LEVEL)/Makefile.common
>
> Added: cfe/trunk/tools/index-test/CMakeLists.txt
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/index-test/CMakeLists.txt?rev=74198&view=auto
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/tools/index-test/CMakeLists.txt (added)
> +++ cfe/trunk/tools/index-test/CMakeLists.txt Thu Jun 25 13:22:52 2009
> @@ -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
> +  )
>
> Added: cfe/trunk/tools/index-test/Makefile
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/index-test/Makefile?rev=74198&view=auto
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/tools/index-test/Makefile (added)
> +++ cfe/trunk/tools/index-test/Makefile Thu Jun 25 13:22:52 2009
> @@ -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
>
> Added: cfe/trunk/tools/index-test/index-test.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/index-test/index-test.cpp?rev=74198&view=auto
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/tools/index-test/index-test.cpp (added)
> +++ cfe/trunk/tools/index-test/index-test.cpp Thu Jun 25 13:22:52 2009
> @@ -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;
> +  }

It's great that this is all the code it takes to load in a PCH file  
and start making queries.

> +  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);

Have you thought about what happens when the location points to a  
macro invocation?
(I know that's something to be dealt with "later")

> +    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";
> +    }
> +  }

This looks like a good test hardness. Great!

   - Doug



More information about the cfe-commits mailing list