Skip to content

Commit 24e5229

Browse files
[clang-apply-replacements] Deduplicate Implementation of collectReplacementsFromDirectory (NFC) (#78630)
* Convert `collectReplacementsFromDirectory` into a function template. * Employ explicit specialization to maintain implementation in the source file. * Utilize the function template in the source file to eliminate code duplication. * Update the documentation for the function.
1 parent b4f24be commit 24e5229

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,27 @@ using FileToChangesMap =
5151
/// Directories starting with '.' are ignored during traversal.
5252
///
5353
/// \param[in] Directory Directory to begin search for serialized
54-
/// TranslationUnitReplacements.
54+
/// TranslationUnitReplacements or TranslationUnitDiagnostics.
5555
/// \param[out] TUs Collection of all found and deserialized
5656
/// TranslationUnitReplacements or TranslationUnitDiagnostics.
57-
/// \param[out] TUFiles Collection of all TranslationUnitReplacement files
58-
/// found in \c Directory.
57+
/// \param[out] TUFiles Collection of all TranslationUnitReplacement or
58+
/// TranslationUnitDiagnostics files found in \c Directory.
5959
/// \param[in] Diagnostics DiagnosticsEngine used for error output.
6060
///
6161
/// \returns An error_code indicating success or failure in navigating the
6262
/// directory structure.
63+
template <typename TranslationUnits>
64+
std::error_code collectReplacementsFromDirectory(
65+
const llvm::StringRef Directory, TranslationUnits &TUs,
66+
TUReplacementFiles &TUFiles,
67+
clang::DiagnosticsEngine &Diagnostics) = delete;
68+
69+
template <>
6370
std::error_code collectReplacementsFromDirectory(
6471
const llvm::StringRef Directory, TUReplacements &TUs,
6572
TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
6673

74+
template <>
6775
std::error_code collectReplacementsFromDirectory(
6876
const llvm::StringRef Directory, TUDiagnostics &TUs,
6977
TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);

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

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ static void eatDiagnostics(const SMDiagnostic &, void *) {}
3838
namespace clang {
3939
namespace replace {
4040

41-
std::error_code collectReplacementsFromDirectory(
42-
const llvm::StringRef Directory, TUReplacements &TUs,
41+
namespace detail {
42+
template <typename TranslationUnits>
43+
static std::error_code collectReplacementsFromDirectory(
44+
const llvm::StringRef Directory, TranslationUnits &TUs,
4345
TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
4446
using namespace llvm::sys::fs;
4547
using namespace llvm::sys::path;
@@ -68,7 +70,7 @@ std::error_code collectReplacementsFromDirectory(
6870
}
6971

7072
yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
71-
tooling::TranslationUnitReplacements TU;
73+
typename TranslationUnits::value_type TU;
7274
YIn >> TU;
7375
if (YIn.error()) {
7476
// File doesn't appear to be a header change description. Ignore it.
@@ -81,49 +83,22 @@ std::error_code collectReplacementsFromDirectory(
8183

8284
return ErrorCode;
8385
}
86+
} // namespace detail
8487

88+
template <>
8589
std::error_code collectReplacementsFromDirectory(
86-
const llvm::StringRef Directory, TUDiagnostics &TUs,
90+
const llvm::StringRef Directory, TUReplacements &TUs,
8791
TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
88-
using namespace llvm::sys::fs;
89-
using namespace llvm::sys::path;
90-
91-
std::error_code ErrorCode;
92-
93-
for (recursive_directory_iterator I(Directory, ErrorCode), E;
94-
I != E && !ErrorCode; I.increment(ErrorCode)) {
95-
if (filename(I->path())[0] == '.') {
96-
// Indicate not to descend into directories beginning with '.'
97-
I.no_push();
98-
continue;
99-
}
100-
101-
if (extension(I->path()) != ".yaml")
102-
continue;
103-
104-
TUFiles.push_back(I->path());
105-
106-
ErrorOr<std::unique_ptr<MemoryBuffer>> Out =
107-
MemoryBuffer::getFile(I->path());
108-
if (std::error_code BufferError = Out.getError()) {
109-
errs() << "Error reading " << I->path() << ": " << BufferError.message()
110-
<< "\n";
111-
continue;
112-
}
113-
114-
yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics);
115-
tooling::TranslationUnitDiagnostics TU;
116-
YIn >> TU;
117-
if (YIn.error()) {
118-
// File doesn't appear to be a header change description. Ignore it.
119-
continue;
120-
}
121-
122-
// Only keep files that properly parse.
123-
TUs.push_back(TU);
124-
}
92+
return detail::collectReplacementsFromDirectory(Directory, TUs, TUFiles,
93+
Diagnostics);
94+
}
12595

126-
return ErrorCode;
96+
template <>
97+
std::error_code collectReplacementsFromDirectory(
98+
const llvm::StringRef Directory, TUDiagnostics &TUs,
99+
TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) {
100+
return detail::collectReplacementsFromDirectory(Directory, TUs, TUFiles,
101+
Diagnostics);
127102
}
128103

129104
/// Extract replacements from collected TranslationUnitReplacements and

0 commit comments

Comments
 (0)