Add 'enumDecl' matcher for 'enum' cases. Change a bit logic in 'check' method and add test for this case.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
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. |
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. |
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:
- Create file Header.h
- Header.h
typedef size_t sometype;
- 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 :) "you can add it to the AST matchers header as a separate patch and then base this patch off that work."
? | |
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 |
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. |
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 |
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? :) |
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. |
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).