[clang] [clang-format] Fix working -assume-filename with .clang-format-ignore (PR #113100)

via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 20 12:02:23 PDT 2024


https://github.com/kakkoko updated https://github.com/llvm/llvm-project/pull/113100

>From 7a315297c29244fb9a7358b962784c2fd984986c Mon Sep 17 00:00:00 2001
From: kakkoko <kakkoko at pushf.jp>
Date: Mon, 21 Oct 2024 03:48:13 +0900
Subject: [PATCH] [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  |  6 +++++-
 2 files changed, 16 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..84eaf32cc44498 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -707,8 +707,12 @@ 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())) {



More information about the cfe-commits mailing list