This is an archive of the discontinued LLVM Phabricator instance.

Refactor: Move remove-cstr-calls from a standalone executable to a clang-tidy check readability-redundant-string-cstr
ClosedPublic

Authored by LegalizeAdulthood on Jan 31 2015, 4:25 PM.

Details

Summary

Moved the files and moved the test inputs, adjusting for use within clang-tidy instead of as a standalone utility.

Diff Detail

Event Timeline

LegalizeAdulthood retitled this revision from to Refactor: Move remove-cstr-calls from a standalone executable to a clang-tidy check.
LegalizeAdulthood updated this object.
LegalizeAdulthood edited the test plan for this revision. (Show Details)
LegalizeAdulthood added a subscriber: Unknown Object (MLST).
alexfh edited edge metadata.Feb 4 2015, 8:54 AM

It looks like if you add reviewers and subscribers after creating a differential revision, Phabricator won't send notification mails to them (unless you add a comment afterwards). In any case, it makes sense double-checking that a message found its way to cfe-commits at least.

Thanks for the patch! A few comments inline.

clang-tidy/misc/MiscTidyModule.cpp
39 ↗(On Diff #19104)

I'd put this check under readability/ and call it "readability-redundant-cstr-calls".

clang-tidy/misc/RemoveCStrCall.cpp
32 ↗(On Diff #19104)

I suspect that most of this method can be replaced to a call to clang::Lexer::getSourceText (and this method can return StringRef).

84 ↗(On Diff #19104)

Seems like a good use case for "auto".

171 ↗(On Diff #19104)

nit: Please remove the trailing period.

I'd also use 'c_str()' instead of c_str.

test/clang-tidy/misc-remove-cstr-call.cpp
23 ↗(On Diff #19104)

Please make the check more specific to avoid incorrect matches:

// CHECK-FIXES: {{^  }}f1(s);{{$}}
28 ↗(On Diff #19104)

This should be CHECK-FIXES:. Please also add CHECK-MESSAGES to verify the error messages.

// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant call ....

Thanks for the comments!

I'll see if I can get an updated version tonight.

LegalizeAdulthood edited edge metadata.

Corrected issues found in review.

Use Lexer::getSourceText instead of SourceManager to get source text

I don't have a problem with making this a clang-tidy module, but it was (also) meant as an example of how to write a refactoring tool.

I don't have a problem with making this a clang-tidy module, but it was (also) meant as an example of how to write a refactoring tool.

Do we now have enough examples of refactoring tools that the example motivation is no longer a concern?

clang-tidy/misc/RemoveCStrCall.cpp
32 ↗(On Diff #19104)

Diff in progress...

clang-tidy/readability/RemoveCStrCall.h
10–11 ↗(On Diff #19867)

Oops, this still says misc

I don't have a problem with making this a clang-tidy module, but it was (also) meant as an example of how to write a refactoring tool.

Do we now have enough examples of refactoring tools that the example motivation is no longer a concern?

I didn't find any references to remove-cstr-calls from docs, so I don't think the tool is in any sense special. There is a clang-rename which can be used as an example of a refactoring tool, and there is a dedicated tool-template. IMO, these should be enough.

sbenza added inline comments.Feb 23 2015, 9:25 AM
clang-tidy/readability/RemoveCStrCall.cpp
37 ↗(On Diff #19873)

Use isa<> instead

Use isa<> instead of dyn_cast<>.
Move the check from namespace clang::tidy into namespace clang::tidy::readability

alexfh added inline comments.Mar 2 2015, 7:27 AM
clang-tidy/readability/RemoveCStrCall.cpp
23 ↗(On Diff #20937)

clang-format?

50 ↗(On Diff #20937)

clang-format?

58 ↗(On Diff #20937)

It's better to make this a StringRef and replace the concatenation of strings below with:

return (llvm::Twine("*(") + Text + ")").str().
129 ↗(On Diff #20937)

This is a good place to use auto or const auto *.

131 ↗(On Diff #20937)

s/const bool/bool/

134 ↗(On Diff #20937)

s/const std::string/std::string/

136 ↗(On Diff #20937)

Why static_cast instead of .str()?

clang-tidy/readability/RemoveCStrCall.h
20 ↗(On Diff #20937)

The fact that it removes something doesn't make it much different from other checks, and it doesn't deserve a place in the name. Let's call this check "readability-redundant-cstr" or "readability-redundant-cstr-calls" instead (and the class RedundantCStrCheck or RedundantCStrCallsCheck).

I posted a relevant comment earlier, but it has fallen through the cracks.

Run clang-format on the source files.
Use the name readability-redundant-cstr-call for the check.
Update types of locals as requested

alexfh added a comment.Mar 3 2015, 7:26 AM

Run clang-format on the source files.
Use the name readability-redundant-cstr-call for the check.

Thanks for updating this. However could you use one of the alternatives I suggested? If you lean toward using 'call' in the name, let it be readability-redundant-cstr-calls and correspondingly RedundantCStrCallsCheck for the name of the class and the source files. Something seems wrong to me in using singular call in the name of this check, as it's supposed to find any number of them, not just one. Also note the Check suffix for the class name (and corresponding file names. It's kind of a naming convention for the checks.

Thanks.

For the reference, here's my previous comment:

The fact that it removes something doesn't make it much different from other checks, and it doesn't deserve a place in the name. Let's call this check "readability-redundant-cstr" or "readability-redundant-cstr-calls" instead (and the class RedundantCStrCheck or RedundantCStrCallsCheck).

clang-tidy/misc/MiscTidyModule.cpp
39 ↗(On Diff #19104)

Fixed.

clang-tidy/misc/RemoveCStrCall.cpp
32 ↗(On Diff #19104)

Fixed.

84 ↗(On Diff #19104)

Fixed.

171 ↗(On Diff #19104)

Fixed

clang-tidy/readability/RemoveCStrCall.cpp
23 ↗(On Diff #20937)

Fixed.

50 ↗(On Diff #20937)

Fixed.

58 ↗(On Diff #20937)

Fixed

129 ↗(On Diff #20937)

Fixed.

131 ↗(On Diff #20937)

Fixed.

134 ↗(On Diff #20937)

Fixed.

136 ↗(On Diff #20937)

Fixed.

37 ↗(On Diff #19873)

Fixed.

clang-tidy/readability/RemoveCStrCall.h
20 ↗(On Diff #20937)

Fixed.

10–11 ↗(On Diff #19867)

Fixed.

test/clang-tidy/misc-remove-cstr-call.cpp
23 ↗(On Diff #19104)

Fixed.

28 ↗(On Diff #19104)

Fixed.

Run clang-format on the source files.
Use the name readability-redundant-cstr-call for the check.

Thanks for updating this. However could you use one of the alternatives I suggested? If you lean toward using 'call' in the name, let it be readability-redundant-cstr-calls and correspondingly RedundantCStrCallsCheck for the name of the class and the source files. Something seems wrong to me in using singular call in the name of this check, as it's supposed to find any number of them, not just one. Also note the Check suffix for the class name (and corresponding file names. It's kind of a naming convention for the checks.

Thanks.

For the reference, here's my previous comment:

The fact that it removes something doesn't make it much different from other checks, and it doesn't deserve a place in the name. Let's call this check "readability-redundant-cstr" or "readability-redundant-cstr-calls" instead (and the class RedundantCStrCheck or RedundantCStrCallsCheck).

clang-tidy/readability/RemoveCStrCall.h
20 ↗(On Diff #20937)

I was thinking about names; we have RedundantSmartPtrGet and check name readability-redundant-smartptr-get, a check for redundant calls to get() on a smartptr class.

Would that make this RedundantStringCStr and check name readability-redundant-string-cstr?

alexfh added inline comments.Mar 5 2015, 3:56 AM
clang-tidy/readability/RemoveCStrCall.h
20 ↗(On Diff #20937)

I'd prefer the checks have the Check suffix in the class name. This convention is not strictly followed, but it probably should. The motivation is discussed in this review thread: http://reviews.llvm.org/D4189.

Thus it can be RedundantStringCStrCheck and the check name readability-redundant-string-cstr.

clang-tidy/readability/RemoveCStrCall.h
20 ↗(On Diff #20937)

Sounds good, I'll see if I can update that tonight. I did notice that not all checks had classes ending in Check. I'll create another changeset that fixes that.

LegalizeAdulthood retitled this revision from Refactor: Move remove-cstr-calls from a standalone executable to a clang-tidy check to Refactor: Move remove-cstr-calls from a standalone executable to a clang-tidy check readability-redundant-string-cstr.

Rename class to RedundantStringCStrCheck

Improve the naming to be more consistent with other checks:
RedundantStringCStrCheck
readability-redundant-string-cstr

Align comments and include guard with changed names

alexfh accepted this revision.Mar 9 2015, 5:39 AM
alexfh edited edge metadata.

The code looks fine now, but if you don't mind, I'd ask you to fix a few more nits (mostly left from the original tool's code). Once done, I'd be happy to commit the patch for you.

Thanks!

clang-tidy/readability/RedundantStringCStrCheck.cpp
39

s/const auto/const auto*/

39

Use LLVM naming style: Op

52

s/const auto/const auto*/

68

const char StringConstructor[] = ...

72

ditto

93

id is an old-style construct, now we prefer using .bind():

id("...", X) -> X.bind("...")
96

ditto

117

ditto

120

ditto

125

const auto*

test/clang-tidy/readability-redundant-string-cstr.cpp
25

nit: The number of spaces here should remain the same, so I'd better replace {{^ *}} with {{^ }}.

This revision is now accepted and ready to land.Mar 9 2015, 5:39 AM
clang-tidy/readability/RedundantStringCStrCheck.cpp
39

Fixed

52

Fixed

68

Fixed

72

Fixed

93

Fixed

96

Fixed

117

Fixed

120

Fixed

125

Fixed

LegalizeAdulthood edited edge metadata.

Prefer bind() over id()
Change types to be more consistent with llvm/clang
Chang header guard to be more consistent with llvm/clang

Make test cases more specific.

test/clang-tidy/readability-redundant-string-cstr.cpp
25

Fixed

alexfh closed this revision.Mar 15 2015, 5:36 PM

Thank you!

Committed revision 232338.

test/clang-tidy/readability-redundant-string-cstr.cpp