Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[clang-tidy] modernize-use-digit-separator issue #35414 #76153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 78 commits into
base: main
Choose a base branch
Loading
from

Conversation

IvanArkhipov1999
Copy link

Closes #35414

The check that looks for long integral constants and inserts the digits separator (') appropriately. Groupings:

  • decimal integral constants, groups of 3 digits, e.g. int x = 1'000;
  • binary integral constants, groups of 4 digits, e.g. int x = 0b0010'0011;
  • octal integral constants, groups of 3 digits, e.g. int x = 0377'777;
  • hexadecimal integral constants, groups of 4 digits, e.g. unsigned long x = 0xffff'0000;
  • floating-point constants, group into 3 digits on either side of the decimal point, e.g. float x = 3'456.001'25f;

@IvanArkhipov1999 IvanArkhipov1999 changed the title [clang-tidy] modernize-use-digit-separator issue #35414 [clang-tidy] modernize-use-digit-separator Dec 21, 2023
@IvanArkhipov1999 IvanArkhipov1999 changed the title [clang-tidy] modernize-use-digit-separator [clang-tidy] modernize-use-digit-separator issue #35414 Dec 21, 2023
Copy link

github-actions bot commented Dec 21, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@IvanArkhipov1999
Copy link
Author

I think I should tag @PiotrZSL.

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some quick glace, some work still needed, my main concern is performance of this check.

std::string
getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString);

std::vector<std::basic_string<char>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid std::vector, use things like llvm::SmallVector, it will work fine with return value optimization here.


using namespace clang::ast_matchers;

namespace {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free standing functions should be "static" instead of in anonymous namespace, per codding guidelines

std::vector<std::basic_string<char>>
splitStringByGroupSize(const std::basic_string<char> &String,
size_t GroupSize) {
std::vector<std::basic_string<char>> Result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not use std::basic_string, use std::string.

Comment on lines 33 to 42
for (size_t I = 0; I < ReversedString.size(); I += GroupSize) {
Result.push_back(ReversedString.substr(I, GroupSize));
}

std::reverse(Result.begin(), Result.end());
std::for_each(Result.begin(), Result.end(), [](std::basic_string<char> &Str) {
return std::reverse(Str.begin(), Str.end());
});

return Result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not like this algorithm, is not efficient. work something out without multiple copies,.

Radix = 2;
GroupSize = 4;
Prefix = "0b";
} else if (OriginalLiteralString.starts_with("0x")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if it starts with 0X ? or ends with 0B

///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-digit-separator.html
class UseDigitSeparatorCheck : public ClangTidyCheck {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restrict this check to C++14 and later.

- hexadecimal integral constants, groups of 4 digits, e.g. unsigned long
x = 0xffff'0000;
- floating-point constants, group into 3 digits on either side of the
decimal point, e.g. float x = 3'456.001'25f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add examples in a form of source code.

float PostfixScientificShortFloat1 = 1.23e10f;
float PostfixScientificShortFloat2 = 1.23E+10f;
float PostfixScientificShortFloat3 = 1.23e-10F;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add tests where number is behind macro, and where number is an macro argument.

float PostfixScientificShortFloat2 = 1.23E+10f;
float PostfixScientificShortFloat3 = 1.23e-10F;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests where CHECK-FIXES verify entire source line, to make sure that ; or = is not removed.

@@ -0,0 +1,239 @@
// RUN: %check_clang_tidy %s modernize-use-digit-separator %t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restrict tests to std standards that support this notation


int NotFormattedInteger = 1234567;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234567' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234'567

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case like this:

int OldStyleFormattedInteger = 12*1000*1000;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: legacy formatted representation of integer literal '12*1000*1000' [modernize-use-digit-separator]
// CHECK-FIXES: 12'000'000

See #35414 (comment) for more details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this suggestion; see my comments on the original issue. A check should have a single responsibility, not two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

modernize-use-digit-separator
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.