Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 1 | //===---- ClangQuery.cpp - clang-query tool -------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This tool is for interactive exploration of the Clang AST using AST matchers. |
| 10 | // It currently allows the user to enter a matcher at an interactive prompt and |
| 11 | // view the resulting bindings as diagnostics, AST pretty prints or AST dumps. |
| 12 | // Example session: |
| 13 | // |
| 14 | // $ cat foo.c |
| 15 | // void foo(void) {} |
| 16 | // $ clang-query foo.c -- |
| 17 | // clang-query> match functionDecl() |
| 18 | // |
| 19 | // Match #1: |
| 20 | // |
| 21 | // foo.c:1:1: note: "root" binds here |
| 22 | // void foo(void) {} |
| 23 | // ^~~~~~~~~~~~~~~~~ |
| 24 | // 1 match. |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "Query.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 29 | #include "QueryParser.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 30 | #include "QuerySession.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/ASTUnit.h" |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 32 | #include "clang/Tooling/CommonOptionsParser.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 33 | #include "clang/Tooling/Tooling.h" |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 34 | #include "llvm/LineEditor/LineEditor.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
| 36 | #include "llvm/Support/MemoryBuffer.h" |
| 37 | #include "llvm/Support/Signals.h" |
| 38 | #include <fstream> |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 39 | #include <string> |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace clang; |
| 42 | using namespace clang::ast_matchers; |
| 43 | using namespace clang::ast_matchers::dynamic; |
| 44 | using namespace clang::query; |
| 45 | using namespace clang::tooling; |
| 46 | using namespace llvm; |
| 47 | |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 48 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
| 49 | static cl::OptionCategory ClangQueryCategory("clang-query options"); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 50 | |
| 51 | 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] | 52 | cl::value_desc("command"), |
| 53 | cl::cat(ClangQueryCategory)); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 54 | |
| 55 | static cl::list<std::string> CommandFiles("f", |
| 56 | cl::desc("Read commands from file"), |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 57 | cl::value_desc("file"), |
| 58 | cl::cat(ClangQueryCategory)); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 59 | |
Stephen Kelly | 7cbfd8b | 2018-08-30 23:25:44 +0000 | [diff] [blame] | 60 | static cl::opt<std::string> PreloadFile( |
| 61 | "preload", |
| 62 | cl::desc("Preload commands from file and start interactive mode"), |
| 63 | cl::value_desc("file"), cl::cat(ClangQueryCategory)); |
| 64 | |
Stephen Kelly | 7dfed0b | 2018-08-30 23:25:38 +0000 | [diff] [blame] | 65 | bool runCommandsInFile(const char *ExeName, std::string const &FileName, |
| 66 | QuerySession &QS) { |
| 67 | std::ifstream Input(FileName.c_str()); |
| 68 | if (!Input.is_open()) { |
| 69 | llvm::errs() << ExeName << ": cannot open " << FileName << "\n"; |
| 70 | return 1; |
| 71 | } |
| 72 | while (Input.good()) { |
| 73 | std::string Line; |
| 74 | std::getline(Input, Line); |
| 75 | |
| 76 | QueryRef Q = QueryParser::parse(Line, QS); |
| 77 | if (!Q->run(llvm::outs(), QS)) |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 83 | int main(int argc, const char **argv) { |
Richard Smith | 22252f9 | 2016-06-09 00:54:42 +0000 | [diff] [blame] | 84 | llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 85 | |
| 86 | CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 87 | |
| 88 | if (!Commands.empty() && !CommandFiles.empty()) { |
| 89 | llvm::errs() << argv[0] << ": cannot specify both -c and -f\n"; |
| 90 | return 1; |
| 91 | } |
| 92 | |
Stephen Kelly | 7cbfd8b | 2018-08-30 23:25:44 +0000 | [diff] [blame] | 93 | if ((!Commands.empty() || !CommandFiles.empty()) && !PreloadFile.empty()) { |
| 94 | llvm::errs() << argv[0] |
| 95 | << ": cannot specify both -c or -f with --preload\n"; |
| 96 | return 1; |
| 97 | } |
| 98 | |
Alexander Kornienko | 228dda5 | 2014-08-02 01:02:33 +0000 | [diff] [blame] | 99 | ClangTool Tool(OptionsParser.getCompilations(), |
| 100 | OptionsParser.getSourcePathList()); |
David Blaikie | 329be89 | 2014-04-25 15:06:18 +0000 | [diff] [blame] | 101 | std::vector<std::unique_ptr<ASTUnit>> ASTs; |
George Karpenkov | d499725 | 2018-12-05 02:02:40 +0000 | [diff] [blame] | 102 | int Status = Tool.buildASTs(ASTs); |
| 103 | int ASTStatus = 0; |
| 104 | if (Status == 1) { |
| 105 | // Building ASTs failed. |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 106 | return 1; |
George Karpenkov | d499725 | 2018-12-05 02:02:40 +0000 | [diff] [blame] | 107 | } else if (Status == 2) { |
| 108 | ASTStatus |= 1; |
| 109 | llvm::errs() << "Failed to build AST for some of the files, " |
| 110 | << "results may be incomplete." |
| 111 | << "\n"; |
| 112 | } else { |
| 113 | assert(Status == 0 && "Unexpected status returned"); |
| 114 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 115 | |
| 116 | QuerySession QS(ASTs); |
| 117 | |
| 118 | if (!Commands.empty()) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 119 | for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) { |
Malcolm Parsons | d03c254 | 2016-11-01 20:07:05 +0000 | [diff] [blame] | 120 | QueryRef Q = QueryParser::parse(*I, QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 121 | if (!Q->run(llvm::outs(), QS)) |
| 122 | return 1; |
| 123 | } |
| 124 | } else if (!CommandFiles.empty()) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 125 | for (auto I = CommandFiles.begin(), E = CommandFiles.end(); I != E; ++I) { |
Stephen Kelly | 7dfed0b | 2018-08-30 23:25:38 +0000 | [diff] [blame] | 126 | if (runCommandsInFile(argv[0], *I, QS)) |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 127 | return 1; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 128 | } |
| 129 | } else { |
Stephen Kelly | 7cbfd8b | 2018-08-30 23:25:44 +0000 | [diff] [blame] | 130 | if (!PreloadFile.empty()) { |
| 131 | if (runCommandsInFile(argv[0], PreloadFile, QS)) |
| 132 | return 1; |
| 133 | } |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 134 | LineEditor LE("clang-query"); |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 135 | LE.setListCompleter([&QS](StringRef Line, size_t Pos) { |
| 136 | return QueryParser::complete(Line, Pos, QS); |
| 137 | }); |
Peter Collingbourne | c31176d | 2014-02-01 01:42:42 +0000 | [diff] [blame] | 138 | while (llvm::Optional<std::string> Line = LE.readLine()) { |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 139 | QueryRef Q = QueryParser::parse(*Line, QS); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 140 | Q->run(llvm::outs(), QS); |
Manuel Klimek | 5a1ef9c | 2014-05-21 18:10:47 +0000 | [diff] [blame] | 141 | llvm::outs().flush(); |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 +0000 | [diff] [blame] | 142 | if (QS.Terminate) |
| 143 | break; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 144 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 145 | } |
| 146 | |
George Karpenkov | d499725 | 2018-12-05 02:02:40 +0000 | [diff] [blame] | 147 | return ASTStatus; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 148 | } |