Skip to content

Commit 506a4a4

Browse files
committed
[clang-format] Fix working -assume-filename with .clang-format-ignore
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.
1 parent 5a47d48 commit 506a4a4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/test/Format/clang-format-ignore.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,16 @@
4646
// CHECK5-NEXT: {{Formatting \[4/5] .*foo\.c}}
4747
// CHECK5-NOT: foo.js
4848

49+
// RUN: echo "foo.*" > .clang-format-ignore
50+
// RUN: touch foo.c
51+
// RUN: echo foo | clang-format -assume-filename=foo.c 2>&1 \
52+
// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
53+
// CHECK6-NOT: foo
54+
55+
// RUN: touch bar.c
56+
// RUN: echo foo | clang-format -assume-filename=bar.c 2>&1 \
57+
// RUN: | FileCheck %s -check-prefix=CHECK7 -allow-empty
58+
// CHECK7: foo
59+
4960
// RUN: cd ..
5061
// RUN: rm -r %t.dir

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,11 @@ int main(int argc, const char **argv) {
707707
errs() << "Clang-formatting " << LineNo << " files\n";
708708
}
709709

710-
if (FileNames.empty())
710+
if (FileNames.empty()) {
711+
if (!AssumeFileName.empty() && isIgnored(AssumeFileName))
712+
return 0;
711713
return clang::format::format("-", FailOnIncompleteFormat);
714+
}
712715

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

0 commit comments

Comments
 (0)