This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Harden PreferMemberInitializerCheck
ClosedPublic

Authored by njames93 on Feb 20 2021, 5:36 PM.

Details

Summary

Prevent warning when the values are initialized using fields that will be initialized later or VarDecls defined in the constructors body.
Both of these cases can't be safely fixed.
Also improve logic of finding where to insert member initializers, previously it could be confused by in class member initializers.

Diff Detail

Event Timeline

njames93 created this revision.Feb 20 2021, 5:36 PM
njames93 requested review of this revision.Feb 20 2021, 5:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 20 2021, 5:36 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
njames93 updated this revision to Diff 325428.Feb 22 2021, 6:06 AM

Fix potential crash if macros are used, Now we just don't try to emit a fix.
Fix issue where FixIt for member initializers would be inserted at the start of the constructor decl due to implicit member initializers confusing it.
Streamlined method for finding where to create member initializers when there's no member initializer list.

Fix potential crash if macros are used, Now we just don't try to emit a fix.

Can you add a test case that covers this change?

clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
103
121

Fix potential crash if macros are used, Now we just don't try to emit a fix.

Can you add a test case that covers this change?

I can try, but I'm not sure of the exact cause of it. It happened when running the check over clang codebase.
The assert was caused by not checking the Optional returned from findNextToken which can fail if the location passed is at the end of a macro expansion.

njames93 updated this revision to Diff 325482.Feb 22 2021, 10:20 AM

Add a test for the crash fix.

njames93 updated this revision to Diff 325484.Feb 22 2021, 10:22 AM
njames93 marked 2 inline comments as done.

Address auto comments.

aaron.ballman accepted this revision.Feb 22 2021, 11:14 AM

LGTM, thanks!

This revision is now accepted and ready to land.Feb 22 2021, 11:14 AM
This revision was automatically updated to reflect the committed changes.