31 #include "clang/Frontend/ASTUnit.h"
32 #include "clang/Tooling/CommonOptionsParser.h"
33 #include "clang/Tooling/Tooling.h"
34 #include "llvm/LineEditor/LineEditor.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Signals.h"
38 #include "llvm/Support/WithColor.h"
42 using namespace clang;
44 using namespace clang::ast_matchers::dynamic;
49 static cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
52 static cl::list<std::string>
Commands(
"c", cl::desc(
"Specify command to run"),
53 cl::value_desc(
"command"),
57 cl::desc(
"Read commands from file"),
58 cl::value_desc(
"file"),
63 cl::desc(
"Preload commands from file and start interactive mode"),
68 std::ifstream Input(
FileName.c_str());
69 if (!Input.is_open()) {
70 llvm::errs() << ExeName <<
": cannot open " <<
FileName <<
"\n";
74 std::string FileContent((std::istreambuf_iterator<char>(Input)),
75 std::istreambuf_iterator<char>());
77 StringRef FileContentRef(FileContent);
78 while (!FileContentRef.empty()) {
79 QueryRef Q = QueryParser::parse(FileContentRef, QS);
80 if (!Q->run(llvm::outs(), QS))
82 FileContentRef = Q->RemainingContent;
87 int main(
int argc,
const char **argv) {
88 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
90 llvm::Expected<CommonOptionsParser> OptionsParser =
100 llvm::errs() << argv[0] <<
": cannot specify both -c and -f\n";
105 llvm::errs() << argv[0]
106 <<
": cannot specify both -c or -f with --preload\n";
110 ClangTool Tool(OptionsParser->getCompilations(),
111 OptionsParser->getSourcePathList());
112 std::vector<std::unique_ptr<ASTUnit>> ASTs;
113 int Status = Tool.buildASTs(ASTs);
118 }
else if (Status == 2) {
120 llvm::errs() <<
"Failed to build AST for some of the files, "
121 <<
"results may be incomplete."
124 assert(Status == 0 &&
"Unexpected status returned");
131 QueryRef Q = QueryParser::parse(*I, QS);
132 if (!Q->run(llvm::outs(), QS))
145 LineEditor LE(
"clang-query");
146 LE.setListCompleter([&QS](StringRef
Line,
size_t Pos) {
147 return QueryParser::complete(
Line,
Pos, QS);
149 while (llvm::Optional<std::string>
Line = LE.readLine()) {
151 Q->run(llvm::outs(), QS);
152 llvm::outs().flush();