[PATCH] D39652: [clang-diff] NFC: factor out getCompilationDatabase()

Johannes Altmanninger via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 5 11:03:47 PST 2017


johannes created this revision.

https://reviews.llvm.org/D39652

Files:
  tools/clang-diff/ClangDiff.cpp


Index: tools/clang-diff/ClangDiff.cpp
===================================================================
--- tools/clang-diff/ClangDiff.cpp
+++ tools/clang-diff/ClangDiff.cpp
@@ -84,27 +84,33 @@
   Compilations = std::move(AdjustingCompilations);
 }
 
-static std::unique_ptr<ASTUnit>
-getAST(const std::unique_ptr<CompilationDatabase> &CommonCompilations,
-       const StringRef Filename) {
+static std::unique_ptr<CompilationDatabase>
+getCompilationDatabase(StringRef Filename) {
   std::string ErrorMessage;
-  std::unique_ptr<CompilationDatabase> Compilations;
-  if (!CommonCompilations) {
-    Compilations = CompilationDatabase::autoDetectFromSource(
-        BuildPath.empty() ? Filename : BuildPath, ErrorMessage);
-    if (!Compilations) {
-      llvm::errs()
-          << "Error while trying to load a compilation database, running "
-             "without flags.\n"
-          << ErrorMessage;
-      Compilations =
-          llvm::make_unique<clang::tooling::FixedCompilationDatabase>(
-              ".", std::vector<std::string>());
-    }
+  std::unique_ptr<CompilationDatabase> Compilations =
+      CompilationDatabase::autoDetectFromSource(
+          BuildPath.empty() ? Filename : BuildPath, ErrorMessage);
+  if (!Compilations) {
+    llvm::errs()
+        << "Error while trying to load a compilation database, running "
+           "without flags.\n"
+        << ErrorMessage;
+    Compilations = llvm::make_unique<clang::tooling::FixedCompilationDatabase>(
+        ".", std::vector<std::string>());
   }
   addExtraArgs(Compilations);
+  return Compilations;
+}
+
+static std::unique_ptr<ASTUnit>
+getAST(const std::unique_ptr<CompilationDatabase> &CommonCompilations,
+       const StringRef Filename) {
   std::array<std::string, 1> Files = {{Filename}};
-  ClangTool Tool(Compilations ? *Compilations : *CommonCompilations, Files);
+  std::unique_ptr<CompilationDatabase> FileCompilations;
+  if (!CommonCompilations)
+    FileCompilations = getCompilationDatabase(Filename);
+  ClangTool Tool(CommonCompilations ? *CommonCompilations : *FileCompilations,
+                 Files);
   std::vector<std::unique_ptr<ASTUnit>> ASTs;
   Tool.buildASTs(ASTs);
   if (ASTs.size() != Files.size())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39652.121640.patch
Type: text/x-patch
Size: 2224 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171105/0fad8876/attachment.bin>


More information about the cfe-commits mailing list