Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 1 | //===--- 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 Xu | 96bac6b | 2009-07-07 07:14:55 +0000 | [diff] [blame] | 20 | // -point-at [file:line:column] |
Argyrios Kyrtzidis | 6e4a86d | 2009-06-25 21:54:50 +0000 | [diff] [blame] | 21 | // Point at a declaration/statement/expression. If no other operation is |
| 22 | // specified, prints some info about it. |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 23 | // |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 24 | // -print-refs |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 25 | // Print ASTLocations that reference the -point-at node |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 26 | // |
| 27 | // -print-defs |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 28 | // Print ASTLocations that define the -point-at node |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 29 | // |
| 30 | // -print-decls |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 31 | // Print ASTLocations that declare the -point-at node |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 32 | // |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 35 | #include "clang/Index/Program.h" |
Argyrios Kyrtzidis | b17dc46 | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 36 | #include "clang/Index/Indexer.h" |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 37 | #include "clang/Index/Entity.h" |
| 38 | #include "clang/Index/TranslationUnit.h" |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 39 | #include "clang/Index/ASTLocation.h" |
| 40 | #include "clang/Index/DeclReferenceMap.h" |
Argyrios Kyrtzidis | 0f39848 | 2009-07-29 23:41:08 +0000 | [diff] [blame^] | 41 | #include "clang/Index/SelectorMap.h" |
Argyrios Kyrtzidis | 94431b5 | 2009-07-29 23:39:03 +0000 | [diff] [blame] | 42 | #include "clang/Index/Handlers.h" |
Argyrios Kyrtzidis | 7f4656e | 2009-07-29 23:40:14 +0000 | [diff] [blame] | 43 | #include "clang/Index/Analyzer.h" |
Argyrios Kyrtzidis | 818e15b | 2009-07-06 21:35:02 +0000 | [diff] [blame] | 44 | #include "clang/Index/Utils.h" |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 45 | #include "clang/Frontend/ASTUnit.h" |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 46 | #include "clang/Frontend/CommandLineSourceLoc.h" |
| 47 | #include "clang/AST/Decl.h" |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 48 | #include "clang/AST/Expr.h" |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 49 | #include "clang/Basic/FileManager.h" |
| 50 | #include "clang/Basic/SourceManager.h" |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 51 | #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" |
| 56 | using namespace clang; |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 57 | using namespace idx; |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 58 | |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 59 | class TUnit : public TranslationUnit { |
| 60 | public: |
| 61 | TUnit(ASTUnit *ast, const std::string &filename) |
Argyrios Kyrtzidis | 0f39848 | 2009-07-29 23:41:08 +0000 | [diff] [blame^] | 62 | : AST(ast), Filename(filename), |
| 63 | DeclRefMap(ast->getASTContext()), |
| 64 | SelMap(ast->getASTContext()) { } |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 65 | |
| 66 | virtual ASTContext &getASTContext() { return AST->getASTContext(); } |
Argyrios Kyrtzidis | 768a6c9 | 2009-07-29 23:39:18 +0000 | [diff] [blame] | 67 | virtual DeclReferenceMap &getDeclReferenceMap() { return DeclRefMap; } |
Argyrios Kyrtzidis | 0f39848 | 2009-07-29 23:41:08 +0000 | [diff] [blame^] | 68 | virtual SelectorMap &getSelectorMap() { return SelMap; } |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 69 | |
| 70 | llvm::OwningPtr<ASTUnit> AST; |
| 71 | std::string Filename; |
Argyrios Kyrtzidis | 768a6c9 | 2009-07-29 23:39:18 +0000 | [diff] [blame] | 72 | DeclReferenceMap DeclRefMap; |
Argyrios Kyrtzidis | 0f39848 | 2009-07-29 23:41:08 +0000 | [diff] [blame^] | 73 | SelectorMap SelMap; |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 74 | }; |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 75 | |
| 76 | static llvm::cl::list<ParsedSourceLocation> |
| 77 | PointAtLocation("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 Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 81 | enum 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 | |
| 88 | static llvm::cl::opt<ProgActions> |
| 89 | ProgAction( |
| 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 Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 102 | static llvm::cl::opt<bool> |
| 103 | DisableFree("disable-free", |
| 104 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 105 | llvm::cl::init(false)); |
| 106 | |
Argyrios Kyrtzidis | 1258554 | 2009-07-14 03:18:40 +0000 | [diff] [blame] | 107 | static bool HadErrors = false; |
| 108 | |
Argyrios Kyrtzidis | b17dc46 | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 109 | static void ProcessASTLocation(ASTLocation ASTLoc, Indexer &Idxer) { |
Argyrios Kyrtzidis | 4499405 | 2009-07-06 21:35:26 +0000 | [diff] [blame] | 110 | assert(ASTLoc.isValid()); |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 111 | |
Argyrios Kyrtzidis | dc50c64 | 2009-07-18 21:17:58 +0000 | [diff] [blame] | 112 | Decl *D = ASTLoc.getReferencedDecl(); |
Argyrios Kyrtzidis | 1258554 | 2009-07-14 03:18:40 +0000 | [diff] [blame] | 113 | if (D == 0) { |
Argyrios Kyrtzidis | ec930d3 | 2009-07-21 00:05:10 +0000 | [diff] [blame] | 114 | llvm::errs() << "Error: Couldn't get referenced Decl for the ASTLocation\n"; |
Argyrios Kyrtzidis | 1258554 | 2009-07-14 03:18:40 +0000 | [diff] [blame] | 115 | HadErrors = true; |
| 116 | return; |
| 117 | } |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 118 | |
Argyrios Kyrtzidis | 7f4656e | 2009-07-29 23:40:14 +0000 | [diff] [blame] | 119 | llvm::raw_ostream &OS = llvm::outs(); |
| 120 | typedef Storing<TULocationHandler> ResultsTy; |
| 121 | ResultsTy Results; |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 122 | |
Argyrios Kyrtzidis | 7f4656e | 2009-07-29 23:40:14 +0000 | [diff] [blame] | 123 | Analyzer Analyz(Idxer.getProgram(), Idxer); |
Argyrios Kyrtzidis | 94431b5 | 2009-07-29 23:39:03 +0000 | [diff] [blame] | 124 | |
Argyrios Kyrtzidis | 7f4656e | 2009-07-29 23:40:14 +0000 | [diff] [blame] | 125 | 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 Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 163 | static llvm::cl::list<std::string> |
| 164 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); |
| 165 | |
| 166 | int 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 Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 173 | |
| 174 | Program Prog; |
Argyrios Kyrtzidis | b17dc46 | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 175 | Indexer Idxer(Prog); |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 176 | llvm::SmallVector<TUnit*, 4> TUnits; |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 177 | |
| 178 | // If no input was specified, read from stdin. |
| 179 | if (InputFilenames.empty()) |
| 180 | InputFilenames.push_back("-"); |
| 181 | |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 182 | 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 Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 187 | |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 188 | AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg)); |
| 189 | if (!AST) { |
| 190 | llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n'; |
| 191 | return 1; |
| 192 | } |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 193 | |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 194 | TUnit *TU = new TUnit(AST.take(), InFile); |
| 195 | TUnits.push_back(TU); |
| 196 | |
Argyrios Kyrtzidis | b17dc46 | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 197 | Idxer.IndexAST(TU); |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Argyrios Kyrtzidis | 4499405 | 2009-07-06 21:35:26 +0000 | [diff] [blame] | 200 | ASTLocation ASTLoc; |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 201 | const std::string &FirstFile = TUnits[0]->Filename; |
| 202 | ASTUnit *FirstAST = TUnits[0]->AST.get(); |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 203 | |
| 204 | if (!PointAtLocation.empty()) { |
| 205 | const std::string &Filename = PointAtLocation[0].FileName; |
| 206 | const FileEntry *File = FileMgr.getFile(Filename); |
Argyrios Kyrtzidis | 119b7fe | 2009-06-25 22:15:12 +0000 | [diff] [blame] | 207 | |
| 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 Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 211 | const FileEntry *ASTFile = FileMgr.getFile(FirstFile); |
Argyrios Kyrtzidis | 119b7fe | 2009-06-25 22:15:12 +0000 | [diff] [blame] | 212 | if (File->getModificationTime() > ASTFile->getModificationTime()) { |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 213 | llvm::errs() << "[" << FirstFile << "] Error: " << |
Argyrios Kyrtzidis | 119b7fe | 2009-06-25 22:15:12 +0000 | [diff] [blame] | 214 | "Pointing at a source file which was modified after creating " |
| 215 | "the AST file\n"; |
| 216 | return 1; |
| 217 | } |
| 218 | |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 219 | 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 Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 226 | SourceLocation Loc = |
| 227 | FirstAST->getSourceManager().getLocation(File, Line, Col); |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 228 | if (Loc.isInvalid()) { |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 229 | llvm::errs() << "[" << FirstFile << "] Error: " << |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 230 | "Couldn't resolve source location (invalid location)\n"; |
| 231 | return 1; |
| 232 | } |
| 233 | |
Argyrios Kyrtzidis | 4499405 | 2009-07-06 21:35:26 +0000 | [diff] [blame] | 234 | ASTLoc = ResolveLocationInAST(FirstAST->getASTContext(), Loc); |
| 235 | if (ASTLoc.isInvalid()) { |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 236 | llvm::errs() << "[" << FirstFile << "] Error: " << |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 237 | "Couldn't resolve source location (no declaration found)\n"; |
| 238 | return 1; |
| 239 | } |
| 240 | } |
| 241 | |
Argyrios Kyrtzidis | 4499405 | 2009-07-06 21:35:26 +0000 | [diff] [blame] | 242 | if (ASTLoc.isValid()) { |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 243 | if (ProgAction == PrintPoint) { |
| 244 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | 4499405 | 2009-07-06 21:35:26 +0000 | [diff] [blame] | 245 | ASTLoc.print(OS); |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 246 | if (const char *Comment = |
Argyrios Kyrtzidis | 4499405 | 2009-07-06 21:35:26 +0000 | [diff] [blame] | 247 | FirstAST->getASTContext().getCommentForDecl(ASTLoc.getDecl())) |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 248 | OS << "Comment associated with this declaration:\n" << Comment << "\n"; |
| 249 | } else { |
Argyrios Kyrtzidis | b17dc46 | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 250 | ProcessASTLocation(ASTLoc, Idxer); |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
Argyrios Kyrtzidis | 1258554 | 2009-07-14 03:18:40 +0000 | [diff] [blame] | 253 | |
| 254 | if (HadErrors) |
| 255 | return 1; |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 256 | |
Argyrios Kyrtzidis | 7b332d9 | 2009-07-05 22:22:35 +0000 | [diff] [blame] | 257 | if (!DisableFree) { |
| 258 | for (int i=0, e=TUnits.size(); i != e; ++i) |
| 259 | delete TUnits[i]; |
| 260 | } |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 261 | |
| 262 | // Managed static deconstruction. Useful for making things like |
| 263 | // -time-passes usable. |
| 264 | llvm::llvm_shutdown(); |
| 265 | |
| 266 | return 0; |
| 267 | } |