Skip to content

Commit

Permalink
[clang-apply-replacements] Add support for the .yml file extension (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
unterumarmung committed Jan 25, 2024
1 parent f9dc0b6 commit 45fec0c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
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$}}

0 comments on commit 45fec0c

Please sign in to comment.