[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:01:04 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: kakkoko (kakkoko)

<details>
<summary>Changes</summary>

Fixes #<!-- -->113099.

---
Full diff: https://github.com/llvm/llvm-project/pull/113100.diff


2 Files Affected:

- (modified) clang/test/Format/clang-format-ignore.cpp (+11) 
- (modified) clang/tools/clang-format/ClangFormat.cpp (+7-1) 


``````````diff
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..c31f3d97f17eb1 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -707,8 +707,14 @@ int main(int argc, const char **argv) {
     errs() << "Clang-formatting " << LineNo << " files\n";
   }
 
-  if (FileNames.empty())
+  if (FileNames.empty()) {
+    if (!AssumeFileName.empty() && isIgnored(AssumeFileName)) {
+      outs() << "ignored\n";
+      return 0;
+    }
+    outs() << "not ignored\n";
     return clang::format::format("-", FailOnIncompleteFormat);
+  }
 
   if (FileNames.size() > 1 &&
       (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {

``````````

</details>


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


More information about the cfe-commits mailing list