[clang-tools-extra] [clang-apply-replacements] Add support for the `.yml` file extension (PR #78842)
https://github.com/unterumarmung closed https://github.com/llvm/llvm-project/pull/78842 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) (PR #78630)
https://github.com/unterumarmung created https://github.com/llvm/llvm-project/pull/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. >From cc4591e9d21ce8561058eeb36c1017666a7976c6 Mon Sep 17 00:00:00 2001 From: Daniil Dudkin Date: Fri, 19 Jan 2024 00:41:03 +0300 Subject: [PATCH] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) * 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. --- .../Tooling/ApplyReplacements.h | 14 - .../lib/Tooling/ApplyReplacements.cpp | 59 ++- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h b/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h index 1f0d8737191402..7a8408bcdff8f7 100644 --- a/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h +++ b/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h @@ -51,19 +51,27 @@ using FileToChangesMap = /// Directories starting with '.' are ignored during traversal. /// /// \param[in] Directory Directory to begin search for serialized -/// TranslationUnitReplacements. +/// TranslationUnitReplacements or TranslationUnitDiagnostics. /// \param[out] TUs Collection of all found and deserialized /// TranslationUnitReplacements or TranslationUnitDiagnostics. -/// \param[out] TUFiles Collection of all TranslationUnitReplacement files -/// found in \c Directory. +/// \param[out] TUFiles Collection of all TranslationUnitReplacement or +/// TranslationUnitDiagnostics files found in \c Directory. /// \param[in] Diagnostics DiagnosticsEngine used for error output. /// /// \returns An error_code indicating success or failure in navigating the /// directory structure. +template +std::error_code collectReplacementsFromDirectory( +const llvm::StringRef Directory, TranslationUnits &TUs, +TUReplacementFiles &TUFiles, +clang::DiagnosticsEngine &Diagnostics) = delete; + +template <> std::error_code collectReplacementsFromDirectory( const llvm::StringRef Directory, TUReplacements &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics); +template <> std::error_code collectReplacementsFromDirectory( const llvm::StringRef Directory, TUDiagnostics &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics); diff --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp index b490780f48529e..87ed1b8797cb05 100644 --- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp +++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp @@ -38,8 +38,10 @@ static void eatDiagnostics(const SMDiagnostic &, void *) {} namespace clang { namespace replace { -std::error_code collectReplacementsFromDirectory( -const llvm::StringRef Directory, TUReplacements &TUs, +namespace detail { +template +static std::error_code collectReplacementsFromDirectory( +const llvm::StringRef Directory, TranslationUnits &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) { using namespace llvm::sys::fs; using namespace llvm::sys::path; @@ -68,7 +70,7 @@ std::error_code collectReplacementsFromDirectory( } yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics); -tooling::TranslationUnitReplacements TU; +typename TranslationUnits::value_type TU; YIn >> TU; if (YIn.error()) { // File doesn't appear to be a header change description. Ignore it. @@ -81,49 +83,22 @@ std::error_code collectReplacementsFromDirectory( return ErrorCode; } +} // namespace detail +template <> 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
[clang-tools-extra] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) (PR #78630)
unterumarmung wrote: I honestly don't who should review/approve this, but hopefully the change is not complicated. https://github.com/llvm/llvm-project/pull/78630 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) (PR #78630)
https://github.com/unterumarmung updated https://github.com/llvm/llvm-project/pull/78630 >From 1951badf6ba8302da1431747412edf960f53b0b1 Mon Sep 17 00:00:00 2001 From: Daniil Dudkin Date: Fri, 19 Jan 2024 00:41:03 +0300 Subject: [PATCH] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) * 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. --- .../Tooling/ApplyReplacements.h | 14 - .../lib/Tooling/ApplyReplacements.cpp | 59 ++- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h b/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h index 1f0d8737191402..7a8408bcdff8f7 100644 --- a/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h +++ b/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h @@ -51,19 +51,27 @@ using FileToChangesMap = /// Directories starting with '.' are ignored during traversal. /// /// \param[in] Directory Directory to begin search for serialized -/// TranslationUnitReplacements. +/// TranslationUnitReplacements or TranslationUnitDiagnostics. /// \param[out] TUs Collection of all found and deserialized /// TranslationUnitReplacements or TranslationUnitDiagnostics. -/// \param[out] TUFiles Collection of all TranslationUnitReplacement files -/// found in \c Directory. +/// \param[out] TUFiles Collection of all TranslationUnitReplacement or +/// TranslationUnitDiagnostics files found in \c Directory. /// \param[in] Diagnostics DiagnosticsEngine used for error output. /// /// \returns An error_code indicating success or failure in navigating the /// directory structure. +template +std::error_code collectReplacementsFromDirectory( +const llvm::StringRef Directory, TranslationUnits &TUs, +TUReplacementFiles &TUFiles, +clang::DiagnosticsEngine &Diagnostics) = delete; + +template <> std::error_code collectReplacementsFromDirectory( const llvm::StringRef Directory, TUReplacements &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics); +template <> std::error_code collectReplacementsFromDirectory( const llvm::StringRef Directory, TUDiagnostics &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics); diff --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp index b490780f48529e..87ed1b8797cb05 100644 --- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp +++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp @@ -38,8 +38,10 @@ static void eatDiagnostics(const SMDiagnostic &, void *) {} namespace clang { namespace replace { -std::error_code collectReplacementsFromDirectory( -const llvm::StringRef Directory, TUReplacements &TUs, +namespace detail { +template +static std::error_code collectReplacementsFromDirectory( +const llvm::StringRef Directory, TranslationUnits &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics) { using namespace llvm::sys::fs; using namespace llvm::sys::path; @@ -68,7 +70,7 @@ std::error_code collectReplacementsFromDirectory( } yaml::Input YIn(Out.get()->getBuffer(), nullptr, &eatDiagnostics); -tooling::TranslationUnitReplacements TU; +typename TranslationUnits::value_type TU; YIn >> TU; if (YIn.error()) { // File doesn't appear to be a header change description. Ignore it. @@ -81,49 +83,22 @@ std::error_code collectReplacementsFromDirectory( return ErrorCode; } +} // namespace detail +template <> 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> Out = -MemoryBuffer::getFile(I->path()); -if (std::error_code BufferError = Out.getError()) { - errs() << "Error read
[clang-tools-extra] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) (PR #78630)
unterumarmung wrote: The Windows build in the CI is stuck. The change is pretty trivial and doesn't use any features that cannot be found in C++98. So, we can skip the Windows check. https://github.com/llvm/llvm-project/pull/78630 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-apply-replacements] Deduplicate Implementation of `collectReplacementsFromDirectory` (NFC) (PR #78630)
https://github.com/unterumarmung closed https://github.com/llvm/llvm-project/pull/78630 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-apply-replacements] Add support for the `.yml` file extension (PR #78842)
https://github.com/unterumarmung created https://github.com/llvm/llvm-project/pull/78842 The `.yml` file extension is a valid extension for the YAML files, but it was not previously supported by the Clang Apply Replacements tool. This commit adds support for processing `.yml` files. Without this change, running the tool on a folder containing `.yml` files generated by clang-tidy would have no effect. >From 9c6d5fec23bb0e1cc92f6463f91717cb50b5ee65 Mon Sep 17 00:00:00 2001 From: Daniil Dudkin Date: Sat, 20 Jan 2024 14:07:40 +0300 Subject: [PATCH] [clang-apply-replacements] Add support for the `.yml` file extension The `.yml` file extension is a valid extension for the YAML files, but it was not previously supported by the Clang Apply Replacements tool. This commit adds support for processing `.yml` files. Without this change, running the tool on a folder containing `.yml` files generated by clang-tidy would have no effect. --- .../lib/Tooling/ApplyReplacements.cpp | 9 +- .../Inputs/yml-basic/basic.h | 32 +++ .../Inputs/yml-basic/file1.yml| 26 +++ .../Inputs/yml-basic/file2.yml| 14 .../clang-apply-replacements/yml-basic.cpp| 17 ++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h create mode 100644 clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml create mode 100644 clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file2.yml create mode 100644 clang-tools-extra/test/clang-apply-replacements/yml-basic.cpp diff --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp index 87ed1b8797cb05e..b316532dacdb168 100644 --- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp +++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp @@ -23,11 +23,15 @@ #include "clang/Tooling/DiagnosticsYaml.h" #include "clang/Tooling/ReplacementsYaml.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include #include using namespace llvm; @@ -39,6 +43,9 @@ namespace clang { namespace replace { namespace detail { + +static constexpr std::array AllowedExtensions = {".yaml", ".yml"}; + template static std::error_code collectReplacementsFromDirectory( const llvm::StringRef Directory, TranslationUnits &TUs, @@ -56,7 +63,7 @@ static std::error_code collectReplacementsFromDirectory( continue; } -if (extension(I->path()) != ".yaml") +if (!is_contained(AllowedExtensions, extension(I->path( continue; TUFiles.push_back(I->path()); diff --git a/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h new file mode 100644 index 000..48509684b7725cd --- /dev/null +++ b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h @@ -0,0 +1,32 @@ +#ifndef BASIC_H +#define BASIC_H + + +class Parent { +public: + virtual void func() {} +}; + +class Derived : public Parent { +public: + virtual void func() {} + // CHECK: virtual void func() override {} +}; + +extern void ext(int (&)[5], const Parent &); + +void func(int t) { + int ints[5]; + for (unsigned i = 0; i < 5; ++i) { +int &e = ints[i]; +e = t; +// CHECK: for (auto & elem : ints) { +// CHECK-NEXT: elem = t; + } + + Derived d; + + ext(ints, d); +} + +#endif // BASIC_H diff --git a/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml new file mode 100644 index 000..757f8d2ac18529c --- /dev/null +++ b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml @@ -0,0 +1,26 @@ +--- +MainSourceFile: source1.cpp +Diagnostics: + - DiagnosticName: test-basic +DiagnosticMessage: + Message: Fix + FilePath: $(path)/basic.h + FileOffset: 242 + Replacements: +- FilePath:$(path)/basic.h + Offset: 242 + Length: 26 + ReplacementText: 'auto & elem : ints' +- FilePath:$(path)/basic.h + Offset: 276 + Length: 22 + ReplacementText: '' +- FilePath:$(path)/basic.h + Offset: 298 + Length: 1 + ReplacementText: elem +- FilePath:$(path)/../yml-basic/basic.h + Offset: 1
[clang-tools-extra] [clang-apply-replacements] Add support for the `.yml` file extension (PR #78842)
https://github.com/unterumarmung updated https://github.com/llvm/llvm-project/pull/78842 >From 14767cd17ca81ceded59b630968bfd5f2dfab26b Mon Sep 17 00:00:00 2001 From: Daniil Dudkin Date: Sat, 20 Jan 2024 14:07:40 +0300 Subject: [PATCH] [clang-apply-replacements] Add support for the `.yml` file extension The `.yml` file extension is a valid extension for the YAML files, but it was not previously supported by the Clang Apply Replacements tool. This commit adds support for processing `.yml` files. Without this change, running the tool on a folder containing `.yml` files generated by clang-tidy would have no effect. --- .../lib/Tooling/ApplyReplacements.cpp | 8 - .../Inputs/yml-basic/basic.h | 32 +++ .../Inputs/yml-basic/file1.yml| 26 +++ .../Inputs/yml-basic/file2.yml| 14 .../clang-apply-replacements/yml-basic.cpp| 17 ++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h create mode 100644 clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml create mode 100644 clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file2.yml create mode 100644 clang-tools-extra/test/clang-apply-replacements/yml-basic.cpp diff --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp index 87ed1b8797cb05..9e0da82dfd3806 100644 --- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp +++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp @@ -23,11 +23,14 @@ #include "clang/Tooling/DiagnosticsYaml.h" #include "clang/Tooling/ReplacementsYaml.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include #include using namespace llvm; @@ -39,6 +42,9 @@ namespace clang { namespace replace { namespace detail { + +static constexpr std::array AllowedExtensions = {".yaml", ".yml"}; + template static std::error_code collectReplacementsFromDirectory( const llvm::StringRef Directory, TranslationUnits &TUs, @@ -56,7 +62,7 @@ static std::error_code collectReplacementsFromDirectory( continue; } -if (extension(I->path()) != ".yaml") +if (!is_contained(AllowedExtensions, extension(I->path( continue; TUFiles.push_back(I->path()); diff --git a/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h new file mode 100644 index 00..48509684b7725c --- /dev/null +++ b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/basic.h @@ -0,0 +1,32 @@ +#ifndef BASIC_H +#define BASIC_H + + +class Parent { +public: + virtual void func() {} +}; + +class Derived : public Parent { +public: + virtual void func() {} + // CHECK: virtual void func() override {} +}; + +extern void ext(int (&)[5], const Parent &); + +void func(int t) { + int ints[5]; + for (unsigned i = 0; i < 5; ++i) { +int &e = ints[i]; +e = t; +// CHECK: for (auto & elem : ints) { +// CHECK-NEXT: elem = t; + } + + Derived d; + + ext(ints, d); +} + +#endif // BASIC_H diff --git a/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml new file mode 100644 index 00..757f8d2ac18529 --- /dev/null +++ b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file1.yml @@ -0,0 +1,26 @@ +--- +MainSourceFile: source1.cpp +Diagnostics: + - DiagnosticName: test-basic +DiagnosticMessage: + Message: Fix + FilePath: $(path)/basic.h + FileOffset: 242 + Replacements: +- FilePath:$(path)/basic.h + Offset: 242 + Length: 26 + ReplacementText: 'auto & elem : ints' +- FilePath:$(path)/basic.h + Offset: 276 + Length: 22 + ReplacementText: '' +- FilePath:$(path)/basic.h + Offset: 298 + Length: 1 + ReplacementText: elem +- FilePath:$(path)/../yml-basic/basic.h + Offset: 148 + Length: 0 + ReplacementText: 'override ' +... diff --git a/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file2.yml b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file2.yml new file mode 100644 index 00..e8b54e99bc101c --- /dev/null +++ b/clang-tools-extra/test/clang-apply-repl