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" |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 33 | #include "clang/Tooling/CommonOptionsParser.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 49 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
| 50 | static cl::OptionCategory ClangQueryCategory("clang-query options"); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 51 | |
| 52 | static cl::list<std::string> Commands("c", cl::desc("Specify command to run"), |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 53 | cl::value_desc("command"), |
| 54 | cl::cat(ClangQueryCategory)); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 55 | |
| 56 | static cl::list<std::string> CommandFiles("f", |
| 57 | cl::desc("Read commands from file"), |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 58 | cl::value_desc("file"), |
| 59 | cl::cat(ClangQueryCategory)); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 60 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 61 | int main(int argc, const char **argv) { |
| 62 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 63 | |
| 64 | CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 65 | |
| 66 | if (!Commands.empty() && !CommandFiles.empty()) { |
| 67 | llvm::errs() << argv[0] << ": cannot specify both -c and -f\n"; |
| 68 | return 1; |
| 69 | } |
| 70 | |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 71 | ClangTool Tool(OptionsParser.getCompilations(), |
| 72 | OptionsParser.getSourcePathList()); |
David Blaikie | 329be89 | 2014-04-25 15:06:18 +0000 | [diff] [blame] | 73 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 74 | if (Tool.buildASTs(ASTs) != 0) |
| 75 | return 1; |
| 76 | |
| 77 | QuerySession QS(ASTs); |
| 78 | |
| 79 | if (!Commands.empty()) { |
| 80 | for (cl::list<std::string>::iterator I = Commands.begin(), |
| 81 | E = Commands.end(); |
| 82 | I != E; ++I) { |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 83 | QueryRef Q = QueryParser::parse(I->c_str(), QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 84 | if (!Q->run(llvm::outs(), QS)) |
| 85 | return 1; |
| 86 | } |
| 87 | } else if (!CommandFiles.empty()) { |
| 88 | for (cl::list<std::string>::iterator I = CommandFiles.begin(), |
| 89 | E = CommandFiles.end(); |
| 90 | I != E; ++I) { |
| 91 | std::ifstream Input(I->c_str()); |
| 92 | if (!Input.is_open()) { |
| 93 | llvm::errs() << argv[0] << ": cannot open " << *I << "\n"; |
| 94 | return 1; |
| 95 | } |
| 96 | while (Input.good()) { |
| 97 | std::string Line; |
| 98 | std::getline(Input, Line); |
| 99 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 100 | QueryRef Q = QueryParser::parse(Line.c_str(), QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 101 | if (!Q->run(llvm::outs(), QS)) |
| 102 | return 1; |
| 103 | } |
| 104 | } |
| 105 | } else { |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 106 | LineEditor LE("clang-query"); |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 107 | LE.setListCompleter([&QS](StringRef Line, size_t Pos) { |
| 108 | return QueryParser::complete(Line, Pos, QS); |
| 109 | }); |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 110 | while (llvm::Optional<std::string> Line = LE.readLine()) { |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 111 | QueryRef Q = QueryParser::parse(*Line, QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 112 | Q->run(llvm::outs(), QS); |
Manuel Klimek | 5a1ef9c | 2014-05-21 18:10:47 +0000 | [diff] [blame] | 113 | llvm::outs().flush(); |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 +0000 | [diff] [blame] | 114 | if (QS.Terminate) |
| 115 | break; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 116 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |