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 | // |
Argyrios Kyrtzidis | 6e4a86d | 2009-06-25 21:54:50 +0000 | [diff] [blame] | 20 | // -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 Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 23 | // |
| 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" |
| 39 | using namespace clang; |
| 40 | |
| 41 | |
| 42 | static llvm::cl::list<ParsedSourceLocation> |
| 43 | PointAtLocation("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 | |
| 47 | static llvm::cl::opt<bool> |
| 48 | DisableFree("disable-free", |
| 49 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 50 | llvm::cl::init(false)); |
| 51 | |
| 52 | static llvm::cl::list<std::string> |
| 53 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>")); |
| 54 | |
| 55 | int 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 Kyrtzidis | 119b7fe | 2009-06-25 22:15:12 +0000 | [diff] [blame] | 91 | |
| 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 Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 103 | 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 Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 127 | llvm::raw_ostream &OS = llvm::outs(); |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 128 | 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 Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 136 | Point.Node->printPretty(OS, AST->getASTContext(), 0, |
| 137 | PrintingPolicy(AST->getASTContext().getLangOptions())); |
Argyrios Kyrtzidis | 30b983b | 2009-06-25 18:22:52 +0000 | [diff] [blame] | 138 | 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 | } |