[clang-tools-extra] [llvm] [clang] [clang-tidy] modernize-use-digit-separator issue #35414 (PR #76153)
Arkhipov Ivan via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 26 01:59:42 PST 2023
https://github.com/IvanArkhipov1999 updated https://github.com/llvm/llvm-project/pull/76153
>From 3b83f8608791ea0c270424af855acd365d9a7fdd Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sat, 2 Dec 2023 18:00:48 +0300
Subject: [PATCH 01/58] Add empty files for use-digit-separator-check
---
.../clang-tidy/modernize/CMakeLists.txt | 1 +
.../modernize/ModernizeTidyModule.cpp | 3 ++
.../modernize/UseDigitSeparatorCheck.cpp | 33 +++++++++++++++++++
.../modernize/UseDigitSeparatorCheck.h | 30 +++++++++++++++++
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++
.../docs/clang-tidy/checks/list.rst | 1 +
6 files changed, 73 insertions(+)
create mode 100644 clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
create mode 100644 clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index 717c400c479033..872c37147104c8 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -31,6 +31,7 @@ add_clang_library(clangTidyModernizeModule
UseBoolLiteralsCheck.cpp
UseConstraintsCheck.cpp
UseDefaultMemberInitCheck.cpp
+ UseDigitSeparatorCheck.cpp
UseEmplaceCheck.cpp
UseEqualsDefaultCheck.cpp
UseEqualsDeleteCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index 73751cf2705068..e799eafbfc866b 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -32,6 +32,7 @@
#include "UseBoolLiteralsCheck.h"
#include "UseConstraintsCheck.h"
#include "UseDefaultMemberInitCheck.h"
+#include "UseDigitSeparatorCheck.h"
#include "UseEmplaceCheck.h"
#include "UseEqualsDefaultCheck.h"
#include "UseEqualsDeleteCheck.h"
@@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule {
CheckFactories.registerCheck<MakeSharedCheck>("modernize-make-shared");
CheckFactories.registerCheck<MakeUniqueCheck>("modernize-make-unique");
CheckFactories.registerCheck<PassByValueCheck>("modernize-pass-by-value");
+ CheckFactories.registerCheck<UseDigitSeparatorCheck>(
+ "modernize-use-digit-separator");
CheckFactories.registerCheck<UseStdPrintCheck>("modernize-use-std-print");
CheckFactories.registerCheck<RawStringLiteralCheck>(
"modernize-raw-string-literal");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
new file mode 100644
index 00000000000000..3d9ddb14e49245
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -0,0 +1,33 @@
+//===--- UseDigitSeparatorCheck.cpp - clang-tidy --------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseDigitSeparatorCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
+ // FIXME: Add matchers.
+ Finder->addMatcher(functionDecl().bind("x"), this);
+}
+
+void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
+ // FIXME: Add callback implementation.
+ const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
+ if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
+ return;
+ diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
+ << MatchedDecl
+ << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
+ diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note);
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
new file mode 100644
index 00000000000000..8bbd2b3947c931
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
@@ -0,0 +1,30 @@
+//===--- UseDigitSeparatorCheck.h - clang-tidy ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USEDIGITSEPARATORCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USEDIGITSEPARATORCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::modernize {
+
+/// FIXME: Write a short description.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-digit-separator.html
+class UseDigitSeparatorCheck : public ClangTidyCheck {
+public:
+ UseDigitSeparatorCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace clang::tidy::modernize
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USEDIGITSEPARATORCHECK_H
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6d5f49dc062545..ae9d69e1434df9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -186,6 +186,11 @@ New checks
Replace ``enable_if`` with C++20 requires clauses.
+- New :doc:`modernize-use-digit-separator
+ <clang-tidy/checks/modernize/use-digit-separator>` check.
+
+ FIXME: add release notes.
+
- New :doc:`performance-enum-size
<clang-tidy/checks/performance/enum-size>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index 6f987ba1672e3f..f4fe9726e9b2b0 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -285,6 +285,7 @@ Clang-Tidy Checks
:doc:`modernize-use-bool-literals <modernize/use-bool-literals>`, "Yes"
:doc:`modernize-use-constraints <modernize/use-constraints>`, "Yes"
:doc:`modernize-use-default-member-init <modernize/use-default-member-init>`, "Yes"
+ :doc:`modernize-use-digit-separator <modernize/use-digit-separator>`, "Yes"
:doc:`modernize-use-emplace <modernize/use-emplace>`, "Yes"
:doc:`modernize-use-equals-default <modernize/use-equals-default>`, "Yes"
:doc:`modernize-use-equals-delete <modernize/use-equals-delete>`, "Yes"
>From 0560b3c2518b797849c15d7c9c502c46c009d2a3 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sat, 2 Dec 2023 19:21:34 +0300
Subject: [PATCH 02/58] Finding integer value
---
.../modernize/UseDigitSeparatorCheck.cpp | 22 ++++++++++++-------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 3d9ddb14e49245..7a5b40840eab9d 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -16,18 +16,24 @@ namespace clang::tidy::modernize {
void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
// FIXME: Add matchers.
- Finder->addMatcher(functionDecl().bind("x"), this);
+// Finder->addMatcher(functionDecl().bind("x"), this);
+ Finder->addMatcher(integerLiteral().bind("integerLiteral"), this);
}
void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// FIXME: Add callback implementation.
- const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
- if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
- return;
- diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
- << MatchedDecl
- << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
- diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note);
+// const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
+// if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
+// return;
+// diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
+// << MatchedDecl
+// << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
+// diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note);
+ const auto *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
+ const auto IntegerValue = MatchedInteger->getValue();
+ diag(MatchedInteger->getLocation(), "integer warning %0") << toString(IntegerValue, 10, true)
+ << FixItHint::CreateInsertion(MatchedInteger->getLocation(), "this is integer");
+ diag(MatchedInteger->getLocation(), "integer", DiagnosticIDs::Note);
}
} // namespace clang::tidy::modernize
>From 7e648539bf0514f897760de5db4a2e4439e7c73e Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 3 Dec 2023 18:06:17 +0300
Subject: [PATCH 03/58] Splitting integer string by 3 digits
---
.../modernize/UseDigitSeparatorCheck.cpp | 32 ++++++++++++-------
1 file changed, 21 insertions(+), 11 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 7a5b40840eab9d..2cc6aab4b96615 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -6,32 +6,42 @@
//
//===----------------------------------------------------------------------===//
+#include <algorithm>
+#include <numeric>
+
#include "UseDigitSeparatorCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
+namespace {
+ std::vector<std::basic_string<char>> splitString3Symbols(const std::basic_string<char> &String) {
+ std::vector<std::basic_string<char>> Result;
+ std::basic_string<char> ReversedString(String.rbegin(), String.rend());
+
+ for (size_t I = 0; I < ReversedString.size(); I += 3) {
+ Result.push_back(ReversedString.substr(I, 3));
+ }
+
+ std::reverse(Result.begin(), Result.end());
+
+ return Result;
+ }
+} // namespace
+
namespace clang::tidy::modernize {
void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
- // FIXME: Add matchers.
-// Finder->addMatcher(functionDecl().bind("x"), this);
Finder->addMatcher(integerLiteral().bind("integerLiteral"), this);
}
void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
- // FIXME: Add callback implementation.
-// const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
-// if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
-// return;
-// diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
-// << MatchedDecl
-// << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
-// diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note);
const auto *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
const auto IntegerValue = MatchedInteger->getValue();
- diag(MatchedInteger->getLocation(), "integer warning %0") << toString(IntegerValue, 10, true)
+ const auto IntegerString = splitString3Symbols(toString(IntegerValue, 10, true));
+ const auto FinalString = std::accumulate(IntegerString.begin(), IntegerString.end(), std::string(""), [](std::basic_string<char> S1, std::basic_string<char> S2) {return S1 + "\'" + S2;}).erase(0, 1);
+ diag(MatchedInteger->getLocation(), "integer warning %0") << FinalString
<< FixItHint::CreateInsertion(MatchedInteger->getLocation(), "this is integer");
diag(MatchedInteger->getLocation(), "integer", DiagnosticIDs::Note);
}
>From 43f66ac8da7a67beb2d31ec7bfc23d10391c3c77 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 3 Dec 2023 18:07:16 +0300
Subject: [PATCH 04/58] Formatting
---
.../modernize/UseDigitSeparatorCheck.cpp | 35 ++++++++++++-------
1 file changed, 22 insertions(+), 13 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 2cc6aab4b96615..2e18f289e1a37b 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -16,18 +16,19 @@
using namespace clang::ast_matchers;
namespace {
- std::vector<std::basic_string<char>> splitString3Symbols(const std::basic_string<char> &String) {
- std::vector<std::basic_string<char>> Result;
- std::basic_string<char> ReversedString(String.rbegin(), String.rend());
+std::vector<std::basic_string<char>>
+splitString3Symbols(const std::basic_string<char> &String) {
+ std::vector<std::basic_string<char>> Result;
+ std::basic_string<char> ReversedString(String.rbegin(), String.rend());
- for (size_t I = 0; I < ReversedString.size(); I += 3) {
- Result.push_back(ReversedString.substr(I, 3));
- }
+ for (size_t I = 0; I < ReversedString.size(); I += 3) {
+ Result.push_back(ReversedString.substr(I, 3));
+ }
- std::reverse(Result.begin(), Result.end());
+ std::reverse(Result.begin(), Result.end());
- return Result;
- }
+ return Result;
+}
} // namespace
namespace clang::tidy::modernize {
@@ -39,10 +40,18 @@ void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
const auto IntegerValue = MatchedInteger->getValue();
- const auto IntegerString = splitString3Symbols(toString(IntegerValue, 10, true));
- const auto FinalString = std::accumulate(IntegerString.begin(), IntegerString.end(), std::string(""), [](std::basic_string<char> S1, std::basic_string<char> S2) {return S1 + "\'" + S2;}).erase(0, 1);
- diag(MatchedInteger->getLocation(), "integer warning %0") << FinalString
- << FixItHint::CreateInsertion(MatchedInteger->getLocation(), "this is integer");
+ const auto IntegerString =
+ splitString3Symbols(toString(IntegerValue, 10, true));
+ const auto FinalString =
+ std::accumulate(IntegerString.begin(), IntegerString.end(),
+ std::string(""),
+ [](std::basic_string<char> S1,
+ std::basic_string<char> S2) { return S1 + "\'" + S2; })
+ .erase(0, 1);
+ diag(MatchedInteger->getLocation(), "integer warning %0")
+ << FinalString
+ << FixItHint::CreateInsertion(MatchedInteger->getLocation(),
+ "this is integer");
diag(MatchedInteger->getLocation(), "integer", DiagnosticIDs::Note);
}
>From 3a3667939df9599cb2c193709b215f701f185b21 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 4 Dec 2023 13:25:52 +0300
Subject: [PATCH 05/58] Checking formatting of integer literal
---
.../modernize/UseDigitSeparatorCheck.cpp | 33 +++++++++++++------
1 file changed, 23 insertions(+), 10 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 2e18f289e1a37b..bbc1008553904d 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -12,6 +12,7 @@
#include "UseDigitSeparatorCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
@@ -38,21 +39,33 @@ void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
}
void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
- const auto *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
- const auto IntegerValue = MatchedInteger->getValue();
- const auto IntegerString =
+ const ASTContext &Context = *Result.Context;
+ const SourceManager &Source = Context.getSourceManager();
+ const IntegerLiteral *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
+
+ // Get original literal source text
+ const StringRef OriginalLiteralString = Lexer::getSourceText(CharSourceRange::getTokenRange(MatchedInteger->getSourceRange()), Source, Context.getLangOpts());
+
+ // Get formatting literal text
+ const llvm::APInt IntegerValue = MatchedInteger->getValue();
+ const std::vector<std::string> SplittedIntegerLiteral =
splitString3Symbols(toString(IntegerValue, 10, true));
- const auto FinalString =
- std::accumulate(IntegerString.begin(), IntegerString.end(),
+ const std::string FormatedLiteralString =
+ std::accumulate(SplittedIntegerLiteral.begin(),
+ SplittedIntegerLiteral.end(),
std::string(""),
[](std::basic_string<char> S1,
std::basic_string<char> S2) { return S1 + "\'" + S2; })
.erase(0, 1);
- diag(MatchedInteger->getLocation(), "integer warning %0")
- << FinalString
- << FixItHint::CreateInsertion(MatchedInteger->getLocation(),
- "this is integer");
- diag(MatchedInteger->getLocation(), "integer", DiagnosticIDs::Note);
+
+ // Compare the original and formatted representation of a literal
+ if (OriginalLiteralString != FormatedLiteralString) {
+ diag(MatchedInteger->getLocation(), "integer warning %0 %1")
+ << FormatedLiteralString << OriginalLiteralString
+ << FixItHint::CreateInsertion(MatchedInteger->getLocation(),
+ "this is integer");
+ diag(MatchedInteger->getLocation(), "integer", DiagnosticIDs::Note);
+ }
}
} // namespace clang::tidy::modernize
>From 524aa8b7bfb806d0819dfc6a4e23a20d458313d2 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 4 Dec 2023 13:46:56 +0300
Subject: [PATCH 06/58] Messages of checking formatting integer literals
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index bbc1008553904d..f8251a23b1326f 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -60,11 +60,10 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
- diag(MatchedInteger->getLocation(), "integer warning %0 %1")
- << FormatedLiteralString << OriginalLiteralString
+ diag(MatchedInteger->getLocation(), "unformatted representation of integer literal '%0'")
+ << OriginalLiteralString
<< FixItHint::CreateInsertion(MatchedInteger->getLocation(),
- "this is integer");
- diag(MatchedInteger->getLocation(), "integer", DiagnosticIDs::Note);
+ FormatedLiteralString);
}
}
>From 78b1bf18d4a3f060fd7254197c3e97de5fcb02f3 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 4 Dec 2023 15:28:19 +0300
Subject: [PATCH 07/58] Support for various number systems
---
.../modernize/UseDigitSeparatorCheck.cpp | 28 +++++++++++++++----
1 file changed, 23 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index f8251a23b1326f..b02179b29290f1 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -18,15 +18,16 @@ using namespace clang::ast_matchers;
namespace {
std::vector<std::basic_string<char>>
-splitString3Symbols(const std::basic_string<char> &String) {
+splitStringByGroupSize(const std::basic_string<char> &String, size_t GroupSize) {
std::vector<std::basic_string<char>> Result;
std::basic_string<char> ReversedString(String.rbegin(), String.rend());
- for (size_t I = 0; I < ReversedString.size(); I += 3) {
- Result.push_back(ReversedString.substr(I, 3));
+ 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;
}
@@ -46,11 +47,28 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Get original literal source text
const StringRef OriginalLiteralString = Lexer::getSourceText(CharSourceRange::getTokenRange(MatchedInteger->getSourceRange()), Source, Context.getLangOpts());
+ // Configure formatting
+ unsigned int Radix;
+ size_t GroupSize;
+ std::string Prefix;
+ if (OriginalLiteralString.starts_with("0b")) {
+ Radix = 2;
+ GroupSize = 4;
+ Prefix = "0b";
+ } else if (OriginalLiteralString.starts_with("0x")) {
+ Radix = 16;
+ GroupSize = 4;
+ Prefix = "0x";
+ } else {
+ Radix = 10;
+ GroupSize = 3;
+ }
+
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
const std::vector<std::string> SplittedIntegerLiteral =
- splitString3Symbols(toString(IntegerValue, 10, true));
- const std::string FormatedLiteralString =
+ splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+ const std::string FormatedLiteralString = Prefix +
std::accumulate(SplittedIntegerLiteral.begin(),
SplittedIntegerLiteral.end(),
std::string(""),
>From 9d4430d1e6690a67a4177f84825b58267e0db4bb Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 4 Dec 2023 15:51:03 +0300
Subject: [PATCH 08/58] Support for octal number system
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index b02179b29290f1..eafc7349837dc8 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -59,6 +59,10 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
Radix = 16;
GroupSize = 4;
Prefix = "0x";
+ } else if (OriginalLiteralString.starts_with("0") && OriginalLiteralString != "0") {
+ Radix = 8;
+ GroupSize = 3;
+ Prefix = "0";
} else {
Radix = 10;
GroupSize = 3;
>From d80dbbe6bf5ed50115e43bb1441b030893bb39f4 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 4 Dec 2023 15:51:53 +0300
Subject: [PATCH 09/58] Formatting
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index eafc7349837dc8..82c0959c8b30ff 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -59,13 +59,14 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
Radix = 16;
GroupSize = 4;
Prefix = "0x";
- } else if (OriginalLiteralString.starts_with("0") && OriginalLiteralString != "0") {
+ } else if (OriginalLiteralString.starts_with("0") &&
+ OriginalLiteralString != "0") {
Radix = 8;
GroupSize = 3;
Prefix = "0";
} else {
- Radix = 10;
- GroupSize = 3;
+ Radix = 10;
+ GroupSize = 3;
}
// Get formatting literal text
>From 4bd9517ad87eff53adde22651efe7ef878c1a939 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 5 Dec 2023 17:10:17 +0300
Subject: [PATCH 10/58] Support integer literals with postfix
---
.../modernize/UseDigitSeparatorCheck.cpp | 23 ++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 82c0959c8b30ff..98027a725f7b63 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -8,6 +8,7 @@
#include <algorithm>
#include <numeric>
+//#include <regex>
#include "UseDigitSeparatorCheck.h"
#include "clang/AST/ASTContext.h"
@@ -51,6 +52,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
unsigned int Radix;
size_t GroupSize;
std::string Prefix;
+ std::string Postfix;
if (OriginalLiteralString.starts_with("0b")) {
Radix = 2;
GroupSize = 4;
@@ -69,17 +71,32 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
GroupSize = 3;
}
+ if (OriginalLiteralString.ends_with("L") || OriginalLiteralString.ends_with("l") || OriginalLiteralString.ends_with("U") || OriginalLiteralString.ends_with("u")) {
+ Postfix = OriginalLiteralString.back();
+ }
+
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
- const std::vector<std::string> SplittedIntegerLiteral =
- splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+ std::vector<std::string> SplittedIntegerLiteral = splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+// if (OriginalLiteralString.contains('.')) {
+// std::vector<std::string> SplitedString;
+// const std::regex DotRegex(".");
+// std::sregex_token_iterator
+// Begin(OriginalLiteralString.str().begin(), OriginalLiteralString.str().end(), DotRegex, -1),
+// End;
+//
+// std::copy(Begin, End, std::back_inserter(SplitedString));
+//// const auto SplitedString = std::views::split(OriginalLiteralString, '.');
+// } else {
+// SplittedIntegerLiteral = splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+// }
const std::string FormatedLiteralString = Prefix +
std::accumulate(SplittedIntegerLiteral.begin(),
SplittedIntegerLiteral.end(),
std::string(""),
[](std::basic_string<char> S1,
std::basic_string<char> S2) { return S1 + "\'" + S2; })
- .erase(0, 1);
+ .erase(0, 1) + Postfix;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From c97e527ce9e7306346f4947a09a9d37ae79a4e3e Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 5 Dec 2023 18:08:36 +0300
Subject: [PATCH 11/58] Begin of work with float literals
---
.clang-tidy | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.clang-tidy b/.clang-tidy
index 4e1cb114f43b2c..44eff925939802 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -1,4 +1,4 @@
-Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-const-correctness,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-misc-no-recursion,-misc-use-anonymous-namespace,readability-identifier-naming'
+Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-const-correctness,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-misc-no-recursion,-misc-use-anonymous-namespace,modernize-use-digit-separator,readability-identifier-naming'
CheckOptions:
- key: readability-identifier-naming.ClassCase
value: CamelCase
>From 544d9f89a47b039328ad48cc6ce6f8dcf64be12a Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 5 Dec 2023 18:08:43 +0300
Subject: [PATCH 12/58] Begin of work with float literals
---
.../modernize/UseDigitSeparatorCheck.cpp | 150 +++++++++++-------
1 file changed, 91 insertions(+), 59 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 98027a725f7b63..776c152095c5c9 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -38,72 +38,104 @@ namespace clang::tidy::modernize {
void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(integerLiteral().bind("integerLiteral"), this);
+ Finder->addMatcher(floatLiteral().bind("floatLiteral"), this);
}
void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
const ASTContext &Context = *Result.Context;
const SourceManager &Source = Context.getSourceManager();
const IntegerLiteral *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
-
- // Get original literal source text
- const StringRef OriginalLiteralString = Lexer::getSourceText(CharSourceRange::getTokenRange(MatchedInteger->getSourceRange()), Source, Context.getLangOpts());
-
- // Configure formatting
- unsigned int Radix;
- size_t GroupSize;
- std::string Prefix;
- std::string Postfix;
- if (OriginalLiteralString.starts_with("0b")) {
- Radix = 2;
- GroupSize = 4;
- Prefix = "0b";
- } else if (OriginalLiteralString.starts_with("0x")) {
- Radix = 16;
- GroupSize = 4;
- Prefix = "0x";
- } else if (OriginalLiteralString.starts_with("0") &&
- OriginalLiteralString != "0") {
- Radix = 8;
- GroupSize = 3;
- Prefix = "0";
+ const FloatingLiteral *MatchedFloat = Result.Nodes.getNodeAs<FloatingLiteral>("floatLiteral");
+
+ if (MatchedInteger != nullptr) {
+ // Get original literal source text
+ const StringRef OriginalLiteralString = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(MatchedInteger->getSourceRange()),
+ Source, Context.getLangOpts());
+
+ // Configure formatting
+ unsigned int Radix;
+ size_t GroupSize;
+ std::string Prefix;
+ std::string Postfix;
+ if (OriginalLiteralString.starts_with("0b")) {
+ Radix = 2;
+ GroupSize = 4;
+ Prefix = "0b";
+ } else if (OriginalLiteralString.starts_with("0x")) {
+ Radix = 16;
+ GroupSize = 4;
+ Prefix = "0x";
+ } else if (OriginalLiteralString.starts_with("0") &&
+ OriginalLiteralString != "0") {
+ Radix = 8;
+ GroupSize = 3;
+ Prefix = "0";
+ } else {
+ Radix = 10;
+ GroupSize = 3;
+ }
+
+ if (OriginalLiteralString.ends_with("L") ||
+ OriginalLiteralString.ends_with("l") ||
+ OriginalLiteralString.ends_with("U") ||
+ OriginalLiteralString.ends_with("u")) {
+ Postfix = OriginalLiteralString.back();
+ }
+
+ // Get formatting literal text
+ const llvm::APInt IntegerValue = MatchedInteger->getValue();
+ const std::vector<std::string> SplittedIntegerLiteral =
+ splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+ // if (OriginalLiteralString.contains('.')) {
+ // std::vector<std::string> SplitedString;
+ // const std::regex DotRegex(".");
+ // std::sregex_token_iterator
+ // Begin(OriginalLiteralString.str().begin(), OriginalLiteralString.str().end(), DotRegex, -1), End;
+ //
+ // std::copy(Begin, End, std::back_inserter(SplitedString));
+ //// const auto SplitedString = std::views::split(OriginalLiteralString, '.');
+ // } else {
+ // SplittedIntegerLiteral = splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+ // }
+ const std::string FormatedLiteralString =
+ Prefix +
+ std::accumulate(
+ SplittedIntegerLiteral.begin(), SplittedIntegerLiteral.end(),
+ std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ })
+ .erase(0, 1) +
+ Postfix;
+
+ // Compare the original and formatted representation of a literal
+ if (OriginalLiteralString != FormatedLiteralString) {
+ diag(MatchedInteger->getLocation(),
+ "unformatted representation of integer literal '%0'")
+ << OriginalLiteralString
+ << FixItHint::CreateInsertion(MatchedInteger->getLocation(),
+ FormatedLiteralString);
+ }
+ } else if (MatchedFloat != nullptr) {
+ // Get original literal source text
+ const StringRef OriginalLiteralString = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()),
+ Source, Context.getLangOpts());
+
+ // Get formatting literal text
+ const std::string FormatedLiteralString;
+
+ // Compare the original and formatted representation of a literal
+ if (OriginalLiteralString != FormatedLiteralString) {
+ diag(MatchedFloat->getLocation(),
+ "unformatted representation of integer literal '%0'")
+ << OriginalLiteralString
+ << FixItHint::CreateInsertion(MatchedFloat->getLocation(),
+ FormatedLiteralString);
+ }
} else {
- Radix = 10;
- GroupSize = 3;
- }
-
- if (OriginalLiteralString.ends_with("L") || OriginalLiteralString.ends_with("l") || OriginalLiteralString.ends_with("U") || OriginalLiteralString.ends_with("u")) {
- Postfix = OriginalLiteralString.back();
- }
-
- // Get formatting literal text
- const llvm::APInt IntegerValue = MatchedInteger->getValue();
- std::vector<std::string> SplittedIntegerLiteral = splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
-// if (OriginalLiteralString.contains('.')) {
-// std::vector<std::string> SplitedString;
-// const std::regex DotRegex(".");
-// std::sregex_token_iterator
-// Begin(OriginalLiteralString.str().begin(), OriginalLiteralString.str().end(), DotRegex, -1),
-// End;
-//
-// std::copy(Begin, End, std::back_inserter(SplitedString));
-//// const auto SplitedString = std::views::split(OriginalLiteralString, '.');
-// } else {
-// SplittedIntegerLiteral = splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
-// }
- const std::string FormatedLiteralString = Prefix +
- std::accumulate(SplittedIntegerLiteral.begin(),
- SplittedIntegerLiteral.end(),
- std::string(""),
- [](std::basic_string<char> S1,
- std::basic_string<char> S2) { return S1 + "\'" + S2; })
- .erase(0, 1) + Postfix;
-
- // Compare the original and formatted representation of a literal
- if (OriginalLiteralString != FormatedLiteralString) {
- diag(MatchedInteger->getLocation(), "unformatted representation of integer literal '%0'")
- << OriginalLiteralString
- << FixItHint::CreateInsertion(MatchedInteger->getLocation(),
- FormatedLiteralString);
+ assert(0);
}
}
>From 81151489e84f34a41010eac702e1be2a72a25d25 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 5 Dec 2023 18:09:04 +0300
Subject: [PATCH 13/58] Fix clang tidy
---
.clang-tidy | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.clang-tidy b/.clang-tidy
index 44eff925939802..4e1cb114f43b2c 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -1,4 +1,4 @@
-Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-const-correctness,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-misc-no-recursion,-misc-use-anonymous-namespace,modernize-use-digit-separator,readability-identifier-naming'
+Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-const-correctness,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-misc-no-recursion,-misc-use-anonymous-namespace,readability-identifier-naming'
CheckOptions:
- key: readability-identifier-naming.ClassCase
value: CamelCase
>From c9091cb64ad2778daaba66868a2b66ab035384f9 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 5 Dec 2023 22:32:41 +0300
Subject: [PATCH 14/58] Crazy (dirty) support for float literals
---
.../modernize/UseDigitSeparatorCheck.cpp | 42 ++++++++++++-------
1 file changed, 27 insertions(+), 15 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 776c152095c5c9..849ddbd32d23eb 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -8,7 +8,7 @@
#include <algorithm>
#include <numeric>
-//#include <regex>
+#include <regex>
#include "UseDigitSeparatorCheck.h"
#include "clang/AST/ASTContext.h"
@@ -87,17 +87,6 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
const llvm::APInt IntegerValue = MatchedInteger->getValue();
const std::vector<std::string> SplittedIntegerLiteral =
splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
- // if (OriginalLiteralString.contains('.')) {
- // std::vector<std::string> SplitedString;
- // const std::regex DotRegex(".");
- // std::sregex_token_iterator
- // Begin(OriginalLiteralString.str().begin(), OriginalLiteralString.str().end(), DotRegex, -1), End;
- //
- // std::copy(Begin, End, std::back_inserter(SplitedString));
- //// const auto SplitedString = std::views::split(OriginalLiteralString, '.');
- // } else {
- // SplittedIntegerLiteral = splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
- // }
const std::string FormatedLiteralString =
Prefix +
std::accumulate(
@@ -119,12 +108,35 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
}
} else if (MatchedFloat != nullptr) {
// Get original literal source text
- const StringRef OriginalLiteralString = Lexer::getSourceText(
+ const std::string OriginalLiteralString = Lexer::getSourceText(
CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()),
- Source, Context.getLangOpts());
+ Source, Context.getLangOpts()).str();
// Get formatting literal text
- const std::string FormatedLiteralString;
+ std::string::size_type DotPosition = OriginalLiteralString.find('.');
+ std::string FirstSubString = OriginalLiteralString.substr(0, DotPosition);
+ std::string SecondSubString = OriginalLiteralString.substr(DotPosition + 1, OriginalLiteralString.size());
+ std::reverse(SecondSubString.begin(), SecondSubString.end());
+
+ std::vector<std::string> SplitedString = {FirstSubString, SecondSubString};
+ std::vector<std::string> SplittedFloatLiteral0 = splitStringByGroupSize(SplitedString[0], 3);
+ std::vector<std::string> SplittedFloatLiteral1 = splitStringByGroupSize(SplitedString[1], 3);
+ std::string SecondReversedString = std::accumulate(
+ SplittedFloatLiteral1.begin(), SplittedFloatLiteral1.end(),
+ std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ })
+ .erase(0, 1);
+ std::reverse(SecondReversedString.begin(), SecondReversedString.end());
+ const std::string FormatedLiteralString =
+ std::accumulate(
+ SplittedFloatLiteral0.begin(), SplittedFloatLiteral0.end(),
+ std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ })
+ .erase(0, 1) + '.' + SecondReversedString;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From ceb711a50723dfb38da4f9a516967ae649eba538 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 5 Dec 2023 22:34:52 +0300
Subject: [PATCH 15/58] Formatting
---
.../modernize/UseDigitSeparatorCheck.cpp | 35 +++++++++++--------
1 file changed, 21 insertions(+), 14 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 849ddbd32d23eb..965462fcdd76aa 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -108,26 +108,32 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
}
} else if (MatchedFloat != nullptr) {
// Get original literal source text
- const std::string OriginalLiteralString = Lexer::getSourceText(
- CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()),
- Source, Context.getLangOpts()).str();
+ const std::string OriginalLiteralString =
+ Lexer::getSourceText(
+ CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()),
+ Source, Context.getLangOpts())
+ .str();
// Get formatting literal text
std::string::size_type DotPosition = OriginalLiteralString.find('.');
std::string FirstSubString = OriginalLiteralString.substr(0, DotPosition);
- std::string SecondSubString = OriginalLiteralString.substr(DotPosition + 1, OriginalLiteralString.size());
+ std::string SecondSubString = OriginalLiteralString.substr(
+ DotPosition + 1, OriginalLiteralString.size());
std::reverse(SecondSubString.begin(), SecondSubString.end());
std::vector<std::string> SplitedString = {FirstSubString, SecondSubString};
- std::vector<std::string> SplittedFloatLiteral0 = splitStringByGroupSize(SplitedString[0], 3);
- std::vector<std::string> SplittedFloatLiteral1 = splitStringByGroupSize(SplitedString[1], 3);
- std::string SecondReversedString = std::accumulate(
- SplittedFloatLiteral1.begin(), SplittedFloatLiteral1.end(),
- std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- })
- .erase(0, 1);
+ std::vector<std::string> SplittedFloatLiteral0 =
+ splitStringByGroupSize(SplitedString[0], 3);
+ std::vector<std::string> SplittedFloatLiteral1 =
+ splitStringByGroupSize(SplitedString[1], 3);
+ std::string SecondReversedString =
+ std::accumulate(
+ SplittedFloatLiteral1.begin(), SplittedFloatLiteral1.end(),
+ std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ })
+ .erase(0, 1);
std::reverse(SecondReversedString.begin(), SecondReversedString.end());
const std::string FormatedLiteralString =
std::accumulate(
@@ -136,7 +142,8 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
[](std::basic_string<char> S1, std::basic_string<char> S2) {
return S1 + "\'" + S2;
})
- .erase(0, 1) + '.' + SecondReversedString;
+ .erase(0, 1) +
+ '.' + SecondReversedString;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From 6b933a90e10be7eb5cfb88e3f34b60a029a594fb Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 10 Dec 2023 13:18:13 +0300
Subject: [PATCH 16/58] Getting float value string
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 965462fcdd76aa..80d02373357456 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -115,10 +115,14 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
.str();
// Get formatting literal text
- std::string::size_type DotPosition = OriginalLiteralString.find('.');
- std::string FirstSubString = OriginalLiteralString.substr(0, DotPosition);
- std::string SecondSubString = OriginalLiteralString.substr(
- DotPosition + 1, OriginalLiteralString.size());
+ const llvm::APFloat FloatValue = MatchedFloat->getValue();
+ llvm::SmallString<128> FloatString;
+ FloatValue.toString(FloatString);
+ const std::string SmallString = FloatString.str().str();
+ std::string::size_type DotPosition = SmallString.find('.');
+ std::string FirstSubString = SmallString.substr(0, DotPosition);
+ std::string SecondSubString = SmallString.substr(
+ DotPosition + 1, SmallString.size());
std::reverse(SecondSubString.begin(), SecondSubString.end());
std::vector<std::string> SplitedString = {FirstSubString, SecondSubString};
>From e66e18007c397148762c6bd966308e402c514682 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 10 Dec 2023 13:46:39 +0300
Subject: [PATCH 17/58] Some refactoring
---
.../modernize/UseDigitSeparatorCheck.cpp | 45 +++++++++++--------
1 file changed, 26 insertions(+), 19 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 80d02373357456..3a5288cf5d168a 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -115,39 +115,46 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
.str();
// Get formatting literal text
+
+ // Get string representation of float value
const llvm::APFloat FloatValue = MatchedFloat->getValue();
- llvm::SmallString<128> FloatString;
- FloatValue.toString(FloatString);
- const std::string SmallString = FloatString.str().str();
- std::string::size_type DotPosition = SmallString.find('.');
- std::string FirstSubString = SmallString.substr(0, DotPosition);
- std::string SecondSubString = SmallString.substr(
- DotPosition + 1, SmallString.size());
- std::reverse(SecondSubString.begin(), SecondSubString.end());
-
- std::vector<std::string> SplitedString = {FirstSubString, SecondSubString};
- std::vector<std::string> SplittedFloatLiteral0 =
- splitStringByGroupSize(SplitedString[0], 3);
- std::vector<std::string> SplittedFloatLiteral1 =
- splitStringByGroupSize(SplitedString[1], 3);
- std::string SecondReversedString =
+ llvm::SmallString<128> FloatSmallString;
+ FloatValue.toString(FloatSmallString);
+ const std::string FloatString = FloatSmallString.str().str();
+
+ // Get integer and fractional parts of float number
+ const std::string::size_type DotPosition = FloatString.find('.');
+ const std::string IntegerSubString = FloatString.substr(0, DotPosition);
+ std::string FractionalSubString = FloatString.substr(
+ DotPosition + 1, FloatString.size());
+
+ // Split integer and fractional parts of float number
+ std::reverse(FractionalSubString.begin(), FractionalSubString.end());
+ const std::vector<std::string> PartsOfFloat = {IntegerSubString, FractionalSubString};
+ const std::vector<std::string> SplittedIntegerSubString =
+ splitStringByGroupSize(PartsOfFloat[0], 3);
+ const std::vector<std::string> SplittedFractionalSubString =
+ splitStringByGroupSize(PartsOfFloat[1], 3);
+
+ // Get formatting literal text
+ std::string FormatedFractionalSubString =
std::accumulate(
- SplittedFloatLiteral1.begin(), SplittedFloatLiteral1.end(),
+ SplittedFractionalSubString.begin(), SplittedFractionalSubString.end(),
std::string(""),
[](std::basic_string<char> S1, std::basic_string<char> S2) {
return S1 + "\'" + S2;
})
.erase(0, 1);
- std::reverse(SecondReversedString.begin(), SecondReversedString.end());
+ std::reverse(FormatedFractionalSubString.begin(), FormatedFractionalSubString.end());
const std::string FormatedLiteralString =
std::accumulate(
- SplittedFloatLiteral0.begin(), SplittedFloatLiteral0.end(),
+ SplittedIntegerSubString.begin(), SplittedIntegerSubString.end(),
std::string(""),
[](std::basic_string<char> S1, std::basic_string<char> S2) {
return S1 + "\'" + S2;
})
.erase(0, 1) +
- '.' + SecondReversedString;
+ '.' + FormatedFractionalSubString;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From 0092c076ba996be368cf886e3b08aa6cfbed01c4 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 10 Dec 2023 13:47:12 +0300
Subject: [PATCH 18/58] Formating
---
.../modernize/UseDigitSeparatorCheck.cpp | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 3a5288cf5d168a..d322d9869f0e05 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -125,12 +125,13 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Get integer and fractional parts of float number
const std::string::size_type DotPosition = FloatString.find('.');
const std::string IntegerSubString = FloatString.substr(0, DotPosition);
- std::string FractionalSubString = FloatString.substr(
- DotPosition + 1, FloatString.size());
+ std::string FractionalSubString =
+ FloatString.substr(DotPosition + 1, FloatString.size());
// Split integer and fractional parts of float number
std::reverse(FractionalSubString.begin(), FractionalSubString.end());
- const std::vector<std::string> PartsOfFloat = {IntegerSubString, FractionalSubString};
+ const std::vector<std::string> PartsOfFloat = {IntegerSubString,
+ FractionalSubString};
const std::vector<std::string> SplittedIntegerSubString =
splitStringByGroupSize(PartsOfFloat[0], 3);
const std::vector<std::string> SplittedFractionalSubString =
@@ -139,13 +140,14 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Get formatting literal text
std::string FormatedFractionalSubString =
std::accumulate(
- SplittedFractionalSubString.begin(), SplittedFractionalSubString.end(),
- std::string(""),
+ SplittedFractionalSubString.begin(),
+ SplittedFractionalSubString.end(), std::string(""),
[](std::basic_string<char> S1, std::basic_string<char> S2) {
return S1 + "\'" + S2;
})
.erase(0, 1);
- std::reverse(FormatedFractionalSubString.begin(), FormatedFractionalSubString.end());
+ std::reverse(FormatedFractionalSubString.begin(),
+ FormatedFractionalSubString.end());
const std::string FormatedLiteralString =
std::accumulate(
SplittedIntegerSubString.begin(), SplittedIntegerSubString.end(),
>From e7fddc9092a9d0f4c0dbce51b4249fb36288a2f9 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 10 Dec 2023 14:00:01 +0300
Subject: [PATCH 19/58] Support of format postfixes of floats
---
.../modernize/UseDigitSeparatorCheck.cpp | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index d322d9869f0e05..ff34f3b6d0d5d6 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -49,7 +49,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
if (MatchedInteger != nullptr) {
// Get original literal source text
- const StringRef OriginalLiteralString = Lexer::getSourceText(
+ const llvm::StringRef OriginalLiteralString = Lexer::getSourceText(
CharSourceRange::getTokenRange(MatchedInteger->getSourceRange()),
Source, Context.getLangOpts());
@@ -108,11 +108,19 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
}
} else if (MatchedFloat != nullptr) {
// Get original literal source text
- const std::string OriginalLiteralString =
+ const llvm::StringRef OriginalLiteralString =
Lexer::getSourceText(
CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()),
- Source, Context.getLangOpts())
- .str();
+ Source, Context.getLangOpts());
+
+ // Configure formatting
+ std::string Postfix;
+ if (OriginalLiteralString.ends_with("L") ||
+ OriginalLiteralString.ends_with("l") ||
+ OriginalLiteralString.ends_with("F") ||
+ OriginalLiteralString.ends_with("f")) {
+ Postfix = OriginalLiteralString.back();
+ }
// Get formatting literal text
@@ -156,7 +164,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
return S1 + "\'" + S2;
})
.erase(0, 1) +
- '.' + FormatedFractionalSubString;
+ '.' + FormatedFractionalSubString + Postfix;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From e42ba55937dba7e8692fe0daf877318c50c553fd Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 10 Dec 2023 14:00:29 +0300
Subject: [PATCH 20/58] Formatting
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index ff34f3b6d0d5d6..0ef53538d08b20 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -108,10 +108,9 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
}
} else if (MatchedFloat != nullptr) {
// Get original literal source text
- const llvm::StringRef OriginalLiteralString =
- Lexer::getSourceText(
- CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()),
- Source, Context.getLangOpts());
+ const llvm::StringRef OriginalLiteralString = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()), Source,
+ Context.getLangOpts());
// Configure formatting
std::string Postfix;
>From 28838f1092d462108d471b71e23b2e65886ec063 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Sun, 10 Dec 2023 21:34:42 +0300
Subject: [PATCH 21/58] Empty test
---
.../checkers/modernize/use-digit-separator.cpp | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
new file mode 100644
index 00000000000000..57bbc898706d6d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -0,0 +1,14 @@
+// RUN: %check_clang_tidy %s modernize-use-digit-separator %t
+
+// FIXME: Add something that triggers the check here.
+void f();
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [modernize-use-digit-separator]
+
+// FIXME: Verify the applied fix.
+// * Make the CHECK patterns specific enough and try to make verified lines
+// unique to avoid incorrect matches.
+// * Use {{}} for regular expressions.
+// CHECK-FIXES: {{^}}void awesome_f();{{$}}
+
+// FIXME: Add something that doesn't trigger the check here.
+void awesome_f2();
>From 9a9fa52f6b639e55f9d6fd65601d4a5994b5af9b Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 11 Dec 2023 19:01:14 +0300
Subject: [PATCH 22/58] Simple test
---
.../checkers/modernize/use-digit-separator.cpp | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 57bbc898706d6d..dc320837b9b437 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -1,14 +1,5 @@
// RUN: %check_clang_tidy %s modernize-use-digit-separator %t
-// FIXME: Add something that triggers the check here.
-void f();
-// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [modernize-use-digit-separator]
-
-// FIXME: Verify the applied fix.
-// * Make the CHECK patterns specific enough and try to make verified lines
-// unique to avoid incorrect matches.
-// * Use {{}} for regular expressions.
-// CHECK-FIXES: {{^}}void awesome_f();{{$}}
-
-// FIXME: Add something that doesn't trigger the check here.
-void awesome_f2();
+int NotFormattedInteger = 1234567;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234567' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234'567
>From 9ddfb123a62e0e1c36a972ee74fde12d46cf0114 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 12 Dec 2023 13:16:04 +0300
Subject: [PATCH 23/58] Add some tests
---
.../modernize/use-digit-separator.cpp | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index dc320837b9b437..d203b836224e80 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -3,3 +3,35 @@
int NotFormattedInteger = 1234567;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234567' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234'567
+
+int BinaryNotFormattedInteger = 0b11101101;
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '0b11101101' [modernize-use-digit-separator]
+// CHECK-FIXES: 0b1110'1101
+
+int OctNotFormattedInteger = 037512;
+// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unformatted representation of integer literal '037512' [modernize-use-digit-separator]
+// CHECK-FIXES: 037'512
+
+int HexNotFormattedInteger = 0x4f356;
+// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unformatted representation of integer literal '0x4f356' [modernize-use-digit-separator]
+// CHECK-FIXES: 0x4'F356
+
+unsigned int UnsignedNotFormattedInteger = 10004U;
+// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
+// CHECK-FIXES: 10'004U
+
+unsigned int UnsignedNotFormattedInteger1 = 100045u;
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
+// CHECK-FIXES: 100'045u
+
+long LongNotFormattedInteger = 123456789101112L;
+// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
+// CHECK-FIXES: 123'456'789'101'112L
+
+long LongNotFormattedInteger1 = 12345678910111213l;
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
+// CHECK-FIXES: 12'345'678'910'111'213l
+
+float NotFormattedFloat = 1234.56789;
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234.567'89
\ No newline at end of file
>From 04e4091d0d027e4dcaa0bd4de9095efb95fe7d21 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 12 Dec 2023 18:10:41 +0300
Subject: [PATCH 24/58] Add tests with minus
---
.../modernize/use-digit-separator.cpp | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index d203b836224e80..c9f918ec9c5a56 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -4,34 +4,70 @@ int NotFormattedInteger = 1234567;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234567' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234'567
+int MinusNotFormattedInteger = -1234567;
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '1234567' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234'567
+
int BinaryNotFormattedInteger = 0b11101101;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '0b11101101' [modernize-use-digit-separator]
// CHECK-FIXES: 0b1110'1101
+int MinusBinaryNotFormattedInteger = -0b11101101;
+// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of integer literal '0b11101101' [modernize-use-digit-separator]
+// CHECK-FIXES: 0b1110'1101
+
int OctNotFormattedInteger = 037512;
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unformatted representation of integer literal '037512' [modernize-use-digit-separator]
// CHECK-FIXES: 037'512
+int MinusOctNotFormattedInteger = -037512;
+// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: unformatted representation of integer literal '037512' [modernize-use-digit-separator]
+// CHECK-FIXES: 037'512
+
int HexNotFormattedInteger = 0x4f356;
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unformatted representation of integer literal '0x4f356' [modernize-use-digit-separator]
// CHECK-FIXES: 0x4'F356
+int MinusHexNotFormattedInteger = -0x4f356;
+// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: unformatted representation of integer literal '0x4f356' [modernize-use-digit-separator]
+// CHECK-FIXES: 0x4'F356
+
unsigned int UnsignedNotFormattedInteger = 10004U;
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
// CHECK-FIXES: 10'004U
+unsigned int MinusUnsignedNotFormattedInteger = -10004U;
+// CHECK-MESSAGES: :[[@LINE-1]]:50: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
+// CHECK-FIXES: 10'004U
+
unsigned int UnsignedNotFormattedInteger1 = 100045u;
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
// CHECK-FIXES: 100'045u
+unsigned int MinusUnsignedNotFormattedInteger1 = -100045u;
+// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
+// CHECK-FIXES: 100'045u
+
long LongNotFormattedInteger = 123456789101112L;
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
// CHECK-FIXES: 123'456'789'101'112L
+long MinusLongNotFormattedInteger = -123456789101112L;
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
+// CHECK-FIXES: 123'456'789'101'112L
+
long LongNotFormattedInteger1 = 12345678910111213l;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
// CHECK-FIXES: 12'345'678'910'111'213l
+long MinusLongNotFormattedInteger1 = -12345678910111213l;
+// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
+// CHECK-FIXES: 12'345'678'910'111'213l
+
float NotFormattedFloat = 1234.56789;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234.567'89
+
+float MinusNotFormattedFloat = -1234.56789;
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.567'89
\ No newline at end of file
>From 9152e1ef7bbd666b926e4a066d90ca9cda0ccda6 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 12 Dec 2023 19:15:01 +0300
Subject: [PATCH 25/58] Delete postfixes
---
.../modernize/UseDigitSeparatorCheck.cpp | 22 ++--------------
.../modernize/use-digit-separator.cpp | 25 ++++++++++++-------
2 files changed, 18 insertions(+), 29 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 0ef53538d08b20..304973efa60972 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -57,7 +57,6 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
unsigned int Radix;
size_t GroupSize;
std::string Prefix;
- std::string Postfix;
if (OriginalLiteralString.starts_with("0b")) {
Radix = 2;
GroupSize = 4;
@@ -76,13 +75,6 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
GroupSize = 3;
}
- if (OriginalLiteralString.ends_with("L") ||
- OriginalLiteralString.ends_with("l") ||
- OriginalLiteralString.ends_with("U") ||
- OriginalLiteralString.ends_with("u")) {
- Postfix = OriginalLiteralString.back();
- }
-
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
const std::vector<std::string> SplittedIntegerLiteral =
@@ -95,8 +87,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
[](std::basic_string<char> S1, std::basic_string<char> S2) {
return S1 + "\'" + S2;
})
- .erase(0, 1) +
- Postfix;
+ .erase(0, 1);
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
@@ -112,15 +103,6 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()), Source,
Context.getLangOpts());
- // Configure formatting
- std::string Postfix;
- if (OriginalLiteralString.ends_with("L") ||
- OriginalLiteralString.ends_with("l") ||
- OriginalLiteralString.ends_with("F") ||
- OriginalLiteralString.ends_with("f")) {
- Postfix = OriginalLiteralString.back();
- }
-
// Get formatting literal text
// Get string representation of float value
@@ -163,7 +145,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
return S1 + "\'" + S2;
})
.erase(0, 1) +
- '.' + FormatedFractionalSubString + Postfix;
+ '.' + FormatedFractionalSubString;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index c9f918ec9c5a56..fe2102786dc15f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -34,35 +34,35 @@ int MinusHexNotFormattedInteger = -0x4f356;
unsigned int UnsignedNotFormattedInteger = 10004U;
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
-// CHECK-FIXES: 10'004U
+// CHECK-FIXES: 10'004
unsigned int MinusUnsignedNotFormattedInteger = -10004U;
// CHECK-MESSAGES: :[[@LINE-1]]:50: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
-// CHECK-FIXES: 10'004U
+// CHECK-FIXES: 10'004
unsigned int UnsignedNotFormattedInteger1 = 100045u;
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
-// CHECK-FIXES: 100'045u
+// CHECK-FIXES: 100'045
unsigned int MinusUnsignedNotFormattedInteger1 = -100045u;
// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
-// CHECK-FIXES: 100'045u
+// CHECK-FIXES: 100'045
long LongNotFormattedInteger = 123456789101112L;
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
-// CHECK-FIXES: 123'456'789'101'112L
+// CHECK-FIXES: 123'456'789'101'112
long MinusLongNotFormattedInteger = -123456789101112L;
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
-// CHECK-FIXES: 123'456'789'101'112L
+// CHECK-FIXES: 123'456'789'101'112
long LongNotFormattedInteger1 = 12345678910111213l;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
-// CHECK-FIXES: 12'345'678'910'111'213l
+// CHECK-FIXES: 12'345'678'910'111'213
long MinusLongNotFormattedInteger1 = -12345678910111213l;
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
-// CHECK-FIXES: 12'345'678'910'111'213l
+// CHECK-FIXES: 12'345'678'910'111'213
float NotFormattedFloat = 1234.56789;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
@@ -70,4 +70,11 @@ float NotFormattedFloat = 1234.56789;
float MinusNotFormattedFloat = -1234.56789;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
-// CHECK-FIXES: 1'234.567'89
\ No newline at end of file
+// CHECK-FIXES: 1'234.567'89
+
+// FIXME:
+// error: expected ';' after top level declarator [clang-diagnostic-error]
+// 80 | int FormattedInteger = 1'234'567;
+// | ^
+// | ;
+//int FormattedInteger = 1'234'567;
\ No newline at end of file
>From e175e702b6fc456cd22c4714e2ad28aa9b1c51e4 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 14 Dec 2023 15:56:52 +0300
Subject: [PATCH 26/58] Return postfixes
---
.../modernize/UseDigitSeparatorCheck.cpp | 21 +++++++++++++++++--
.../modernize/use-digit-separator.cpp | 16 +++++++-------
2 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 304973efa60972..4a91975f934d4e 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -57,6 +57,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
unsigned int Radix;
size_t GroupSize;
std::string Prefix;
+ std::string Postfix;
if (OriginalLiteralString.starts_with("0b")) {
Radix = 2;
GroupSize = 4;
@@ -75,6 +76,13 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
GroupSize = 3;
}
+ if (OriginalLiteralString.ends_with("L") ||
+ OriginalLiteralString.ends_with("l") ||
+ OriginalLiteralString.ends_with("U") ||
+ OriginalLiteralString.ends_with("u")) {
+ Postfix = OriginalLiteralString.back();
+ }
+
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
const std::vector<std::string> SplittedIntegerLiteral =
@@ -87,7 +95,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
[](std::basic_string<char> S1, std::basic_string<char> S2) {
return S1 + "\'" + S2;
})
- .erase(0, 1);
+ .erase(0, 1) + Postfix;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
@@ -103,6 +111,15 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()), Source,
Context.getLangOpts());
+ // Configure formatting
+ std::string Postfix;
+ if (OriginalLiteralString.ends_with("L") ||
+ OriginalLiteralString.ends_with("l") ||
+ OriginalLiteralString.ends_with("F") ||
+ OriginalLiteralString.ends_with("f")) {
+ Postfix = OriginalLiteralString.back();
+ }
+
// Get formatting literal text
// Get string representation of float value
@@ -145,7 +162,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
return S1 + "\'" + S2;
})
.erase(0, 1) +
- '.' + FormatedFractionalSubString;
+ '.' + FormatedFractionalSubString + Postfix;
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index fe2102786dc15f..f572e10a1c6189 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -34,35 +34,35 @@ int MinusHexNotFormattedInteger = -0x4f356;
unsigned int UnsignedNotFormattedInteger = 10004U;
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
-// CHECK-FIXES: 10'004
+// CHECK-FIXES: 10'004U
unsigned int MinusUnsignedNotFormattedInteger = -10004U;
// CHECK-MESSAGES: :[[@LINE-1]]:50: warning: unformatted representation of integer literal '10004U' [modernize-use-digit-separator]
-// CHECK-FIXES: 10'004
+// CHECK-FIXES: 10'004U
unsigned int UnsignedNotFormattedInteger1 = 100045u;
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
-// CHECK-FIXES: 100'045
+// CHECK-FIXES: 100'045u
unsigned int MinusUnsignedNotFormattedInteger1 = -100045u;
// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: unformatted representation of integer literal '100045u' [modernize-use-digit-separator]
-// CHECK-FIXES: 100'045
+// CHECK-FIXES: 100'045u
long LongNotFormattedInteger = 123456789101112L;
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
-// CHECK-FIXES: 123'456'789'101'112
+// CHECK-FIXES: 123'456'789'101'112L
long MinusLongNotFormattedInteger = -123456789101112L;
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '123456789101112L' [modernize-use-digit-separator]
-// CHECK-FIXES: 123'456'789'101'112
+// CHECK-FIXES: 123'456'789'101'112L
long LongNotFormattedInteger1 = 12345678910111213l;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
-// CHECK-FIXES: 12'345'678'910'111'213
+// CHECK-FIXES: 12'345'678'910'111'213l
long MinusLongNotFormattedInteger1 = -12345678910111213l;
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
-// CHECK-FIXES: 12'345'678'910'111'213
+// CHECK-FIXES: 12'345'678'910'111'213l
float NotFormattedFloat = 1234.56789;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
>From c840c98097c99a9df2365469ad70b473d1b5af8b Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 14 Dec 2023 16:31:01 +0300
Subject: [PATCH 27/58] Support long postfixes
---
.../modernize/UseDigitSeparatorCheck.cpp | 18 ++++++++----------
.../checkers/modernize/use-digit-separator.cpp | 8 ++++++++
2 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 4a91975f934d4e..8c01bad65c6927 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -76,11 +76,10 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
GroupSize = 3;
}
- if (OriginalLiteralString.ends_with("L") ||
- OriginalLiteralString.ends_with("l") ||
- OriginalLiteralString.ends_with("U") ||
- OriginalLiteralString.ends_with("u")) {
- Postfix = OriginalLiteralString.back();
+ for (const char& Character : OriginalLiteralString) {
+ if (!std::isdigit(Character)) {
+ Postfix += Character;
+ }
}
// Get formatting literal text
@@ -113,11 +112,10 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Configure formatting
std::string Postfix;
- if (OriginalLiteralString.ends_with("L") ||
- OriginalLiteralString.ends_with("l") ||
- OriginalLiteralString.ends_with("F") ||
- OriginalLiteralString.ends_with("f")) {
- Postfix = OriginalLiteralString.back();
+ for (const char& Character : OriginalLiteralString) {
+ if (!std::isdigit(Character) && Character != '.') {
+ Postfix += Character;
+ }
}
// Get formatting literal text
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index f572e10a1c6189..31aa3f6d5476be 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -64,6 +64,14 @@ long MinusLongNotFormattedInteger1 = -12345678910111213l;
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of integer literal '12345678910111213l' [modernize-use-digit-separator]
// CHECK-FIXES: 12'345'678'910'111'213l
+unsigned long UnsignedLongNotFormattedInteger1 = 12345678910111213Ul;
+// CHECK-MESSAGES: :[[@LINE-1]]:50: warning: unformatted representation of integer literal '12345678910111213Ul' [modernize-use-digit-separator]
+// CHECK-FIXES: 12'345'678'910'111'213Ul
+
+unsigned long MinusUnsignedLongNotFormattedInteger1 = -12345678910111213Ul;
+// CHECK-MESSAGES: :[[@LINE-1]]:56: warning: unformatted representation of integer literal '12345678910111213Ul' [modernize-use-digit-separator]
+// CHECK-FIXES: 12'345'678'910'111'213Ul
+
float NotFormattedFloat = 1234.56789;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.567'89
>From 43f53c54686894b0b0e42a8518f69be3a1da49e5 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 14 Dec 2023 16:33:36 +0300
Subject: [PATCH 28/58] Format
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 8c01bad65c6927..5a0e5958fd2aea 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -76,7 +76,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
GroupSize = 3;
}
- for (const char& Character : OriginalLiteralString) {
+ for (const char &Character : OriginalLiteralString) {
if (!std::isdigit(Character)) {
Postfix += Character;
}
@@ -112,7 +112,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Configure formatting
std::string Postfix;
- for (const char& Character : OriginalLiteralString) {
+ for (const char &Character : OriginalLiteralString) {
if (!std::isdigit(Character) && Character != '.') {
Postfix += Character;
}
>From 977e689a251b957647d6c8c090f85a8e68d81f06 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 14 Dec 2023 16:56:35 +0300
Subject: [PATCH 29/58] Using getFormatedLiteralString
---
.../modernize/UseDigitSeparatorCheck.cpp | 87 ++++++++++---------
1 file changed, 47 insertions(+), 40 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 5a0e5958fd2aea..e15359d58d9700 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -32,6 +32,52 @@ splitStringByGroupSize(const std::basic_string<char> &String, size_t GroupSize)
return Result;
}
+
+std::string getFormatedLiteralString(const llvm::StringRef OriginalLiteralString, const llvm::APInt IntegerValue) {
+ // Configure formatting
+ unsigned int Radix;
+ size_t GroupSize;
+ std::string Prefix;
+ std::string Postfix;
+ if (OriginalLiteralString.starts_with("0b")) {
+ Radix = 2;
+ GroupSize = 4;
+ Prefix = "0b";
+ } else if (OriginalLiteralString.starts_with("0x")) {
+ Radix = 16;
+ GroupSize = 4;
+ Prefix = "0x";
+ } else if (OriginalLiteralString.starts_with("0") &&
+ OriginalLiteralString != "0") {
+ Radix = 8;
+ GroupSize = 3;
+ Prefix = "0";
+ } else {
+ Radix = 10;
+ GroupSize = 3;
+ }
+
+ for (const char &Character : OriginalLiteralString) {
+ if (!std::isdigit(Character)) {
+ Postfix += Character;
+ }
+ }
+
+ // Get formatting literal text
+ const std::vector<std::string> SplittedIntegerLiteral =
+ splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
+ const std::string FormatedLiteralString =
+ Prefix +
+ std::accumulate(
+ SplittedIntegerLiteral.begin(), SplittedIntegerLiteral.end(),
+ std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ })
+ .erase(0, 1) + Postfix;
+
+ return FormatedLiteralString;
+}
} // namespace
namespace clang::tidy::modernize {
@@ -53,48 +99,9 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
CharSourceRange::getTokenRange(MatchedInteger->getSourceRange()),
Source, Context.getLangOpts());
- // Configure formatting
- unsigned int Radix;
- size_t GroupSize;
- std::string Prefix;
- std::string Postfix;
- if (OriginalLiteralString.starts_with("0b")) {
- Radix = 2;
- GroupSize = 4;
- Prefix = "0b";
- } else if (OriginalLiteralString.starts_with("0x")) {
- Radix = 16;
- GroupSize = 4;
- Prefix = "0x";
- } else if (OriginalLiteralString.starts_with("0") &&
- OriginalLiteralString != "0") {
- Radix = 8;
- GroupSize = 3;
- Prefix = "0";
- } else {
- Radix = 10;
- GroupSize = 3;
- }
-
- for (const char &Character : OriginalLiteralString) {
- if (!std::isdigit(Character)) {
- Postfix += Character;
- }
- }
-
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
- const std::vector<std::string> SplittedIntegerLiteral =
- splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
- const std::string FormatedLiteralString =
- Prefix +
- std::accumulate(
- SplittedIntegerLiteral.begin(), SplittedIntegerLiteral.end(),
- std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- })
- .erase(0, 1) + Postfix;
+ const std::string FormatedLiteralString = getFormatedLiteralString(OriginalLiteralString, IntegerValue);
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From 68d0707cccd8b65d45d215a17be34158e035c076 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 14 Dec 2023 17:01:39 +0300
Subject: [PATCH 30/58] Renaming
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index e15359d58d9700..e3cd4e75f8bc69 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -33,7 +33,7 @@ splitStringByGroupSize(const std::basic_string<char> &String, size_t GroupSize)
return Result;
}
-std::string getFormatedLiteralString(const llvm::StringRef OriginalLiteralString, const llvm::APInt IntegerValue) {
+std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString, const llvm::APInt IntegerValue) {
// Configure formatting
unsigned int Radix;
size_t GroupSize;
@@ -101,7 +101,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
- const std::string FormatedLiteralString = getFormatedLiteralString(OriginalLiteralString, IntegerValue);
+ const std::string FormatedLiteralString = getFormatedIntegerString(OriginalLiteralString, IntegerValue);
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From a7064ec660c55ba915042494050ab7d4fc5a8798 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Fri, 15 Dec 2023 12:53:29 +0300
Subject: [PATCH 31/58] Create getFormatedFloatString
---
.../modernize/UseDigitSeparatorCheck.cpp | 106 +++++++++---------
1 file changed, 56 insertions(+), 50 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index e3cd4e75f8bc69..53c41d61e4a0a0 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -58,7 +58,7 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
}
for (const char &Character : OriginalLiteralString) {
- if (!std::isdigit(Character)) {
+ if (!std::isdigit(Character) && Character != '\'') {
Postfix += Character;
}
}
@@ -78,6 +78,60 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
return FormatedLiteralString;
}
+
+std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString, const llvm::APFloat FloatValue) {
+ // Configure formatting
+ std::string Postfix;
+ for (const char &Character : OriginalLiteralString) {
+ if (!std::isdigit(Character) && Character != '.' && Character != '\'') {
+ Postfix += Character;
+ }
+ }
+
+ // Get formatting literal text
+
+ // Get string representation of float value
+ llvm::SmallString<128> FloatSmallString;
+ FloatValue.toString(FloatSmallString);
+ const std::string FloatString = FloatSmallString.str().str();
+
+ // Get integer and fractional parts of float number
+ const std::string::size_type DotPosition = FloatString.find('.');
+ const std::string IntegerSubString = FloatString.substr(0, DotPosition);
+ std::string FractionalSubString =
+ FloatString.substr(DotPosition + 1, FloatString.size());
+
+ // Split integer and fractional parts of float number
+ std::reverse(FractionalSubString.begin(), FractionalSubString.end());
+ const std::vector<std::string> PartsOfFloat = {IntegerSubString,
+ FractionalSubString};
+ const std::vector<std::string> SplittedIntegerSubString =
+ splitStringByGroupSize(PartsOfFloat[0], 3);
+ const std::vector<std::string> SplittedFractionalSubString =
+ splitStringByGroupSize(PartsOfFloat[1], 3);
+
+ // Get formatting literal text
+ std::string FormatedFractionalSubString =
+ std::accumulate(
+ SplittedFractionalSubString.begin(),
+ SplittedFractionalSubString.end(), std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ }).erase(0, 1);
+ std::reverse(FormatedFractionalSubString.begin(),
+ FormatedFractionalSubString.end());
+ const std::string FormatedLiteralString =
+ std::accumulate(
+ SplittedIntegerSubString.begin(), SplittedIntegerSubString.end(),
+ std::string(""),
+ [](std::basic_string<char> S1, std::basic_string<char> S2) {
+ return S1 + "\'" + S2;
+ })
+ .erase(0, 1) +
+ '.' + FormatedFractionalSubString + Postfix;
+
+ return FormatedLiteralString;
+}
} // namespace
namespace clang::tidy::modernize {
@@ -117,57 +171,9 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
CharSourceRange::getTokenRange(MatchedFloat->getSourceRange()), Source,
Context.getLangOpts());
- // Configure formatting
- std::string Postfix;
- for (const char &Character : OriginalLiteralString) {
- if (!std::isdigit(Character) && Character != '.') {
- Postfix += Character;
- }
- }
-
// Get formatting literal text
-
- // Get string representation of float value
const llvm::APFloat FloatValue = MatchedFloat->getValue();
- llvm::SmallString<128> FloatSmallString;
- FloatValue.toString(FloatSmallString);
- const std::string FloatString = FloatSmallString.str().str();
-
- // Get integer and fractional parts of float number
- const std::string::size_type DotPosition = FloatString.find('.');
- const std::string IntegerSubString = FloatString.substr(0, DotPosition);
- std::string FractionalSubString =
- FloatString.substr(DotPosition + 1, FloatString.size());
-
- // Split integer and fractional parts of float number
- std::reverse(FractionalSubString.begin(), FractionalSubString.end());
- const std::vector<std::string> PartsOfFloat = {IntegerSubString,
- FractionalSubString};
- const std::vector<std::string> SplittedIntegerSubString =
- splitStringByGroupSize(PartsOfFloat[0], 3);
- const std::vector<std::string> SplittedFractionalSubString =
- splitStringByGroupSize(PartsOfFloat[1], 3);
-
- // Get formatting literal text
- std::string FormatedFractionalSubString =
- std::accumulate(
- SplittedFractionalSubString.begin(),
- SplittedFractionalSubString.end(), std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- })
- .erase(0, 1);
- std::reverse(FormatedFractionalSubString.begin(),
- FormatedFractionalSubString.end());
- const std::string FormatedLiteralString =
- std::accumulate(
- SplittedIntegerSubString.begin(), SplittedIntegerSubString.end(),
- std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- })
- .erase(0, 1) +
- '.' + FormatedFractionalSubString + Postfix;
+ const std::string FormatedLiteralString = getFormatedFloatString(OriginalLiteralString, FloatValue);
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From 9edf9f5aac914d42a9c362ae8ef0b093d725584d Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Fri, 15 Dec 2023 13:13:51 +0300
Subject: [PATCH 32/58] Refactor getFormatedFloatString
---
.../modernize/UseDigitSeparatorCheck.cpp | 28 +++----------------
1 file changed, 4 insertions(+), 24 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 53c41d61e4a0a0..64e58d580aa26e 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -100,35 +100,15 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
const std::string IntegerSubString = FloatString.substr(0, DotPosition);
std::string FractionalSubString =
FloatString.substr(DotPosition + 1, FloatString.size());
-
- // Split integer and fractional parts of float number
std::reverse(FractionalSubString.begin(), FractionalSubString.end());
- const std::vector<std::string> PartsOfFloat = {IntegerSubString,
- FractionalSubString};
- const std::vector<std::string> SplittedIntegerSubString =
- splitStringByGroupSize(PartsOfFloat[0], 3);
- const std::vector<std::string> SplittedFractionalSubString =
- splitStringByGroupSize(PartsOfFloat[1], 3);
// Get formatting literal text
- std::string FormatedFractionalSubString =
- std::accumulate(
- SplittedFractionalSubString.begin(),
- SplittedFractionalSubString.end(), std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- }).erase(0, 1);
+ const std::string FormatedIntegerSubString = getFormatedIntegerString(IntegerSubString, llvm::APInt(128, std::stoi(IntegerSubString)));
+ std::string FormatedFractionalSubString = getFormatedIntegerString(FractionalSubString, llvm::APInt(128, std::stoi(FractionalSubString)));
std::reverse(FormatedFractionalSubString.begin(),
FormatedFractionalSubString.end());
- const std::string FormatedLiteralString =
- std::accumulate(
- SplittedIntegerSubString.begin(), SplittedIntegerSubString.end(),
- std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- })
- .erase(0, 1) +
- '.' + FormatedFractionalSubString + Postfix;
+
+ const std::string FormatedLiteralString = FormatedIntegerSubString + '.' + FormatedFractionalSubString + Postfix;
return FormatedLiteralString;
}
>From 5b4460b0ed15b01bbaa8093ee8eac49fb294b418 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Fri, 15 Dec 2023 13:14:21 +0300
Subject: [PATCH 33/58] Formating
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 64e58d580aa26e..f22bbd36d2ed83 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -103,12 +103,15 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
std::reverse(FractionalSubString.begin(), FractionalSubString.end());
// Get formatting literal text
- const std::string FormatedIntegerSubString = getFormatedIntegerString(IntegerSubString, llvm::APInt(128, std::stoi(IntegerSubString)));
- std::string FormatedFractionalSubString = getFormatedIntegerString(FractionalSubString, llvm::APInt(128, std::stoi(FractionalSubString)));
+ const std::string FormatedIntegerSubString = getFormatedIntegerString(
+ IntegerSubString, llvm::APInt(128, std::stoi(IntegerSubString)));
+ std::string FormatedFractionalSubString = getFormatedIntegerString(
+ FractionalSubString, llvm::APInt(128, std::stoi(FractionalSubString)));
std::reverse(FormatedFractionalSubString.begin(),
FormatedFractionalSubString.end());
- const std::string FormatedLiteralString = FormatedIntegerSubString + '.' + FormatedFractionalSubString + Postfix;
+ const std::string FormatedLiteralString =
+ FormatedIntegerSubString + '.' + FormatedFractionalSubString + Postfix;
return FormatedLiteralString;
}
>From d59adac840319ff2c7667fb25ccabf15af1f650f Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 18 Dec 2023 17:45:00 +0300
Subject: [PATCH 34/58] Add precision calculating
---
.../modernize/UseDigitSeparatorCheck.cpp | 22 ++++++++++++++-----
.../modernize/use-digit-separator.cpp | 4 ++++
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index f22bbd36d2ed83..61379f630b2c46 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include <algorithm>
+#include <iomanip>
#include <numeric>
#include <regex>
@@ -14,6 +15,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
+#include "llvm/Support/FormatVariadic.h"
using namespace clang::ast_matchers;
@@ -88,12 +90,22 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
}
}
+ // Get precision
+ const std::string::size_type OriginalDotPosition = OriginalLiteralString.find('.');
+ const llvm::StringRef OriginalFractionalSubString = OriginalLiteralString.substr(OriginalDotPosition + 1, OriginalLiteralString.size());
+ int Precision = 0;
+ for (const char &Character : OriginalFractionalSubString) {
+ if (std::isdigit(Character)) {
+ Precision++;
+ }
+ }
+
// Get formatting literal text
// Get string representation of float value
- llvm::SmallString<128> FloatSmallString;
- FloatValue.toString(FloatSmallString);
- const std::string FloatString = FloatSmallString.str().str();
+ std::ostringstream StringStream;
+ StringStream << std::fixed << std::setprecision(Precision) << FloatValue.convertToDouble();
+ const std::string FloatString = StringStream.str();
// Get integer and fractional parts of float number
const std::string::size_type DotPosition = FloatString.find('.');
@@ -104,9 +116,9 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
// Get formatting literal text
const std::string FormatedIntegerSubString = getFormatedIntegerString(
- IntegerSubString, llvm::APInt(128, std::stoi(IntegerSubString)));
+ IntegerSubString, llvm::APInt(128, std::stoll(IntegerSubString)));
std::string FormatedFractionalSubString = getFormatedIntegerString(
- FractionalSubString, llvm::APInt(128, std::stoi(FractionalSubString)));
+ FractionalSubString, llvm::APInt(128, std::stoll(FractionalSubString)));
std::reverse(FormatedFractionalSubString.begin(),
FormatedFractionalSubString.end());
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 31aa3f6d5476be..c735bd2a4ac9fd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -80,6 +80,10 @@ float MinusNotFormattedFloat = -1234.56789;
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.567'89
+double PostfixNotFormattedFloat = 1234.569F;
+// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: unformatted representation of integer literal '1234.569F' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234.569F
+
// FIXME:
// error: expected ';' after top level declarator [clang-diagnostic-error]
// 80 | int FormattedInteger = 1'234'567;
>From 15627d1edc96c3a18d137e9a7f182a191c6089b4 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 18 Dec 2023 17:45:46 +0300
Subject: [PATCH 35/58] Formating
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 61379f630b2c46..438d2634095469 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -91,8 +91,11 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
}
// Get precision
- const std::string::size_type OriginalDotPosition = OriginalLiteralString.find('.');
- const llvm::StringRef OriginalFractionalSubString = OriginalLiteralString.substr(OriginalDotPosition + 1, OriginalLiteralString.size());
+ const std::string::size_type OriginalDotPosition =
+ OriginalLiteralString.find('.');
+ const llvm::StringRef OriginalFractionalSubString =
+ OriginalLiteralString.substr(OriginalDotPosition + 1,
+ OriginalLiteralString.size());
int Precision = 0;
for (const char &Character : OriginalFractionalSubString) {
if (std::isdigit(Character)) {
@@ -104,7 +107,8 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
// Get string representation of float value
std::ostringstream StringStream;
- StringStream << std::fixed << std::setprecision(Precision) << FloatValue.convertToDouble();
+ StringStream << std::fixed << std::setprecision(Precision)
+ << FloatValue.convertToDouble();
const std::string FloatString = StringStream.str();
// Get integer and fractional parts of float number
>From ba22e57d875afae6d2a0f0beddb71e993ca97624 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 18 Dec 2023 19:03:06 +0300
Subject: [PATCH 36/58] Scientific form of literal support
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 438d2634095469..1cb217b112ca34 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -82,6 +82,19 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
}
std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString, const llvm::APFloat FloatValue) {
+ if (OriginalLiteralString.contains('E') || OriginalLiteralString.contains('e')) {
+ // Split string to mantissa and exponent
+ const std::string::size_type EPosition = OriginalLiteralString.str().find('E') != std::string::npos ? OriginalLiteralString.str().find('E') : OriginalLiteralString.str().find('e');
+ const llvm::StringRef MantissaSubString = OriginalLiteralString.substr(0, EPosition);
+ const llvm::StringRef ExponentSubString =
+ OriginalLiteralString.substr(EPosition + 1, OriginalLiteralString.size());
+
+ // Get formatting literal text
+ const std::string FormatedMantissaString = getFormatedFloatString(MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
+ const std::string FormatedExponentString = getFormatedIntegerString(ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
+ return FormatedMantissaString + (OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e') + FormatedExponentString;
+ }
+
// Configure formatting
std::string Postfix;
for (const char &Character : OriginalLiteralString) {
>From bde206f6683037339676cff84a628e0684b43546 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 18 Dec 2023 19:17:24 +0300
Subject: [PATCH 37/58] Some refactoring and tests
---
.../modernize/UseDigitSeparatorCheck.cpp | 26 ++++++++++++-------
.../modernize/use-digit-separator.cpp | 8 ++++++
2 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 1cb217b112ca34..75dd8e1bf3a05a 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -20,6 +20,8 @@
using namespace clang::ast_matchers;
namespace {
+std::string getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString);
+
std::vector<std::basic_string<char>>
splitStringByGroupSize(const std::basic_string<char> &String, size_t GroupSize) {
std::vector<std::basic_string<char>> Result;
@@ -83,16 +85,7 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString, const llvm::APFloat FloatValue) {
if (OriginalLiteralString.contains('E') || OriginalLiteralString.contains('e')) {
- // Split string to mantissa and exponent
- const std::string::size_type EPosition = OriginalLiteralString.str().find('E') != std::string::npos ? OriginalLiteralString.str().find('E') : OriginalLiteralString.str().find('e');
- const llvm::StringRef MantissaSubString = OriginalLiteralString.substr(0, EPosition);
- const llvm::StringRef ExponentSubString =
- OriginalLiteralString.substr(EPosition + 1, OriginalLiteralString.size());
-
- // Get formatting literal text
- const std::string FormatedMantissaString = getFormatedFloatString(MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
- const std::string FormatedExponentString = getFormatedIntegerString(ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
- return FormatedMantissaString + (OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e') + FormatedExponentString;
+ return getFormatedScientificFloatString(OriginalLiteralString);
}
// Configure formatting
@@ -144,6 +137,19 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
return FormatedLiteralString;
}
+
+std::string getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
+ // Split string to mantissa and exponent
+ const std::string::size_type EPosition = OriginalLiteralString.str().find('E') != std::string::npos ? OriginalLiteralString.str().find('E') : OriginalLiteralString.str().find('e');
+ const llvm::StringRef MantissaSubString = OriginalLiteralString.substr(0, EPosition);
+ const llvm::StringRef ExponentSubString =
+ OriginalLiteralString.substr(EPosition + 1, OriginalLiteralString.size());
+
+ // Get formatting literal text
+ const std::string FormatedMantissaString = getFormatedFloatString(MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
+ const std::string FormatedExponentString = getFormatedIntegerString(ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
+ return FormatedMantissaString + (OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e') + FormatedExponentString;
+}
} // namespace
namespace clang::tidy::modernize {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index c735bd2a4ac9fd..86bde5dd5c752f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -84,6 +84,14 @@ double PostfixNotFormattedFloat = 1234.569F;
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: unformatted representation of integer literal '1234.569F' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.569F
+double ScientificNotFormattedFloat = 1.2345678e10;
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '1.2345678e10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e10
+
+double ScientificNotFormattedFloat1 = 1.2345678E10;
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '1.2345678E10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8E10
+
// FIXME:
// error: expected ';' after top level declarator [clang-diagnostic-error]
// 80 | int FormattedInteger = 1'234'567;
>From 88974008b895f9c6cd9409242f0269d0a702f3e2 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 18 Dec 2023 19:17:54 +0300
Subject: [PATCH 38/58] Formating
---
.../modernize/UseDigitSeparatorCheck.cpp | 25 +++++++++++++------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 75dd8e1bf3a05a..1927be30f0d867 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -20,7 +20,8 @@
using namespace clang::ast_matchers;
namespace {
-std::string getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString);
+std::string
+getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString);
std::vector<std::basic_string<char>>
splitStringByGroupSize(const std::basic_string<char> &String, size_t GroupSize) {
@@ -138,17 +139,27 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
return FormatedLiteralString;
}
-std::string getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
+std::string
+getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
// Split string to mantissa and exponent
- const std::string::size_type EPosition = OriginalLiteralString.str().find('E') != std::string::npos ? OriginalLiteralString.str().find('E') : OriginalLiteralString.str().find('e');
- const llvm::StringRef MantissaSubString = OriginalLiteralString.substr(0, EPosition);
+ const std::string::size_type EPosition =
+ OriginalLiteralString.str().find('E') != std::string::npos
+ ? OriginalLiteralString.str().find('E')
+ : OriginalLiteralString.str().find('e');
+ const llvm::StringRef MantissaSubString =
+ OriginalLiteralString.substr(0, EPosition);
const llvm::StringRef ExponentSubString =
OriginalLiteralString.substr(EPosition + 1, OriginalLiteralString.size());
// Get formatting literal text
- const std::string FormatedMantissaString = getFormatedFloatString(MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
- const std::string FormatedExponentString = getFormatedIntegerString(ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
- return FormatedMantissaString + (OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e') + FormatedExponentString;
+ const std::string FormatedMantissaString = getFormatedFloatString(
+ MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
+ const std::string FormatedExponentString = getFormatedIntegerString(
+ ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
+ return FormatedMantissaString +
+ (OriginalLiteralString.str().find('E') != std::string::npos ? 'E'
+ : 'e') +
+ FormatedExponentString;
}
} // namespace
>From 260e09647dbb7e78dd3e0a96ecae3d54e9047938 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Mon, 18 Dec 2023 19:19:53 +0300
Subject: [PATCH 39/58] Fix warning message for float values
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 2 +-
.../checkers/modernize/use-digit-separator.cpp | 10 +++++-----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 1927be30f0d867..12907404aa2964 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -207,7 +207,7 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
diag(MatchedFloat->getLocation(),
- "unformatted representation of integer literal '%0'")
+ "unformatted representation of float literal '%0'")
<< OriginalLiteralString
<< FixItHint::CreateInsertion(MatchedFloat->getLocation(),
FormatedLiteralString);
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 86bde5dd5c752f..efa83ed500a1fb 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -73,23 +73,23 @@ unsigned long MinusUnsignedLongNotFormattedInteger1 = -12345678910111213Ul;
// CHECK-FIXES: 12'345'678'910'111'213Ul
float NotFormattedFloat = 1234.56789;
-// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
+// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of float literal '1234.56789' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.567'89
float MinusNotFormattedFloat = -1234.56789;
-// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of integer literal '1234.56789' [modernize-use-digit-separator]
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unformatted representation of float literal '1234.56789' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.567'89
double PostfixNotFormattedFloat = 1234.569F;
-// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: unformatted representation of integer literal '1234.569F' [modernize-use-digit-separator]
+// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: unformatted representation of float literal '1234.569F' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.569F
double ScientificNotFormattedFloat = 1.2345678e10;
-// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '1.2345678e10' [modernize-use-digit-separator]
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of float literal '1.2345678e10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8e10
double ScientificNotFormattedFloat1 = 1.2345678E10;
-// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of integer literal '1.2345678E10' [modernize-use-digit-separator]
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of float literal '1.2345678E10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8E10
// FIXME:
>From 808e95cf5c9ee5c65981422f0a67227a9e0f95c1 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 11:39:26 +0300
Subject: [PATCH 40/58] Some refactoring
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 12907404aa2964..78c29f4e09a539 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -142,10 +142,10 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
std::string
getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
// Split string to mantissa and exponent
- const std::string::size_type EPosition =
- OriginalLiteralString.str().find('E') != std::string::npos
- ? OriginalLiteralString.str().find('E')
- : OriginalLiteralString.str().find('e');
+ const char EChar = OriginalLiteralString.str().find('E') != std::string::npos
+ ? 'E'
+ : 'e';
+ const std::string::size_type EPosition = OriginalLiteralString.str().find(EChar);
const llvm::StringRef MantissaSubString =
OriginalLiteralString.substr(0, EPosition);
const llvm::StringRef ExponentSubString =
@@ -157,8 +157,7 @@ getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
const std::string FormatedExponentString = getFormatedIntegerString(
ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
return FormatedMantissaString +
- (OriginalLiteralString.str().find('E') != std::string::npos ? 'E'
- : 'e') +
+ EChar +
FormatedExponentString;
}
} // namespace
>From c22c94b444be886e1772125d926a059836ba48b4 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 11:39:58 +0300
Subject: [PATCH 41/58] Formating
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 78c29f4e09a539..73ca668569e1ab 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -142,10 +142,10 @@ std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
std::string
getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
// Split string to mantissa and exponent
- const char EChar = OriginalLiteralString.str().find('E') != std::string::npos
- ? 'E'
- : 'e';
- const std::string::size_type EPosition = OriginalLiteralString.str().find(EChar);
+ const char EChar =
+ OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e';
+ const std::string::size_type EPosition =
+ OriginalLiteralString.str().find(EChar);
const llvm::StringRef MantissaSubString =
OriginalLiteralString.substr(0, EPosition);
const llvm::StringRef ExponentSubString =
@@ -156,9 +156,7 @@ getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
const std::string FormatedExponentString = getFormatedIntegerString(
ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
- return FormatedMantissaString +
- EChar +
- FormatedExponentString;
+ return FormatedMantissaString + EChar + FormatedExponentString;
}
} // namespace
>From b80f6a187824d91857ed2e8df033ae06fe28cc52 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 12:36:18 +0300
Subject: [PATCH 42/58] Support signs in exponent notation
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 5 +++--
.../checkers/modernize/use-digit-separator.cpp | 10 +++++++++-
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 73ca668569e1ab..71118bfb169bf4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -146,17 +146,18 @@ getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e';
const std::string::size_type EPosition =
OriginalLiteralString.str().find(EChar);
+ const std::string SignSymbol = OriginalLiteralString[EPosition + 1] == '-' ? "-" : (OriginalLiteralString[EPosition + 1] == '+' ? "+" : "");
const llvm::StringRef MantissaSubString =
OriginalLiteralString.substr(0, EPosition);
const llvm::StringRef ExponentSubString =
- OriginalLiteralString.substr(EPosition + 1, OriginalLiteralString.size());
+ OriginalLiteralString.substr(EPosition + SignSymbol.size() + 1, OriginalLiteralString.size());
// Get formatting literal text
const std::string FormatedMantissaString = getFormatedFloatString(
MantissaSubString, llvm::APFloat(std::stod(MantissaSubString.str())));
const std::string FormatedExponentString = getFormatedIntegerString(
ExponentSubString, llvm::APInt(128, std::stoll(ExponentSubString.str())));
- return FormatedMantissaString + EChar + FormatedExponentString;
+ return FormatedMantissaString + EChar + SignSymbol + FormatedExponentString;
}
} // namespace
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index efa83ed500a1fb..9d400ea6d69701 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -89,9 +89,17 @@ double ScientificNotFormattedFloat = 1.2345678e10;
// CHECK-FIXES: 1.234'567'8e10
double ScientificNotFormattedFloat1 = 1.2345678E10;
-// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of float literal '1.2345678E10' [modernize-use-digit-separator]
+// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of float literal '1.2345678E10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8E10
+double ScientificNotFormattedFloat2 = -1.2345678E+10;
+// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unformatted representation of float literal '1.2345678E+10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8E+10
+
+double ScientificNotFormattedFloat3 = +1.2345678e-10;
+// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unformatted representation of float literal '1.2345678e-10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e-10
+
// FIXME:
// error: expected ';' after top level declarator [clang-diagnostic-error]
// 80 | int FormattedInteger = 1'234'567;
>From 9726be55f214a4a85f6e99e164de527b2daeea2a Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 12:36:48 +0300
Subject: [PATCH 43/58] Formating
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 71118bfb169bf4..cfcad53c9f3fff 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -146,11 +146,14 @@ getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString) {
OriginalLiteralString.str().find('E') != std::string::npos ? 'E' : 'e';
const std::string::size_type EPosition =
OriginalLiteralString.str().find(EChar);
- const std::string SignSymbol = OriginalLiteralString[EPosition + 1] == '-' ? "-" : (OriginalLiteralString[EPosition + 1] == '+' ? "+" : "");
+ const std::string SignSymbol =
+ OriginalLiteralString[EPosition + 1] == '-'
+ ? "-"
+ : (OriginalLiteralString[EPosition + 1] == '+' ? "+" : "");
const llvm::StringRef MantissaSubString =
OriginalLiteralString.substr(0, EPosition);
- const llvm::StringRef ExponentSubString =
- OriginalLiteralString.substr(EPosition + SignSymbol.size() + 1, OriginalLiteralString.size());
+ const llvm::StringRef ExponentSubString = OriginalLiteralString.substr(
+ EPosition + SignSymbol.size() + 1, OriginalLiteralString.size());
// Get formatting literal text
const std::string FormatedMantissaString = getFormatedFloatString(
>From 1e00d391d2c1ccffc3520403f838f944f59c1c8e Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 13:32:21 +0300
Subject: [PATCH 44/58] Some tests and fixes
---
.../modernize/UseDigitSeparatorCheck.cpp | 4 +-
.../modernize/use-digit-separator.cpp | 41 +++++++++++++++++++
2 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index cfcad53c9f3fff..fad1f4915066a0 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -62,8 +62,8 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
GroupSize = 3;
}
- for (const char &Character : OriginalLiteralString) {
- if (!std::isdigit(Character) && Character != '\'') {
+ for (const char &Character : OriginalLiteralString.substr(Prefix.size(), OriginalLiteralString.size())) {
+ if (!std::isxdigit(Character) && Character != '\'') {
Postfix += Character;
}
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 9d400ea6d69701..672a87fce2aa01 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -1,5 +1,7 @@
// RUN: %check_clang_tidy %s modernize-use-digit-separator %t
+// Long not formatted literals
+
int NotFormattedInteger = 1234567;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: unformatted representation of integer literal '1234567' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234'567
@@ -84,6 +86,18 @@ double PostfixNotFormattedFloat = 1234.569F;
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: unformatted representation of float literal '1234.569F' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.569F
+double MinusPostfixNotFormattedFloat = -1234.569F;
+// CHECK-MESSAGES: :[[@LINE-1]]:41: warning: unformatted representation of float literal '1234.569F' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234.569F
+
+double PostfixNotFormattedFloat1 = 1234.569f;
+// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: unformatted representation of float literal '1234.569f' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234.569f
+
+double MinusPostfixNotFormattedFloat1 = -1234.569f;
+// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: unformatted representation of float literal '1234.569f' [modernize-use-digit-separator]
+// CHECK-FIXES: 1'234.569f
+
double ScientificNotFormattedFloat = 1.2345678e10;
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of float literal '1.2345678e10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8e10
@@ -100,6 +114,33 @@ double ScientificNotFormattedFloat3 = +1.2345678e-10;
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unformatted representation of float literal '1.2345678e-10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8e-10
+// Short literals
+
+int ShortInteger = 123;
+int MinusShortInteger = -123;
+int ShortBinaryInteger = 0b10;
+int MinusShortBinaryInteger = 0b10;
+int ShortOctInteger = 037;
+int MinusShortOctInteger = -037;
+int ShortHexInteger = 0x3F0;
+int MinusShortHexInteger = -0x3F0;
+unsigned int UnsignedShortInteger = 123U;
+unsigned int MinusUnsignedShortInteger = -123U;
+unsigned int UnsignedShortInteger1 = 123u;
+unsigned int MinusUnsignedShortInteger1 = -123u;
+long LongShortInteger = 123L;
+long MinusLongShortInteger = -123L;
+long LongShortInteger1 = 123l;
+long MinusLongShortInteger1 = -123l;
+unsigned long UnsignedLongShortInteger = 123uL;
+unsigned long MinusUnsignedLongShortInteger = -123uL;
+float ShortFloat = 1.23;
+float MinusShortFloat = -1.23;
+float PostfixShortFloat = 1.23F;
+float MinusPostfixShortFloat = -1.23F;
+float PostfixShortFloat1 = 1.23f;
+float MinusPostfixShortFloat1 = -1.23f;
+
// FIXME:
// error: expected ';' after top level declarator [clang-diagnostic-error]
// 80 | int FormattedInteger = 1'234'567;
>From 5081470340dceaaf15c8703a091c4c2e33776876 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 13:32:51 +0300
Subject: [PATCH 45/58] Formatting
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index fad1f4915066a0..5e692eaff3dfca 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -62,7 +62,8 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
GroupSize = 3;
}
- for (const char &Character : OriginalLiteralString.substr(Prefix.size(), OriginalLiteralString.size())) {
+ for (const char &Character : OriginalLiteralString.substr(
+ Prefix.size(), OriginalLiteralString.size())) {
if (!std::isxdigit(Character) && Character != '\'') {
Postfix += Character;
}
>From f929f69a4b5112a168481711d61fc2949dadecc5 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 16:33:44 +0300
Subject: [PATCH 46/58] Add more tests
---
.../modernize/use-digit-separator.cpp | 50 +++++++++++++++----
1 file changed, 40 insertions(+), 10 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 672a87fce2aa01..110171526bb1a0 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -1,5 +1,6 @@
// RUN: %check_clang_tidy %s modernize-use-digit-separator %t
+
// Long not formatted literals
int NotFormattedInteger = 1234567;
@@ -98,21 +99,38 @@ double MinusPostfixNotFormattedFloat1 = -1234.569f;
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: unformatted representation of float literal '1234.569f' [modernize-use-digit-separator]
// CHECK-FIXES: 1'234.569f
-double ScientificNotFormattedFloat = 1.2345678e10;
-// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of float literal '1.2345678e10' [modernize-use-digit-separator]
-// CHECK-FIXES: 1.234'567'8e10
+double ScientificNotFormattedFloat = 1.2345678E10;
+// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: unformatted representation of float literal '1.2345678E10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8E10
-double ScientificNotFormattedFloat1 = 1.2345678E10;
-// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of float literal '1.2345678E10' [modernize-use-digit-separator]
+double MinusScientificNotFormattedFloat = -1.2345678E10;
+// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: unformatted representation of float literal '1.2345678E10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8E10
-double ScientificNotFormattedFloat2 = -1.2345678E+10;
-// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unformatted representation of float literal '1.2345678E+10' [modernize-use-digit-separator]
+double ScientificNotFormattedFloat1 = 1.2345678e10;
+// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of float literal '1.2345678e10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e10
+
+double MinusScientificNotFormattedFloat1 = -1.2345678e10;
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of float literal '1.2345678e10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e10
+
+double ScientificNotFormattedFloat2 = 1.2345678E+10;
+// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of float literal '1.2345678E+10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8E+10
+
+double MinusScientificNotFormattedFloat2 = -1.2345678E+10;
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of float literal '1.2345678E+10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8E+10
-double ScientificNotFormattedFloat3 = +1.2345678e-10;
-// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: unformatted representation of float literal '1.2345678e-10' [modernize-use-digit-separator]
-// CHECK-FIXES: 1.234'567'8e-10
+double ScientificNotFormattedFloat3 = 1.2345678e+10;
+// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: unformatted representation of float literal '1.2345678e+10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e+10
+
+double MinusScientificNotFormattedFloat3 = -1.2345678e+10;
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of float literal '1.2345678e+10' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e+10
+
// Short literals
@@ -140,6 +158,18 @@ float PostfixShortFloat = 1.23F;
float MinusPostfixShortFloat = -1.23F;
float PostfixShortFloat1 = 1.23f;
float MinusPostfixShortFloat1 = -1.23f;
+float ScientificShortFloat = 1.23E10;
+float MinusScientificShortFloat = -1.23E10;
+float ScientificShortFloat1 = 1.23e10;
+float MinusScientificShortFloat1 = -1.23e10;
+float ScientificShortFloat2 = 1.23E+10;
+float MinusScientificShortFloat2 = -1.23E+10;
+float ScientificShortFloat3 = 1.23e+10;
+float MinusScientificShortFloat3 = -1.23e+10;
+float ScientificShortFloat4 = 1.23E-10;
+float MinusScientificShortFloat4 = -1.23E-10;
+float ScientificShortFloat5 = 1.23e+10;
+float MinusScientificShortFloat5 = -1.23e-10;
// FIXME:
// error: expected ';' after top level declarator [clang-diagnostic-error]
>From 6fd799132eb7cfd7ee9fbcb10510f6ec1ce6a733 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 17:15:24 +0300
Subject: [PATCH 47/58] Add more tests
---
.../modernize/UseDigitSeparatorCheck.cpp | 2 +-
.../modernize/use-digit-separator.cpp | 20 +++++++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 5e692eaff3dfca..3344e458049014 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -64,7 +64,7 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
for (const char &Character : OriginalLiteralString.substr(
Prefix.size(), OriginalLiteralString.size())) {
- if (!std::isxdigit(Character) && Character != '\'') {
+ if (((Prefix == "0x" && !std::isxdigit(Character)) || (Prefix != "0x" && !std::isdigit(Character))) && Character != '\'') {
Postfix += Character;
}
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 110171526bb1a0..1d2f54344fcd6c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -131,6 +131,22 @@ double MinusScientificNotFormattedFloat3 = -1.2345678e+10;
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of float literal '1.2345678e+10' [modernize-use-digit-separator]
// CHECK-FIXES: 1.234'567'8e+10
+double PostfixScientificNotFormattedFloat = 1.2345678E10F;
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: unformatted representation of float literal '1.2345678E10F' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8E10F
+
+double PostfixScientificNotFormattedFloat1 = 1.2345678e10f;
+// CHECK-MESSAGES: :[[@LINE-1]]:46: warning: unformatted representation of float literal '1.2345678e10f' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e10f
+
+double PostfixScientificNotFormattedFloat2 = -1.2345678E+10f;
+// CHECK-MESSAGES: :[[@LINE-1]]:47: warning: unformatted representation of float literal '1.2345678E+10f' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8E+10f
+
+double PostfixScientificNotFormattedFloat4 = -1.2345678e-10F;
+// CHECK-MESSAGES: :[[@LINE-1]]:47: warning: unformatted representation of float literal '1.2345678e-10F' [modernize-use-digit-separator]
+// CHECK-FIXES: 1.234'567'8e-10F
+
// Short literals
@@ -170,6 +186,10 @@ float ScientificShortFloat4 = 1.23E-10;
float MinusScientificShortFloat4 = -1.23E-10;
float ScientificShortFloat5 = 1.23e+10;
float MinusScientificShortFloat5 = -1.23e-10;
+float PostfixScientificShortFloat = 1.23E10F;
+float PostfixScientificShortFloat1 = 1.23e10f;
+float PostfixScientificShortFloat2 = 1.23E+10f;
+float PostfixScientificShortFloat3 = 1.23e-10F;
// FIXME:
// error: expected ';' after top level declarator [clang-diagnostic-error]
>From 78d541956f3e0c790e7f47b685a1287b864040aa Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 19 Dec 2023 17:16:37 +0300
Subject: [PATCH 48/58] Formating
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 3344e458049014..a5503f57ec7edc 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -64,7 +64,9 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
for (const char &Character : OriginalLiteralString.substr(
Prefix.size(), OriginalLiteralString.size())) {
- if (((Prefix == "0x" && !std::isxdigit(Character)) || (Prefix != "0x" && !std::isdigit(Character))) && Character != '\'') {
+ if (((Prefix == "0x" && !std::isxdigit(Character)) ||
+ (Prefix != "0x" && !std::isdigit(Character))) &&
+ Character != '\'') {
Postfix += Character;
}
}
>From fdea346f002b39096b03e978fc505604be60c3a0 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 15:41:28 +0300
Subject: [PATCH 49/58] Add tests for Formatted literals
---
.../modernize/use-digit-separator.cpp | 49 +++++++++++++++++--
1 file changed, 44 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 1d2f54344fcd6c..75f7d4d989f322 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -191,9 +191,48 @@ float PostfixScientificShortFloat1 = 1.23e10f;
float PostfixScientificShortFloat2 = 1.23E+10f;
float PostfixScientificShortFloat3 = 1.23e-10F;
-// FIXME:
+
+// Formatted literals
+
+// FIXME: clang-diagnostic-error like this for the following tests
+// clang-18 and clang-tidy don't produce such error
// error: expected ';' after top level declarator [clang-diagnostic-error]
-// 80 | int FormattedInteger = 1'234'567;
-// | ^
-// | ;
-//int FormattedInteger = 1'234'567;
\ No newline at end of file
+// 203 | int FormattedInteger = 1'234'567;
+// | ^
+// | ;
+//int FormattedInteger = 1'234'567;
+//int MinusFormattedInteger = -1'234'567;
+//int BinaryFormattedInteger = 0b1110'1101;
+//int MinusBinaryFormattedInteger = -0b1110'1101;
+//int OctFormattedInteger = 037'512;
+//int MinusOctFormattedInteger = -037'512;
+//int HexFormattedInteger = 0x4'f356;
+//int MinusHexFormattedInteger = -0x4'f356;
+//unsigned int UnsignedFormattedInteger = 10'004U;
+//unsigned int MinusUnsignedFormattedInteger = -10'004U;
+//unsigned int UnsignedFormattedInteger1 = 100'045u;
+//unsigned int MinusUnsignedFormattedInteger1 = -100'045u;
+//long LongFormattedInteger = 123'456'789'101'112L;
+//long MinusLongFormattedInteger = -123'456'789'101'112L;
+//long LongFormattedInteger1 = 12'345'678'910'111'213l;
+//long MinusLongFormattedInteger1 = -12'345'678'910'111'213l;
+//unsigned long UnsignedLongFormattedInteger1 = 12'345'678'910'111'213Ul;
+//unsigned long MinusUnsignedLongFormattedInteger1 = -12'345'678'910'111'213Ul;
+//float FormattedFloat = 1'234.567'89;
+//float MinusFormattedFloat = -1'234.567'89;
+//double PostfixFormattedFloat = 1'234.569F;
+//double MinusPostfixFormattedFloat = -1'234.569F;
+//double PostfixFormattedFloat1 = 1'234.569f;
+//double MinusPostfixFormattedFloat1 = -1'234.569f;
+//double ScientificFormattedFloat = 1.234'567'8E10;
+//double MinusScientificFormattedFloat = -1.234'567'8E10;
+//double ScientificFormattedFloat1 = 1.234'567'8e10;
+//double MinusScientificFormattedFloat1 = -1.234'567'8e10;
+//double ScientificFormattedFloat2 = 1.234'567'8E+10;
+//double MinusScientificFormattedFloat2 = -1.234'567'8E+10;
+//double ScientificFormattedFloat3 = 1.234'567'8e+10;
+//double MinusScientificFormattedFloat3 = -1.234'567'8e+10;
+//double PostfixScientificFormattedFloat = 1.234'567'8E10F;
+//double PostfixScientificFormattedFloat1 = 1.234'567'8e10f;
+//double PostfixScientificFormattedFloat2 = -1.234'567'8E+10f;
+//double PostfixScientificFormattedFloat4 = -1.234'567'8e-10F;
>From f8446ab3d24ad4555456c4b2db562c570209af79 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 16:04:31 +0300
Subject: [PATCH 50/58] Add tests for wrong Formatted literals
---
.../checkers/modernize/use-digit-separator.cpp | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 75f7d4d989f322..55063e6cfbb8d1 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -197,9 +197,10 @@ float PostfixScientificShortFloat3 = 1.23e-10F;
// FIXME: clang-diagnostic-error like this for the following tests
// clang-18 and clang-tidy don't produce such error
// error: expected ';' after top level declarator [clang-diagnostic-error]
-// 203 | int FormattedInteger = 1'234'567;
+// 204 | int FormattedInteger = 1'234'567;
// | ^
// | ;
+
//int FormattedInteger = 1'234'567;
//int MinusFormattedInteger = -1'234'567;
//int BinaryFormattedInteger = 0b1110'1101;
@@ -236,3 +237,17 @@ float PostfixScientificShortFloat3 = 1.23e-10F;
//double PostfixScientificFormattedFloat1 = 1.234'567'8e10f;
//double PostfixScientificFormattedFloat2 = -1.234'567'8E+10f;
//double PostfixScientificFormattedFloat4 = -1.234'567'8e-10F;
+
+
+// Long wrong formatted literals
+
+// FIXME: clang-diagnostic-error like this for the following tests
+// clang-18 and clang-tidy don't produce such error
+//error: expected ';' after top level declarator [clang-diagnostic-error]
+// 249 | int WrongFormattedInteger = 1234'56'7;
+// | ^
+// | ;
+
+//int WrongFormattedInteger = 1234'56'7;
+//// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: unformatted representation of integer literal '1234'56'7' [modernize-use-digit-separator]
+//// CHECK-FIXES: 1'234'567
>From 11f80912ccd3a8841cf689ad0293748e512dcca7 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 16:04:47 +0300
Subject: [PATCH 51/58] Delete tests for wrong Formatted literals
---
.../checkers/modernize/use-digit-separator.cpp | 14 --------------
1 file changed, 14 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index 55063e6cfbb8d1..ec4bdaecfa87de 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -237,17 +237,3 @@ float PostfixScientificShortFloat3 = 1.23e-10F;
//double PostfixScientificFormattedFloat1 = 1.234'567'8e10f;
//double PostfixScientificFormattedFloat2 = -1.234'567'8E+10f;
//double PostfixScientificFormattedFloat4 = -1.234'567'8e-10F;
-
-
-// Long wrong formatted literals
-
-// FIXME: clang-diagnostic-error like this for the following tests
-// clang-18 and clang-tidy don't produce such error
-//error: expected ';' after top level declarator [clang-diagnostic-error]
-// 249 | int WrongFormattedInteger = 1234'56'7;
-// | ^
-// | ;
-
-//int WrongFormattedInteger = 1234'56'7;
-//// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: unformatted representation of integer literal '1234'56'7' [modernize-use-digit-separator]
-//// CHECK-FIXES: 1'234'567
>From f754d5448c7edd6265ece34086e4152be7105506 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 16:13:25 +0300
Subject: [PATCH 52/58] Add docs
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.h | 8 +++++++-
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
index 8bbd2b3947c931..55083e2817574d 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
@@ -13,7 +13,13 @@
namespace clang::tidy::modernize {
-/// FIXME: Write a short description.
+/// 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;
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-digit-separator.html
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 4b9a0c1525e153..6812a8cefdbcec 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -195,7 +195,7 @@ New checks
- New :doc:`modernize-use-digit-separator
<clang-tidy/checks/modernize/use-digit-separator>` check.
- FIXME: add release notes.
+ Looks for long integral constants and inserts the digits separator (') appropriately.
- New :doc:`modernize-use-starts-ends-with
<clang-tidy/checks/modernize/use-starts-ends-with>` check.
>From ab6dedfc0cd168682f9f11215d07e9f709e22ab8 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 16:14:19 +0300
Subject: [PATCH 53/58] Formatting
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.h | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
index 55083e2817574d..7f819420f85797 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.h
@@ -13,13 +13,16 @@
namespace clang::tidy::modernize {
-/// The check that looks for long integral constants and inserts the digits separator (') appropriately.
-/// Groupings:
+/// 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;
+/// - 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;
+/// - 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;
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-digit-separator.html
>From e31f05f48dcb8f086c9da0a3f7d3c08d266bcf8b Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 16:35:42 +0300
Subject: [PATCH 54/58] Formatting
---
.../modernize/UseDigitSeparatorCheck.cpp | 41 +++++++++++--------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index a5503f57ec7edc..37c3804c43be4f 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -24,7 +24,8 @@ std::string
getFormatedScientificFloatString(const llvm::StringRef OriginalLiteralString);
std::vector<std::basic_string<char>>
-splitStringByGroupSize(const std::basic_string<char> &String, size_t GroupSize) {
+splitStringByGroupSize(const std::basic_string<char> &String,
+ size_t GroupSize) {
std::vector<std::basic_string<char>> Result;
std::basic_string<char> ReversedString(String.rbegin(), String.rend());
@@ -33,12 +34,15 @@ splitStringByGroupSize(const std::basic_string<char> &String, size_t 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());});
+ std::for_each(Result.begin(), Result.end(), [](std::basic_string<char> &Str) {
+ return std::reverse(Str.begin(), Str.end());
+ });
return Result;
}
-std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString, const llvm::APInt IntegerValue) {
+std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString,
+ const llvm::APInt IntegerValue) {
// Configure formatting
unsigned int Radix;
size_t GroupSize;
@@ -76,19 +80,20 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
splitStringByGroupSize(toString(IntegerValue, Radix, true), GroupSize);
const std::string FormatedLiteralString =
Prefix +
- std::accumulate(
- SplittedIntegerLiteral.begin(), SplittedIntegerLiteral.end(),
- std::string(""),
- [](std::basic_string<char> S1, std::basic_string<char> S2) {
- return S1 + "\'" + S2;
- })
- .erase(0, 1) + Postfix;
+ std::accumulate(SplittedIntegerLiteral.begin(),
+ SplittedIntegerLiteral.end(),std::string(""),
+ [](std::basic_string<char> S1,
+ std::basic_string<char> S2) { return S1 + "\'" + S2; })
+ .erase(0, 1) +
+ Postfix;
return FormatedLiteralString;
}
-std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString, const llvm::APFloat FloatValue) {
- if (OriginalLiteralString.contains('E') || OriginalLiteralString.contains('e')) {
+std::string getFormatedFloatString(const llvm::StringRef OriginalLiteralString,
+ const llvm::APFloat FloatValue) {
+ if (OriginalLiteralString.contains('E') ||
+ OriginalLiteralString.contains('e')) {
return getFormatedScientificFloatString(OriginalLiteralString);
}
@@ -177,8 +182,10 @@ void UseDigitSeparatorCheck::registerMatchers(MatchFinder *Finder) {
void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
const ASTContext &Context = *Result.Context;
const SourceManager &Source = Context.getSourceManager();
- const IntegerLiteral *MatchedInteger = Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
- const FloatingLiteral *MatchedFloat = Result.Nodes.getNodeAs<FloatingLiteral>("floatLiteral");
+ const IntegerLiteral *MatchedInteger =
+ Result.Nodes.getNodeAs<IntegerLiteral>("integerLiteral");
+ const FloatingLiteral *MatchedFloat =
+ Result.Nodes.getNodeAs<FloatingLiteral>("floatLiteral");
if (MatchedInteger != nullptr) {
// Get original literal source text
@@ -188,7 +195,8 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Get formatting literal text
const llvm::APInt IntegerValue = MatchedInteger->getValue();
- const std::string FormatedLiteralString = getFormatedIntegerString(OriginalLiteralString, IntegerValue);
+ const std::string FormatedLiteralString =
+ getFormatedIntegerString(OriginalLiteralString, IntegerValue);
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
@@ -206,7 +214,8 @@ void UseDigitSeparatorCheck::check(const MatchFinder::MatchResult &Result) {
// Get formatting literal text
const llvm::APFloat FloatValue = MatchedFloat->getValue();
- const std::string FormatedLiteralString = getFormatedFloatString(OriginalLiteralString, FloatValue);
+ const std::string FormatedLiteralString =
+ getFormatedFloatString(OriginalLiteralString, FloatValue);
// Compare the original and formatted representation of a literal
if (OriginalLiteralString != FormatedLiteralString) {
>From b3624ce38f7bd20f52494c405db60540fbf181f6 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 16:41:47 +0300
Subject: [PATCH 55/58] Formatting
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 37c3804c43be4f..34c3cc1822dd2a 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -41,8 +41,9 @@ splitStringByGroupSize(const std::basic_string<char> &String,
return Result;
}
-std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString,
- const llvm::APInt IntegerValue) {
+std::string
+getFormatedIntegerString(const llvm::StringRef OriginalLiteralString,
+ const llvm::APInt IntegerValue) {
// Configure formatting
unsigned int Radix;
size_t GroupSize;
@@ -81,8 +82,8 @@ std::string getFormatedIntegerString(const llvm::StringRef OriginalLiteralString
const std::string FormatedLiteralString =
Prefix +
std::accumulate(SplittedIntegerLiteral.begin(),
- SplittedIntegerLiteral.end(),std::string(""),
- [](std::basic_string<char> S1,
+ SplittedIntegerLiteral.end(), std::string(""),
+ [](std::basic_string<char> S1,
std::basic_string<char> S2) { return S1 + "\'" + S2; })
.erase(0, 1) +
Postfix;
>From 93f18850b8bfbfc2caf657a0c064e47c9b919910 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 17:07:16 +0300
Subject: [PATCH 56/58] Documentation
---
.../checks/modernize/use-digit-separator.rst | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-digit-separator.rst
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-digit-separator.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-digit-separator.rst
new file mode 100644
index 00000000000000..0a895fda4e2e51
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-digit-separator.rst
@@ -0,0 +1,14 @@
+.. title:: clang-tidy - modernize-use-digit-separator
+
+modernize-use-digit-separator
+=============================
+
+The check 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;
>From 004f02ea45d465972a9c336df7046a20d1c9eb6a Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Thu, 21 Dec 2023 17:54:59 +0300
Subject: [PATCH 57/58] Add include sstream
---
.../clang-tidy/modernize/UseDigitSeparatorCheck.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
index 34c3cc1822dd2a..3f1d4b0e7c8ef6 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDigitSeparatorCheck.cpp
@@ -10,6 +10,7 @@
#include <iomanip>
#include <numeric>
#include <regex>
+#include <sstream>
#include "UseDigitSeparatorCheck.h"
#include "clang/AST/ASTContext.h"
>From dd09ad2abf5f5abad5da73bc89c065530d2f7e06 Mon Sep 17 00:00:00 2001
From: Ivan Arkhipov <arkhipov.iv99 at mail.ru>
Date: Tue, 26 Dec 2023 12:58:57 +0300
Subject: [PATCH 58/58] Fix some tests
---
.../modernize/use-digit-separator.cpp | 57 ++++++++-----------
1 file changed, 25 insertions(+), 32 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
index ec4bdaecfa87de..bebeac52f4251f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-digit-separator.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s modernize-use-digit-separator %t
+// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-use-digit-separator %t
// Long not formatted literals
@@ -194,37 +194,30 @@ float PostfixScientificShortFloat3 = 1.23e-10F;
// Formatted literals
-// FIXME: clang-diagnostic-error like this for the following tests
-// clang-18 and clang-tidy don't produce such error
-// error: expected ';' after top level declarator [clang-diagnostic-error]
-// 204 | int FormattedInteger = 1'234'567;
-// | ^
-// | ;
-
-//int FormattedInteger = 1'234'567;
-//int MinusFormattedInteger = -1'234'567;
-//int BinaryFormattedInteger = 0b1110'1101;
-//int MinusBinaryFormattedInteger = -0b1110'1101;
-//int OctFormattedInteger = 037'512;
-//int MinusOctFormattedInteger = -037'512;
-//int HexFormattedInteger = 0x4'f356;
-//int MinusHexFormattedInteger = -0x4'f356;
-//unsigned int UnsignedFormattedInteger = 10'004U;
-//unsigned int MinusUnsignedFormattedInteger = -10'004U;
-//unsigned int UnsignedFormattedInteger1 = 100'045u;
-//unsigned int MinusUnsignedFormattedInteger1 = -100'045u;
-//long LongFormattedInteger = 123'456'789'101'112L;
-//long MinusLongFormattedInteger = -123'456'789'101'112L;
-//long LongFormattedInteger1 = 12'345'678'910'111'213l;
-//long MinusLongFormattedInteger1 = -12'345'678'910'111'213l;
-//unsigned long UnsignedLongFormattedInteger1 = 12'345'678'910'111'213Ul;
-//unsigned long MinusUnsignedLongFormattedInteger1 = -12'345'678'910'111'213Ul;
-//float FormattedFloat = 1'234.567'89;
-//float MinusFormattedFloat = -1'234.567'89;
-//double PostfixFormattedFloat = 1'234.569F;
-//double MinusPostfixFormattedFloat = -1'234.569F;
-//double PostfixFormattedFloat1 = 1'234.569f;
-//double MinusPostfixFormattedFloat1 = -1'234.569f;
+int FormattedInteger = 1'234'567;
+int MinusFormattedInteger = -1'234'567;
+int BinaryFormattedInteger = 0b1110'1101;
+int MinusBinaryFormattedInteger = -0b1110'1101;
+int OctFormattedInteger = 037'512;
+int MinusOctFormattedInteger = -037'512;
+int HexFormattedInteger = 0x4'F356;
+int MinusHexFormattedInteger = -0x4'F356;
+unsigned int UnsignedFormattedInteger = 10'004U;
+unsigned int MinusUnsignedFormattedInteger = -10'004U;
+unsigned int UnsignedFormattedInteger1 = 100'045u;
+unsigned int MinusUnsignedFormattedInteger1 = -100'045u;
+long LongFormattedInteger = 123'456'789'101'112L;
+long MinusLongFormattedInteger = -123'456'789'101'112L;
+long LongFormattedInteger1 = 12'345'678'910'111'213l;
+long MinusLongFormattedInteger1 = -12'345'678'910'111'213l;
+unsigned long UnsignedLongFormattedInteger1 = 12'345'678'910'111'213Ul;
+unsigned long MinusUnsignedLongFormattedInteger1 = -12'345'678'910'111'213Ul;
+float FormattedFloat = 1'234.567'89;
+float MinusFormattedFloat = -1'234.567'89;
+double PostfixFormattedFloat = 1'234.569F;
+double MinusPostfixFormattedFloat = -1'234.569F;
+double PostfixFormattedFloat1 = 1'234.569f;
+double MinusPostfixFormattedFloat1 = -1'234.569f;
//double ScientificFormattedFloat = 1.234'567'8E10;
//double MinusScientificFormattedFloat = -1.234'567'8E10;
//double ScientificFormattedFloat1 = 1.234'567'8e10;
More information about the cfe-commits
mailing list