This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix PR#44528 'modernize-use-using and enums'
ClosedPublic

Authored by f00kat on Jan 21 2020, 3:07 AM.

Details

Summary

Add 'enumDecl' matcher for 'enum' cases. Change a bit logic in 'check' method and add test for this case.

Diff Detail

Event Timeline

f00kat created this revision.Jan 21 2020, 3:07 AM
Eugene.Zelenko set the repository for this revision to rG LLVM Github Monorepo.
Eugene.Zelenko added a subscriber: cfe-commits.
Herald added a project: Restricted Project. · View Herald TranscriptJan 21 2020, 7:07 AM

If you are using diff to create a patch, use -U999999 to get the full context of the patch

clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
274

Can you put a new line here.

f00kat updated this revision to Diff 239491.Jan 21 2020, 10:12 PM

Add new line in the end of file checkers/modernize-use-using.cpp

aaron.ballman added inline comments.Jan 25 2020, 11:18 AM
clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
28

It looks like there's a backtick in the comment rather than a quotation mark, that should probably be matcher's instead. Also, instead of struct/enums, I think it should be tag declarations (unions count, for instance).

30–31

Rather than matching on these, I think it would make more sense to add a new matcher for tagDecl(). It can be local to the check for now, or you can add it to the AST matchers header as a separate patch and then base this patch off that work.

f00kat marked 4 inline comments as done.Jan 30 2020, 7:15 AM

I have changed the code to work with tagDecl. It`s work fine on simple test cases such as

typedef enum { ea, eb } EnumT;

But not work with

#include <string>
typedef enum { ea, eb } EnumT;

It is not related with new tagDecl matcher. I simplify this case:

  1. Create file Header.h
  2. Header.h
typedef size_t sometype;
  1. main.cpp
#include "Header.h"
using EnumT = enum { ea, eb };

It`s happens because in class UseUsingCheck we have variable LastReplacementEnd and it may contains SourceLocation for another file.
First time AST matcher trigger on file Header.h and we remember SourceLocation for code "typedef size_t sometype".
Then AST matcher trigger on "using EnumT = enum { ea, eb };" in main.cpp and we trying to use the value of LastReplacementEnd storing SourceLocation for Header.h.

So this 'if case' does not execute

if (ReplaceRange.getBegin().isMacroID() ||
   ReplaceRange.getBegin() >= LastReplacementEnd) {

execute this one

// This is additional TypedefDecl in a comma-separated typedef declaration.
// Start replacement *after* prior replacement and separate with semicolon.
ReplaceRange.setBegin(LastReplacementEnd);

Simple fix

if (ReplaceRange.getBegin().isMacroID() ||
   (Result.SourceManager->getFileID(ReplaceRange.getBegin()) != Result.SourceManager->getFileID(LastReplacementEnd)) || \\ new check
   (ReplaceRange.getBegin() >= LastReplacementEnd)) {

But maybe I wrong. I don`t know API well enough.

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
28

Ok

30–31

"Add new AST Matcher" - it's the Jedi level :)
Where can I find some manual that describe this? Or maybe some example(git commit).

"you can add it to the AST matchers header as a separate patch and then base this patch off that work."
So the sequencing will be:

  1. I create another new patch with new AST matcher 'tagDecl'
  2. Wait until you reviewed it
  3. Merge to master
  4. Pull latest master
  5. Modify code and update this patch

?

30–31

I add new TagDecl matcher in this patch https://reviews.llvm.org/D73464. Check please.

clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
274

Yeah

f00kat updated this revision to Diff 241448.Jan 30 2020, 7:23 AM
  1. Apply new tagDecl matcher
  2. Trying to fix some bug
aaron.ballman added inline comments.Feb 1 2020, 8:21 AM
clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
76–78

Be sure to run clang-format over the patch; this looks well beyond the usual 80-col limit. You can also drop the unnecessary parens around the sub expressions.

Also, a comment explaining why this code is needed would help future folks as would a test case showing what this corrects.

f00kat updated this revision to Diff 241931.Feb 2 2020, 10:23 AM
  1. clang-format
  2. Add comment for additional "files IDs equals" check
  3. Add test
aaron.ballman accepted this revision.Feb 2 2020, 1:38 PM

LGTM! I'm happy to commit on your behalf once the minor nits are fixed up.

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
73

that ranges -> that the ranges
belongs -> belong to
overlap -> overlapping

This revision is now accepted and ready to land.Feb 2 2020, 1:38 PM
f00kat updated this revision to Diff 241971.Feb 2 2020, 10:12 PM
f00kat marked an inline comment as done.

Clean up

Sorry for my english. Sometimes it's more difficult than bug fixing

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
76–78

I left the brackets because they seem to increase readability. Or not? :)

aaron.ballman closed this revision.Feb 3 2020, 4:55 AM

Sorry for my english. Sometimes it's more difficult than bug fixing

It's not a problem at all, that's why we help one another out spotting those things! :-)

I've commit on your behalf in 6423ae417e17611c4ee529f5848e839e6d9cb795

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
76–78

We don't typically use them unless there's a need, but it doesn't harm readability to keep them, so this is fine.