core_main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/CodeGen/ObjectFilePCHContainerOperations.h"
  10. #include "clang/Frontend/ASTUnit.h"
  11. #include "clang/Frontend/CompilerInstance.h"
  12. #include "clang/Frontend/CompilerInvocation.h"
  13. #include "clang/Frontend/FrontendAction.h"
  14. #include "clang/Index/IndexingAction.h"
  15. #include "clang/Index/IndexDataConsumer.h"
  16. #include "clang/Index/USRGeneration.h"
  17. #include "clang/Index/CodegenNameGenerator.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/Signals.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "llvm/Support/PrettyStackTrace.h"
  22. using namespace clang;
  23. using namespace clang::index;
  24. using namespace llvm;
  25. extern "C" int indextest_core_main(int argc, const char **argv);
  26. namespace {
  27. enum class ActionType {
  28. None,
  29. PrintSourceSymbols,
  30. };
  31. namespace options {
  32. static cl::OptionCategory IndexTestCoreCategory("index-test-core options");
  33. static cl::opt<ActionType>
  34. Action(cl::desc("Action:"), cl::init(ActionType::None),
  35. cl::values(
  36. clEnumValN(ActionType::PrintSourceSymbols,
  37. "print-source-symbols", "Print symbols from source")),
  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. static cl::opt<std::string>
  44. ModuleFilePath("module-file",
  45. cl::desc("Path to module file to print symbols from"));
  46. static cl::opt<std::string>
  47. ModuleFormat("fmodule-format", cl::init("raw"),
  48. cl::desc("Container format for clang modules and PCH, 'raw' or 'obj'"));
  49. }
  50. } // anonymous namespace
  51. static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS);
  52. static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
  53. raw_ostream &OS);
  54. namespace {
  55. class PrintIndexDataConsumer : public IndexDataConsumer {
  56. raw_ostream &OS;
  57. std::unique_ptr<CodegenNameGenerator> CGNameGen;
  58. public:
  59. PrintIndexDataConsumer(raw_ostream &OS) : OS(OS) {
  60. }
  61. void initialize(ASTContext &Ctx) override {
  62. CGNameGen.reset(new CodegenNameGenerator(Ctx));
  63. }
  64. bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
  65. ArrayRef<SymbolRelation> Relations,
  66. FileID FID, unsigned Offset,
  67. ASTNodeInfo ASTNode) override {
  68. ASTContext &Ctx = D->getASTContext();
  69. SourceManager &SM = Ctx.getSourceManager();
  70. unsigned Line = SM.getLineNumber(FID, Offset);
  71. unsigned Col = SM.getColumnNumber(FID, Offset);
  72. OS << Line << ':' << Col << " | ";
  73. printSymbolInfo(getSymbolInfo(D), OS);
  74. OS << " | ";
  75. printSymbolNameAndUSR(D, Ctx, OS);
  76. OS << " | ";
  77. if (CGNameGen->writeName(D, OS))
  78. OS << "<no-cgname>";
  79. OS << " | ";
  80. printSymbolRoles(Roles, OS);
  81. OS << " | ";
  82. OS << "rel: " << Relations.size() << '\n';
  83. for (auto &SymRel : Relations) {
  84. OS << '\t';
  85. printSymbolRoles(SymRel.Roles, OS);
  86. OS << " | ";
  87. printSymbolNameAndUSR(SymRel.RelatedSymbol, Ctx, OS);
  88. OS << '\n';
  89. }
  90. return true;
  91. }
  92. bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles,
  93. FileID FID, unsigned Offset) override {
  94. ASTContext &Ctx = ImportD->getASTContext();
  95. SourceManager &SM = Ctx.getSourceManager();
  96. unsigned Line = SM.getLineNumber(FID, Offset);
  97. unsigned Col = SM.getColumnNumber(FID, Offset);
  98. OS << Line << ':' << Col << " | ";
  99. printSymbolInfo(getSymbolInfo(ImportD), OS);
  100. OS << " | ";
  101. OS << ImportD->getImportedModule()->getFullModuleName() << " | ";
  102. printSymbolRoles(Roles, OS);
  103. OS << " |\n";
  104. return true;
  105. }
  106. };
  107. } // anonymous namespace
  108. //===----------------------------------------------------------------------===//
  109. // Print Source Symbols
  110. //===----------------------------------------------------------------------===//
  111. static bool printSourceSymbols(ArrayRef<const char *> Args) {
  112. SmallVector<const char *, 4> ArgsWithProgName;
  113. ArgsWithProgName.push_back("clang");
  114. ArgsWithProgName.append(Args.begin(), Args.end());
  115. IntrusiveRefCntPtr<DiagnosticsEngine>
  116. Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
  117. auto CInvok = createInvocationFromCommandLine(ArgsWithProgName, Diags);
  118. if (!CInvok)
  119. return true;
  120. auto DataConsumer = std::make_shared<PrintIndexDataConsumer>(outs());
  121. IndexingOptions IndexOpts;
  122. std::unique_ptr<FrontendAction> IndexAction;
  123. IndexAction = createIndexingAction(DataConsumer, IndexOpts,
  124. /*WrappedAction=*/nullptr);
  125. auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
  126. std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
  127. std::move(CInvok), PCHContainerOps, Diags, IndexAction.get()));
  128. if (!Unit)
  129. return true;
  130. return false;
  131. }
  132. static bool printSourceSymbolsFromModule(StringRef modulePath,
  133. StringRef format) {
  134. FileSystemOptions FileSystemOpts;
  135. auto pchContOps = std::make_shared<PCHContainerOperations>();
  136. // Register the support for object-file-wrapped Clang modules.
  137. pchContOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
  138. auto pchRdr = pchContOps->getReaderOrNull(format);
  139. if (!pchRdr) {
  140. errs() << "unknown module format: " << format << '\n';
  141. return true;
  142. }
  143. IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
  144. CompilerInstance::createDiagnostics(new DiagnosticOptions());
  145. std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
  146. modulePath, *pchRdr, Diags,
  147. FileSystemOpts, /*UseDebugInfo=*/false,
  148. /*OnlyLocalDecls=*/true, None,
  149. /*CaptureDiagnostics=*/false,
  150. /*AllowPCHWithCompilerErrors=*/true,
  151. /*UserFilesAreVolatile=*/false);
  152. if (!AU) {
  153. errs() << "failed to create TU for: " << modulePath << '\n';
  154. return true;
  155. }
  156. auto DataConsumer = std::make_shared<PrintIndexDataConsumer>(outs());
  157. IndexingOptions IndexOpts;
  158. indexASTUnit(*AU, DataConsumer, IndexOpts);
  159. return false;
  160. }
  161. //===----------------------------------------------------------------------===//
  162. // Helper Utils
  163. //===----------------------------------------------------------------------===//
  164. static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS) {
  165. OS << getSymbolKindString(SymInfo.Kind);
  166. if (SymInfo.SubKind != SymbolSubKind::None)
  167. OS << '/' << getSymbolSubKindString(SymInfo.SubKind);
  168. if (SymInfo.Properties) {
  169. OS << '(';
  170. printSymbolProperties(SymInfo.Properties, OS);
  171. OS << ')';
  172. }
  173. OS << '/' << getSymbolLanguageString(SymInfo.Lang);
  174. }
  175. static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
  176. raw_ostream &OS) {
  177. if (printSymbolName(D, Ctx.getLangOpts(), OS)) {
  178. OS << "<no-name>";
  179. }
  180. OS << " | ";
  181. SmallString<256> USRBuf;
  182. if (generateUSRForDecl(D, USRBuf)) {
  183. OS << "<no-usr>";
  184. } else {
  185. OS << USRBuf;
  186. }
  187. }
  188. //===----------------------------------------------------------------------===//
  189. // Command line processing.
  190. //===----------------------------------------------------------------------===//
  191. int indextest_core_main(int argc, const char **argv) {
  192. sys::PrintStackTraceOnErrorSignal(argv[0]);
  193. PrettyStackTraceProgram X(argc, argv);
  194. assert(argv[1] == StringRef("core"));
  195. ++argv;
  196. --argc;
  197. std::vector<const char *> CompArgs;
  198. const char **DoubleDash = std::find(argv, argv + argc, StringRef("--"));
  199. if (DoubleDash != argv + argc) {
  200. CompArgs = std::vector<const char *>(DoubleDash + 1, argv + argc);
  201. argc = DoubleDash - argv;
  202. }
  203. cl::HideUnrelatedOptions(options::IndexTestCoreCategory);
  204. cl::ParseCommandLineOptions(argc, argv, "index-test-core");
  205. if (options::Action == ActionType::None) {
  206. errs() << "error: action required; pass '-help' for options\n";
  207. return 1;
  208. }
  209. if (options::Action == ActionType::PrintSourceSymbols) {
  210. if (!options::ModuleFilePath.empty()) {
  211. return printSourceSymbolsFromModule(options::ModuleFilePath,
  212. options::ModuleFormat);
  213. }
  214. if (CompArgs.empty()) {
  215. errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n";
  216. return 1;
  217. }
  218. return printSourceSymbols(CompArgs);
  219. }
  220. return 0;
  221. }