This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] readability-identifier-naming: fix hungarian enum prefix in C
ClosedPublic

Authored by amurzeau on Feb 27 2023, 1:56 PM.

Details

Summary

When checking a C file, enum tags are of type int instead of the enum
type as in C++.
So the checker was using i as the prefix for enum tags instead of main
letters of the enum type name (for example rt for enum REV_TYPE).

This commit fixes this divergence and makes the behavior the same between
C and C++ files.

For example, enum REV_TYPE { rtRevValid }; won't be reported as badly
named when in a C file. Previously, it would have proposed a rename to
iRevValid.

This commit also add a file to test the hungarian notation checker with C
files.
The test file was made from identifier-naming-hungarian-notation.cpp
with C++-only features removed.

Diff Detail

Event Timeline

amurzeau created this revision.Feb 27 2023, 1:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 1:56 PM
Herald added a subscriber: xazax.hun. · View Herald Transcript
amurzeau requested review of this revision.Feb 27 2023, 1:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 1:56 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
amurzeau updated this revision to Diff 500905.Feb 27 2023, 2:07 PM

Update the release notes too, to notify of this changes with C files.

Suggest the user to use EnumConstantPrefix instead of
EnumConstantHungarianPrefix to keep the previous behavior of using i for
all enum tags.

PiotrZSL added inline comments.
clang-tools-extra/docs/ReleaseNotes.rst
204–210

consider making it shorter (fit in 4/5 lines)...

maybe:

Updated the Hungarian prefixes for enums in C files to match those used in C++ 
files for improved readability, as checked by :doc:`readability-identifier-naming
<clang-tidy/checks/readability/identifier-naming>`. To preserve the previous
behavior of using `i` as the prefix for enum tags, set the EnumConstantPrefix 
option to `i` instead of using EnumConstantHungarianPrefix.
clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c
4

those typedefs looks to be duplicated already in identifier-naming-hungarian-notation.cpp and identifier-naming-hungarian-notation-cfgfile.cpp, move them to separate header file

amurzeau updated this revision to Diff 501247.Feb 28 2023, 11:42 AM

Fix clang-format error, release notes and factorize typedefs in tests.

amurzeau marked an inline comment as done.Feb 28 2023, 1:41 PM

I see the build is still marked as failed in phabricator, but succeed in buildkite, do I need to do something, like a rebuild ?

amurzeau marked an inline comment as done.Feb 28 2023, 2:45 PM

Hi, thanks PiotrZSL, the build is now passed and is green.

Looks good but I fail to understand what exactly the patch fixes, can you point me to an example in the tests? It would be easier to review if the NFC changes had been done in a separate patch.

clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c
4

Next time please apply this change in a separate NFC patch, for easier review.

Looks good but I fail to understand what exactly the patch fixes, can you point me to an example in the tests?

The patch fixes these enum tests in C language mode:

enum REV_TYPE { RevValid };
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: invalid case style for enum constant 'RevValid' [readability-identifier-naming]
// CHECK-FIXES: {{^}}enum REV_TYPE { rtRevValid };

enum EnumConstantCase { OneByte, TwoByte };
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: invalid case style for enum constant 'OneByte' [readability-identifier-naming]
// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: invalid case style for enum constant 'TwoByte' [readability-identifier-naming]
// CHECK-FIXES: {{^}}enum EnumConstantCase { eccOneByte, eccTwoByte };

Without this patch, the suggested fix in a C file by clang-tidy is iRevValid instead of rtRevValid.
This patch makes the behavior on C files the same as C++ files.

It would be easier to review if the NFC changes had been done in a separate patch.

Ok sorry, will do that next time.
Would the new C test file would have been better in a separate patch too (thus keep only the main change in the checker) ?

carlosgalvezp accepted this revision.Mar 18 2023, 10:38 AM

Without this patch, the suggested fix in a C file by clang-tidy is iRevValid instead of rtRevValid.

Ah there it was, thanks for the clarification! It was not highlighted in the diff.

Would the new C test file would have been better in a separate patch too (thus keep only the main change in the checker) ?

That would definitely have helped reviewing! Coming into the patch I was expecting the diff to look like either fixing an existing test, or adding a new test (in the same file). You did add a new test, but it happened to be an almost exact copy of another one, so Phabricator only displayed the diff against the copy, not highlighting the issue the patch is fixing.

Looks great, approved! Thanks for fixing :)

This revision is now accepted and ready to land.Mar 18 2023, 10:38 AM

Looks great, approved! Thanks for fixing :)

Thanks for the review :)

I don't have push access, so please can you (or anyone else) land it for me ?
(as Alexis Murzeau <amubtdx@gmail.com> (phabricator mail and handle is the same as github))