Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 1 | //===---- ClangQuery.cpp - clang-query tool -------------------------------===// |
| 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 tool is for interactive exploration of the Clang AST using AST matchers. |
| 11 | // It currently allows the user to enter a matcher at an interactive prompt and |
| 12 | // view the resulting bindings as diagnostics, AST pretty prints or AST dumps. |
| 13 | // Example session: |
| 14 | // |
| 15 | // $ cat foo.c |
| 16 | // void foo(void) {} |
| 17 | // $ clang-query foo.c -- |
| 18 | // clang-query> match functionDecl() |
| 19 | // |
| 20 | // Match #1: |
| 21 | // |
| 22 | // foo.c:1:1: note: "root" binds here |
| 23 | // void foo(void) {} |
| 24 | // ^~~~~~~~~~~~~~~~~ |
| 25 | // 1 match. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #include "Query.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 30 | #include "QueryParser.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 31 | #include "QuerySession.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/ASTUnit.h" |
| 33 | #include "clang/Tooling/CompilationDatabase.h" |
| 34 | #include "clang/Tooling/Tooling.h" |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 35 | #include "llvm/LineEditor/LineEditor.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
| 37 | #include "llvm/Support/MemoryBuffer.h" |
| 38 | #include "llvm/Support/Signals.h" |
| 39 | #include <fstream> |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 40 | #include <string> |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace clang; |
| 43 | using namespace clang::ast_matchers; |
| 44 | using namespace clang::ast_matchers::dynamic; |
| 45 | using namespace clang::query; |
| 46 | using namespace clang::tooling; |
| 47 | using namespace llvm; |
| 48 | |
| 49 | static cl::opt<std::string> BuildPath("b", cl::desc("Specify build path"), |
| 50 | cl::value_desc("<path>")); |
| 51 | |
| 52 | static cl::list<std::string> Commands("c", cl::desc("Specify command to run"), |
| 53 | cl::value_desc("<command>")); |
| 54 | |
| 55 | static cl::list<std::string> CommandFiles("f", |
| 56 | cl::desc("Read commands from file"), |
| 57 | cl::value_desc("<file>")); |
| 58 | |
| 59 | static cl::list<std::string> SourcePaths(cl::Positional, |
| 60 | cl::desc("<source0> [... <sourceN>]"), |
| 61 | cl::OneOrMore); |
| 62 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 63 | int main(int argc, const char **argv) { |
| 64 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 65 | cl::ParseCommandLineOptions(argc, argv); |
| 66 | |
| 67 | if (!Commands.empty() && !CommandFiles.empty()) { |
| 68 | llvm::errs() << argv[0] << ": cannot specify both -c and -f\n"; |
| 69 | return 1; |
| 70 | } |
| 71 | |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 72 | std::unique_ptr<CompilationDatabase> Compilations( |
| 73 | FixedCompilationDatabase::loadFromCommandLine(argc, argv)); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 74 | if (!Compilations) { // Couldn't find a compilation DB from the command line |
| 75 | std::string ErrorMessage; |
| 76 | Compilations.reset( |
| 77 | !BuildPath.empty() ? |
| 78 | CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage) : |
| 79 | CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage) |
| 80 | ); |
| 81 | |
| 82 | // Still no compilation DB? - bail. |
| 83 | if (!Compilations) |
| 84 | llvm::report_fatal_error(ErrorMessage); |
| 85 | } |
| 86 | |
| 87 | ClangTool Tool(*Compilations, SourcePaths); |
David Blaikie | 329be89 | 2014-04-25 15:06:18 +0000 | [diff] [blame] | 88 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 89 | if (Tool.buildASTs(ASTs) != 0) |
| 90 | return 1; |
| 91 | |
| 92 | QuerySession QS(ASTs); |
| 93 | |
| 94 | if (!Commands.empty()) { |
| 95 | for (cl::list<std::string>::iterator I = Commands.begin(), |
| 96 | E = Commands.end(); |
| 97 | I != E; ++I) { |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 98 | QueryRef Q = QueryParser::parse(I->c_str(), QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 99 | if (!Q->run(llvm::outs(), QS)) |
| 100 | return 1; |
| 101 | } |
| 102 | } else if (!CommandFiles.empty()) { |
| 103 | for (cl::list<std::string>::iterator I = CommandFiles.begin(), |
| 104 | E = CommandFiles.end(); |
| 105 | I != E; ++I) { |
| 106 | std::ifstream Input(I->c_str()); |
| 107 | if (!Input.is_open()) { |
| 108 | llvm::errs() << argv[0] << ": cannot open " << *I << "\n"; |
| 109 | return 1; |
| 110 | } |
| 111 | while (Input.good()) { |
| 112 | std::string Line; |
| 113 | std::getline(Input, Line); |
| 114 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 115 | QueryRef Q = QueryParser::parse(Line.c_str(), QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 116 | if (!Q->run(llvm::outs(), QS)) |
| 117 | return 1; |
| 118 | } |
| 119 | } |
| 120 | } else { |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 121 | LineEditor LE("clang-query"); |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 122 | LE.setListCompleter([&QS](StringRef Line, size_t Pos) { |
| 123 | return QueryParser::complete(Line, Pos, QS); |
| 124 | }); |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 125 | while (llvm::Optional<std::string> Line = LE.readLine()) { |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 126 | QueryRef Q = QueryParser::parse(*Line, QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 127 | Q->run(llvm::outs(), QS); |
Manuel Klimek | 5a1ef9c | 2014-05-21 18:10:47 +0000 | [diff] [blame^] | 128 | llvm::outs().flush(); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 129 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |