[clang] [clang-format] Fix working -assume-filename with .clang-format-ignore (PR #113100)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 21 03:43:14 PDT 2024
https://github.com/kakkoko updated https://github.com/llvm/llvm-project/pull/113100
>From 96811d0cdb880466ec293eaf71fd27f5859fd97d 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.
If "ignore" is implemented as "do not output anything," the formatting
result will be empty, so appropriate processing such as outputting
standard input directly to standard output is necessary.
---
clang/test/Format/clang-format-ignore.cpp | 11 +++++++
clang/tools/clang-format/ClangFormat.cpp | 36 ++++++++++++++++++++++-
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/clang/test/Format/clang-format-ignore.cpp b/clang/test/Format/clang-format-ignore.cpp
index fb49fa9dd52c65..5fb4bbbc0bdf95 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,bar" | clang-format -assume-filename=foo.c 2>&1 \
+// RUN: | FileCheck %s -check-prefix=CHECK6
+// CHECK6: foo,bar
+
+// RUN: touch bar.c
+// RUN: echo "foo,bar" | clang-format -assume-filename=bar.c 2>&1 \
+// RUN: | FileCheck %s -check-prefix=CHECK7 -allow-empty
+// CHECK7: foo, bar
+
// 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..a8e844d73e2c4d 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -707,8 +707,42 @@ int main(int argc, const char **argv) {
errs() << "Clang-formatting " << LineNo << " files\n";
}
- if (FileNames.empty())
+ if (FileNames.empty()) {
+ if (!AssumeFileName.empty() && isIgnored(AssumeFileName)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
+ MemoryBuffer::getSTDIN();
+ if (std::error_code EC = CodeOrErr.getError()) {
+ errs() << EC.message() << "\n";
+ return 1;
+ }
+ std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
+ if (Code->getBufferSize() == 0)
+ return 0; // Empty files are formatted correctly.
+
+ StringRef BufStr = Code->getBuffer();
+
+ const char *InvalidBOM =
+ clang::SrcMgr::ContentCache::getInvalidBOM(BufStr);
+
+ if (InvalidBOM) {
+ errs() << "error: encoding with unsupported byte order mark \""
+ << InvalidBOM << "\" detected.\n";
+ return 1;
+ }
+
+ // Output the input to STDIN as is
+ if (OutputXML) {
+ unsigned CursorPosition = Cursor;
+ clang::format::outputXML(Replacements{}, Replacements{},
+ clang::format::FormattingAttemptStatus{},
+ Cursor, CursorPosition);
+ } else {
+ outs() << BufStr;
+ }
+ 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