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/Error.h"
37#include "llvm/Support/MemoryBuffer.h"
38#include "llvm/Support/Signals.h"
39#include "llvm/Support/WithColor.h"
45using namespace clang::ast_matchers::dynamic;
50static cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
56 R
"(Use colors in detailed AST output. If not set, colors
57will be used if the terminal connected to
58standard output supports colors.)"),
61static cl::list<std::string>
Commands(
"c", cl::desc(
"Specify command to run"),
62 cl::value_desc(
"command"),
66 cl::desc(
"Read commands from file"),
67 cl::value_desc(
"file"),
72 cl::desc(
"Preload commands from file and start interactive mode"),
81int main(
int argc,
const char **argv) {
82 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
84 llvm::Expected<CommonOptionsParser> OptionsParser =
89 llvm::WithColor::error() << llvm::toString(OptionsParser.takeError());
94 llvm::errs() << argv[0] <<
": cannot specify both -c and -f\n";
99 llvm::errs() << argv[0]
100 <<
": cannot specify both -c or -f with --preload\n";
104 ClangTool Tool(OptionsParser->getCompilations(),
105 OptionsParser->getSourcePathList());
107 if (
UseColor.getNumOccurrences() > 0) {
108 ArgumentsAdjuster colorAdjustor = [](
const CommandLineArguments &
Args, StringRef ) {
109 CommandLineArguments AdjustedArgs =
Args;
111 AdjustedArgs.push_back(
"-fdiagnostics-color");
113 AdjustedArgs.push_back(
"-fno-diagnostics-color");
116 Tool.appendArgumentsAdjuster(colorAdjustor);
119 std::vector<std::unique_ptr<ASTUnit>> ASTs;
121 switch (Tool.buildASTs(ASTs)) {
128 llvm::errs() <<
"Failed to build AST for some of the files, "
129 <<
"results may be incomplete."
133 llvm_unreachable(
"Unexpected status returned");
141 if (!Q->run(llvm::outs(), QS))
154 LineEditor LE(
"clang-query");
155 LE.setListCompleter([&QS](StringRef
Line,
size_t Pos) {
158 while (std::optional<std::string>
Line = LE.readLine()) {
160 Q->run(llvm::outs(), QS);
161 llvm::outs().flush();
static cl::OptionCategory ClangQueryCategory("clang-query options")
bool runCommandsInFile(const char *ExeName, std::string const &FileName, QuerySession &QS)
int main(int argc, const char **argv)
static cl::list< std::string > CommandFiles("f", cl::desc("Read commands from file"), cl::value_desc("file"), cl::cat(ClangQueryCategory))
static cl::opt< std::string > PreloadFile("preload", cl::desc("Preload commands from file and start interactive mode"), cl::value_desc("file"), cl::cat(ClangQueryCategory))
static cl::opt< bool > UseColor("use-color", cl::desc(R"(Use colors in detailed AST output. If not set, colors
will be used if the terminal connected to
standard output supports colors.)"), cl::init(false), cl::cat(ClangQueryCategory))
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage)
static QueryRef parse(StringRef Line, const QuerySession &QS)
Parse Line as a query.
static std::vector< llvm::LineEditor::Completion > complete(StringRef Line, size_t Pos, const QuerySession &QS)
Compute a list of completions for Line assuming a cursor at.
Represents the state for a particular clang-query session.
llvm::IntrusiveRefCntPtr< Query > QueryRef
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const =0
Perform the query on QS and print output to OS.