Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 25, 2024

Conversation

kakkoko
Copy link
Contributor

@kakkoko kakkoko commented Oct 20, 2024

Fixes #113099.

Copy link

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.

@llvmbot
Copy link
Member

llvmbot commented Oct 20, 2024

@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())) {

@kakkoko kakkoko force-pushed the assume-filename-with-clang-format-ignore branch from e095b55 to 7a31529 Compare October 20, 2024 19:02
Copy link

github-actions bot commented Oct 20, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@kakkoko kakkoko force-pushed the assume-filename-with-clang-format-ignore branch 2 times, most recently from 1a265dc to 96811d0 Compare October 21, 2024 10:42
Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. I'd like to hear @owenca opinion

@mydeveloperday mydeveloperday requested a review from owenca October 22, 2024 11:14
@owenca
Copy link
Contributor

owenca commented Oct 22, 2024

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.

@kakkoko
Copy link
Contributor Author

kakkoko commented Oct 22, 2024

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?

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.
@kakkoko kakkoko force-pushed the assume-filename-with-clang-format-ignore branch from 509a048 to 07ac17e Compare October 22, 2024 17:32
@kakkoko
Copy link
Contributor Author

kakkoko commented Oct 22, 2024

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.

@mydeveloperday
Copy link
Contributor

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'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...)

Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense now, please wait for @owenca and @HazardyKnusperkeks opinion

@kakkoko
Copy link
Contributor Author

kakkoko commented Oct 23, 2024

@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.

Co-authored-by: Owen Pan <owenpiano@gmail.com>
@kakkoko
Copy link
Contributor Author

kakkoko commented Oct 24, 2024

@owenca Thanks for the review. I applied all of the suggestions as I thought they were all appropriate.

@owenca owenca merged commit efd8938 into llvm:main Oct 25, 2024
8 checks passed
Copy link

@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!

@frobtech frobtech mentioned this pull request Oct 25, 2024
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] .clang-format-ignore ignores -assume-filename option
5 participants