Page MenuHomePhabricator

[clang-tidy] Add portability-non-portable-integer-constant check
Needs RevisionPublic

Authored by Discookie on Mar 23 2023, 5:16 AM.

Details

Summary

This check finds integer literals that are being used in a non-portable manner.

Currently, the check detects cases where maximum or minimum values should be used
instead, as well as error-prone integer literals having leading zeroes, or
relying on most significant bits.

Diff Detail

Event Timeline

Discookie created this revision.Mar 23 2023, 5:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2023, 5:16 AM
Discookie requested review of this revision.Mar 23 2023, 5:16 AM

Please add alias entry in Release Notes.

clang-tools-extra/clang-tidy/portability/NonPortableIntegerConstantCheck.cpp
95

Excessive newline.

clang-tools-extra/docs/ReleaseNotes.rst
136

Incorrect entry.

clang-tools-extra/docs/clang-tidy/checks/portability/non-portable-integer-constant.rst
4

Please make is same length as title.

PiotrZSL added inline comments.Mar 23 2023, 8:21 AM
clang-tools-extra/clang-tidy/portability/NonPortableIntegerConstantCheck.cpp
92

consider excluding system headers, or even exclude things like "equals(0)" to speed up check.

103

use type instead of auto

146

add here some hint to developer what to do, in cpp suggest using std::numeric_limits, in C some other MAX macro.
Same for other.

clang-tools-extra/clang-tidy/portability/NonPortableIntegerConstantCheck.h
20

this check is already in portability group, no need to duplicate "portable".
Maybe just portability-integer-constant

clang-tools-extra/docs/clang-tidy/checks/portability/non-portable-integer-constant.rst
22

well, not, right way would be using std::numeric_limits<std::uint32_t>::max().
And unsigned long can be 4 bytes or 8 bytes or anything compiler decide.
So this example got more issues., and 0XFFF.. is a last of them, and -1 is not valid in such case.

clang-tools-extra/test/clang-tidy/checkers/portability/non-portable-integer-constant.cpp
1

no need to limit this check to c++17 or later.
when it's targeting all versions of C and C++

229

exclude system macros, otherwise it may warn for things like ULONG_MAX

PiotrZSL requested changes to this revision.Mar 23 2023, 3:00 PM

Additionally, fix build.
Once you finish making changes to this review, switch it back to ready for review.

This revision now requires changes to proceed.Mar 23 2023, 3:00 PM
LegalizeAdulthood resigned from this revision.Mar 28 2023, 1:23 PM