Skip to content

[clang-apply-replacements] Add support for the .yml file extension #78842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 '
...
Original file line number Diff line number Diff line change
@@ -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
...
17 changes: 17 additions & 0 deletions clang-tools-extra/test/clang-apply-replacements/yml-basic.cpp
Original file line number Diff line number Diff line change
@@ -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$}}