This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add option "LiteralInitializers" to cppcoreguidelines-pro-type-member-init
ClosedPublic

Authored by mgehre on Sep 24 2016, 2:56 PM.

Details

Summary

Adds the option "LiteralInitializers" to
cppcoreguidelines-pro-type-member-init.
If set to non-zero, the check will provide fix-its with literal
initializers (`int i = 0;) instead of curly braces (int i{};`).

If find that more readable. Of course, this is purely a question of style,
thus the option is disabled by default.

Diff Detail

Repository
rL LLVM

Event Timeline

mgehre updated this revision to Diff 72393.Sep 24 2016, 2:56 PM
mgehre retitled this revision from to [clang-tidy] Add option "LiteralInitializers" to cppcoreguidelines-pro-type-member-init.
mgehre updated this object.
mgehre added reviewers: alexfh, aaron.ballman, hokein.
mgehre added a subscriber: cfe-commits.
mgehre updated this revision to Diff 72394.Sep 24 2016, 3:04 PM

Extend test to negative case

aaron.ballman added inline comments.Sep 26 2016, 8:49 AM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
377 ↗(On Diff #72394)

What about cases where a suffix is desired, like U or LL?

379 ↗(On Diff #72394)

Same here for f.

test/clang-tidy/cppcoreguidelines-pro-type-member-init-literal-initializers.cpp
14 ↗(On Diff #72394)

The * should bind to ptr.

hokein added inline comments.Oct 11 2016, 3:57 AM
test/clang-tidy/cppcoreguidelines-pro-type-member-init-literal-initializers.cpp
1 ↗(On Diff #72394)

-std=c++11 is not needed. This extra compile argument is added by default when running check_clang_tidy.

mgehre updated this revision to Diff 93056.Mar 25 2017, 2:19 PM
mgehre marked 2 inline comments as done.

Add suffix for floating point literals; clang-format

clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
377 ↗(On Diff #72394)

Is this really necessary for initializing to zero?
The compiler will figure out the correct thing, and I personally find "0" easier to read than "0ULL".

379 ↗(On Diff #72394)

This is now fixed in the check.

test/clang-tidy/cppcoreguidelines-pro-type-member-init-literal-initializers.cpp
1 ↗(On Diff #72394)

If I remove `-std=c++11`, the behavior changes and Context.getLangOpts().CPlusPlus11 is false.

aaron.ballman added inline comments.Mar 25 2017, 3:15 PM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
377 ↗(On Diff #72394)

It is necessary because of the way literals are typed. [lex.icon]p2 lists what happens when there is no suffix on a literal, but it boils down to the compiler picking the first type that can represent the value, out of int, long int, or long long int. Without the U or the LL, the type of the literal will not match the type of the field. This shouldn't have an impact on the generated code, but it may cause diagnostics for some compilers.

mgehre updated this revision to Diff 93511.Mar 30 2017, 11:39 AM

Put 'u' and 'l' on integer literals

aaron.ballman added inline comments.Apr 3 2017, 4:45 PM
clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
348 ↗(On Diff #93511)

type doesn't follow our usual naming conventions. I would recommend QT.

357 ↗(On Diff #93511)

You shouldn't be calling an "internal" function here. Instead, you can do dyn_cast<BuiltinType>(type.getCanonicalType().getTypePtr())

369 ↗(On Diff #93511)

Some users care deeply about l vs L because of how hard it is to distinguish depending on code fonts. I don't know that we need an option for that sort of thing, but my personal preference is to use capital letters rather than lowercase ones. (I originally read this as initializing to 0.01, FWIW).

test/clang-tidy/cppcoreguidelines-pro-type-member-init-literal-initializers.cpp
1 ↗(On Diff #72394)

That's because the extra args you specify disable the c++11 specification, so this is correct to do.

The modernize-use-default-member-init check now has an option with the same effect, but it is called UseAssignment.
We should use consistent option names.
Is there any way for multiple checks to share an option?

alexfh edited edge metadata.Apr 20 2017, 9:02 AM

The modernize-use-default-member-init check now has an option with the same effect, but it is called UseAssignment.
We should use consistent option names.
Is there any way for multiple checks to share an option?

There's OptionsView::getLocalOrGlobal. See how StrictMode option is read in ArgumentCommentCheck, for example.

Is there any way for multiple checks to share an option?

There's OptionsView::getLocalOrGlobal. See how StrictMode option is read in ArgumentCommentCheck, for example.

ArgumentCommentCheck uses getLocalOrGlobal, but InefficientStringConcatenationCheck and SuspiciousEnumUsageCheck don't.
6 checks have an IncludeStyle option that isn't shared.
4 checks share a HeaderFileExtensions option, but with different defaults.

Is there any way for multiple checks to share an option?

There's OptionsView::getLocalOrGlobal. See how StrictMode option is read in ArgumentCommentCheck, for example.

ArgumentCommentCheck uses getLocalOrGlobal, but InefficientStringConcatenationCheck and SuspiciousEnumUsageCheck don't.
6 checks have an IncludeStyle option that isn't shared.
4 checks share a HeaderFileExtensions option, but with different defaults.

The right thing would be to fix these. I might get around to this, if nobody does this before me.

alexfh requested changes to this revision.Jul 20 2017, 5:07 AM

Is there any way for multiple checks to share an option?

There's OptionsView::getLocalOrGlobal. See how StrictMode option is read in ArgumentCommentCheck, for example.

ArgumentCommentCheck uses getLocalOrGlobal, but InefficientStringConcatenationCheck and SuspiciousEnumUsageCheck don't.
6 checks have an IncludeStyle option that isn't shared.
4 checks share a HeaderFileExtensions option, but with different defaults.

The right thing would be to fix these. I might get around to this, if nobody does this before me.

Better late than never: r308605 ;)

Any specific plans re: this patch?

This revision now requires changes to proceed.Jul 20 2017, 5:07 AM
mgehre updated this revision to Diff 107632.Jul 20 2017, 10:43 PM
mgehre edited edge metadata.
mgehre marked 3 inline comments as done.

Implemented all review comments
Use global settings "UseAssignment"

alexfh requested changes to this revision.Jul 24 2017, 9:07 AM

A few more nits.

clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h
68 ↗(On Diff #107632)

nit: Please add an empty line before the comment.

68 ↗(On Diff #107632)

"use literals" and "UseAssignment" should be consistent, I guess.

test/clang-tidy/cppcoreguidelines-pro-type-member-init-use-assignment.cpp
38 ↗(On Diff #107632)

No need for a semicolon here.

This revision now requires changes to proceed.Jul 24 2017, 9:07 AM
mgehre updated this revision to Diff 198714.May 8 2019, 1:24 PM

Implement review comments

Herald added a project: Restricted Project. · View Herald TranscriptMay 8 2019, 1:24 PM
mgehre marked 3 inline comments as done.May 8 2019, 1:24 PM
aaron.ballman accepted this revision.May 9 2019, 5:31 AM

LGTM

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
361 ↗(On Diff #198714)

We may not have to care about this for this version of the patch, but I believe we have other checks with options to add literal suffixes in lowercase vs uppercase form. We should try to honor that the same way we're honoring using assignment vs braces.

Do I wait for @alexfh to turn his red into a green, too?

Do I wait for @alexfh to turn his red into a green, too?

I think you covered all of the concerns he raised. If he doesn't give you an LG in the next 24 hours, I think you can go ahead and commit. If there are remaining issues, we can deal with them post-commit.

lebedev.ri added inline comments.
clang-tools-extra/docs/ReleaseNotes.rst
66–69 ↗(On Diff #198714)

This is in the wrong category

mgehre updated this revision to Diff 199330.May 13 2019, 2:36 PM

Fix wrong merge of ReleaseNotes

mgehre marked an inline comment as done.May 13 2019, 2:36 PM
This revision was not accepted when it landed; it landed in state Needs Review.May 23 2019, 10:48 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMay 23 2019, 10:48 PM