blob: 63d37b2baaf8d0cd2c1a111727d93a970085d5aa [file] [log] [blame]
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +00001//===--- index-test.cpp - Indexing test bed -------------------------------===//
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// This utility may be invoked in the following manner:
11// index-test --help - Output help info.
12// index-test [options] - Read from stdin.
13// index-test [options] file - Read from "file".
14// index-test [options] file1 file2 - Read these files.
15//
16// Files must be AST files.
17//
18//===----------------------------------------------------------------------===//
19//
Argyrios Kyrtzidis6e4a86d2009-06-25 21:54:50 +000020// -point-at [file:column:line]
21// Point at a declaration/statement/expression. If no other operation is
22// specified, prints some info about it.
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000023//
24//===----------------------------------------------------------------------===//
25
26#include "clang/Frontend/ASTUnit.h"
27#include "clang/Frontend/Utils.h"
28#include "clang/Frontend/CommandLineSourceLoc.h"
29#include "clang/AST/Decl.h"
30#include "clang/AST/Stmt.h"
31#include "clang/Basic/FileManager.h"
32#include "clang/Basic/SourceManager.h"
33#include "llvm/ADT/STLExtras.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/ManagedStatic.h"
36#include "llvm/Support/PrettyStackTrace.h"
37#include "llvm/Support/raw_ostream.h"
38#include "llvm/System/Signals.h"
39using namespace clang;
40
41
42static llvm::cl::list<ParsedSourceLocation>
43PointAtLocation("point-at", llvm::cl::Optional,
44 llvm::cl::value_desc("source-location"),
45 llvm::cl::desc("Point at the given source location of the first AST file"));
46
47static llvm::cl::opt<bool>
48DisableFree("disable-free",
49 llvm::cl::desc("Disable freeing of memory on exit"),
50 llvm::cl::init(false));
51
52static llvm::cl::list<std::string>
53InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
54
55int main(int argc, char **argv) {
56 llvm::sys::PrintStackTraceOnErrorSignal();
57 llvm::PrettyStackTraceProgram X(argc, argv);
58 llvm::cl::ParseCommandLineOptions(argc, argv,
59 "LLVM 'Clang' Indexing Test Bed: http://clang.llvm.org\n");
60
61 FileManager FileMgr;
62
63 // If no input was specified, read from stdin.
64 if (InputFilenames.empty())
65 InputFilenames.push_back("-");
66
67 // FIXME: Only the first AST file is used for now.
68
69 const std::string &InFile = InputFilenames[0];
70
71 std::string ErrMsg;
72 llvm::OwningPtr<ASTUnit> AST;
73
74 AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg));
75 if (!AST) {
76 llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n';
77 return 1;
78 }
79
80 struct ASTPoint {
81 Decl *D;
82 Stmt *Node;
83 ASTPoint() : D(0), Node(0) {}
84 };
85
86 ASTPoint Point;
87
88 if (!PointAtLocation.empty()) {
89 const std::string &Filename = PointAtLocation[0].FileName;
90 const FileEntry *File = FileMgr.getFile(Filename);
91 if (File == 0) {
92 llvm::errs() << "File '" << Filename << "' does not exist\n";
93 return 1;
94 }
95 unsigned Line = PointAtLocation[0].Line;
96 unsigned Col = PointAtLocation[0].Column;
97
98 SourceLocation Loc = AST->getSourceManager().getLocation(File, Line, Col);
99 if (Loc.isInvalid()) {
100 llvm::errs() << "[" << InFile << "] Error: " <<
101 "Couldn't resolve source location (invalid location)\n";
102 return 1;
103 }
104
105 llvm::tie(Point.D, Point.Node) =
106 ResolveLocationInAST(AST->getASTContext(), Loc);
107 if (Point.D == 0) {
108 llvm::errs() << "[" << InFile << "] Error: " <<
109 "Couldn't resolve source location (no declaration found)\n";
110 return 1;
111 }
112 }
113
114 if (Point.D) {
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000115 llvm::raw_ostream &OS = llvm::outs();
116 assert(Point.D && "If no node was found we should have exited with error");
117 OS << "Declaration node at point: " << Point.D->getDeclKindName() << " ";
118 if (NamedDecl *ND = dyn_cast<NamedDecl>(Point.D))
119 OS << ND->getNameAsString();
120 OS << "\n";
121
122 if (Point.Node) {
123 OS << "Statement node at point: " << Point.Node->getStmtClassName()
124 << " ";
125 Point.Node->printPretty(OS, AST->getASTContext());
126 OS << "\n";
127 }
128 }
129
130 if (DisableFree)
131 AST.take();
132
133 // Managed static deconstruction. Useful for making things like
134 // -time-passes usable.
135 llvm::llvm_shutdown();
136
137 return 0;
138}