blob: 80e1c602796c10da074744cac46085db9a20aeea [file] [log] [blame]
Peter Collingbourne8b1265b2013-11-08 00:08:23 +00001//===---- ClangQuery.cpp - clang-query tool -------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Collingbourne8b1265b2013-11-08 00:08:23 +00006//
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 Collingbourne8b1265b2013-11-08 00:08:23 +000029#include "QueryParser.h"
Chandler Carruth85e6e872014-01-07 20:05:01 +000030#include "QuerySession.h"
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000031#include "clang/Frontend/ASTUnit.h"
Alexander Kornienko228dda52014-08-02 01:02:33 +000032#include "clang/Tooling/CommonOptionsParser.h"
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000033#include "clang/Tooling/Tooling.h"
Peter Collingbournec31176d2014-02-01 01:42:42 +000034#include "llvm/LineEditor/LineEditor.h"
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/Signals.h"
38#include <fstream>
Chandler Carruth85e6e872014-01-07 20:05:01 +000039#include <string>
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000040
41using namespace clang;
42using namespace clang::ast_matchers;
43using namespace clang::ast_matchers::dynamic;
44using namespace clang::query;
45using namespace clang::tooling;
46using namespace llvm;
47
Alexander Kornienko228dda52014-08-02 01:02:33 +000048static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
49static cl::OptionCategory ClangQueryCategory("clang-query options");
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000050
51static cl::list<std::string> Commands("c", cl::desc("Specify command to run"),
Alexander Kornienko228dda52014-08-02 01:02:33 +000052 cl::value_desc("command"),
53 cl::cat(ClangQueryCategory));
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000054
55static cl::list<std::string> CommandFiles("f",
56 cl::desc("Read commands from file"),
Alexander Kornienko228dda52014-08-02 01:02:33 +000057 cl::value_desc("file"),
58 cl::cat(ClangQueryCategory));
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000059
Stephen Kelly7cbfd8b2018-08-30 23:25:44 +000060static 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 Kelly7dfed0b2018-08-30 23:25:38 +000065bool 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 Collingbourne8b1265b2013-11-08 00:08:23 +000083int main(int argc, const char **argv) {
Richard Smith22252f92016-06-09 00:54:42 +000084 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Alexander Kornienko228dda52014-08-02 01:02:33 +000085
86 CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000087
88 if (!Commands.empty() && !CommandFiles.empty()) {
89 llvm::errs() << argv[0] << ": cannot specify both -c and -f\n";
90 return 1;
91 }
92
Stephen Kelly7cbfd8b2018-08-30 23:25:44 +000093 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 Kornienko228dda52014-08-02 01:02:33 +000099 ClangTool Tool(OptionsParser.getCompilations(),
100 OptionsParser.getSourcePathList());
David Blaikie329be892014-04-25 15:06:18 +0000101 std::vector<std::unique_ptr<ASTUnit>> ASTs;
George Karpenkovd4997252018-12-05 02:02:40 +0000102 int Status = Tool.buildASTs(ASTs);
103 int ASTStatus = 0;
104 if (Status == 1) {
105 // Building ASTs failed.
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000106 return 1;
George Karpenkovd4997252018-12-05 02:02:40 +0000107 } 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 Collingbourne8b1265b2013-11-08 00:08:23 +0000115
116 QuerySession QS(ASTs);
117
118 if (!Commands.empty()) {
Piotr Padlewski08124b12016-12-14 15:29:23 +0000119 for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) {
Malcolm Parsonsd03c2542016-11-01 20:07:05 +0000120 QueryRef Q = QueryParser::parse(*I, QS);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000121 if (!Q->run(llvm::outs(), QS))
122 return 1;
123 }
124 } else if (!CommandFiles.empty()) {
Piotr Padlewski08124b12016-12-14 15:29:23 +0000125 for (auto I = CommandFiles.begin(), E = CommandFiles.end(); I != E; ++I) {
Stephen Kelly7dfed0b2018-08-30 23:25:38 +0000126 if (runCommandsInFile(argv[0], *I, QS))
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000127 return 1;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000128 }
129 } else {
Stephen Kelly7cbfd8b2018-08-30 23:25:44 +0000130 if (!PreloadFile.empty()) {
131 if (runCommandsInFile(argv[0], PreloadFile, QS))
132 return 1;
133 }
Peter Collingbournec31176d2014-02-01 01:42:42 +0000134 LineEditor LE("clang-query");
Samuel Benzaquen1f6066c2014-04-23 14:04:52 +0000135 LE.setListCompleter([&QS](StringRef Line, size_t Pos) {
136 return QueryParser::complete(Line, Pos, QS);
137 });
Peter Collingbournec31176d2014-02-01 01:42:42 +0000138 while (llvm::Optional<std::string> Line = LE.readLine()) {
Samuel Benzaquen1f6066c2014-04-23 14:04:52 +0000139 QueryRef Q = QueryParser::parse(*Line, QS);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000140 Q->run(llvm::outs(), QS);
Manuel Klimek5a1ef9c2014-05-21 18:10:47 +0000141 llvm::outs().flush();
Aaron Ballman58907172015-08-06 11:56:57 +0000142 if (QS.Terminate)
143 break;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000144 }
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000145 }
146
George Karpenkovd4997252018-12-05 02:02:40 +0000147 return ASTStatus;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000148}