core_main.cpp 6.6 KB

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