This is an archive of the discontinued LLVM Phabricator instance.

Add warning to clang-reorder-fields when reordering breaks member init list dependencies
ClosedPublic

Authored by sameconrad on Jul 27 2017, 7:16 PM.

Details

Summary

This adds a warning emitted by clang-reorder-fields when the reordering fields breaks dependencies in the initializer list (such that -Wuninitialized would warn when compiling). For example, given:

Foo::Foo(int x)
  : a(x)
  , b(a) {}

Reordering fields to [b,a] gives:

Foo::Foo(int x)
  : b(a)
  , a(x) {}

Emits the warning:
2: Warning: reordering field a after b makes a uninitialized when used in init expression.

Diff Detail

Event Timeline

sameconrad created this revision.Jul 27 2017, 7:16 PM
sameconrad edited the summary of this revision. (Show Details)
alexander-shaposhnikov set the repository for this revision to rL LLVM.
compnerd added inline comments.Jul 27 2017, 9:37 PM
clang-reorder-fields/ReorderFieldsAction.cpp
111

What if the FieldDecl belongs to a base class? Can you add a test case for that scenario?

test/clang-reorder-fields/FieldDependencyWarning.cpp
3

tests should not depend on STL (for good examples see how things are done in clang-tidy tests), so simply remove this include and define a small class with a constructor right here

Add additional tests for cases with base and derived classes.
Add clarifying comments about emitting warnings for base class members during reordering checks.
Add fix for bug where reordering in init lists that contain non-member initializers causes segfault.

sameconrad added inline comments.Jul 29 2017, 7:04 PM
clang-reorder-fields/ReorderFieldsAction.cpp
111

I've added tests for this scenario and an explanatory comment. It doesn't seem this is actually a problem due to the way Clang constructs the AST. Sema::PerformObjectMemberConversion always inserts an implicit UncheckedDerivedToBase cast when accessing a base class member which means that hasObjectExpression(cxxThisExpr()) won't actually match against such an expression.

174

While adding tests for emitting warnings about base class members I found another bug here- non-member initializers don't have a field decl and so would result in undefind behavior further down when accessing field indexes (I added the test case ClassDerived which would cause a segfault before this change).

test/clang-reorder-fields/FieldDependencyWarning.cpp
3

This has been updated to use a simple struct.

LGTM, thanks!
do you have commit access ? (if not i can commit this patch for you)

This revision is now accepted and ready to land.Jul 29 2017, 8:22 PM
In D35972#825348, @alexshap wrote:

LGTM, thanks!
do you have commit access ? (if not i can commit this patch for you)

I don't have commit access, so you will have to commit it.
Thank you

This revision was automatically updated to reflect the committed changes.

https://reviews.llvm.org/rL309505
plus I changed the code to emit this warning via DiagnosticEngine and updated the tests accordingly.