blob: 9b10c55bde0c85dc2a55707d581ed8b915ef82c1 [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);
Argyrios Kyrtzidis119b7fe2009-06-25 22:15:12 +000091
92 // Safety check. Using an out-of-date AST file will only lead to crashes
93 // or incorrect results.
94 // FIXME: Check all the source files that make up the AST file.
95 const FileEntry *ASTFile = FileMgr.getFile(InFile);
96 if (File->getModificationTime() > ASTFile->getModificationTime()) {
97 llvm::errs() << "[" << InFile << "] Error: " <<
98 "Pointing at a source file which was modified after creating "
99 "the AST file\n";
100 return 1;
101 }
102
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000103 if (File == 0) {
104 llvm::errs() << "File '" << Filename << "' does not exist\n";
105 return 1;
106 }
107 unsigned Line = PointAtLocation[0].Line;
108 unsigned Col = PointAtLocation[0].Column;
109
110 SourceLocation Loc = AST->getSourceManager().getLocation(File, Line, Col);
111 if (Loc.isInvalid()) {
112 llvm::errs() << "[" << InFile << "] Error: " <<
113 "Couldn't resolve source location (invalid location)\n";
114 return 1;
115 }
116
117 llvm::tie(Point.D, Point.Node) =
118 ResolveLocationInAST(AST->getASTContext(), Loc);
119 if (Point.D == 0) {
120 llvm::errs() << "[" << InFile << "] Error: " <<
121 "Couldn't resolve source location (no declaration found)\n";
122 return 1;
123 }
124 }
125
126 if (Point.D) {
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000127 llvm::raw_ostream &OS = llvm::outs();
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000128 OS << "Declaration node at point: " << Point.D->getDeclKindName() << " ";
129 if (NamedDecl *ND = dyn_cast<NamedDecl>(Point.D))
130 OS << ND->getNameAsString();
131 OS << "\n";
132
133 if (Point.Node) {
134 OS << "Statement node at point: " << Point.Node->getStmtClassName()
135 << " ";
Chris Lattnere4f21422009-06-30 01:26:17 +0000136 Point.Node->printPretty(OS, AST->getASTContext(), 0,
137 PrintingPolicy(AST->getASTContext().getLangOptions()));
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000138 OS << "\n";
139 }
140 }
141
142 if (DisableFree)
143 AST.take();
144
145 // Managed static deconstruction. Useful for making things like
146 // -time-passes usable.
147 llvm::llvm_shutdown();
148
149 return 0;
150}