This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] fix for readability-identifier-naming incorrectly fixes variables which become keywords
ClosedPublic

Authored by Daniel599 on Oct 5 2019, 6:23 AM.

Details

Summary

This patch fixes 'Bug 41120'
https://bugs.llvm.org/show_bug.cgi?id=41120
When clang-tidy encounter a fixup name that would become a keyword, it just display a warning with a reason and doesn't attempt to fix it.
Please let me know if this patch is OK, Thanks.

Diff Detail

Event Timeline

Daniel599 created this revision.Oct 5 2019, 6:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 5 2019, 6:23 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Daniel599 edited the summary of this revision. (Show Details)Oct 5 2019, 6:24 AM
Daniel599 edited the summary of this revision. (Show Details)Oct 5 2019, 6:29 AM
Eugene.Zelenko added inline comments.
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
865

Please don't use auto when type is not spelled in same statement or not iterator.

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
68

Does value really matter?

99

Please use default member initialization and = default;

Eugene.Zelenko retitled this revision from fix for readability-identifier-naming incorrectly fixes variables which become keywords to [clang-tidy] fix for readability-identifier-naming incorrectly fixes variables which become keywords.Oct 5 2019, 6:35 AM
Eugene.Zelenko edited reviewers, added: hokein, aaron.ballman; removed: llvm-commits, alexfh_.
Eugene.Zelenko removed a project: Restricted Project.
Daniel599 updated this revision to Diff 223372.Oct 5 2019, 9:43 AM

code fixes according to code-review comments

Daniel599 marked 3 inline comments as done.Oct 5 2019, 9:44 AM

This should really be a full context diff -U999999999

Daniel599 updated this revision to Diff 223429.Oct 6 2019, 10:08 AM

added -U999999 to diff cmd

I logged the original bug and I like it!

I think the warning is better than mutating with a prefix, Thank you.

I'll let the code owners approve it, but you have my vote!

I logged the original bug and I like it!

I think the warning is better than mutating with a prefix, Thank you.

I'll let the code owners approve it, but you have my vote!

glad to hear it :)
I saw this issue when I submitted mine (https://bugs.llvm.org/show_bug.cgi?id=43306) which for now seems to be a harder fix, still looking for solution there.

I saw this issue when I submitted mine (https://bugs.llvm.org/show_bug.cgi?id=43306) which for now seems to be a harder fix, still looking for a solution there.

oh boy!...are you gonna have to look at every variable/macro in scope and compare?

I saw this issue when I submitted mine (https://bugs.llvm.org/show_bug.cgi?id=43306) which for now seems to be a harder fix, still looking for a solution there.

oh boy!...are you gonna have to look at every variable/macro in scope and compare?

Don't know yet, I thought about using Sema::CheckShadow or Sema::LookupName, but I can't find a Sema instance, not sure how those methods effect performance

aaron.ballman added inline comments.Oct 9 2019, 12:15 PM
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
877

Can this be const IdentifierTable&?

880

What if changing it would switch to using a macro instead of a keyword? e.g.,

#define foo 12

void func(int Foo); // Changing Foo to foo would be bad, no?
951

Diagnostics are not full sentences, so the full stop and capitalization are wrong here. I think this should be combined with the diagnostic above using a %select.

auto Diag = diag(Decl.first, "invalid case style for %0 '%1'%select{; cannot be fixed because '%3' would conflict with a keyword|}2")
                  << Failure.KindName << Decl.second << (Failure.FixStatus != ShouldFixStatus::ConflictsWithKeyword) << Failure.Fixup;
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
70

if -> If

73

Missing a full stop at the end of the comment.

99

This seems a bit confusing to me. It seems like the reason to not generate a fixit is being used to determine whether to diagnose at all, which seems incorrect despite sort of being what the old code did.

100

Drop the parens.

clang-tools-extra/test/clang-tidy/readability-identifier-naming-bugfix-name-conflicts.cpp
28

Please add the newline to the end of the file.

clang/include/clang/Basic/IdentifierTable.h
584

Is this actually needed? Pretty sure you can use std::find(table.begin(), table.end(), Name); at the call site (or something similar).

Daniel599 marked 3 inline comments as done.Oct 9 2019, 1:47 PM
Daniel599 added inline comments.
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
880

That's another type of bug, just like the one I found https://bugs.llvm.org/show_bug.cgi?id=43306
I don't aim on solving all of them in one patch, my patch just fixes keywords.
Also, I don't think my patch makes the above situation worse.

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
99

I`m sorry, I didn't understand you.
I tried to keep the old behavior of "cannot fix inside macro" and that's why I needed the method ShouldNotify.
Do you suggest another idea or other code flow?

clang/include/clang/Basic/IdentifierTable.h
584

Yes, because std::find would be slower here compare to HashTable.find

Daniel599 updated this revision to Diff 224151.Oct 9 2019, 1:51 PM

code-review fixes

Daniel599 marked 7 inline comments as done.Oct 9 2019, 1:52 PM
Daniel599 marked an inline comment as done.
Daniel599 marked an inline comment as done.Oct 12 2019, 9:59 AM
Daniel599 added inline comments.
clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
880

Regarding your comment about macro name, I can fix it using IdentifierInfo::hasMacroDefinition.
Should I fix it in this patch? I`ll add another value to ShouldFixStatus and another error message

Daniel599 updated this revision to Diff 224783.Oct 13 2019, 7:17 AM

Added check for macro definition, please re-review the enum ShouldFixStatus as now I use it as a switch-case within the diagnostic message.
Thanks.

Daniel599 marked an inline comment as done.Oct 18 2019, 2:06 AM

Hi, any updates regarding my patch? issues to fix?
Thanks.

aaron.ballman accepted this revision.Oct 23 2019, 6:39 AM

LGTM aside from a minor nit.

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
880

Thanks for fixing this!

881–885

You can elide the curly braces.

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h
99

I see now that the reason we don't diagnose is because the construct is within a macro, which is a pretty reasonable thing to do, so I'm no longer confused. :-)

This revision is now accepted and ready to land.Oct 23 2019, 6:39 AM
Daniel599 updated this revision to Diff 226403.Oct 25 2019, 3:53 AM

removed curly braces

Daniel599 marked 3 inline comments as done.Oct 25 2019, 3:54 AM

Do you need someone to commit this on your behalf?

Daniel599 added a comment.EditedOct 28 2019, 10:01 AM

Do you need someone to commit this on your behalf?

Yes that will be great,
Thank you :)

Thank you for the patch! I've commit on your behalf in e477988309dbde214a6d16ec690a416882714aac

Thank you for the patch! I've commit on your behalf in e477988309dbde214a6d16ec690a416882714aac

Thank you for the commit,
just a little question. shouldn't the test file clang-tools-extra/test/clang-tidy/readability-identifier-naming-bugfix-name-conflicts.cpp be added?

Thank you for the patch! I've commit on your behalf in e477988309dbde214a6d16ec690a416882714aac

Thank you for the commit,
just a little question. shouldn't the test file clang-tools-extra/test/clang-tidy/readability-identifier-naming-bugfix-name-conflicts.cpp be added?

It absolutely should have been, but I don't see why it didn't get added. I've fixed it in 7b6174bb147957b4695023ae57c95ca07af7b917, thank you for bringing it to my attention!