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 bool runCommandsInFile(const char *ExeName, std::string const &FileName, 62 QuerySession &QS) { 63 std::ifstream Input(FileName.c_str()); 64 if (!Input.is_open()) { 65 llvm::errs() << ExeName << ": cannot open " << FileName << "\n"; 66 return 1; 67 } 68 while (Input.good()) { 69 std::string Line; 70 std::getline(Input, Line); 71 72 QueryRef Q = QueryParser::parse(Line, QS); 73 if (!Q->run(llvm::outs(), QS)) 74 return true; 75 } 76 return false; 77 } 78 79 int main(int argc, const char **argv) { 80 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); 81 82 CommonOptionsParser OptionsParser(argc, argv, ClangQueryCategory); 83 84 if (!Commands.empty() && !CommandFiles.empty()) { 85 llvm::errs() << argv[0] << ": cannot specify both -c and -f\n"; 86 return 1; 87 } 88 89 ClangTool Tool(OptionsParser.getCompilations(), 90 OptionsParser.getSourcePathList()); 91 std::vector<std::unique_ptr<ASTUnit>> ASTs; 92 if (Tool.buildASTs(ASTs) != 0) 93 return 1; 94 95 QuerySession QS(ASTs); 96 97 if (!Commands.empty()) { 98 for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) { 99 QueryRef Q = QueryParser::parse(*I, QS); 100 if (!Q->run(llvm::outs(), QS)) 101 return 1; 102 } 103 } else if (!CommandFiles.empty()) { 104 for (auto I = CommandFiles.begin(), E = CommandFiles.end(); I != E; ++I) { 105 if (runCommandsInFile(argv[0], *I, QS)) 106 return 1; 107 } 108 } else { 109 LineEditor LE("clang-query"); 110 LE.setListCompleter([&QS](StringRef Line, size_t Pos) { 111 return QueryParser::complete(Line, Pos, QS); 112 }); 113 while (llvm::Optional<std::string> Line = LE.readLine()) { 114 QueryRef Q = QueryParser::parse(*Line, QS); 115 Q->run(llvm::outs(), QS); 116 llvm::outs().flush(); 117 if (QS.Terminate) 118 break; 119 } 120 } 121 122 return 0; 123 } 124