blob: 59c49baabecf0471391acaabf2fb6c55a8949865 [file] [log] [blame]
Peter Collingbourne8b1265b2013-11-08 00:08:23 +00001//===---- 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 Collingbourne8b1265b2013-11-08 00:08:23 +000030#include "QueryParser.h"
Chandler Carruth85e6e872014-01-07 20:05:01 +000031#include "QuerySession.h"
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000032#include "clang/Frontend/ASTUnit.h"
Alexander Kornienko228dda52014-08-02 01:02:33 +000033#include "clang/Tooling/CommonOptionsParser.h"
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000034#include "clang/Tooling/Tooling.h"
Peter Collingbournec31176d2014-02-01 01:42:42 +000035#include "llvm/LineEditor/LineEditor.h"
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/MemoryBuffer.h"
38#include "llvm/Support/Signals.h"
39#include <fstream>
Chandler Carruth85e6e872014-01-07 20:05:01 +000040#include <string>
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000041
42using namespace clang;
43using namespace clang::ast_matchers;
44using namespace clang::ast_matchers::dynamic;
45using namespace clang::query;
46using namespace clang::tooling;
47using namespace llvm;
48
Alexander Kornienko228dda52014-08-02 01:02:33 +000049static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
50static cl::OptionCategory ClangQueryCategory("clang-query options");
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000051
52static cl::list<std::string> Commands("c", cl::desc("Specify command to run"),
Alexander Kornienko228dda52014-08-02 01:02:33 +000053 cl::value_desc("command"),
54 cl::cat(ClangQueryCategory));
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000055
56static cl::list<std::string> CommandFiles("f",
57 cl::desc("Read commands from file"),
Alexander Kornienko228dda52014-08-02 01:02:33 +000058 cl::value_desc("file"),
59 cl::cat(ClangQueryCategory));
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000060
Stephen Kelly7cbfd8b2018-08-30 23:25:44 +000061static cl::opt<std::string> PreloadFile(
62 "preload",
63 cl::desc("Preload commands from file and start interactive mode"),
64 cl::value_desc("file"), cl::cat(ClangQueryCategory));
65
Stephen Kelly7dfed0b2018-08-30 23:25:38 +000066bool runCommandsInFile(const char *ExeName, std::string const &FileName,
67 QuerySession &QS) {
68 std::ifstream Input(FileName.c_str());
69 if (!Input.is_open()) {
70 llvm::errs() << ExeName << ": cannot open " << FileName << "\n";
71 return 1;
72 }
73 while (Input.good()) {
74 std::string Line;
75 std::getline(Input, Line);
76
77 QueryRef Q = QueryParser::parse(Line, QS);
78 if (!Q->run(llvm::outs(), QS))
79 return true;
80 }
81 return false;
82}
83
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000084int main(int argc, const char **argv) {
Richard Smith22252f92016-06-09 00:54:42 +000085 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Alexander Kornienko228dda52014-08-02 01:02:33 +000086
87 CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000088
89 if (!Commands.empty() && !CommandFiles.empty()) {
90 llvm::errs() << argv[0] << ": cannot specify both -c and -f\n";
91 return 1;
92 }
93
Stephen Kelly7cbfd8b2018-08-30 23:25:44 +000094 if ((!Commands.empty() || !CommandFiles.empty()) && !PreloadFile.empty()) {
95 llvm::errs() << argv[0]
96 << ": cannot specify both -c or -f with --preload\n";
97 return 1;
98 }
99
Alexander Kornienko228dda52014-08-02 01:02:33 +0000100 ClangTool Tool(OptionsParser.getCompilations(),
101 OptionsParser.getSourcePathList());
David Blaikie329be892014-04-25 15:06:18 +0000102 std::vector<std::unique_ptr<ASTUnit>> ASTs;
George Karpenkovd4997252018-12-05 02:02:40 +0000103 int Status = Tool.buildASTs(ASTs);
104 int ASTStatus = 0;
105 if (Status == 1) {
106 // Building ASTs failed.
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000107 return 1;
George Karpenkovd4997252018-12-05 02:02:40 +0000108 } else if (Status == 2) {
109 ASTStatus |= 1;
110 llvm::errs() << "Failed to build AST for some of the files, "
111 << "results may be incomplete."
112 << "\n";
113 } else {
114 assert(Status == 0 && "Unexpected status returned");
115 }
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000116
117 QuerySession QS(ASTs);
118
119 if (!Commands.empty()) {
Piotr Padlewski08124b12016-12-14 15:29:23 +0000120 for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) {
Malcolm Parsonsd03c2542016-11-01 20:07:05 +0000121 QueryRef Q = QueryParser::parse(*I, QS);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000122 if (!Q->run(llvm::outs(), QS))
123 return 1;
124 }
125 } else if (!CommandFiles.empty()) {
Piotr Padlewski08124b12016-12-14 15:29:23 +0000126 for (auto I = CommandFiles.begin(), E = CommandFiles.end(); I != E; ++I) {
Stephen Kelly7dfed0b2018-08-30 23:25:38 +0000127 if (runCommandsInFile(argv[0], *I, QS))
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000128 return 1;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000129 }
130 } else {
Stephen Kelly7cbfd8b2018-08-30 23:25:44 +0000131 if (!PreloadFile.empty()) {
132 if (runCommandsInFile(argv[0], PreloadFile, QS))
133 return 1;
134 }
Peter Collingbournec31176d2014-02-01 01:42:42 +0000135 LineEditor LE("clang-query");
Samuel Benzaquen1f6066c2014-04-23 14:04:52 +0000136 LE.setListCompleter([&QS](StringRef Line, size_t Pos) {
137 return QueryParser::complete(Line, Pos, QS);
138 });
Peter Collingbournec31176d2014-02-01 01:42:42 +0000139 while (llvm::Optional<std::string> Line = LE.readLine()) {
Samuel Benzaquen1f6066c2014-04-23 14:04:52 +0000140 QueryRef Q = QueryParser::parse(*Line, QS);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000141 Q->run(llvm::outs(), QS);
Manuel Klimek5a1ef9c2014-05-21 18:10:47 +0000142 llvm::outs().flush();
Aaron Ballman58907172015-08-06 11:56:57 +0000143 if (QS.Terminate)
144 break;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000145 }
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000146 }
147
George Karpenkovd4997252018-12-05 02:02:40 +0000148 return ASTStatus;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000149}