[clang-format] Fix working -assume-filename with .clang-format-ignore
Fixes #113099.
Thank you for submitting a Pull Request (PR) to the LLVM Project!
This PR will be automatically labeled and the relevant teams will be notified.
If you wish to, you can add reviewers by using the "Reviewers" section on this page.
If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.
If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.
If you have further questions, they may be answered by the LLVM GitHub User Guide.
You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.
@llvm/pr-subscribers-clang-format
Author: kakkoko (kakkoko)
Changes
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 --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())) {
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
Also, the main purpose of -assume-filename is to set the language for input from stdin, and IMO the pseudo filename should not be filtered out by any .clang-format-ignore file.
Also, the main purpose of -assume-filename is to set the language for input from stdin, and IMO the pseudo filename should not be filtered out by any .clang-format-ignore file.
It is also worth noting that if the -style=file option is specified, it will look for .clang-format/_clang-format file in the same or parent directory based on the pathname specified by the -assume-filename option.
Since the specification is such for .clang-format/_clang-format file, users would naturally expect the same behavior for .clang-format-ignore file, wouldn't they?
I get what you are doing here, simply passing the input to the output even if we've said ignore the file.. Its not that I don't think this is ok I'm just wondering why the fix is here and not in the VS Code extension.
Sorry. This was my mistake. I had misconfigured some settings. The VSCode extension worked perfectly even when clang-format outputs nothing.
Therefore, I replaced the commit with a very simple one.
Also, the main purpose of -assume-filename is to set the language for input from stdin, and IMO the pseudo filename should not be filtered out by any .clang-format-ignore file.
It is also worth noting that if the
-style=fileoption is specified, it will look for.clang-format/_clang-formatfile in the same or parent directory based on the pathname specified by the-assume-filenameoption.Since the specification is such for
.clang-format/_clang-formatfile, users would naturally expect the same behavior for.clang-format-ignorefile, wouldn't they?
I'm a little confused..
So it seems you are saying... that an IDE that is using clang-format, formats the contents of the editor using stdin to clang-format, sort of like
echo "editor_buffer" | clang-format
and because it knows editor_buffer came from c:/project/myproject/src/lib/myfile.cxx its passing
echo "editor_buffer" | clang-format --assume-filename c:/project/myproject/src/lib/myfile.cxx
I presume in both case the IDE reads the output and puts it back in the editor rather than writing it to the file incase the user isn't ready to save just yet.
which means if you are running with --stage=file its using that assume-filename to get the directory of that assume-file and then look up that tree for a .clang-format
echo "buffer" | clang-format --style=file --assume-filename c:/project/myproject/src/lib/myfile.cxx
However what you are saying is, its not using that assume-filename to locate the .clang-format-ignore file? which if it had might have seen that myfile.* should have been ignored.
Hence in the IDE it reformats myfile.* meaning you can't save it unformatted
Did I understand that correctly? (its likely exactly what you said...)
@mydeveloperday
However what you are saying is, its not using that assume-filename to locate the .clang-format-ignore file?
Hence in the IDE it reformats myfile.* meaning you can't save it unformatted
Yes, that is correct.
You can see that in the following file of Clang-Format.cpp, main():
// If no file name is specified on the command line to be processed,
// formatting is performed on standard input and terminates.
if (FileNames.empty())
return clang::format::format("-", FailOnIncompleteFormat);
...
// Sequentially process each file specified on the command line
for (const auto &FileName : FileNames) {
// Check for .clang-format-ignore
const bool Ignored = isIgnored(FileName);
...
// If the pattern matches the pattern written in .clang-format-ignore,
// do nothing and proceed to the next file
if (Ignored)
continue;
...
// do formatting
Error |= clang::format::format(FileName, FailOnIncompleteFormat);
}
The isIgnored() function, which determines whether or not to ignore using .clang-format-ignore, is only called in the part that processes the files enumerated on the command line in order.
@owenca Thanks for the review. I applied all of the suggestions as I thought they were all appropriate.
@kakkoko Congratulations on having your first Pull Request (PR) merged into the LLVM Project!
Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.
Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.
How to do this, and the rest of the post-merge process, is covered in detail here.
If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.
If you don't get any reports, no action is required from you. Your changes are working as expected, well done!