Skip to content

Commit 45fec0c

Browse files
[clang-apply-replacements] Add support for the .yml file extension (#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.
1 parent f9dc0b6 commit 45fec0c

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
#include "clang/Tooling/DiagnosticsYaml.h"
2424
#include "clang/Tooling/ReplacementsYaml.h"
2525
#include "llvm/ADT/ArrayRef.h"
26+
#include "llvm/ADT/STLExtras.h"
27+
#include "llvm/ADT/StringRef.h"
2628
#include "llvm/ADT/StringSet.h"
2729
#include "llvm/Support/FileSystem.h"
2830
#include "llvm/Support/MemoryBuffer.h"
2931
#include "llvm/Support/Path.h"
3032
#include "llvm/Support/raw_ostream.h"
33+
#include <array>
3134
#include <optional>
3235

3336
using namespace llvm;
@@ -39,6 +42,9 @@ namespace clang {
3942
namespace replace {
4043

4144
namespace detail {
45+
46+
static constexpr std::array<StringRef, 2> AllowedExtensions = {".yaml", ".yml"};
47+
4248
template <typename TranslationUnits>
4349
static std::error_code collectReplacementsFromDirectory(
4450
const llvm::StringRef Directory, TranslationUnits &TUs,
@@ -56,7 +62,7 @@ static std::error_code collectReplacementsFromDirectory(
5662
continue;
5763
}
5864

59-
if (extension(I->path()) != ".yaml")
65+
if (!is_contained(AllowedExtensions, extension(I->path())))
6066
continue;
6167

6268
TUFiles.push_back(I->path());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef BASIC_H
2+
#define BASIC_H
3+
4+
5+
class Parent {
6+
public:
7+
virtual void func() {}
8+
};
9+
10+
class Derived : public Parent {
11+
public:
12+
virtual void func() {}
13+
// CHECK: virtual void func() override {}
14+
};
15+
16+
extern void ext(int (&)[5], const Parent &);
17+
18+
void func(int t) {
19+
int ints[5];
20+
for (unsigned i = 0; i < 5; ++i) {
21+
int &e = ints[i];
22+
e = t;
23+
// CHECK: for (auto & elem : ints) {
24+
// CHECK-NEXT: elem = t;
25+
}
26+
27+
Derived d;
28+
29+
ext(ints, d);
30+
}
31+
32+
#endif // BASIC_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
MainSourceFile: source1.cpp
3+
Diagnostics:
4+
- DiagnosticName: test-basic
5+
DiagnosticMessage:
6+
Message: Fix
7+
FilePath: $(path)/basic.h
8+
FileOffset: 242
9+
Replacements:
10+
- FilePath: $(path)/basic.h
11+
Offset: 242
12+
Length: 26
13+
ReplacementText: 'auto & elem : ints'
14+
- FilePath: $(path)/basic.h
15+
Offset: 276
16+
Length: 22
17+
ReplacementText: ''
18+
- FilePath: $(path)/basic.h
19+
Offset: 298
20+
Length: 1
21+
ReplacementText: elem
22+
- FilePath: $(path)/../yml-basic/basic.h
23+
Offset: 148
24+
Length: 0
25+
ReplacementText: 'override '
26+
...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
MainSourceFile: source2.cpp
3+
Diagnostics:
4+
- DiagnosticName: test-basic
5+
DiagnosticMessage:
6+
Message: Fix
7+
FilePath: $(path)/basic.h
8+
FileOffset: 148
9+
Replacements:
10+
- FilePath: $(path)/../yml-basic/basic.h
11+
Offset: 298
12+
Length: 1
13+
ReplacementText: elem
14+
...
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: mkdir -p %T/Inputs/yml-basic
2+
// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/yml-basic/basic.h > %T/Inputs/yml-basic/basic.h
3+
// RUN: sed "s#\$(path)#%/T/Inputs/yml-basic#" %S/Inputs/yml-basic/file1.yml > %T/Inputs/yml-basic/file1.yml
4+
// RUN: sed "s#\$(path)#%/T/Inputs/yml-basic#" %S/Inputs/yml-basic/file2.yml > %T/Inputs/yml-basic/file2.yml
5+
// RUN: clang-apply-replacements %T/Inputs/yml-basic
6+
// RUN: FileCheck -input-file=%T/Inputs/yml-basic/basic.h %S/Inputs/yml-basic/basic.h
7+
//
8+
// Check that the yml files are *not* deleted after running clang-apply-replacements without remove-change-desc-files.
9+
// RUN: ls -1 %T/Inputs/yml-basic | FileCheck %s --check-prefix=YML
10+
//
11+
// Check that the yml files *are* deleted after running clang-apply-replacements with remove-change-desc-files.
12+
// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/yml-basic/basic.h > %T/Inputs/yml-basic/basic.h
13+
// RUN: clang-apply-replacements -remove-change-desc-files %T/Inputs/yml-basic
14+
// RUN: ls -1 %T/Inputs/yml-basic | FileCheck %s --check-prefix=NO_YML
15+
//
16+
// YML: {{^file.\.yml$}}
17+
// NO_YML-NOT: {{^file.\.yml$}}

0 commit comments

Comments
 (0)