This is an archive of the discontinued LLVM Phabricator instance.

Clang tidy utility to generate a fix hint for a subsequent expression to the existing one
Needs ReviewPublic

Authored by barcisz on Sep 15 2022, 7:35 AM.

Details

Summary

This is a utility function for adding a statement to be executed after a specified one using FixItHints. It is designed to provide good formatting and protection against lack of curly brackets (for example when your preceding statement is in a for loop). This is a prerequisite to D133956 - one of the new CUDA checks. It also relies on D133725 to be able to search for comments right after a certain token.

Diff Detail

Event Timeline

barcisz created this revision.Sep 15 2022, 7:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 15 2022, 7:35 AM
barcisz requested review of this revision.Sep 15 2022, 7:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 15 2022, 7:35 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
barcisz updated this revision to Diff 460405.Sep 15 2022, 7:38 AM

Use the new Lexer option to include comments while finding next token

Would I be correct in assuming you have uploaded the wrong diff here?

Would I be correct in assuming you have uploaded the wrong diff here?

Yes, sorry, got mixed up with D133956 (6 diffs ain't easy to shuffle :-/)

barcisz updated this revision to Diff 460551.Sep 15 2022, 4:35 PM

Misplaced diff

njames93 requested changes to this revision.Sep 17 2022, 2:55 AM

There's some merit in this, like wrapping the previous statement in braces.. However, clang-tidy should not focus on the formatting aspect as we have clang-format built in to address formatting decisions, and any special formatting you do in here would likely be undone by the users configuration.
It would also be nice to add in some unittests to demonstrate that braces are currently inserted etc.

How does this handle pathological cases like the statement being the iteration-expression of a for loop, or a init-statement in an if/switch/range-for loop. The documentation looks like it tries to explain that, but it doesn't do a great job IMHO.

This revision now requires changes to proceed.Sep 17 2022, 2:55 AM

It would also be nice to add in some unittests to demonstrate that braces are currently inserted etc.

Right sorry, forgot to port unit tests, will do that swiftly

barcisz edited the summary of this revision. (Show Details)Sep 17 2022, 10:11 AM

How does this handle pathological cases like the statement being the iteration-expression of a for loop, or a init-statement in an if/switch/range-for loop. The documentation looks like it tries to explain that, but it doesn't do a great job IMHO.

It doesn't really, it's mostly meant for cases where we know that the statement will be in the body and it's only added in the utils because it's needed for D133956, but I can just move it to inside the check if you believe that it won't be as useful for the general case. The reason I want to preserve the statement to which the comments relate to is because if I put the new statement/macro invocation right after the current statement then the formatted will move the comments to the new line with the new statement instead of keeping them in the same line as the current statement (and in case of macros I'm not sure if it will even move it to the next line at all)

barcisz updated this revision to Diff 461955.Sep 21 2022, 11:04 AM

Tests for the utility

LegalizeAdulthood resigned from this revision.Mar 29 2023, 8:19 AM