This is an archive of the discontinued LLVM Phabricator instance.

Handle C++11 brace initializers in readability-braces-around-statements
Needs RevisionPublic

Authored by adek05 on Jan 16 2016, 11:03 PM.

Details

Reviewers
alexfh
Summary

C++11 brace initialization messes up with a condition which is used to check whether we are past the end of the statement intended to be enclosed with braces. Fix looks one token ahead, to check whether we are in a situation of "};" which could suggest a brace initialization is used and shoud work fine in other cases.

Diff Detail

Event Timeline

adek05 updated this revision to Diff 45088.Jan 16 2016, 11:03 PM
adek05 retitled this revision from to Handle C++11 brace initializers in readability-braces-around-statements.
adek05 updated this object.
adek05 added a reviewer: alexfh.
adek05 added inline comments.Jan 17 2016, 10:39 AM
clang-tidy/readability/BracesAroundStatementsCheck.cpp
63–72

This might not be the best approach. Maybe it will be better to try to find the matching opening brace and see whether it is the opening brace for 'then' block. Feel free to throw more suggestions.

alexfh added inline comments.Jan 18 2016, 1:19 AM
clang-tidy/readability/BracesAroundStatementsCheck.cpp
63–72

Maybe just skip correct bracket sequences (a statement can also contain lambdas, subscript expressions and normal parentheses in various combinations)?

adek05 added inline comments.Jan 18 2016, 8:42 PM
clang-tidy/readability/BracesAroundStatementsCheck.cpp
63–72

Well, we could just remove TokKind == tok::r_brace in line 64. I don't really understand why this check is there. The test suite passes if I remove that check, including the new cases I have added. Maybe that's the way to go?

I tracked the initial commit which introduced TokKind == tok::r_brace: D5395 and I don't see a clear reason why it needs to be there. Do you know why we need it?

alexfh added inline comments.Jan 19 2016, 7:41 AM
clang-tidy/readability/BracesAroundStatementsCheck.cpp
63–72

Well, we could just remove TokKind == tok::r_brace in line 64.

You mean line 74, I guess. It should be easy to check if removing it helps by running tests and applying fixes to a large enough codebase, e.g. LLVM+Clang, and running its tests to ensure the correctness of changes + manual inspection of a few replacements.

alexfh requested changes to this revision.Mar 18 2016, 7:29 AM
alexfh edited edge metadata.
This revision now requires changes to proceed.Mar 18 2016, 7:29 AM

Here is the r_brace use case:

if (true) while ("ok") {}

Unfortunately it looks quite similar to:

if (true) s = {};