[PATCH] D69186: Refactor DependencyScanningTool to its own file

2019-10-21 Thread Kousik Kumar via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGfb042b094fda: Refactor DependencyScanningTool to its own 
file (authored by kousikk).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69186/new/

https://reviews.llvm.org/D69186

Files:
  clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h
  clang/lib/Tooling/DependencyScanning/CMakeLists.txt
  clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
  clang/tools/clang-scan-deps/ClangScanDeps.cpp

Index: clang/tools/clang-scan-deps/ClangScanDeps.cpp
===
--- clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -9,6 +9,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
 #include "clang/Tooling/JSONCompilationDatabase.h"
 #include "llvm/Support/InitLLVM.h"
@@ -38,101 +39,6 @@
   raw_ostream 
 };
 
-/// The high-level implementation of the dependency discovery tool that runs on
-/// an individual worker thread.
-class DependencyScanningTool {
-public:
-  /// Construct a dependency scanning tool.
-  ///
-  /// \param Compilations The reference to the compilation database that's
-  /// used by the clang tool.
-  DependencyScanningTool(DependencyScanningService ,
- const tooling::CompilationDatabase ,
- SharedStream , SharedStream )
-  : Worker(Service), Compilations(Compilations), OS(OS), Errs(Errs) {}
-
-  /// Print out the dependency information into a string using the dependency
-  /// file format that is specified in the options (-MD is the default) and
-  /// return it.
-  ///
-  /// \returns A \c StringError with the diagnostic output if clang errors
-  /// occurred, dependency file contents otherwise.
-  llvm::Expected getDependencyFile(const std::string ,
-StringRef CWD) {
-/// Prints out all of the gathered dependencies into a string.
-class DependencyPrinterConsumer : public DependencyConsumer {
-public:
-  void handleFileDependency(const DependencyOutputOptions ,
-StringRef File) override {
-if (!this->Opts)
-  this->Opts = std::make_unique(Opts);
-Dependencies.push_back(File);
-  }
-
-  void printDependencies(std::string ) {
-if (!Opts)
-  return;
-
-class DependencyPrinter : public DependencyFileGenerator {
-public:
-  DependencyPrinter(DependencyOutputOptions ,
-ArrayRef Dependencies)
-  : DependencyFileGenerator(Opts) {
-for (const auto  : Dependencies)
-  addDependency(Dep);
-  }
-
-  void printDependencies(std::string ) {
-llvm::raw_string_ostream OS(S);
-outputDependencyFile(OS);
-  }
-};
-
-DependencyPrinter Generator(*Opts, Dependencies);
-Generator.printDependencies(S);
-  }
-
-private:
-  std::unique_ptr Opts;
-  std::vector Dependencies;
-};
-
-DependencyPrinterConsumer Consumer;
-auto Result =
-Worker.computeDependencies(Input, CWD, Compilations, Consumer);
-if (Result)
-  return std::move(Result);
-std::string Output;
-Consumer.printDependencies(Output);
-return Output;
-  }
-
-  /// Computes the dependencies for the given file and prints them out.
-  ///
-  /// \returns True on error.
-  bool runOnFile(const std::string , StringRef CWD) {
-auto MaybeFile = getDependencyFile(Input, CWD);
-if (!MaybeFile) {
-  llvm::handleAllErrors(
-  MaybeFile.takeError(), [this, ](llvm::StringError ) {
-Errs.applyLocked([&](raw_ostream ) {
-  OS << "Error while scanning dependencies for " << Input << ":\n";
-  OS << Err.getMessage();
-});
-  });
-  return true;
-}
-OS.applyLocked([&](raw_ostream ) { OS << *MaybeFile; });
-return false;
-  }
-
-private:
-  DependencyScanningWorker Worker;
-  const tooling::CompilationDatabase 
-  SharedStream 
-  SharedStream 
-};
-
 llvm::cl::opt Help("h", llvm::cl::desc("Alias for -help"),
  llvm::cl::Hidden);
 
@@ -191,6 +97,27 @@
   return ObjFileName.str();
 }
 
+/// Takes the result of a dependency scan and prints error / dependency files
+/// based on the result.
+///
+/// \returns True on error.
+static bool handleDependencyToolResult(const std::string ,
+   llvm::Expected ,
+   SharedStream , SharedStream ) {
+  if (!MaybeFile) {
+

[PATCH] D69186: Refactor DependencyScanningTool to its own file

2019-10-21 Thread Kousik Kumar via Phabricator via cfe-commits
kousikk updated this revision to Diff 225994.
kousikk added a comment.

Run clang format


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69186/new/

https://reviews.llvm.org/D69186

Files:
  clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h
  clang/lib/Tooling/DependencyScanning/CMakeLists.txt
  clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
  clang/tools/clang-scan-deps/ClangScanDeps.cpp

Index: clang/tools/clang-scan-deps/ClangScanDeps.cpp
===
--- clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -9,6 +9,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
 #include "clang/Tooling/JSONCompilationDatabase.h"
 #include "llvm/Support/InitLLVM.h"
@@ -38,101 +39,6 @@
   raw_ostream 
 };
 
-/// The high-level implementation of the dependency discovery tool that runs on
-/// an individual worker thread.
-class DependencyScanningTool {
-public:
-  /// Construct a dependency scanning tool.
-  ///
-  /// \param Compilations The reference to the compilation database that's
-  /// used by the clang tool.
-  DependencyScanningTool(DependencyScanningService ,
- const tooling::CompilationDatabase ,
- SharedStream , SharedStream )
-  : Worker(Service), Compilations(Compilations), OS(OS), Errs(Errs) {}
-
-  /// Print out the dependency information into a string using the dependency
-  /// file format that is specified in the options (-MD is the default) and
-  /// return it.
-  ///
-  /// \returns A \c StringError with the diagnostic output if clang errors
-  /// occurred, dependency file contents otherwise.
-  llvm::Expected getDependencyFile(const std::string ,
-StringRef CWD) {
-/// Prints out all of the gathered dependencies into a string.
-class DependencyPrinterConsumer : public DependencyConsumer {
-public:
-  void handleFileDependency(const DependencyOutputOptions ,
-StringRef File) override {
-if (!this->Opts)
-  this->Opts = std::make_unique(Opts);
-Dependencies.push_back(File);
-  }
-
-  void printDependencies(std::string ) {
-if (!Opts)
-  return;
-
-class DependencyPrinter : public DependencyFileGenerator {
-public:
-  DependencyPrinter(DependencyOutputOptions ,
-ArrayRef Dependencies)
-  : DependencyFileGenerator(Opts) {
-for (const auto  : Dependencies)
-  addDependency(Dep);
-  }
-
-  void printDependencies(std::string ) {
-llvm::raw_string_ostream OS(S);
-outputDependencyFile(OS);
-  }
-};
-
-DependencyPrinter Generator(*Opts, Dependencies);
-Generator.printDependencies(S);
-  }
-
-private:
-  std::unique_ptr Opts;
-  std::vector Dependencies;
-};
-
-DependencyPrinterConsumer Consumer;
-auto Result =
-Worker.computeDependencies(Input, CWD, Compilations, Consumer);
-if (Result)
-  return std::move(Result);
-std::string Output;
-Consumer.printDependencies(Output);
-return Output;
-  }
-
-  /// Computes the dependencies for the given file and prints them out.
-  ///
-  /// \returns True on error.
-  bool runOnFile(const std::string , StringRef CWD) {
-auto MaybeFile = getDependencyFile(Input, CWD);
-if (!MaybeFile) {
-  llvm::handleAllErrors(
-  MaybeFile.takeError(), [this, ](llvm::StringError ) {
-Errs.applyLocked([&](raw_ostream ) {
-  OS << "Error while scanning dependencies for " << Input << ":\n";
-  OS << Err.getMessage();
-});
-  });
-  return true;
-}
-OS.applyLocked([&](raw_ostream ) { OS << *MaybeFile; });
-return false;
-  }
-
-private:
-  DependencyScanningWorker Worker;
-  const tooling::CompilationDatabase 
-  SharedStream 
-  SharedStream 
-};
-
 llvm::cl::opt Help("h", llvm::cl::desc("Alias for -help"),
  llvm::cl::Hidden);
 
@@ -191,6 +97,27 @@
   return ObjFileName.str();
 }
 
+/// Takes the result of a dependency scan and prints error / dependency files
+/// based on the result.
+///
+/// \returns True on error.
+static bool handleDependencyToolResult(const std::string ,
+   llvm::Expected ,
+   SharedStream , SharedStream ) {
+  if (!MaybeFile) {
+llvm::handleAllErrors(
+MaybeFile.takeError(), [, ](llvm::StringError ) {
+  

[PATCH] D69186: Refactor DependencyScanningTool to its own file

2019-10-21 Thread Michael Spencer via Phabricator via cfe-commits
Bigcheese accepted this revision.
Bigcheese added a comment.
This revision is now accepted and ready to land.

LGTM with style nit. I like that this decouples `DependencyScanningTool` from 
printing the results.




Comment at: clang/tools/clang-scan-deps/ClangScanDeps.cpp:105-107
+  llvm::Expected ,
+  SharedStream ,
+  SharedStream ) {

This looks like it needs to be clang-formatted.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69186/new/

https://reviews.llvm.org/D69186



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D69186: Refactor DependencyScanningTool to its own file

2019-10-18 Thread Kousik Kumar via Phabricator via cfe-commits
kousikk created this revision.
kousikk added reviewers: arphaman, jkorous, Bigcheese, dexonsmith.
Herald added subscribers: cfe-commits, mgorny.
Herald added a project: clang.
kousikk updated this revision to Diff 225667.
kousikk added a comment.

Lint fixes


There's no behavior change - just moving DependencyScanningTool to its own file 
since this tool can be reused across both clang-scan-deps binary and an 
interface
exposed as part of libClang APIs.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69186

Files:
  clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h
  clang/lib/Tooling/DependencyScanning/CMakeLists.txt
  clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
  clang/tools/clang-scan-deps/ClangScanDeps.cpp

Index: clang/tools/clang-scan-deps/ClangScanDeps.cpp
===
--- clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -10,6 +10,7 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
 #include "clang/Tooling/JSONCompilationDatabase.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/Options.h"
@@ -38,101 +39,6 @@
   raw_ostream 
 };
 
-/// The high-level implementation of the dependency discovery tool that runs on
-/// an individual worker thread.
-class DependencyScanningTool {
-public:
-  /// Construct a dependency scanning tool.
-  ///
-  /// \param Compilations The reference to the compilation database that's
-  /// used by the clang tool.
-  DependencyScanningTool(DependencyScanningService ,
- const tooling::CompilationDatabase ,
- SharedStream , SharedStream )
-  : Worker(Service), Compilations(Compilations), OS(OS), Errs(Errs) {}
-
-  /// Print out the dependency information into a string using the dependency
-  /// file format that is specified in the options (-MD is the default) and
-  /// return it.
-  ///
-  /// \returns A \c StringError with the diagnostic output if clang errors
-  /// occurred, dependency file contents otherwise.
-  llvm::Expected getDependencyFile(const std::string ,
-StringRef CWD) {
-/// Prints out all of the gathered dependencies into a string.
-class DependencyPrinterConsumer : public DependencyConsumer {
-public:
-  void handleFileDependency(const DependencyOutputOptions ,
-StringRef File) override {
-if (!this->Opts)
-  this->Opts = std::make_unique(Opts);
-Dependencies.push_back(File);
-  }
-
-  void printDependencies(std::string ) {
-if (!Opts)
-  return;
-
-class DependencyPrinter : public DependencyFileGenerator {
-public:
-  DependencyPrinter(DependencyOutputOptions ,
-ArrayRef Dependencies)
-  : DependencyFileGenerator(Opts) {
-for (const auto  : Dependencies)
-  addDependency(Dep);
-  }
-
-  void printDependencies(std::string ) {
-llvm::raw_string_ostream OS(S);
-outputDependencyFile(OS);
-  }
-};
-
-DependencyPrinter Generator(*Opts, Dependencies);
-Generator.printDependencies(S);
-  }
-
-private:
-  std::unique_ptr Opts;
-  std::vector Dependencies;
-};
-
-DependencyPrinterConsumer Consumer;
-auto Result =
-Worker.computeDependencies(Input, CWD, Compilations, Consumer);
-if (Result)
-  return std::move(Result);
-std::string Output;
-Consumer.printDependencies(Output);
-return Output;
-  }
-
-  /// Computes the dependencies for the given file and prints them out.
-  ///
-  /// \returns True on error.
-  bool runOnFile(const std::string , StringRef CWD) {
-auto MaybeFile = getDependencyFile(Input, CWD);
-if (!MaybeFile) {
-  llvm::handleAllErrors(
-  MaybeFile.takeError(), [this, ](llvm::StringError ) {
-Errs.applyLocked([&](raw_ostream ) {
-  OS << "Error while scanning dependencies for " << Input << ":\n";
-  OS << Err.getMessage();
-});
-  });
-  return true;
-}
-OS.applyLocked([&](raw_ostream ) { OS << *MaybeFile; });
-return false;
-  }
-
-private:
-  DependencyScanningWorker Worker;
-  const tooling::CompilationDatabase 
-  SharedStream 
-  SharedStream 
-};
-
 llvm::cl::opt Help("h", llvm::cl::desc("Alias for -help"),
  llvm::cl::Hidden);
 
@@ -191,6 +97,28 @@
   return ObjFileName.str();
 }
 
+/// Takes the result of a dependency scan and prints error / dependency files
+/// based on the result.
+///
+/// \returns True on error.
+static bool 

[PATCH] D69186: Refactor DependencyScanningTool to its own file

2019-10-18 Thread Kousik Kumar via Phabricator via cfe-commits
kousikk updated this revision to Diff 225667.
kousikk added a comment.

Lint fixes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69186/new/

https://reviews.llvm.org/D69186

Files:
  clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h
  clang/lib/Tooling/DependencyScanning/CMakeLists.txt
  clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
  clang/tools/clang-scan-deps/ClangScanDeps.cpp

Index: clang/tools/clang-scan-deps/ClangScanDeps.cpp
===
--- clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -10,6 +10,7 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
 #include "clang/Tooling/JSONCompilationDatabase.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/Options.h"
@@ -38,101 +39,6 @@
   raw_ostream 
 };
 
-/// The high-level implementation of the dependency discovery tool that runs on
-/// an individual worker thread.
-class DependencyScanningTool {
-public:
-  /// Construct a dependency scanning tool.
-  ///
-  /// \param Compilations The reference to the compilation database that's
-  /// used by the clang tool.
-  DependencyScanningTool(DependencyScanningService ,
- const tooling::CompilationDatabase ,
- SharedStream , SharedStream )
-  : Worker(Service), Compilations(Compilations), OS(OS), Errs(Errs) {}
-
-  /// Print out the dependency information into a string using the dependency
-  /// file format that is specified in the options (-MD is the default) and
-  /// return it.
-  ///
-  /// \returns A \c StringError with the diagnostic output if clang errors
-  /// occurred, dependency file contents otherwise.
-  llvm::Expected getDependencyFile(const std::string ,
-StringRef CWD) {
-/// Prints out all of the gathered dependencies into a string.
-class DependencyPrinterConsumer : public DependencyConsumer {
-public:
-  void handleFileDependency(const DependencyOutputOptions ,
-StringRef File) override {
-if (!this->Opts)
-  this->Opts = std::make_unique(Opts);
-Dependencies.push_back(File);
-  }
-
-  void printDependencies(std::string ) {
-if (!Opts)
-  return;
-
-class DependencyPrinter : public DependencyFileGenerator {
-public:
-  DependencyPrinter(DependencyOutputOptions ,
-ArrayRef Dependencies)
-  : DependencyFileGenerator(Opts) {
-for (const auto  : Dependencies)
-  addDependency(Dep);
-  }
-
-  void printDependencies(std::string ) {
-llvm::raw_string_ostream OS(S);
-outputDependencyFile(OS);
-  }
-};
-
-DependencyPrinter Generator(*Opts, Dependencies);
-Generator.printDependencies(S);
-  }
-
-private:
-  std::unique_ptr Opts;
-  std::vector Dependencies;
-};
-
-DependencyPrinterConsumer Consumer;
-auto Result =
-Worker.computeDependencies(Input, CWD, Compilations, Consumer);
-if (Result)
-  return std::move(Result);
-std::string Output;
-Consumer.printDependencies(Output);
-return Output;
-  }
-
-  /// Computes the dependencies for the given file and prints them out.
-  ///
-  /// \returns True on error.
-  bool runOnFile(const std::string , StringRef CWD) {
-auto MaybeFile = getDependencyFile(Input, CWD);
-if (!MaybeFile) {
-  llvm::handleAllErrors(
-  MaybeFile.takeError(), [this, ](llvm::StringError ) {
-Errs.applyLocked([&](raw_ostream ) {
-  OS << "Error while scanning dependencies for " << Input << ":\n";
-  OS << Err.getMessage();
-});
-  });
-  return true;
-}
-OS.applyLocked([&](raw_ostream ) { OS << *MaybeFile; });
-return false;
-  }
-
-private:
-  DependencyScanningWorker Worker;
-  const tooling::CompilationDatabase 
-  SharedStream 
-  SharedStream 
-};
-
 llvm::cl::opt Help("h", llvm::cl::desc("Alias for -help"),
  llvm::cl::Hidden);
 
@@ -191,6 +97,28 @@
   return ObjFileName.str();
 }
 
+/// Takes the result of a dependency scan and prints error / dependency files
+/// based on the result.
+///
+/// \returns True on error.
+static bool handleDependencyToolResult(const std::string ,
+  llvm::Expected ,
+  SharedStream ,
+  SharedStream ) {
+  if (!MaybeFile) {
+llvm::handleAllErrors(
+MaybeFile.takeError(), [, ](llvm::StringError ) {