Skip to content

Commit 96811d0

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. 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.
1 parent 5a47d48 commit 96811d0

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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,bar" | clang-format -assume-filename=foo.c 2>&1 \
52+
// RUN: | FileCheck %s -check-prefix=CHECK6
53+
// CHECK6: foo,bar
54+
55+
// RUN: touch bar.c
56+
// RUN: echo "foo,bar" | clang-format -assume-filename=bar.c 2>&1 \
57+
// RUN: | FileCheck %s -check-prefix=CHECK7 -allow-empty
58+
// CHECK7: foo, bar
59+
4960
// RUN: cd ..
5061
// RUN: rm -r %t.dir

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,42 @@ 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+
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
713+
MemoryBuffer::getSTDIN();
714+
if (std::error_code EC = CodeOrErr.getError()) {
715+
errs() << EC.message() << "\n";
716+
return 1;
717+
}
718+
std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get());
719+
if (Code->getBufferSize() == 0)
720+
return 0; // Empty files are formatted correctly.
721+
722+
StringRef BufStr = Code->getBuffer();
723+
724+
const char *InvalidBOM =
725+
clang::SrcMgr::ContentCache::getInvalidBOM(BufStr);
726+
727+
if (InvalidBOM) {
728+
errs() << "error: encoding with unsupported byte order mark \""
729+
<< InvalidBOM << "\" detected.\n";
730+
return 1;
731+
}
732+
733+
// Output the input to STDIN as is
734+
if (OutputXML) {
735+
unsigned CursorPosition = Cursor;
736+
clang::format::outputXML(Replacements{}, Replacements{},
737+
clang::format::FormattingAttemptStatus{},
738+
Cursor, CursorPosition);
739+
} else {
740+
outs() << BufStr;
741+
}
742+
return 0;
743+
}
711744
return clang::format::format("-", FailOnIncompleteFormat);
745+
}
712746

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

0 commit comments

Comments
 (0)