This is an archive of the discontinued LLVM Phabricator instance.

[clang-format][NFC] Clean up class HeaderIncludes and Format.cpp
ClosedPublic

Authored by owenpan on Sep 28 2022, 11:37 PM.

Diff Detail

Event Timeline

owenpan created this revision.Sep 28 2022, 11:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2022, 11:37 PM
owenpan requested review of this revision.Sep 28 2022, 11:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2022, 11:37 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
clang/lib/Format/Format.cpp
2774

Did I miss where this comes from now?

kwk added a comment.EditedSep 29 2022, 1:18 AM

I didn't see much difference in what this patch does compare to mine but I saw that it removes the need for instantiating multiple llvm::Regex objects from a single static pattern. But that's something I've just done in a new revision of my own patch: https://reviews.llvm.org/D134733?vs=463205&id=463786#toc . This fast clean up makes it only harder for me to get my patch in but essentially both do the same thing except that I also have a single convenience function for trimming include names from " and <>". Also I have removed the need to duplicate the knowledge of which matching group one has to pick in order to get to the include name. This knowledge is now local to a function.

clang/lib/Format/Format.cpp
2774

@MyDeveloperDay clang/lib/Tooling/Inclusions/HeaderIncludes.cpp still has this:

const char IncludeRegexPattern[] =
    R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";

And in clang/lib/Tooling/Inclusions/HeaderIncludes.cpp @owenpan uses it to initialize the final regex

const llvm::Regex HeaderIncludes::IncludeRegex(IncludeRegexPattern);

The fact that we have two regex that are identical is an issue on its own that I tried to address with my patch as well. I didn't initialize the regex like @owenpan does here but I had a function to return it. Eventually a function makes it easier to apply the injection from a config file as you've suggested here. So I favor my solution.

@kwk D134733 reminded me of https://reviews.llvm.org/D121370#3401173, which I realized didn't go far enough. This NFC patch is what I should have suggested to you then. If you want to do the cleanup differently, you can either make suggestions here or create another NFC patch.

clang/lib/Format/Format.cpp
2774

The fact that we have two regex that are identical is an issue on its own that I tried to address with my patch as well.

It should be addressed in a separate NFC patch such as this one.

I didn't initialize the regex like @owenpan does here but I had a function to return it. Eventually a function makes it easier to apply the injection from a config file as you've suggested here. So I favor my solution.

Making IncludeRegex a public static const member is one of the better solutions when IncludeRegexPattern is fixed as it has been. If and when we decide to support user specified patterns, we will make any necessary changes then.

kwk added inline comments.Sep 30 2022, 2:50 AM
clang/lib/Format/Format.cpp
2774

The fact that we have two regex that are identical is an issue on its own that I tried to address with my patch as well.

It should be addressed in a separate NFC patch such as this one.

Ehm, I thought I *did* create a separate NFC patch with D134733. I prefixed it with [chore] but that is as good as NFC. I can rename it if you want.

I didn't initialize the regex like @owenpan does here but I had a function to return it. Eventually a function makes it easier to apply the injection from a config file as you've suggested here. So I favor my solution.

Making IncludeRegex a public static const member is one of the better solutions when IncludeRegexPattern is fixed as it has been. If and when we decide to support user specified patterns, we will make any necessary changes then.

Okay, but you could have suggested that in D134733, no? I've made the change in D134733 here: https://reviews.llvm.org/D134733?vs=463205&id=464196#toc, so the regex is static const. But I've also outsourced the function for accessing the include name so the logic is at one place not scattered over and over and the trimming is also in its own function. Having everything going through that functions is easier for maintenance IMHO. Before I wondered why we had two include regex patterns (both the same btw.) and why an include name wasn't found when I had changed the Matches[2] to Matches[3] for example. That won't happen when its going through the function. You just change it in one place and not "plenty".

I hope we can agree that my change is now complete with additions from yours here. I most certainly don't want to disrupt your workflow and I apologize if I had. Unfortunately text can be misinterpreted way too much.

owenpan added inline comments.Sep 30 2022, 8:31 PM
clang/lib/Format/Format.cpp
2774

Ehm, I thought I *did* create a separate NFC patch with D134733. I prefixed it with [chore] but that is as good as NFC. I can rename it if you want.

I didn't know by chore you meant NFC. Thanks for renaming it.

Okay, but you could have suggested that in D134733, no?

It would be too many places to point out as you can tell from this patch. When mixed with other unrelated changes in D134733, it would be more difficult for me to review and other people to follow. Making this patch allows me to focus on the best way to solve the problem without worrying about any additional "noise".

I hope we can agree that my change is now complete with additions from yours here.

It seems that you have applied this patch in its entirety to D134733. Why not just wait for it to land and rebase D134733 afterward?

I most certainly don't want to disrupt your workflow and I apologize if I had.

Not at all. If anything, it's probably I who have disrupted your workflow. :)

kwk accepted this revision.Oct 5 2022, 8:14 AM
kwk added inline comments.
clang/lib/Format/Format.cpp
2774

Ehm, I thought I *did* create a separate NFC patch with D134733. I prefixed it with [chore] but that is as good as NFC. I can rename it if you want.

I didn't know by chore you meant NFC. Thanks for renaming it.

Your welcome.

Okay, but you could have suggested that in D134733, no?

It would be too many places to point out as you can tell from this patch. When mixed with other unrelated changes in D134733, it would be more difficult for me to review and other people to follow. Making this patch allows me to focus on the best way to solve the problem without worrying about any additional "noise".

I get that but I thought that my NFC patch was doing exactly that:

  1. Make it one not two regexes
  2. Make the regex a static member and not call a function to get it.

I hope we can agree that my change is now complete with additions from yours here.

It seems that you have applied this patch in its entirety to D134733. Why not just wait for it to land and rebase D134733 afterward?

I feel that I introduced this cleanup myself by having only one regex and not two. The only difference from mine to your patch was that you used a static member and I made a function call and didn't avoid the instantiations of regex objects.

I most certainly don't want to disrupt your workflow and I apologize if I had.

Not at all. If anything, it's probably I who have disrupted your workflow. :)

This seems just weird afterall. So why don't you land your patch and I submit mine?

This revision is now accepted and ready to land.Oct 5 2022, 8:14 AM
This revision was landed with ongoing or failed builds.Oct 5 2022, 9:54 PM
This revision was automatically updated to reflect the committed changes.