[clang-tools-extra] [clang-apply-replacements] Add support for the `.yml` file extension (PR #78842)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 20 03:10:17 PST 2024
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 <unterumarmung at yandex.ru>
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 <array>
#include <optional>
using namespace llvm;
@@ -39,6 +42,9 @@ namespace clang {
namespace replace {
namespace detail {
+
+static constexpr std::array<StringRef, 2> AllowedExtensions = {".yaml", ".yml"};
+
template <typename TranslationUnits>
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 00000000000000..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 00000000000000..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 00000000000000..e8b54e99bc101c
--- /dev/null
+++ b/clang-tools-extra/test/clang-apply-replacements/Inputs/yml-basic/file2.yml
@@ -0,0 +1,14 @@
+---
+MainSourceFile: source2.cpp
+Diagnostics:
+ - DiagnosticName: test-basic
+ DiagnosticMessage:
+ Message: Fix
+ FilePath: $(path)/basic.h
+ FileOffset: 148
+ Replacements:
+ - FilePath: $(path)/../yml-basic/basic.h
+ Offset: 298
+ Length: 1
+ ReplacementText: elem
+...
diff --git a/clang-tools-extra/test/clang-apply-replacements/yml-basic.cpp b/clang-tools-extra/test/clang-apply-replacements/yml-basic.cpp
new file mode 100644
index 00000000000000..e6ee91940f3003
--- /dev/null
+++ b/clang-tools-extra/test/clang-apply-replacements/yml-basic.cpp
@@ -0,0 +1,17 @@
+// RUN: mkdir -p %T/Inputs/yml-basic
+// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/yml-basic/basic.h > %T/Inputs/yml-basic/basic.h
+// RUN: sed "s#\$(path)#%/T/Inputs/yml-basic#" %S/Inputs/yml-basic/file1.yml > %T/Inputs/yml-basic/file1.yml
+// RUN: sed "s#\$(path)#%/T/Inputs/yml-basic#" %S/Inputs/yml-basic/file2.yml > %T/Inputs/yml-basic/file2.yml
+// RUN: clang-apply-replacements %T/Inputs/yml-basic
+// RUN: FileCheck -input-file=%T/Inputs/yml-basic/basic.h %S/Inputs/yml-basic/basic.h
+//
+// Check that the yml files are *not* deleted after running clang-apply-replacements without remove-change-desc-files.
+// RUN: ls -1 %T/Inputs/yml-basic | FileCheck %s --check-prefix=YML
+//
+// Check that the yml files *are* deleted after running clang-apply-replacements with remove-change-desc-files.
+// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/yml-basic/basic.h > %T/Inputs/yml-basic/basic.h
+// RUN: clang-apply-replacements -remove-change-desc-files %T/Inputs/yml-basic
+// RUN: ls -1 %T/Inputs/yml-basic | FileCheck %s --check-prefix=NO_YML
+//
+// YML: {{^file.\.yml$}}
+// NO_YML-NOT: {{^file.\.yml$}}
More information about the cfe-commits
mailing list