[clang] [clang-format] Fix working -assume-filename with .clang-format-ignore (PR #113100)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 24 11:19:31 PDT 2024
https://github.com/kakkoko updated https://github.com/llvm/llvm-project/pull/113100
>From 07ac17e531315126c932b130963798a296736736 Mon Sep 17 00:00:00 2001
From: kakkoko <kakkoko at pushf.jp>
Date: Mon, 21 Oct 2024 03:48:13 +0900
Subject: [PATCH 1/2] [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.
---
clang/test/Format/clang-format-ignore.cpp | 11 +++++++++++
clang/tools/clang-format/ClangFormat.cpp | 5 ++++-
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp b/clang/test/Format/clang-format-ignore.cpp
index fb49fa9dd52c65..198ef3200a3845 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -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
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 108db7204aa68a..4f4a1b2c6be9cf 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -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())) {
>From 34de1224e42987e156fa45a8b65cda226a5c1723 Mon Sep 17 00:00:00 2001
From: kakkoko <parenthesized.ko at gmail.com>
Date: Fri, 25 Oct 2024 03:19:22 +0900
Subject: [PATCH 2/2] Apply suggestions from code review
Co-authored-by: Owen Pan <owenpiano at gmail.com>
---
clang/test/Format/clang-format-ignore.cpp | 13 ++++++-------
clang/tools/clang-format/ClangFormat.cpp | 2 +-
2 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp b/clang/test/Format/clang-format-ignore.cpp
index 198ef3200a3845..67d68aebde5d04 100644
--- a/clang/test/Format/clang-format-ignore.cpp
+++ b/clang/test/Format/clang-format-ignore.cpp
@@ -47,15 +47,14 @@
// 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: echo "int i ;" > foo.c
+// RUN: clang-format -assume-filename=foo.c < foo.c \
// RUN: | FileCheck %s -check-prefix=CHECK6 -allow-empty
-// CHECK6-NOT: foo
+// CHECK6-NOT: int
-// 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: clang-format -assume-filename=bar.c < foo.c \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -match-full-lines
+// CHECK7: int i;
// RUN: cd ..
// RUN: rm -r %t.dir
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 4f4a1b2c6be9cf..96fb85e99bf5f0 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -708,7 +708,7 @@ int main(int argc, const char **argv) {
}
if (FileNames.empty()) {
- if (!AssumeFileName.empty() && isIgnored(AssumeFileName))
+ if (isIgnored(AssumeFileName))
return 0;
return clang::format::format("-", FailOnIncompleteFormat);
}
More information about the cfe-commits
mailing list