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"),
77 auto Buffer = llvm::MemoryBuffer::getFile(
FileName);
79 llvm::errs() << ExeName <<
": cannot open " <<
FileName <<
": "
80 << Buffer.getError().message() <<
"\n";
84 StringRef FileContentRef(Buffer.get()->getBuffer());
86 while (!FileContentRef.empty()) {
88 if (!Q->run(llvm::outs(), QS))
90 FileContentRef = Q->RemainingContent;
95int main(
int argc,
const char **argv) {
96 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
98 llvm::Expected<CommonOptionsParser> OptionsParser =
100 llvm::cl::OneOrMore);
102 if (!OptionsParser) {
103 llvm::WithColor::error() << llvm::toString(OptionsParser.takeError());
108 llvm::errs() << argv[0] <<
": cannot specify both -c and -f\n";
113 llvm::errs() << argv[0]
114 <<
": cannot specify both -c or -f with --preload\n";
118 ClangTool Tool(OptionsParser->getCompilations(),
119 OptionsParser->getSourcePathList());
121 if (
UseColor.getNumOccurrences() > 0) {
122 ArgumentsAdjuster colorAdjustor = [](
const CommandLineArguments &
Args, StringRef ) {
123 CommandLineArguments AdjustedArgs =
Args;
125 AdjustedArgs.push_back(
"-fdiagnostics-color");
127 AdjustedArgs.push_back(
"-fno-diagnostics-color");
130 Tool.appendArgumentsAdjuster(colorAdjustor);
133 std::vector<std::unique_ptr<ASTUnit>> ASTs;
135 switch (Tool.buildASTs(ASTs)) {
142 llvm::errs() <<
"Failed to build AST for some of the files, "
143 <<
"results may be incomplete."
147 llvm_unreachable(
"Unexpected status returned");
155 if (!Q->run(llvm::outs(), QS))
168 LineEditor LE(
"clang-query");
169 LE.setListCompleter([&QS](StringRef
Line,
size_t Pos) {
172 while (std::optional<std::string>
Line = LE.readLine()) {
174 Q->run(llvm::outs(), QS);
175 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.