[PATCH] D130678: [clang-tools-extra] Refactor to reduce code duplication

ppenguin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 27 23:10:09 PDT 2022


prehistoric-penguin updated this revision to Diff 448240.
prehistoric-penguin added a comment.

Refine commit msg


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D130678

Files:
  clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp


Index: clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
===================================================================
--- clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
+++ clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
@@ -37,9 +37,11 @@
 namespace clang {
 namespace replace {
 
-std::error_code collectReplacementsFromDirectory(
-    const llvm::StringRef Directory, TUReplacements &TUs,
-    TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
+template <typename T>
+std::error_code
+collectReplacementsFromDirectoryImpl(const llvm::StringRef Directory, T &TUs,
+                                     TUReplacementFiles &TUFiles,
+                                     clang::DiagnosticsEngine &Diagnostics) {
   using namespace llvm::sys::fs;
   using namespace llvm::sys::path;
 
@@ -67,7 +69,7 @@
     }
 
     yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
-    tooling::TranslationUnitReplacements TU;
+    typename T::value_type TU;
     YIn >> TU;
     if (YIn.error()) {
       // File doesn't appear to be a header change description. Ignore it.
@@ -82,47 +84,17 @@
 }
 
 std::error_code collectReplacementsFromDirectory(
-    const llvm::StringRef Directory, TUDiagnostics &TUs,
+    const llvm::StringRef Directory, TUReplacements &TUs,
     TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
-  using namespace llvm::sys::fs;
-  using namespace llvm::sys::path;
-
-  std::error_code ErrorCode;
-
-  for (recursive_directory_iterator I(Directory, ErrorCode), E;
-       I != E && !ErrorCode; I.increment(ErrorCode)) {
-    if (filename(I->path())[0] == '.') {
-      // Indicate not to descend into directories beginning with '.'
-      I.no_push();
-      continue;
-    }
-
-    if (extension(I->path()) != ".yaml")
-      continue;
-
-    TUFiles.push_back(I->path());
-
-    ErrorOr<std::unique_ptr<MemoryBuffer>> Out =
-        MemoryBuffer::getFile(I->path());
-    if (std::error_code BufferError = Out.getError()) {
-      errs() << "Error reading " << I->path() << ": " << BufferError.message()
-             << "\n";
-      continue;
-    }
-
-    yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
-    tooling::TranslationUnitDiagnostics TU;
-    YIn >> TU;
-    if (YIn.error()) {
-      // File doesn't appear to be a header change description. Ignore it.
-      continue;
-    }
-
-    // Only keep files that properly parse.
-    TUs.push_back(TU);
-  }
+  return collectReplacementsFromDirectoryImpl(Directory, TUs, TUFiles,
+                                              Diagnostics);
+}
 
-  return ErrorCode;
+std::error_code collectReplacementsFromDirectory(
+    const llvm::StringRef Directory, TUDiagnostics &TUs,
+    TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
+  return collectReplacementsFromDirectoryImpl(Directory, TUs, TUFiles,
+                                              Diagnostics);
 }
 
 /// Extract replacements from collected TranslationUnitReplacements and


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130678.448240.patch
Type: text/x-patch
Size: 3105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220728/73f2b1bc/attachment-0001.bin>


More information about the cfe-commits mailing list