thieta created this revision.
thieta added reviewers: steakhal, martong, NoQ.
Herald added a subscriber: rnkovacs.
Herald added a project: All.
thieta requested review of this revision.
Herald added a project: clang.

When doing CTU analysis setup you pre-compile .cpp to .ast and then
you run clang-extdef-mapping on the .cpp file as well. This is a
pretty slow process since we have to recompile the file each time.

With this patch you can now run clang-extdef-mapping directly on
the .ast file. That saves a lot of time.

I tried this on llvm/lib/AsmParser/Parser.cpp and running
extdef-mapping on the .cpp file took 5.4s on my machine.

While running it on the .ast file it took 2s.

This can save a lot of time for the setup phase of CTU analysis.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128704

Files:
  clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp

Index: clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp
===================================================================
--- clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp
+++ clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp
@@ -1,4 +1,4 @@
-//===- ClangExtDefMapGen.cpp -----------------------------------------------===//
+//===- ClangExtDefMapGen.cpp ---------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -13,10 +13,12 @@
 
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/Basic/DiagnosticOptions.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/CrossTU/CrossTranslationUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendActions.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
@@ -29,12 +31,16 @@
 using namespace clang::cross_tu;
 using namespace clang::tooling;
 
-static cl::OptionCategory ClangExtDefMapGenCategory("clang-extdefmapgen options");
+static cl::OptionCategory
+    ClangExtDefMapGenCategory("clang-extdefmapgen options");
 
 class MapExtDefNamesConsumer : public ASTConsumer {
 public:
-  MapExtDefNamesConsumer(ASTContext &Context)
-      : Ctx(Context), SM(Context.getSourceManager()) {}
+  MapExtDefNamesConsumer(ASTContext &Context,
+                         StringRef astFilePath = StringRef())
+      : Ctx(Context), SM(Context.getSourceManager()) {
+    CurrentFileName = astFilePath.str();
+  }
 
   ~MapExtDefNamesConsumer() {
     // Flush results to standard output.
@@ -111,6 +117,46 @@
 
 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
 
+void HandleAST(StringRef astPath) {
+
+  CompilerInstance CI;
+
+  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+  TextDiagnosticPrinter *DiagClient =
+      new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
+  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+  IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
+      new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
+
+  std::unique_ptr<ASTUnit> unit = ASTUnit::LoadFromASTFile(
+      astPath.str(), CI.getPCHContainerOperations()->getRawReader(),
+      ASTUnit::LoadASTOnly, Diags, CI.getFileSystemOpts());
+
+  FileManager fm(CI.getFileSystemOpts());
+  SmallString<128> absPath(astPath);
+  fm.makeAbsolutePath(absPath);
+
+  std::unique_ptr<MapExtDefNamesConsumer> consumer =
+      std::make_unique<MapExtDefNamesConsumer>(unit->getASTContext(), absPath);
+  consumer->HandleTranslationUnit(unit->getASTContext());
+}
+
+void HandleFiles(ArrayRef<std::string> sourceFiles,
+                 CompilationDatabase &compilations) {
+  std::vector<std::string> sourceToBeParsed;
+  for (StringRef src : sourceFiles) {
+    if (src.endswith(".ast"))
+      HandleAST(src);
+    else
+      sourceToBeParsed.push_back(src.str());
+  }
+
+  if (!sourceToBeParsed.empty()) {
+    ClangTool Tool(compilations, sourceToBeParsed);
+    Tool.run(newFrontendActionFactory<MapExtDefNamesAction>().get());
+  }
+}
+
 int main(int argc, const char **argv) {
   // Print a stack trace if we signal out.
   sys::PrintStackTraceOnErrorSignal(argv[0], false);
@@ -127,8 +173,6 @@
   }
   CommonOptionsParser &OptionsParser = ExpectedParser.get();
 
-  ClangTool Tool(OptionsParser.getCompilations(),
-                 OptionsParser.getSourcePathList());
-
-  return Tool.run(newFrontendActionFactory<MapExtDefNamesAction>().get());
+  HandleFiles(OptionsParser.getSourcePathList(),
+              OptionsParser.getCompilations());
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to