blob: d8b411b43bdcdeb4262f287b685cc89f5ca00bef [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"
30#include "QuerySession.h"
31#include "QueryParser.h"
32
33#include "clang/Frontend/ASTUnit.h"
34#include "clang/Tooling/CompilationDatabase.h"
35#include "clang/Tooling/Tooling.h"
36#include "llvm/ADT/OwningPtr.h"
37#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/MemoryBuffer.h"
39#include "llvm/Support/Signals.h"
40#include <fstream>
41#include <string>
42
43#include <histedit.h>
44
45using namespace clang;
46using namespace clang::ast_matchers;
47using namespace clang::ast_matchers::dynamic;
48using namespace clang::query;
49using namespace clang::tooling;
50using namespace llvm;
51
52static cl::opt<std::string> BuildPath("b", cl::desc("Specify build path"),
53 cl::value_desc("<path>"));
54
55static cl::list<std::string> Commands("c", cl::desc("Specify command to run"),
56 cl::value_desc("<command>"));
57
58static cl::list<std::string> CommandFiles("f",
59 cl::desc("Read commands from file"),
60 cl::value_desc("<file>"));
61
62static cl::list<std::string> SourcePaths(cl::Positional,
63 cl::desc("<source0> [... <sourceN>]"),
64 cl::OneOrMore);
65
66static char *ReturnPrompt(EditLine *EL) {
67 static char Prompt[] = "clang-query> ";
68 return Prompt;
69}
70
71int main(int argc, const char **argv) {
72 llvm::sys::PrintStackTraceOnErrorSignal();
73 cl::ParseCommandLineOptions(argc, argv);
74
75 if (!Commands.empty() && !CommandFiles.empty()) {
76 llvm::errs() << argv[0] << ": cannot specify both -c and -f\n";
77 return 1;
78 }
79
80 llvm::OwningPtr<CompilationDatabase> Compilations(
81 FixedCompilationDatabase::loadFromCommandLine(argc, argv));
82 if (!Compilations) { // Couldn't find a compilation DB from the command line
83 std::string ErrorMessage;
84 Compilations.reset(
85 !BuildPath.empty() ?
86 CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage) :
87 CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage)
88 );
89
90 // Still no compilation DB? - bail.
91 if (!Compilations)
92 llvm::report_fatal_error(ErrorMessage);
93 }
94
95 ClangTool Tool(*Compilations, SourcePaths);
96 std::vector<ASTUnit *> ASTs;
97 if (Tool.buildASTs(ASTs) != 0)
98 return 1;
99
100 QuerySession QS(ASTs);
101
102 if (!Commands.empty()) {
103 for (cl::list<std::string>::iterator I = Commands.begin(),
104 E = Commands.end();
105 I != E; ++I) {
106 QueryRef Q = ParseQuery(I->c_str());
107 if (!Q->run(llvm::outs(), QS))
108 return 1;
109 }
110 } else if (!CommandFiles.empty()) {
111 for (cl::list<std::string>::iterator I = CommandFiles.begin(),
112 E = CommandFiles.end();
113 I != E; ++I) {
114 std::ifstream Input(I->c_str());
115 if (!Input.is_open()) {
116 llvm::errs() << argv[0] << ": cannot open " << *I << "\n";
117 return 1;
118 }
119 while (Input.good()) {
120 std::string Line;
121 std::getline(Input, Line);
122
123 QueryRef Q = ParseQuery(Line.c_str());
124 if (!Q->run(llvm::outs(), QS))
125 return 1;
126 }
127 }
128 } else {
129 History *Hist = history_init();
130 HistEvent Event;
131 history(Hist, &Event, H_SETSIZE, 100);
132
133 EditLine *EL = el_init("clang-query", stdin, stdout, stderr);
134 el_set(EL, EL_PROMPT, ReturnPrompt);
135 el_set(EL, EL_EDITOR, "emacs");
136 el_set(EL, EL_HIST, history, Hist);
137
138 int Count;
139 while (const char *Line = el_gets(EL, &Count)) {
140 if (Count == 0)
141 break;
142
143 history(Hist, &Event, H_ENTER, Line);
144
145 QueryRef Q = ParseQuery(Line);
146 Q->run(llvm::outs(), QS);
147 }
148
149 history_end(Hist);
150 el_end(EL);
151
152 llvm::outs() << "\n";
153 }
154
155 llvm::DeleteContainerPointers(ASTs);
156
157 return 0;
158}