blob: 037221fcef5228c8127bcabe4fbf6c997e95e597 [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//
Zhongxing Xu96bac6b2009-07-07 07:14:55 +000020// -point-at [file:line:column]
Argyrios Kyrtzidis6e4a86d2009-06-25 21:54:50 +000021// 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//
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000024// -print-refs
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000025// Print ASTLocations that reference the -point-at node
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000026//
27// -print-defs
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000028// Print ASTLocations that define the -point-at node
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000029//
30// -print-decls
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000031// Print ASTLocations that declare the -point-at node
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000032//
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000033//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000035#include "clang/Index/Program.h"
Argyrios Kyrtzidisb17dc462009-07-29 23:38:21 +000036#include "clang/Index/Indexer.h"
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000037#include "clang/Index/Entity.h"
38#include "clang/Index/TranslationUnit.h"
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +000039#include "clang/Index/ASTLocation.h"
40#include "clang/Index/DeclReferenceMap.h"
Argyrios Kyrtzidis0f398482009-07-29 23:41:08 +000041#include "clang/Index/SelectorMap.h"
Argyrios Kyrtzidis94431b52009-07-29 23:39:03 +000042#include "clang/Index/Handlers.h"
Argyrios Kyrtzidis7f4656e2009-07-29 23:40:14 +000043#include "clang/Index/Analyzer.h"
Argyrios Kyrtzidis818e15b2009-07-06 21:35:02 +000044#include "clang/Index/Utils.h"
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000045#include "clang/Frontend/ASTUnit.h"
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000046#include "clang/Frontend/CommandLineSourceLoc.h"
47#include "clang/AST/Decl.h"
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000048#include "clang/AST/Expr.h"
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000049#include "clang/Basic/FileManager.h"
50#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000051#include "llvm/Support/CommandLine.h"
52#include "llvm/Support/ManagedStatic.h"
53#include "llvm/Support/PrettyStackTrace.h"
54#include "llvm/Support/raw_ostream.h"
55#include "llvm/System/Signals.h"
56using namespace clang;
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000057using namespace idx;
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000058
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000059class TUnit : public TranslationUnit {
60public:
61 TUnit(ASTUnit *ast, const std::string &filename)
Argyrios Kyrtzidis0f398482009-07-29 23:41:08 +000062 : AST(ast), Filename(filename),
63 DeclRefMap(ast->getASTContext()),
64 SelMap(ast->getASTContext()) { }
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000065
66 virtual ASTContext &getASTContext() { return AST->getASTContext(); }
Argyrios Kyrtzidis768a6c92009-07-29 23:39:18 +000067 virtual DeclReferenceMap &getDeclReferenceMap() { return DeclRefMap; }
Argyrios Kyrtzidis0f398482009-07-29 23:41:08 +000068 virtual SelectorMap &getSelectorMap() { return SelMap; }
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000069
70 llvm::OwningPtr<ASTUnit> AST;
71 std::string Filename;
Argyrios Kyrtzidis768a6c92009-07-29 23:39:18 +000072 DeclReferenceMap DeclRefMap;
Argyrios Kyrtzidis0f398482009-07-29 23:41:08 +000073 SelectorMap SelMap;
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000074};
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +000075
76static llvm::cl::list<ParsedSourceLocation>
77PointAtLocation("point-at", llvm::cl::Optional,
78 llvm::cl::value_desc("source-location"),
79 llvm::cl::desc("Point at the given source location of the first AST file"));
80
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +000081enum ProgActions {
82 PrintPoint, // Just print the point-at node
83 PrintRefs, // Print references of the point-at node
84 PrintDefs, // Print definitions of the point-at node
85 PrintDecls // Print declarations of the point-at node
86};
87
88static llvm::cl::opt<ProgActions>
89ProgAction(
90 llvm::cl::desc("Choose action to perform on the pointed-at AST node:"),
91 llvm::cl::ZeroOrMore,
92 llvm::cl::init(PrintPoint),
93 llvm::cl::values(
94 clEnumValN(PrintRefs, "print-refs",
95 "Print references"),
96 clEnumValN(PrintDefs, "print-defs",
97 "Print definitions"),
98 clEnumValN(PrintDecls, "print-decls",
99 "Print declarations"),
100 clEnumValEnd));
101
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000102static llvm::cl::opt<bool>
103DisableFree("disable-free",
104 llvm::cl::desc("Disable freeing of memory on exit"),
105 llvm::cl::init(false));
106
Argyrios Kyrtzidis12585542009-07-14 03:18:40 +0000107static bool HadErrors = false;
108
Argyrios Kyrtzidisb17dc462009-07-29 23:38:21 +0000109static void ProcessASTLocation(ASTLocation ASTLoc, Indexer &Idxer) {
Argyrios Kyrtzidis44994052009-07-06 21:35:26 +0000110 assert(ASTLoc.isValid());
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000111
Argyrios Kyrtzidisdc50c642009-07-18 21:17:58 +0000112 Decl *D = ASTLoc.getReferencedDecl();
Argyrios Kyrtzidis12585542009-07-14 03:18:40 +0000113 if (D == 0) {
Argyrios Kyrtzidisec930d32009-07-21 00:05:10 +0000114 llvm::errs() << "Error: Couldn't get referenced Decl for the ASTLocation\n";
Argyrios Kyrtzidis12585542009-07-14 03:18:40 +0000115 HadErrors = true;
116 return;
117 }
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000118
Argyrios Kyrtzidis7f4656e2009-07-29 23:40:14 +0000119 llvm::raw_ostream &OS = llvm::outs();
120 typedef Storing<TULocationHandler> ResultsTy;
121 ResultsTy Results;
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000122
Argyrios Kyrtzidis7f4656e2009-07-29 23:40:14 +0000123 Analyzer Analyz(Idxer.getProgram(), Idxer);
Argyrios Kyrtzidis94431b52009-07-29 23:39:03 +0000124
Argyrios Kyrtzidis7f4656e2009-07-29 23:40:14 +0000125 switch (ProgAction) {
126 default: assert(0);
127 case PrintRefs: {
128 Analyz.FindReferences(D, Results);
129 for (ResultsTy::iterator
130 I = Results.begin(), E = Results.end(); I != E; ++I)
131 I->print(OS);
132 break;
133 }
134
135 case PrintDecls: {
136 Analyz.FindDeclarations(D, Results);
137 for (ResultsTy::iterator
138 I = Results.begin(), E = Results.end(); I != E; ++I)
139 I->print(OS);
140 break;
141 }
142
143 case PrintDefs:{
144 Analyz.FindDeclarations(D, Results);
145 for (ResultsTy::iterator
146 I = Results.begin(), E = Results.end(); I != E; ++I) {
147 const Decl *D = I->getDecl();
148 bool isDef = false;
149 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
150 isDef = FD->isThisDeclarationADefinition();
151 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
152 isDef = VD->getInit() != 0;
153
154 if (isDef)
155 I->print(OS);
156 }
157 break;
158 }
159
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000160 }
161}
162
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000163static llvm::cl::list<std::string>
164InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
165
166int main(int argc, char **argv) {
167 llvm::sys::PrintStackTraceOnErrorSignal();
168 llvm::PrettyStackTraceProgram X(argc, argv);
169 llvm::cl::ParseCommandLineOptions(argc, argv,
170 "LLVM 'Clang' Indexing Test Bed: http://clang.llvm.org\n");
171
172 FileManager FileMgr;
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000173
174 Program Prog;
Argyrios Kyrtzidisb17dc462009-07-29 23:38:21 +0000175 Indexer Idxer(Prog);
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000176 llvm::SmallVector<TUnit*, 4> TUnits;
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000177
178 // If no input was specified, read from stdin.
179 if (InputFilenames.empty())
180 InputFilenames.push_back("-");
181
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000182 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
183 const std::string &InFile = InputFilenames[i];
184
185 std::string ErrMsg;
186 llvm::OwningPtr<ASTUnit> AST;
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000187
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000188 AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg));
189 if (!AST) {
190 llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n';
191 return 1;
192 }
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000193
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000194 TUnit *TU = new TUnit(AST.take(), InFile);
195 TUnits.push_back(TU);
196
Argyrios Kyrtzidisb17dc462009-07-29 23:38:21 +0000197 Idxer.IndexAST(TU);
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000198 }
199
Argyrios Kyrtzidis44994052009-07-06 21:35:26 +0000200 ASTLocation ASTLoc;
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000201 const std::string &FirstFile = TUnits[0]->Filename;
202 ASTUnit *FirstAST = TUnits[0]->AST.get();
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000203
204 if (!PointAtLocation.empty()) {
205 const std::string &Filename = PointAtLocation[0].FileName;
206 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis119b7fe2009-06-25 22:15:12 +0000207
208 // Safety check. Using an out-of-date AST file will only lead to crashes
209 // or incorrect results.
210 // FIXME: Check all the source files that make up the AST file.
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000211 const FileEntry *ASTFile = FileMgr.getFile(FirstFile);
Argyrios Kyrtzidis119b7fe2009-06-25 22:15:12 +0000212 if (File->getModificationTime() > ASTFile->getModificationTime()) {
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000213 llvm::errs() << "[" << FirstFile << "] Error: " <<
Argyrios Kyrtzidis119b7fe2009-06-25 22:15:12 +0000214 "Pointing at a source file which was modified after creating "
215 "the AST file\n";
216 return 1;
217 }
218
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000219 if (File == 0) {
220 llvm::errs() << "File '" << Filename << "' does not exist\n";
221 return 1;
222 }
223 unsigned Line = PointAtLocation[0].Line;
224 unsigned Col = PointAtLocation[0].Column;
225
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000226 SourceLocation Loc =
227 FirstAST->getSourceManager().getLocation(File, Line, Col);
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000228 if (Loc.isInvalid()) {
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000229 llvm::errs() << "[" << FirstFile << "] Error: " <<
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000230 "Couldn't resolve source location (invalid location)\n";
231 return 1;
232 }
233
Argyrios Kyrtzidis44994052009-07-06 21:35:26 +0000234 ASTLoc = ResolveLocationInAST(FirstAST->getASTContext(), Loc);
235 if (ASTLoc.isInvalid()) {
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000236 llvm::errs() << "[" << FirstFile << "] Error: " <<
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000237 "Couldn't resolve source location (no declaration found)\n";
238 return 1;
239 }
240 }
241
Argyrios Kyrtzidis44994052009-07-06 21:35:26 +0000242 if (ASTLoc.isValid()) {
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000243 if (ProgAction == PrintPoint) {
244 llvm::raw_ostream &OS = llvm::outs();
Argyrios Kyrtzidis44994052009-07-06 21:35:26 +0000245 ASTLoc.print(OS);
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000246 if (const char *Comment =
Argyrios Kyrtzidis44994052009-07-06 21:35:26 +0000247 FirstAST->getASTContext().getCommentForDecl(ASTLoc.getDecl()))
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000248 OS << "Comment associated with this declaration:\n" << Comment << "\n";
249 } else {
Argyrios Kyrtzidisb17dc462009-07-29 23:38:21 +0000250 ProcessASTLocation(ASTLoc, Idxer);
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000251 }
252 }
Argyrios Kyrtzidis12585542009-07-14 03:18:40 +0000253
254 if (HadErrors)
255 return 1;
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000256
Argyrios Kyrtzidis7b332d92009-07-05 22:22:35 +0000257 if (!DisableFree) {
258 for (int i=0, e=TUnits.size(); i != e; ++i)
259 delete TUnits[i];
260 }
Argyrios Kyrtzidis30b983b2009-06-25 18:22:52 +0000261
262 // Managed static deconstruction. Useful for making things like
263 // -time-passes usable.
264 llvm::llvm_shutdown();
265
266 return 0;
267}