2013-11-08 08:08:23 +08:00
|
|
|
//===---- ClangQuery.cpp - clang-query tool -------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tool is for interactive exploration of the Clang AST using AST matchers.
|
|
|
|
// It currently allows the user to enter a matcher at an interactive prompt and
|
|
|
|
// view the resulting bindings as diagnostics, AST pretty prints or AST dumps.
|
|
|
|
// Example session:
|
|
|
|
//
|
|
|
|
// $ cat foo.c
|
|
|
|
// void foo(void) {}
|
|
|
|
// $ clang-query foo.c --
|
|
|
|
// clang-query> match functionDecl()
|
|
|
|
//
|
|
|
|
// Match #1:
|
|
|
|
//
|
|
|
|
// foo.c:1:1: note: "root" binds here
|
|
|
|
// void foo(void) {}
|
|
|
|
// ^~~~~~~~~~~~~~~~~
|
|
|
|
// 1 match.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Query.h"
|
|
|
|
#include "QueryParser.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "QuerySession.h"
|
2013-11-08 08:08:23 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2014-08-02 09:02:33 +08:00
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
2013-11-08 08:08:23 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
2014-02-01 09:42:42 +08:00
|
|
|
#include "llvm/LineEditor/LineEditor.h"
|
2013-11-08 08:08:23 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include <fstream>
|
2014-01-08 04:05:01 +08:00
|
|
|
#include <string>
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::ast_matchers::dynamic;
|
|
|
|
using namespace clang::query;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-08-02 09:02:33 +08:00
|
|
|
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
|
|
|
|
static cl::OptionCategory ClangQueryCategory("clang-query options");
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
static cl::list<std::string> Commands("c", cl::desc("Specify command to run"),
|
2014-08-02 09:02:33 +08:00
|
|
|
cl::value_desc("command"),
|
|
|
|
cl::cat(ClangQueryCategory));
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
static cl::list<std::string> CommandFiles("f",
|
|
|
|
cl::desc("Read commands from file"),
|
2014-08-02 09:02:33 +08:00
|
|
|
cl::value_desc("file"),
|
|
|
|
cl::cat(ClangQueryCategory));
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
2016-06-09 08:54:42 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2014-08-02 09:02:33 +08:00
|
|
|
|
|
|
|
CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory);
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
if (!Commands.empty() && !CommandFiles.empty()) {
|
|
|
|
llvm::errs() << argv[0] << ": cannot specify both -c and -f\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-02 09:02:33 +08:00
|
|
|
ClangTool Tool(OptionsParser.getCompilations(),
|
|
|
|
OptionsParser.getSourcePathList());
|
2014-04-25 23:06:18 +08:00
|
|
|
std::vector<std::unique_ptr<ASTUnit>> ASTs;
|
2013-11-08 08:08:23 +08:00
|
|
|
if (Tool.buildASTs(ASTs) != 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
QuerySession QS(ASTs);
|
|
|
|
|
|
|
|
if (!Commands.empty()) {
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) {
|
2016-11-02 04:07:05 +08:00
|
|
|
QueryRef Q = QueryParser::parse(*I, QS);
|
2013-11-08 08:08:23 +08:00
|
|
|
if (!Q->run(llvm::outs(), QS))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (!CommandFiles.empty()) {
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto I = CommandFiles.begin(), E = CommandFiles.end(); I != E; ++I) {
|
2013-11-08 08:08:23 +08:00
|
|
|
std::ifstream Input(I->c_str());
|
|
|
|
if (!Input.is_open()) {
|
|
|
|
llvm::errs() << argv[0] << ": cannot open " << *I << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while (Input.good()) {
|
|
|
|
std::string Line;
|
|
|
|
std::getline(Input, Line);
|
|
|
|
|
2016-11-02 04:07:05 +08:00
|
|
|
QueryRef Q = QueryParser::parse(Line, QS);
|
2013-11-08 08:08:23 +08:00
|
|
|
if (!Q->run(llvm::outs(), QS))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-01 09:42:42 +08:00
|
|
|
LineEditor LE("clang-query");
|
2014-04-23 22:04:52 +08:00
|
|
|
LE.setListCompleter([&QS](StringRef Line, size_t Pos) {
|
|
|
|
return QueryParser::complete(Line, Pos, QS);
|
|
|
|
});
|
2014-02-01 09:42:42 +08:00
|
|
|
while (llvm::Optional<std::string> Line = LE.readLine()) {
|
2014-04-23 22:04:52 +08:00
|
|
|
QueryRef Q = QueryParser::parse(*Line, QS);
|
2013-11-08 08:08:23 +08:00
|
|
|
Q->run(llvm::outs(), QS);
|
2014-05-22 02:10:47 +08:00
|
|
|
llvm::outs().flush();
|
2015-08-06 19:56:57 +08:00
|
|
|
if (QS.Terminate)
|
|
|
|
break;
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|