Skip to content

Commit

Permalink
[clang-format] Fix working -assume-filename with .clang-format-ignore
Browse files Browse the repository at this point in the history
The filename given by the `-assume-filename` option is used to search
for `.clang-format` files, etc., but is not used to match the contents
of the `.clang-format-ignore` file.

Fixed that when the `-assume-filename` option is specified, the
`.clang-format-ignore` file is processed for that filename.
  • Loading branch information
kakkoko committed Oct 22, 2024
1 parent 5a47d48 commit 07ac17e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions clang/test/Format/clang-format-ignore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,16 @@
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
// CHECK5-NOT: foo.js

// RUN: echo "foo.*" > .clang-format-ignore
// RUN: touch foo.c
// RUN: echo foo | clang-format -assume-filename=foo.c 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
// CHECK6-NOT: foo

// RUN: touch bar.c
// RUN: echo foo | clang-format -assume-filename=bar.c 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK7 -allow-empty
// CHECK7: foo

// RUN: cd ..
// RUN: rm -r %t.dir
5 changes: 4 additions & 1 deletion clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,11 @@ int main(int argc, const char **argv) {
errs() << "Clang-formatting " << LineNo << " files\n";
}

if (FileNames.empty())
if (FileNames.empty()) {
if (!AssumeFileName.empty() && isIgnored(AssumeFileName))
return 0;
return clang::format::format("-", FailOnIncompleteFormat);
}

if (FileNames.size() > 1 &&
(!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
Expand Down

0 comments on commit 07ac17e

Please sign in to comment.