This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add a checker that warns on const string & members.
ClosedPublic

Authored by bkramer on Jul 15 2014, 9:57 AM.

Details

Diff Detail

Repository
rL LLVM

Event Timeline

bkramer updated this revision to Diff 11451.Jul 15 2014, 9:57 AM
bkramer retitled this revision from to [clang-tidy] Add a checker that warns on const string & members..
bkramer updated this object.
bkramer added reviewers: alexfh, djasper.
bkramer added a subscriber: Unknown Object (MLST).
bkramer updated this revision to Diff 11453.Jul 15 2014, 10:03 AM

Remove useless link, there doesn't seem to be a reachable version of this.

alexfh edited edge metadata.Jul 15 2014, 10:09 AM

Tests?

bkramer updated this revision to Diff 11454.Jul 15 2014, 10:28 AM
bkramer edited edge metadata.

Add test case that got attached to the wrong patch somehow.

djasper added inline comments.Jul 16 2014, 2:16 AM
clang-tidy/google/CMakeLists.txt
8 ↗(On Diff #11454)

I'd probably call this StringReferenceMemberCheck, but I don't have a strong objection against this name.

clang-tidy/google/GoogleTidyModule.cpp
35 ↗(On Diff #11454)

Where does "runtime" come from? Does that make sense?

clang-tidy/google/MemberStringReferencesCheck.h
22 ↗(On Diff #11454)

Just for my benefit, why are non-const string reference members ok?

bkramer added inline comments.Jul 16 2014, 2:44 AM
clang-tidy/google/CMakeLists.txt
8 ↗(On Diff #11454)

Less backwards that suggestion is, rename the file I will.

clang-tidy/google/GoogleTidyModule.cpp
35 ↗(On Diff #11454)

We inherited the categories from cpplint.py and I've been following the existing style. I think it makes sense to have a direct mapping from cpplint categories to tidy checks.

clang-tidy/google/MemberStringReferencesCheck.h
22 ↗(On Diff #11454)

The rationale is that you can initialize the following class with a const char *, creating a string temporary and leave a dangling reference. This behavior won't come up with non-const refs.

struct S {
  S(const string &S) : Str(S) {}

  const string &Str;
};

I'll add that info to the checker description comment.

alexfh added inline comments.Jul 16 2014, 2:59 AM
clang-tidy/google/GoogleTidyModule.cpp
35 ↗(On Diff #11454)

Right. Having references to cpplint.py in some form is valueable, as we could then use the same check identifiers to support targeted NOLINT suppressions already present in the code.

The categories ("runtime", "readability", "build", "compatibility" etc.) used in cpplint.py make some sense: they usually reflect which aspects of the code quality the check addresses.

In this case "runtime" means that patterns detected by this check may lead to runtime errors (as opposed to just readability issues or compatibility problems).

clang-tidy/google/MemberStringReferencesCheck.cpp
28 ↗(On Diff #11454)

Did you mean "in template instantiations"? We don't (and shouldn't) ignore members in template declarations, if the type is not dependent on template arguments.

bkramer updated this revision to Diff 11489.Jul 16 2014, 2:59 AM

Rename check, add more doxygen comments.

alexfh accepted this revision.Jul 16 2014, 3:01 AM
alexfh edited edge metadata.

Looks good provided Daniel's concerns are addressed.

This revision is now accepted and ready to land.Jul 16 2014, 3:01 AM
djasper accepted this revision.Jul 16 2014, 3:05 AM
djasper edited edge metadata.

Sure.

bkramer closed this revision.Jul 16 2014, 3:09 AM
bkramer updated this revision to Diff 11492.

Closed by commit rL213133 (authored by d0k).