gribozavr created this revision.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66947

Files:
  clang-tools-extra/clang-doc/ClangDoc.cpp
  clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h
  clang-tools-extra/clang-move/Move.h
  clang-tools-extra/clang-tidy/ClangTidy.cpp
  clang-tools-extra/clangd/indexer/IndexerMain.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/modularize/CoverageChecker.cpp
  clang-tools-extra/modularize/Modularize.cpp
  clang-tools-extra/pp-trace/PPTrace.cpp
  clang/include/clang/Tooling/Tooling.h
  clang/lib/Tooling/Tooling.cpp
  clang/tools/clang-refactor/ClangRefactor.cpp
  clang/unittests/Tooling/ExecutionTest.cpp

Index: clang/unittests/Tooling/ExecutionTest.cpp
===================================================================
--- clang/unittests/Tooling/ExecutionTest.cpp
+++ clang/unittests/Tooling/ExecutionTest.cpp
@@ -78,7 +78,9 @@
 class ReportResultActionFactory : public FrontendActionFactory {
 public:
   ReportResultActionFactory(ExecutionContext *Context) : Context(Context) {}
-  FrontendAction *create() override { return new ReportResultAction(Context); }
+  std::unique_ptr<FrontendAction> create() override {
+    return std::make_unique<ReportResultAction>(Context);
+  }
 
 private:
   ExecutionContext *const Context;
Index: clang/tools/clang-refactor/ClangRefactor.cpp
===================================================================
--- clang/tools/clang-refactor/ClangRefactor.cpp
+++ clang/tools/clang-refactor/ClangRefactor.cpp
@@ -461,7 +461,9 @@
       ToolActionFactory(TUCallbackType Callback)
           : Callback(std::move(Callback)) {}
 
-      FrontendAction *create() override { return new ToolASTAction(Callback); }
+      std::unique_ptr<FrontendAction> create() override {
+        return std::make_unique<ToolASTAction>(Callback);
+      }
 
     private:
       TUCallbackType Callback;
Index: clang/lib/Tooling/Tooling.cpp
===================================================================
--- clang/lib/Tooling/Tooling.cpp
+++ clang/lib/Tooling/Tooling.cpp
@@ -247,12 +247,15 @@
 namespace {
 
 class SingleFrontendActionFactory : public FrontendActionFactory {
-  FrontendAction *Action;
+  std::unique_ptr<FrontendAction> Action;
 
 public:
-  SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
+  SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
+      : Action(std::move(Action)) {}
 
-  FrontendAction *create() override { return Action; }
+  std::unique_ptr<FrontendAction> create() override {
+    return std::move(Action);
+  }
 };
 
 } // namespace
@@ -267,8 +270,10 @@
     std::vector<std::string> CommandLine, FrontendAction *FAction,
     FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
     : CommandLine(std::move(CommandLine)),
-      Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true),
-      Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
+      Action(new SingleFrontendActionFactory(
+          std::unique_ptr<FrontendAction>(FAction))),
+      OwnsAction(true), Files(Files),
+      PCHContainerOps(std::move(PCHContainerOps)) {}
 
 ToolInvocation::~ToolInvocation() {
   if (OwnsAction)
Index: clang/include/clang/Tooling/Tooling.h
===================================================================
--- clang/include/clang/Tooling/Tooling.h
+++ clang/include/clang/Tooling/Tooling.h
@@ -99,9 +99,7 @@
                      DiagnosticConsumer *DiagConsumer) override;
 
   /// Returns a new clang::FrontendAction.
-  ///
-  /// The caller takes ownership of the returned action.
-  virtual FrontendAction *create() = 0;
+  virtual std::unique_ptr<FrontendAction> create() = 0;
 };
 
 /// Returns a new FrontendActionFactory for a given type.
@@ -161,6 +159,14 @@
                    std::shared_ptr<PCHContainerOperations> PCHContainerOps =
                        std::make_shared<PCHContainerOperations>());
 
+inline bool
+runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
+              const Twine &FileName = "input.cc",
+              std::shared_ptr<PCHContainerOperations> PCHContainerOps =
+                  std::make_shared<PCHContainerOperations>()) {
+  return runToolOnCode(ToolAction.release(), Code, FileName, PCHContainerOps);
+}
+
 /// The first part of the pair is the filename, the second part the
 /// file-content.
 using FileContentMappings = std::vector<std::pair<std::string, std::string>>;
@@ -186,6 +192,17 @@
         std::make_shared<PCHContainerOperations>(),
     const FileContentMappings &VirtualMappedFiles = FileContentMappings());
 
+inline bool runToolOnCodeWithArgs(
+    std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
+    const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
+    const Twine &ToolName = "clang-tool",
+    std::shared_ptr<PCHContainerOperations> PCHContainerOps =
+        std::make_shared<PCHContainerOperations>(),
+    const FileContentMappings &VirtualMappedFiles = FileContentMappings()) {
+  return runToolOnCodeWithArgs(ToolAction.release(), Code, Args, FileName,
+                               ToolName, PCHContainerOps, VirtualMappedFiles);
+}
+
 // Similar to the overload except this takes a VFS.
 bool runToolOnCodeWithArgs(
     FrontendAction *ToolAction, const Twine &Code,
@@ -195,6 +212,17 @@
     std::shared_ptr<PCHContainerOperations> PCHContainerOps =
         std::make_shared<PCHContainerOperations>());
 
+inline bool runToolOnCodeWithArgs(
+    std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
+    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
+    const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
+    const Twine &ToolName = "clang-tool",
+    std::shared_ptr<PCHContainerOperations> PCHContainerOps =
+        std::make_shared<PCHContainerOperations>()) {
+  return runToolOnCodeWithArgs(ToolAction.release(), Code, VFS, Args, FileName,
+                               ToolName, PCHContainerOps);
+}
+
 /// Builds an AST for 'Code'.
 ///
 /// \param Code C++ code.
@@ -247,6 +275,13 @@
                  std::shared_ptr<PCHContainerOperations> PCHContainerOps =
                      std::make_shared<PCHContainerOperations>());
 
+  ToolInvocation(std::vector<std::string> CommandLine,
+                 std::unique_ptr<FrontendAction> FAction, FileManager *Files,
+                 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
+                     std::make_shared<PCHContainerOperations>())
+      : ToolInvocation(std::move(CommandLine), FAction.release(), Files,
+                       PCHContainerOps) {}
+
   /// Create a tool invocation.
   ///
   /// \param CommandLine The command line arguments to clang.
@@ -397,7 +432,9 @@
 std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
   class SimpleFrontendActionFactory : public FrontendActionFactory {
   public:
-    FrontendAction *create() override { return new T; }
+    std::unique_ptr<FrontendAction> create() override {
+      return std::make_unique<T>();
+    }
   };
 
   return std::unique_ptr<FrontendActionFactory>(
@@ -413,8 +450,9 @@
                                           SourceFileCallbacks *Callbacks)
         : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
 
-    FrontendAction *create() override {
-      return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
+    std::unique_ptr<FrontendAction> create() override {
+      return std::make_unique<ConsumerFactoryAdaptor>(ConsumerFactory,
+                                                      Callbacks);
     }
 
   private:
Index: clang-tools-extra/pp-trace/PPTrace.cpp
===================================================================
--- clang-tools-extra/pp-trace/PPTrace.cpp
+++ clang-tools-extra/pp-trace/PPTrace.cpp
@@ -30,6 +30,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Driver/Options.h"
 #include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Tooling/Execution.h"
@@ -112,7 +113,9 @@
   PPTraceFrontendActionFactory(const FilterType &Filters, raw_ostream &OS)
       : Filters(Filters), OS(OS) {}
 
-  PPTraceAction *create() override { return new PPTraceAction(Filters, OS); }
+  std::unique_ptr<FrontendAction> create() override {
+    return std::make_unique<PPTraceAction>(Filters, OS);
+  }
 
 private:
   const FilterType &Filters;
Index: clang-tools-extra/modularize/Modularize.cpp
===================================================================
--- clang-tools-extra/modularize/Modularize.cpp
+++ clang-tools-extra/modularize/Modularize.cpp
@@ -233,6 +233,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Driver/Options.h"
 #include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Tooling/CompilationDatabase.h"
@@ -721,8 +722,9 @@
       : Entities(Entities), PPTracker(preprocessorTracker),
         HadErrors(HadErrors) {}
 
-  CollectEntitiesAction *create() override {
-    return new CollectEntitiesAction(Entities, PPTracker, HadErrors);
+  std::unique_ptr<FrontendAction> create() override {
+    return std::make_unique<CollectEntitiesAction>(Entities, PPTracker,
+                                                   HadErrors);
   }
 
 private:
@@ -801,8 +803,8 @@
 public:
   CompileCheckFrontendActionFactory() {}
 
-  CompileCheckAction *create() override {
-    return new CompileCheckAction();
+  std::unique_ptr<FrontendAction> create() override {
+    return std::make_unique<CompileCheckAction>();
   }
 };
 
@@ -886,6 +888,7 @@
       CompileCheckTool.appendArgumentsAdjuster(
         getModularizeArgumentsAdjuster(ModUtil->Dependencies));
       int CompileCheckFileErrors = 0;
+      // FIXME: use newFrontendActionFactory.
       CompileCheckFrontendActionFactory CompileCheckFactory;
       CompileCheckFileErrors |= CompileCheckTool.run(&CompileCheckFactory);
       if (CompileCheckFileErrors != 0) {
Index: clang-tools-extra/modularize/CoverageChecker.cpp
===================================================================
--- clang-tools-extra/modularize/CoverageChecker.cpp
+++ clang-tools-extra/modularize/CoverageChecker.cpp
@@ -58,6 +58,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Driver/Options.h"
 #include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -129,8 +130,8 @@
   CoverageCheckerFrontendActionFactory(CoverageChecker &Checker)
     : Checker(Checker) {}
 
-  CoverageCheckerAction *create() override {
-    return new CoverageCheckerAction(Checker);
+  std::unique_ptr<FrontendAction> create() override {
+    return std::make_unique<CoverageCheckerAction>(Checker);
   }
 
 private:
Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -200,7 +200,7 @@
                            CommentHandler *PragmaHandler)
       : COpts(std::move(COpts)), PragmaHandler(PragmaHandler) {}
 
-  clang::FrontendAction *create() override {
+  std::unique_ptr<FrontendAction> create() override {
     class IndexAction : public ASTFrontendAction {
     public:
       IndexAction(std::shared_ptr<index::IndexDataConsumer> DataConsumer,
@@ -232,7 +232,8 @@
         index::IndexingOptions::SystemSymbolFilterKind::All;
     IndexOpts.IndexFunctionLocals = false;
     Collector = std::make_shared<SymbolCollector>(COpts);
-    return new IndexAction(Collector, std::move(IndexOpts), PragmaHandler);
+    return std::make_unique<IndexAction>(Collector, std::move(IndexOpts),
+                                         PragmaHandler);
   }
 
   std::shared_ptr<SymbolCollector> Collector;
Index: clang-tools-extra/clangd/indexer/IndexerMain.cpp
===================================================================
--- clang-tools-extra/clangd/indexer/IndexerMain.cpp
+++ clang-tools-extra/clangd/indexer/IndexerMain.cpp
@@ -39,37 +39,36 @@
 public:
   IndexActionFactory(IndexFileIn &Result) : Result(Result) {}
 
-  clang::FrontendAction *create() override {
+  std::unique_ptr<FrontendAction> create() override {
     SymbolCollector::Options Opts;
     Opts.CountReferences = true;
     return createStaticIndexingAction(
-               Opts,
-               [&](SymbolSlab S) {
-                 // Merge as we go.
-                 std::lock_guard<std::mutex> Lock(SymbolsMu);
-                 for (const auto &Sym : S) {
-                   if (const auto *Existing = Symbols.find(Sym.ID))
-                     Symbols.insert(mergeSymbol(*Existing, Sym));
-                   else
-                     Symbols.insert(Sym);
-                 }
-               },
-               [&](RefSlab S) {
-                 std::lock_guard<std::mutex> Lock(SymbolsMu);
-                 for (const auto &Sym : S) {
-                   // Deduplication happens during insertion.
-                   for (const auto &Ref : Sym.second)
-                     Refs.insert(Sym.first, Ref);
-                 }
-               },
-               [&](RelationSlab S) {
-                 std::lock_guard<std::mutex> Lock(SymbolsMu);
-                 for (const auto &R : S) {
-                   Relations.insert(R);
-                 }
-               },
-               /*IncludeGraphCallback=*/nullptr)
-        .release();
+        Opts,
+        [&](SymbolSlab S) {
+          // Merge as we go.
+          std::lock_guard<std::mutex> Lock(SymbolsMu);
+          for (const auto &Sym : S) {
+            if (const auto *Existing = Symbols.find(Sym.ID))
+              Symbols.insert(mergeSymbol(*Existing, Sym));
+            else
+              Symbols.insert(Sym);
+          }
+        },
+        [&](RefSlab S) {
+          std::lock_guard<std::mutex> Lock(SymbolsMu);
+          for (const auto &Sym : S) {
+            // Deduplication happens during insertion.
+            for (const auto &Ref : Sym.second)
+              Refs.insert(Sym.first, Ref);
+          }
+        },
+        [&](RelationSlab S) {
+          std::lock_guard<std::mutex> Lock(SymbolsMu);
+          for (const auto &R : S) {
+            Relations.insert(R);
+          }
+        },
+        /*IncludeGraphCallback=*/nullptr);
   }
 
   // Awkward: we write the result in the destructor, because the executor
Index: clang-tools-extra/clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -530,7 +530,9 @@
     ActionFactory(ClangTidyContext &Context,
                   IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS)
         : ConsumerFactory(Context, BaseFS) {}
-    FrontendAction *create() override { return new Action(&ConsumerFactory); }
+    std::unique_ptr<FrontendAction> create() override {
+      return std::make_unique<Action>(&ConsumerFactory);
+    }
 
     bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
                        FileManager *Files,
Index: clang-tools-extra/clang-move/Move.h
===================================================================
--- clang-tools-extra/clang-move/Move.h
+++ clang-tools-extra/clang-move/Move.h
@@ -224,8 +224,8 @@
                          DeclarationReporter *const Reporter = nullptr)
       : Context(Context), Reporter(Reporter) {}
 
-  clang::FrontendAction *create() override {
-    return new ClangMoveAction(Context, Reporter);
+  std::unique_ptr<clang::FrontendAction> create() override {
+    return std::make_unique<ClangMoveAction>(Context, Reporter);
   }
 
 private:
Index: clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h
===================================================================
--- clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h
+++ clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbolsAction.h
@@ -47,8 +47,8 @@
       const HeaderMapCollector::RegexHeaderMap *RegexHeaderMap = nullptr)
       : Reporter(Reporter), RegexHeaderMap(RegexHeaderMap) {}
 
-  clang::FrontendAction *create() override {
-    return new FindAllSymbolsAction(Reporter, RegexHeaderMap);
+  std::unique_ptr<FrontendAction> create() override {
+    return std::make_unique<FindAllSymbolsAction>(Reporter, RegexHeaderMap);
   }
 
 private:
Index: clang-tools-extra/clang-doc/ClangDoc.cpp
===================================================================
--- clang-tools-extra/clang-doc/ClangDoc.cpp
+++ clang-tools-extra/clang-doc/ClangDoc.cpp
@@ -29,13 +29,13 @@
 class MapperActionFactory : public tooling::FrontendActionFactory {
 public:
   MapperActionFactory(ClangDocContext CDCtx) : CDCtx(CDCtx) {}
-  clang::FrontendAction *create() override;
+  std::unique_ptr<FrontendAction> create() override;
 
 private:
   ClangDocContext CDCtx;
 };
 
-clang::FrontendAction *MapperActionFactory::create() {
+std::unique_ptr<FrontendAction> MapperActionFactory::create() {
   class ClangDocAction : public clang::ASTFrontendAction {
   public:
     ClangDocAction(ClangDocContext CDCtx) : CDCtx(CDCtx) {}
@@ -49,7 +49,7 @@
   private:
     ClangDocContext CDCtx;
   };
-  return new ClangDocAction(CDCtx);
+  return std::make_unique<ClangDocAction>(CDCtx);
 }
 
 std::unique_ptr<tooling::FrontendActionFactory>
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to