Skip to content

Commit b562309

Browse files
committed
fixup! [clang-format] Add an option to format integer literal case
1 parent 7a0eacb commit b562309

File tree

5 files changed

+139
-148
lines changed

5 files changed

+139
-148
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,103 +5083,103 @@ the configuration (without a prefix: ``Auto``).
50835083

50845084
Nested configuration flags:
50855085

5086-
Character case format for different components of a numeric literal.
5086+
Separate control for each numeric literal component.
50875087

5088-
* ``NumericLiteralComponentStyle UpperCaseFloatExponentSeparator``
5089-
Format floating point exponent separator character case.
5088+
* ``NumericLiteralComponentStyle ExponentLetter``
5089+
Format floating point exponent separator letter case.
50905090

5091-
.. code-block:: text
5091+
.. code-block:: c++
50925092

5093-
/* UpperCaseFloatExponentSeparator = Leave */
5093+
/* ExponentLetter = Leave */
50945094
float a = 6.02e23 + 1.0E10;
5095-
/* UpperCaseFloatExponentSeparator = Always */
5095+
/* ExponentLetter = Upper */
50965096
float a = 6.02E23 + 1.0E10;
5097-
/* UpperCaseFloatExponentSeparator = Never */
5097+
/* ExponentLetter = Lower */
50985098
float a = 6.02e23 + 1.0e10;
50995099
51005100
Possible values:
51015101

51025102
* ``NLCS_Leave`` (in configuration: ``Leave``)
51035103
Leave this component of the literal as is.
51045104

5105-
* ``NLCS_Always`` (in configuration: ``Always``)
5106-
Always format this component with upper case characters.
5105+
* ``NLCS_Upper`` (in configuration: ``Upper``)
5106+
Format this component with upper case characters.
51075107

5108-
* ``NLCS_Never`` (in configuration: ``Never``)
5109-
Never format this component with upper case characters.
5108+
* ``NLCS_Lower`` (in configuration: ``Lower``)
5109+
Format this component with lower case characters.
51105110

51115111

5112-
* ``NumericLiteralComponentStyle UpperCaseHexDigit``
5112+
* ``NumericLiteralComponentStyle HexDigit``
51135113
Format hexadecimal digit case.
51145114

5115-
.. code-block:: text
5115+
.. code-block:: c++
51165116

5117-
/* UpperCaseHexDigit = Leave */
5117+
/* HexDigit = Leave */
51185118
a = 0xaBcDeF;
5119-
/* UpperCaseHexDigit = Always */
5119+
/* HexDigit = Upper */
51205120
a = 0xABCDEF;
5121-
/* UpperCaseHexDigit = Never */
5121+
/* HexDigit = Lower */
51225122
a = 0xabcdef;
51235123
51245124
Possible values:
51255125

51265126
* ``NLCS_Leave`` (in configuration: ``Leave``)
51275127
Leave this component of the literal as is.
51285128

5129-
* ``NLCS_Always`` (in configuration: ``Always``)
5130-
Always format this component with upper case characters.
5129+
* ``NLCS_Upper`` (in configuration: ``Upper``)
5130+
Format this component with upper case characters.
51315131

5132-
* ``NLCS_Never`` (in configuration: ``Never``)
5133-
Never format this component with upper case characters.
5132+
* ``NLCS_Lower`` (in configuration: ``Lower``)
5133+
Format this component with lower case characters.
51345134

51355135

5136-
* ``NumericLiteralComponentStyle UpperCasePrefix``
5136+
* ``NumericLiteralComponentStyle Prefix``
51375137
Format integer prefix case.
51385138

5139-
.. code-block:: text
5139+
.. code-block:: c++
51405140

5141-
/* UpperCasePrefix = Leave */
5141+
/* Prefix = Leave */
51425142
a = 0XF0 | 0b1;
5143-
/* UpperCasePrefix = Always */
5143+
/* Prefix = Upper */
51445144
a = 0XF0 | 0B1;
5145-
/* UpperCasePrefix = Never */
5145+
/* Prefix = Lower */
51465146
a = 0xF0 | 0b1;
51475147
51485148
Possible values:
51495149

51505150
* ``NLCS_Leave`` (in configuration: ``Leave``)
51515151
Leave this component of the literal as is.
51525152

5153-
* ``NLCS_Always`` (in configuration: ``Always``)
5154-
Always format this component with upper case characters.
5153+
* ``NLCS_Upper`` (in configuration: ``Upper``)
5154+
Format this component with upper case characters.
51555155

5156-
* ``NLCS_Never`` (in configuration: ``Never``)
5157-
Never format this component with upper case characters.
5156+
* ``NLCS_Lower`` (in configuration: ``Lower``)
5157+
Format this component with lower case characters.
51585158

51595159

5160-
* ``NumericLiteralComponentStyle UpperCaseSuffix``
5161-
Format suffix case. This option excludes case-specific reserved
5160+
* ``NumericLiteralComponentStyle Suffix``
5161+
Format suffix case. This option excludes case-sensitive reserved
51625162
suffixes, such as ``min`` in C++.
51635163

5164-
.. code-block:: text
5164+
.. code-block:: c++
51655165

5166-
/* UpperCaseSuffix = Leave */
5166+
/* Suffix = Leave */
51675167
a = 1uLL;
5168-
/* UpperCaseSuffix = Always */
5168+
/* Suffix = Upper */
51695169
a = 1ULL;
5170-
/* UpperCaseSuffix = Never */
5170+
/* Suffix = Lower */
51715171
a = 1ull;
51725172
51735173
Possible values:
51745174

51755175
* ``NLCS_Leave`` (in configuration: ``Leave``)
51765176
Leave this component of the literal as is.
51775177

5178-
* ``NLCS_Always`` (in configuration: ``Always``)
5179-
Always format this component with upper case characters.
5178+
* ``NLCS_Upper`` (in configuration: ``Upper``)
5179+
Format this component with upper case characters.
51805180

5181-
* ``NLCS_Never`` (in configuration: ``Never``)
5182-
Never format this component with upper case characters.
5181+
* ``NLCS_Lower`` (in configuration: ``Lower``)
5182+
Format this component with lower case characters.
51835183

51845184

51855185

clang/include/clang/Format/Format.h

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3562,63 +3562,61 @@ struct FormatStyle {
35623562
enum NumericLiteralComponentStyle : int8_t {
35633563
/// Leave this component of the literal as is.
35643564
NLCS_Leave,
3565-
/// Always format this component with upper case characters.
3566-
NLCS_Always,
3567-
/// Never format this component with upper case characters.
3568-
NLCS_Never,
3565+
/// Format this component with upper case characters.
3566+
NLCS_Upper,
3567+
/// Format this component with lower case characters.
3568+
NLCS_Lower,
35693569
};
35703570

3571-
/// Character case format for different components of a numeric literal.
3571+
/// Separate control for each numeric literal component.
35723572
struct NumericLiteralCaseStyle {
3573-
/// Format floating point exponent separator character case.
3574-
/// \code{.text}
3575-
/// /* UpperCaseFloatExponentSeparator = Leave */
3573+
/// Format floating point exponent separator letter case.
3574+
/// \code
3575+
/// /* ExponentLetter = Leave */
35763576
/// float a = 6.02e23 + 1.0E10;
3577-
/// /* UpperCaseFloatExponentSeparator = Always */
3577+
/// /* ExponentLetter = Upper */
35783578
/// float a = 6.02E23 + 1.0E10;
3579-
/// /* UpperCaseFloatExponentSeparator = Never */
3579+
/// /* ExponentLetter = Lower */
35803580
/// float a = 6.02e23 + 1.0e10;
35813581
/// \endcode
3582-
NumericLiteralComponentStyle UpperCaseFloatExponentSeparator;
3582+
NumericLiteralComponentStyle ExponentLetter;
35833583
/// Format hexadecimal digit case.
3584-
/// \code{.text}
3585-
/// /* UpperCaseHexDigit = Leave */
3584+
/// \code
3585+
/// /* HexDigit = Leave */
35863586
/// a = 0xaBcDeF;
3587-
/// /* UpperCaseHexDigit = Always */
3587+
/// /* HexDigit = Upper */
35883588
/// a = 0xABCDEF;
3589-
/// /* UpperCaseHexDigit = Never */
3589+
/// /* HexDigit = Lower */
35903590
/// a = 0xabcdef;
35913591
/// \endcode
3592-
NumericLiteralComponentStyle UpperCaseHexDigit;
3592+
NumericLiteralComponentStyle HexDigit;
35933593
/// Format integer prefix case.
3594-
/// \code{.text}
3595-
/// /* UpperCasePrefix = Leave */
3594+
/// \code
3595+
/// /* Prefix = Leave */
35963596
/// a = 0XF0 | 0b1;
3597-
/// /* UpperCasePrefix = Always */
3597+
/// /* Prefix = Upper */
35983598
/// a = 0XF0 | 0B1;
3599-
/// /* UpperCasePrefix = Never */
3599+
/// /* Prefix = Lower */
36003600
/// a = 0xF0 | 0b1;
36013601
/// \endcode
3602-
NumericLiteralComponentStyle UpperCasePrefix;
3603-
/// Format suffix case. This option excludes case-specific reserved
3602+
NumericLiteralComponentStyle Prefix;
3603+
/// Format suffix case. This option excludes case-sensitive reserved
36043604
/// suffixes, such as ``min`` in C++.
3605-
/// \code{.text}
3606-
/// /* UpperCaseSuffix = Leave */
3605+
/// \code
3606+
/// /* Suffix = Leave */
36073607
/// a = 1uLL;
3608-
/// /* UpperCaseSuffix = Always */
3608+
/// /* Suffix = Upper */
36093609
/// a = 1ULL;
3610-
/// /* UpperCaseSuffix = Never */
3610+
/// /* Suffix = Lower */
36113611
/// a = 1ull;
36123612
/// \endcode
3613-
NumericLiteralComponentStyle UpperCaseSuffix;
3613+
NumericLiteralComponentStyle Suffix;
36143614

36153615
bool operator==(const NumericLiteralCaseStyle &R) const {
3616-
return UpperCaseFloatExponentSeparator ==
3617-
R.UpperCaseFloatExponentSeparator &&
3618-
UpperCaseHexDigit == R.UpperCaseHexDigit &&
3619-
UpperCasePrefix == R.UpperCasePrefix &&
3620-
UpperCaseSuffix == R.UpperCaseSuffix;
3616+
return ExponentLetter == R.ExponentLetter && HexDigit == R.HexDigit &&
3617+
Prefix == R.Prefix && Suffix == R.Suffix;
36213618
}
3619+
36223620
bool operator!=(const NumericLiteralCaseStyle &R) const {
36233621
return !(*this == R);
36243622
}

clang/lib/Format/Format.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -477,19 +477,18 @@ template <>
477477
struct ScalarEnumerationTraits<FormatStyle::NumericLiteralComponentStyle> {
478478
static void enumeration(IO &IO,
479479
FormatStyle::NumericLiteralComponentStyle &Value) {
480-
IO.enumCase(Value, "Leave", FormatStyle::NLCS_Leave);
481-
IO.enumCase(Value, "Always", FormatStyle::NLCS_Always);
482-
IO.enumCase(Value, "Never", FormatStyle::NLCS_Never);
480+
IO.enumCase(Value, "Leave", FormatStyle::NLCS_Leave);
481+
IO.enumCase(Value, "Upper", FormatStyle::NLCS_Upper);
482+
IO.enumCase(Value, "Lower", FormatStyle::NLCS_Lower);
483483
}
484484
};
485485

486486
template <> struct MappingTraits<FormatStyle::NumericLiteralCaseStyle> {
487487
static void mapping(IO &IO, FormatStyle::NumericLiteralCaseStyle &Base) {
488-
IO.mapOptional("UpperCaseFloatExponentSeparatorCase",
489-
Base.UpperCaseFloatExponentSeparator);
490-
IO.mapOptional("UpperCaseHexDigit", Base.UpperCaseHexDigit);
491-
IO.mapOptional("UpperCasePrefix", Base.UpperCasePrefix);
492-
IO.mapOptional("UpperCaseSuffix", Base.UpperCaseSuffix);
488+
IO.mapOptional("ExponentLetter", Base.ExponentLetter);
489+
IO.mapOptional("HexDigit", Base.HexDigit);
490+
IO.mapOptional("Prefix", Base.Prefix);
491+
IO.mapOptional("Suffix", Base.Suffix);
493492
}
494493
};
495494

@@ -1657,11 +1656,10 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16571656
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
16581657
LLVMStyle.MaxEmptyLinesToKeep = 1;
16591658
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
1660-
LLVMStyle.NumericLiteralCase = {
1661-
/*UpperCaseFloatExponentSeparator=*/FormatStyle::NLCS_Leave,
1662-
/*UpperCaseHexDigit=*/FormatStyle::NLCS_Leave,
1663-
/*UpperCasePrefix=*/FormatStyle::NLCS_Leave,
1664-
/*UpperCaseSuffix=*/FormatStyle::NLCS_Leave};
1659+
LLVMStyle.NumericLiteralCase = {/*ExponentLetter=*/FormatStyle::NLCS_Leave,
1660+
/*HexDigit=*/FormatStyle::NLCS_Leave,
1661+
/*Prefix=*/FormatStyle::NLCS_Leave,
1662+
/*Suffix=*/FormatStyle::NLCS_Leave};
16651663
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
16661664
LLVMStyle.ObjCBlockIndentWidth = 2;
16671665
LLVMStyle.ObjCBreakBeforeNestedBlockParam = true;

clang/lib/Format/NumericLiteralCaseFixer.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using CharTransformFn = char (*)(char C);
2525
namespace {
2626

2727
/// @brief Collection of std::transform predicates for each part of a numeric
28-
/// literal
28+
/// literal.
2929
struct FormatParameters {
3030
FormatParameters(FormatStyle::LanguageKind Language,
3131
const FormatStyle::NumericLiteralCaseStyle &CaseStyle);
@@ -74,11 +74,12 @@ class QuickNumericalConstantParser {
7474

7575
static char noOpTransform(char C) { return C; }
7676

77-
static CharTransformFn getTransform(int8_t config_value) {
78-
switch (config_value) {
79-
case FormatStyle::NLCS_Always:
77+
static CharTransformFn
78+
getTransform(FormatStyle::NumericLiteralComponentStyle ConfigValue) {
79+
switch (ConfigValue) {
80+
case FormatStyle::NLCS_Upper:
8081
return llvm::toUpper;
81-
case FormatStyle::NLCS_Never:
82+
case FormatStyle::NLCS_Lower:
8283
return llvm::toLower;
8384
case FormatStyle::NLCS_Leave:
8485
default:
@@ -89,11 +90,15 @@ static CharTransformFn getTransform(int8_t config_value) {
8990
/// @brief Test if Suffix matches a C++ literal reserved by the library.
9091
/// Matches against all suffixes reserved in the C++23 standard
9192
static bool matchesReservedSuffix(StringRef Suffix) {
92-
static const std::array<StringRef, 11> SortedReservedSuffixes = {
93+
static constexpr std::array<StringRef, 11> SortedReservedSuffixes = {
9394
"d", "h", "i", "if", "il", "min", "ms", "ns", "s", "us", "y"};
9495

95-
auto entry = std::lower_bound(SortedReservedSuffixes.cbegin(),
96-
SortedReservedSuffixes.cend(), Suffix);
96+
// This can be static_assert when we have access to constexpr is_sorted in
97+
// C++ 20.
98+
assert(llvm::is_sorted(SortedReservedSuffixes) &&
99+
"Must be sorted as precondition for lower_bound().");
100+
101+
auto entry = llvm::lower_bound(SortedReservedSuffixes, Suffix);
97102
if (entry == SortedReservedSuffixes.cend())
98103
return false;
99104
return *entry == Suffix;
@@ -102,11 +107,10 @@ static bool matchesReservedSuffix(StringRef Suffix) {
102107
FormatParameters::FormatParameters(
103108
FormatStyle::LanguageKind Language,
104109
const FormatStyle::NumericLiteralCaseStyle &CaseStyle)
105-
: Prefix(getTransform(CaseStyle.UpperCasePrefix)),
106-
HexDigit(getTransform(CaseStyle.UpperCaseHexDigit)),
107-
FloatExponentSeparator(
108-
getTransform(CaseStyle.UpperCaseFloatExponentSeparator)),
109-
Suffix(getTransform(CaseStyle.UpperCaseSuffix)) {
110+
: Prefix(getTransform(CaseStyle.Prefix)),
111+
HexDigit(getTransform(CaseStyle.HexDigit)),
112+
FloatExponentSeparator(getTransform(CaseStyle.ExponentLetter)),
113+
Suffix(getTransform(CaseStyle.Suffix)) {
110114
switch (Language) {
111115
case FormatStyle::LK_CSharp:
112116
case FormatStyle::LK_Java:
@@ -203,9 +207,7 @@ void QuickNumericalConstantParser::parse() {
203207
FloatExponentSeparatorEnd = Cur;
204208

205209
// Fast forward through the exponent part of a floating point literal.
206-
if (!IsFloat) {
207-
} else if (FloatExponentSeparatorBegin == FloatExponentSeparatorEnd) {
208-
} else {
210+
if (IsFloat && FloatExponentSeparatorBegin != FloatExponentSeparatorEnd) {
209211
Cur = std::find_if_not(Cur, End, [](char C) {
210212
return llvm::isDigit(C) || C == '+' || C == '-';
211213
});
@@ -263,9 +265,10 @@ std::optional<std::string> QuickNumericalConstantParser::formatIfNeeded() && {
263265
parse();
264266
applyFormatting();
265267

266-
return (Formatted == IntegerLiteral)
267-
? std::nullopt
268-
: std::make_optional<std::string>(std::move(Formatted));
268+
if (Formatted == IntegerLiteral)
269+
return std::nullopt;
270+
else
271+
return std::move(Formatted);
269272
}
270273

271274
std::pair<tooling::Replacements, unsigned>

0 commit comments

Comments
 (0)