clang-tools  8.0.0
ClangQuery.cpp
Go to the documentation of this file.
1 //===---- 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 "QueryParser.h"
31 #include "QuerySession.h"
32 #include "clang/Frontend/ASTUnit.h"
33 #include "clang/Tooling/CommonOptionsParser.h"
34 #include "clang/Tooling/Tooling.h"
35 #include "llvm/LineEditor/LineEditor.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/Signals.h"
39 #include <fstream>
40 #include <string>
41 
42 using namespace clang;
43 using namespace clang::ast_matchers;
44 using namespace clang::ast_matchers::dynamic;
45 using namespace clang::query;
46 using namespace clang::tooling;
47 using namespace llvm;
48 
49 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
50 static cl::OptionCategory ClangQueryCategory("clang-query options");
51 
52 static cl::list<std::string> Commands("c", cl::desc("Specify command to run"),
53  cl::value_desc("command"),
54  cl::cat(ClangQueryCategory));
55 
56 static cl::list<std::string> CommandFiles("f",
57  cl::desc("Read commands from file"),
58  cl::value_desc("file"),
59  cl::cat(ClangQueryCategory));
60 
61 static cl::opt<std::string> PreloadFile(
62  "preload",
63  cl::desc("Preload commands from file and start interactive mode"),
64  cl::value_desc("file"), cl::cat(ClangQueryCategory));
65 
66 bool runCommandsInFile(const char *ExeName, std::string const &FileName,
67  QuerySession &QS) {
68  std::ifstream Input(FileName.c_str());
69  if (!Input.is_open()) {
70  llvm::errs() << ExeName << ": cannot open " << FileName << "\n";
71  return 1;
72  }
73  while (Input.good()) {
74  std::string Line;
75  std::getline(Input, Line);
76 
77  QueryRef Q = QueryParser::parse(Line, QS);
78  if (!Q->run(llvm::outs(), QS))
79  return true;
80  }
81  return false;
82 }
83 
84 int main(int argc, const char **argv) {
85  llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
86 
87  CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory);
88 
89  if (!Commands.empty() && !CommandFiles.empty()) {
90  llvm::errs() << argv[0] << ": cannot specify both -c and -f\n";
91  return 1;
92  }
93 
94  if ((!Commands.empty() || !CommandFiles.empty()) && !PreloadFile.empty()) {
95  llvm::errs() << argv[0]
96  << ": cannot specify both -c or -f with --preload\n";
97  return 1;
98  }
99 
100  ClangTool Tool(OptionsParser.getCompilations(),
101  OptionsParser.getSourcePathList());
102  std::vector<std::unique_ptr<ASTUnit>> ASTs;
103  int Status = Tool.buildASTs(ASTs);
104  int ASTStatus = 0;
105  if (Status == 1) {
106  // Building ASTs failed.
107  return 1;
108  } else if (Status == 2) {
109  ASTStatus |= 1;
110  llvm::errs() << "Failed to build AST for some of the files, "
111  << "results may be incomplete."
112  << "\n";
113  } else {
114  assert(Status == 0 && "Unexpected status returned");
115  }
116 
117  QuerySession QS(ASTs);
118 
119  if (!Commands.empty()) {
120  for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) {
121  QueryRef Q = QueryParser::parse(*I, QS);
122  if (!Q->run(llvm::outs(), QS))
123  return 1;
124  }
125  } else if (!CommandFiles.empty()) {
126  for (auto I = CommandFiles.begin(), E = CommandFiles.end(); I != E; ++I) {
127  if (runCommandsInFile(argv[0], *I, QS))
128  return 1;
129  }
130  } else {
131  if (!PreloadFile.empty()) {
132  if (runCommandsInFile(argv[0], PreloadFile, QS))
133  return 1;
134  }
135  LineEditor LE("clang-query");
136  LE.setListCompleter([&QS](StringRef Line, size_t Pos) {
137  return QueryParser::complete(Line, Pos, QS);
138  });
139  while (llvm::Optional<std::string> Line = LE.readLine()) {
140  QueryRef Q = QueryParser::parse(*Line, QS);
141  Q->run(llvm::outs(), QS);
142  llvm::outs().flush();
143  if (QS.Terminate)
144  break;
145  }
146  }
147 
148  return ASTStatus;
149 }
Some operations such as code completion produce a set of candidates.
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
Represents the state for a particular clang-query session.
Definition: QuerySession.h:24
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage)
static cl::OptionCategory ClangQueryCategory("clang-query options")
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))
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:51
int main(int argc, const char **argv)
Definition: ClangQuery.cpp:84
PathRef FileName
Position Pos
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool runCommandsInFile(const char *ExeName, std::string const &FileName, QuerySession &QS)
Definition: ClangQuery.cpp:66