Page MenuHomePhabricator

Fix doubled whitespace in modernize-use-auto fixit
AbandonedPublic

Authored by malcolm.parsons on Oct 9 2016, 1:11 PM.

Details

Summary

Check whether the character following the type is whitespace to avoid
doubled whitespace in fixit.

Event Timeline

malcolm.parsons retitled this revision from to Fix doubled whitespace in modernize-use-auto fixit.
malcolm.parsons updated this object.
Prazek accepted this revision.Oct 9 2016, 11:55 PM
Prazek edited edge metadata.

LGTM

This revision is now accepted and ready to land.Oct 9 2016, 11:55 PM
malcolm.parsons added a subscriber: cfe-commits.
aaron.ballman added inline comments.Oct 10 2016, 7:38 AM
clang-tidy/modernize/UseAutoCheck.cpp
378

Oye, this is deceptively expensive because you now have to go back to the actual source file for this information. That source file may live on a network share somewhere, for instance.

Can you use Token::hasLeadingSpace() instead?

Also, doesn't this still need to care about the RemoveStars option?

Only insert whitespace when removing stars

clang-tidy/modernize/UseAutoCheck.cpp
378

Where would I get a Token from?

aaron.ballman added inline comments.Oct 10 2016, 1:36 PM
clang-tidy/modernize/UseAutoCheck.cpp
378

Hrm, might not be as trivial as I was hoping (I thought we had a way to go from a SourceLocation back to a Token, but I'm not seeing one off-hand). Regardless, I worry about the expense of going all the way back to the source for this.

@alexfh -- should this functionality be a part of a more general "we've made a fixit, now make it not look ugly?" pass? At least then, if we go back to the source, we can do it in a more controlled manner and hopefully get some performance back from that.

clang-tidy/modernize/UseAutoCheck.cpp
378

LineIsMarkedWithNOLINT is going to read the source anyway, so I don't see any additional expense.

aaron.ballman added inline comments.Oct 12 2016, 11:00 AM
clang-tidy/modernize/UseAutoCheck.cpp
378

The additional expense comes from many checks needing similar functionality -- generally, fixit replacements are going to require formatting modifications of some sort. It's better to handle all of those in the same place and transparently rather than have every check with a fixit manually handle formatting. Additionally, this means we can go back to the source as infrequently as possible.

clang-tidy/modernize/UseAutoCheck.cpp
378

I ran strace -c on clang-tidy on several real world source files that modernize-use-auto warns about before and after this change and the counts for read, write, stat, open, close, openat and fstat syscalls were all unchanged.
The expense is exactly zero.
It will still be zero when multiplied by many checks.
There is no performance issue here.
Do you have any other issues with this change?

aaron.ballman added inline comments.Oct 12 2016, 1:17 PM
clang-tidy/modernize/UseAutoCheck.cpp
378

Thank you for running strace to see what the behavior is. From what I understand, this is hard to judge because it depends on when the SourceManager elects to drop its underlying memory buffer (if at all). We definitely try to avoid going back to source when possible in Clang; I think clang-tidy should take a similar policy.

I don't think the fix you have is incorrect. I am, however, not convinced this is the best way to solve the problem because we're playing whack-a-mole with fixing replacements already, and this is one more instance of such need. @alexfh had suggested (in a different review thread about a similar need) that we solve this through a convenient bottleneck (such as the diagnostics engine) that basically runs clang-format over replaced text, handles extraneous commas, etc and I would prefer to hear if efforts are being made/planned for that approach before committing another one-off solution. The extra space included in the current behavior is not a correctness issue, so I'm not too worried about getting this fix in immediately if there's work in progress on a better approach.

malcolm.parsons abandoned this revision.Jan 3 2017, 4:20 AM

I'd prefer a -format option to clang-tidy.

alexfh edited edge metadata.Jan 3 2017, 6:59 AM

I'd prefer a -format option to clang-tidy.

Exactly.