core_main.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===-- core_main.cpp - Core Index Tool testbed ---------------------------===//
  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. #include "clang/Frontend/ASTUnit.h"
  10. #include "clang/Frontend/CompilerInstance.h"
  11. #include "clang/Frontend/CompilerInvocation.h"
  12. #include "clang/Frontend/FrontendAction.h"
  13. #include "clang/Index/IndexingAction.h"
  14. #include "clang/Index/IndexDataConsumer.h"
  15. #include "clang/Index/USRGeneration.h"
  16. #include "clang/Index/CodegenNameGenerator.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/Signals.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include "llvm/Support/PrettyStackTrace.h"
  21. using namespace clang;
  22. using namespace clang::index;
  23. using namespace llvm;
  24. extern "C" int indextest_core_main(int argc, const char **argv);
  25. namespace {
  26. enum class ActionType {
  27. None,
  28. PrintSourceSymbols,
  29. };
  30. namespace options {
  31. static cl::OptionCategory IndexTestCoreCategory("index-test-core options");
  32. static cl::opt<ActionType>
  33. Action(cl::desc("Action:"), cl::init(ActionType::None),
  34. cl::values(
  35. clEnumValN(ActionType::PrintSourceSymbols,
  36. "print-source-symbols", "Print symbols from source")),
  37. cl::cat(IndexTestCoreCategory));
  38. static cl::extrahelp MoreHelp(
  39. "\nAdd \"-- <compiler arguments>\" at the end to setup the compiler "
  40. "invocation\n"
  41. );
  42. }
  43. } // anonymous namespace
  44. static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS);
  45. static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
  46. raw_ostream &OS);
  47. namespace {
  48. class PrintIndexDataConsumer : public IndexDataConsumer {
  49. raw_ostream &OS;
  50. std::unique_ptr<CodegenNameGenerator> CGNameGen;
  51. public:
  52. PrintIndexDataConsumer(raw_ostream &OS) : OS(OS) {
  53. }
  54. void initialize(ASTContext &Ctx) override {
  55. CGNameGen.reset(new CodegenNameGenerator(Ctx));
  56. }
  57. bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
  58. ArrayRef<SymbolRelation> Relations,
  59. FileID FID, unsigned Offset,
  60. ASTNodeInfo ASTNode) override {
  61. ASTContext &Ctx = D->getASTContext();
  62. SourceManager &SM = Ctx.getSourceManager();
  63. unsigned Line = SM.getLineNumber(FID, Offset);
  64. unsigned Col = SM.getColumnNumber(FID, Offset);
  65. OS << Line << ':' << Col << " | ";
  66. printSymbolInfo(getSymbolInfo(D), OS);
  67. OS << " | ";
  68. printSymbolNameAndUSR(D, Ctx, OS);
  69. OS << " | ";
  70. if (CGNameGen->writeName(D, OS))
  71. OS << "<no-cgname>";
  72. OS << " | ";
  73. printSymbolRoles(Roles, OS);
  74. OS << " | ";
  75. OS << "rel: " << Relations.size() << '\n';
  76. for (auto &SymRel : Relations) {
  77. OS << '\t';
  78. printSymbolRoles(SymRel.Roles, OS);
  79. OS << " | ";
  80. printSymbolNameAndUSR(SymRel.RelatedSymbol, Ctx, OS);
  81. OS << '\n';
  82. }
  83. return true;
  84. }
  85. bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles,
  86. FileID FID, unsigned Offset) override {
  87. ASTContext &Ctx = ImportD->getASTContext();
  88. SourceManager &SM = Ctx.getSourceManager();
  89. unsigned Line = SM.getLineNumber(FID, Offset);
  90. unsigned Col = SM.getColumnNumber(FID, Offset);
  91. OS << Line << ':' << Col << " | ";
  92. printSymbolInfo(getSymbolInfo(ImportD), OS);
  93. OS << " | ";
  94. OS << ImportD->getImportedModule()->getFullModuleName() << " | ";
  95. printSymbolRoles(Roles, OS);
  96. OS << " |\n";
  97. return true;
  98. }
  99. };
  100. } // anonymous namespace
  101. //===----------------------------------------------------------------------===//
  102. // Print Source Symbols
  103. //===----------------------------------------------------------------------===//
  104. static bool printSourceSymbols(ArrayRef<const char *> Args) {
  105. SmallVector<const char *, 4> ArgsWithProgName;
  106. ArgsWithProgName.push_back("clang");
  107. ArgsWithProgName.append(Args.begin(), Args.end());
  108. IntrusiveRefCntPtr<DiagnosticsEngine>
  109. Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
  110. auto CInvok = createInvocationFromCommandLine(ArgsWithProgName, Diags);
  111. if (!CInvok)
  112. return true;
  113. auto DataConsumer = std::make_shared<PrintIndexDataConsumer>(outs());
  114. IndexingOptions IndexOpts;
  115. std::unique_ptr<FrontendAction> IndexAction;
  116. IndexAction = createIndexingAction(DataConsumer, IndexOpts,
  117. /*WrappedAction=*/nullptr);
  118. auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
  119. auto Unit = ASTUnit::LoadFromCompilerInvocationAction(
  120. std::move(CInvok), PCHContainerOps, Diags, IndexAction.get());
  121. if (!Unit)
  122. return true;
  123. return false;
  124. }
  125. //===----------------------------------------------------------------------===//
  126. // Helper Utils
  127. //===----------------------------------------------------------------------===//
  128. static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS) {
  129. OS << getSymbolKindString(SymInfo.Kind);
  130. if (SymInfo.Properties) {
  131. OS << '(';
  132. printSymbolProperties(SymInfo.Properties, OS);
  133. OS << ')';
  134. }
  135. OS << '/' << getSymbolLanguageString(SymInfo.Lang);
  136. }
  137. static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
  138. raw_ostream &OS) {
  139. if (printSymbolName(D, Ctx.getLangOpts(), OS)) {
  140. OS << "<no-name>";
  141. }
  142. OS << " | ";
  143. SmallString<256> USRBuf;
  144. if (generateUSRForDecl(D, USRBuf)) {
  145. OS << "<no-usr>";
  146. } else {
  147. OS << USRBuf;
  148. }
  149. }
  150. //===----------------------------------------------------------------------===//
  151. // Command line processing.
  152. //===----------------------------------------------------------------------===//
  153. int indextest_core_main(int argc, const char **argv) {
  154. sys::PrintStackTraceOnErrorSignal(argv[0]);
  155. PrettyStackTraceProgram X(argc, argv);
  156. assert(argv[1] == StringRef("core"));
  157. ++argv;
  158. --argc;
  159. std::vector<const char *> CompArgs;
  160. const char **DoubleDash = std::find(argv, argv + argc, StringRef("--"));
  161. if (DoubleDash != argv + argc) {
  162. CompArgs = std::vector<const char *>(DoubleDash + 1, argv + argc);
  163. argc = DoubleDash - argv;
  164. }
  165. cl::HideUnrelatedOptions(options::IndexTestCoreCategory);
  166. cl::ParseCommandLineOptions(argc, argv, "index-test-core");
  167. if (options::Action == ActionType::None) {
  168. errs() << "error: action required; pass '-help' for options\n";
  169. return 1;
  170. }
  171. if (options::Action == ActionType::PrintSourceSymbols) {
  172. if (CompArgs.empty()) {
  173. errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n";
  174. return 1;
  175. }
  176. return printSourceSymbols(CompArgs);
  177. }
  178. return 0;
  179. }