This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] cppcoreguidelines-pro-type-member-init should not complain about static variables
ClosedPublic

Authored by alexfh on Apr 28 2016, 10:45 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

alexfh updated this revision to Diff 55441.Apr 28 2016, 10:45 AM
alexfh retitled this revision from to [clang-tidy] cppcoreguidelines-pro-type-member-init should not complain about static variables.
alexfh updated this object.
alexfh added a reviewer: sbenza.
alexfh added subscribers: cfe-commits, flx, michael_miller.
aaron.ballman added inline comments.Apr 28 2016, 12:14 PM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
284 ↗(On Diff #55441)

We should add a test that this still diagnoses variables with dynamic storage duration. Alternatively, this could be unless(anyOf(hasStaticStorageDuration(), hasThreadStorageDuration()))

alexfh added inline comments.Apr 28 2016, 12:21 PM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
284 ↗(On Diff #55441)

I don't think it ever diagnosed on the dynamic storage duration, since it doesn't match cxxNewExpr ;)

clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
284 ↗(On Diff #55441)

Perhaps unrelated but a new expression without the parens wouldn't value-initialize a record type with a default constructor and potentially be a problematic. Maybe we should be checking cxxNewExpr.

aaron.ballman added inline comments.Apr 28 2016, 12:27 PM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
284 ↗(On Diff #55441)

Shouldn't it have diagnosed:

struct s {
  int *i;
  s() {}
};

?

alexfh added inline comments.Apr 28 2016, 12:57 PM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
284 ↗(On Diff #55441)

This code does not introduce an object with dynamic storage duration. Variable i is just a member variable, and the fact that it's a pointer doesn't change much. The check used to warn in this case and continues to do so:

/tmp/q.cc:3:3: warning: constructor does not initialize these fields: i [cppcoreguidelines-pro-type-member-init]
  s() {}
  ^
      : i()

To resolve the confusion:

[basic.stc]p2

Static, thread, and automatic storage durations are associated with objects introduced by declarations (3.1) and implicitly created by the implementation (12.2). The dynamic storage duration is associated with objects created with operator new (5.3.4).
284 ↗(On Diff #55441)

Michael, we should be checking cxxNewExpr, but I would leave this to you or someone else. In this patch I'm just trying to make the check less noisy.

aaron.ballman accepted this revision.Apr 28 2016, 1:00 PM
aaron.ballman edited edge metadata.
aaron.ballman added inline comments.
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
284 ↗(On Diff #55441)

Ah, that's a very valid point.

This revision is now accepted and ready to land.Apr 28 2016, 1:00 PM
This revision was automatically updated to reflect the committed changes.