Skip to content

Commit 7a0eacb

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

File tree

7 files changed

+326
-264
lines changed

7 files changed

+326
-264
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 83 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,6 +4555,7 @@ the configuration (without a prefix: ``Auto``).
45554555
So inserting a trailing comma counteracts bin-packing.
45564556

45574557

4558+
45584559
.. _IntegerLiteralSeparator:
45594560

45604561
**IntegerLiteralSeparator** (``IntegerLiteralSeparatorStyle``) :versionbadge:`clang-format 16` :ref:`<IntegerLiteralSeparator>`
@@ -5077,74 +5078,109 @@ the configuration (without a prefix: ``Auto``).
50775078

50785079
.. _NumericLiteralCase:
50795080

5080-
**NumericLiteralCase** (``NumericLiteralCaseStyle``) :versionbadge:`clang-format 21` :ref:`<NumericLiteralCase>`
5081-
Controls character case in numeric literals.
5081+
**NumericLiteralCase** (``NumericLiteralCaseStyle``) :versionbadge:`clang-format 22` :ref:`<NumericLiteralCase>`
5082+
Capitalization style for numeric literal constants.
50825083

5083-
Possible values for each nexted configuration flag:
5084+
Nested configuration flags:
50845085

5085-
* ``0`` (Default) Do not modify characters.
5086+
Character case format for different components of a numeric literal.
50865087

5087-
* ``-1`` Convert characters to lower case.
5088+
* ``NumericLiteralComponentStyle UpperCaseFloatExponentSeparator``
5089+
Format floating point exponent separator character case.
50885090

5089-
* ``1`` Convert characters to upper case.
5091+
.. code-block:: text
50905092
5091-
.. code-block:: yaml
5093+
/* UpperCaseFloatExponentSeparator = Leave */
5094+
float a = 6.02e23 + 1.0E10;
5095+
/* UpperCaseFloatExponentSeparator = Always */
5096+
float a = 6.02E23 + 1.0E10;
5097+
/* UpperCaseFloatExponentSeparator = Never */
5098+
float a = 6.02e23 + 1.0e10;
50925099
5093-
# Example of usage:
5094-
NumericLiteralCaseStyle:
5095-
PrefixCase: -1
5096-
HexDigitCase: 1
5097-
FloatExponentSeparatorCase: 0
5098-
SuffixCase: -1
5100+
Possible values:
50995101

5100-
.. code-block:: c++
5102+
* ``NLCS_Leave`` (in configuration: ``Leave``)
5103+
Leave this component of the literal as is.
51015104

5102-
// Lower case prefix, upper case hexadecimal digits, lower case suffix
5103-
unsigned int 0xDEAFBEEFull;
5105+
* ``NLCS_Always`` (in configuration: ``Always``)
5106+
Always format this component with upper case characters.
51045107

5105-
Nested configuration flags:
5108+
* ``NLCS_Never`` (in configuration: ``Never``)
5109+
Never format this component with upper case characters.
51065110

5107-
* ``int PrefixCase`` Control numeric constant prefix case.
51085111

5109-
.. code-block:: c++
5112+
* ``NumericLiteralComponentStyle UpperCaseHexDigit``
5113+
Format hexadecimal digit case.
51105114

5111-
// PrefixCase: 1
5112-
int a = 0B101 | 0XF0;
5113-
// PrefixCase: -1
5114-
int a = 0b101 | 0xF0;
5115-
// PrefixCase: 0
5116-
int c = 0b101 | 0XF0;
5115+
.. code-block:: text
51175116
5118-
* ``int HexDigitCase`` Control hexadecimal digit case.
5117+
/* UpperCaseHexDigit = Leave */
5118+
a = 0xaBcDeF;
5119+
/* UpperCaseHexDigit = Always */
5120+
a = 0xABCDEF;
5121+
/* UpperCaseHexDigit = Never */
5122+
a = 0xabcdef;
51195123
5120-
.. code-block:: c++
5124+
Possible values:
51215125

5122-
// HexDigitCase: 1
5123-
int a = 0xBEAD;
5124-
// PrefixCase: -1
5125-
int b = 0xbead;
5126-
// PrefixCase: 0
5127-
int c = 0xBeAd;
5126+
* ``NLCS_Leave`` (in configuration: ``Leave``)
5127+
Leave this component of the literal as is.
51285128

5129-
* ``int FloatExponentSeparatorCase`` Control exponent separator case.
5129+
* ``NLCS_Always`` (in configuration: ``Always``)
5130+
Always format this component with upper case characters.
51305131

5131-
.. code-block:: c++
5132+
* ``NLCS_Never`` (in configuration: ``Never``)
5133+
Never format this component with upper case characters.
51325134

5133-
// FloatExponentSeparatorCase: 1
5134-
float a = 6.02E+23;
5135-
// FloatExponentSeparatorCase: -1
5136-
float b = 6.02e+23;
51375135

5138-
* ``int SuffixCase`` Control suffix case.
5136+
* ``NumericLiteralComponentStyle UpperCasePrefix``
5137+
Format integer prefix case.
51395138

5140-
.. code-block:: c++
5139+
.. code-block:: text
5140+
5141+
/* UpperCasePrefix = Leave */
5142+
a = 0XF0 | 0b1;
5143+
/* UpperCasePrefix = Always */
5144+
a = 0XF0 | 0B1;
5145+
/* UpperCasePrefix = Never */
5146+
a = 0xF0 | 0b1;
5147+
5148+
Possible values:
5149+
5150+
* ``NLCS_Leave`` (in configuration: ``Leave``)
5151+
Leave this component of the literal as is.
5152+
5153+
* ``NLCS_Always`` (in configuration: ``Always``)
5154+
Always format this component with upper case characters.
5155+
5156+
* ``NLCS_Never`` (in configuration: ``Never``)
5157+
Never format this component with upper case characters.
5158+
5159+
5160+
* ``NumericLiteralComponentStyle UpperCaseSuffix``
5161+
Format suffix case. This option excludes case-specific reserved
5162+
suffixes, such as ``min`` in C++.
5163+
5164+
.. code-block:: text
5165+
5166+
/* UpperCaseSuffix = Leave */
5167+
a = 1uLL;
5168+
/* UpperCaseSuffix = Always */
5169+
a = 1ULL;
5170+
/* UpperCaseSuffix = Never */
5171+
a = 1ull;
5172+
5173+
Possible values:
5174+
5175+
* ``NLCS_Leave`` (in configuration: ``Leave``)
5176+
Leave this component of the literal as is.
5177+
5178+
* ``NLCS_Always`` (in configuration: ``Always``)
5179+
Always format this component with upper case characters.
5180+
5181+
* ``NLCS_Never`` (in configuration: ``Never``)
5182+
Never format this component with upper case characters.
51415183

5142-
// SuffixCase: 1
5143-
unsigned long long a = 1ULL;
5144-
// SuffixCase: -1
5145-
unsigned long long a = 1ull;
5146-
// SuffixCase: 0
5147-
unsigned long long c = 1uLL;
51485184

51495185

51505186
.. _ObjCBinPackProtocolList:

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ AST Matchers
230230

231231
clang-format
232232
------------
233-
- Add ``NumericLiteralCase`` option for for enforcing character case in
234-
numeric literals.
233+
- Add ``NumericLiteralCase`` option for enforcing character case in numeric
234+
literals.
235235

236236
libclang
237237
--------

clang/include/clang/Format/Format.h

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,54 +3100,6 @@ struct FormatStyle {
31003100
/// \version 11
31013101
TrailingCommaStyle InsertTrailingCommas;
31023102

3103-
/// Character case format for different components of a numeric literal.
3104-
///
3105-
/// For all options, ``0`` leave the case unchanged, ``-1``
3106-
/// uses lower case and, ``1`` uses upper case.
3107-
///
3108-
struct NumericLiteralCaseStyle {
3109-
/// Format numeric constant prefixes.
3110-
/// \code{.text}
3111-
/// /* -1: lower case */ b = 0x01;
3112-
/// /* 0: don't care */
3113-
/// /* 1: upper case */ b = 0X01;
3114-
/// \endcode
3115-
int8_t PrefixCase;
3116-
/// Format hexadecimal digit case.
3117-
/// \code{.text}
3118-
/// /* -1: lower case */ b = 0xabcdef;
3119-
/// /* 0: don't care */
3120-
/// /* 1: upper case */ b = 0xABCDEF;
3121-
/// \endcode
3122-
int8_t HexDigitCase;
3123-
/// Format exponent separator character case in floating point literals.
3124-
/// \code{.text}
3125-
/// /* -1: lower case */ b = 6.02e23;
3126-
/// /* 0: don't care */
3127-
/// /* 1: upper case */ b = 6.02E23;
3128-
/// \endcode
3129-
int8_t FloatExponentSeparatorCase;
3130-
/// Format suffix case. This option excludes case-specific reserved
3131-
/// suffixes, such as ``min`` in C++.
3132-
/// \code{.text}
3133-
/// /* -1: lower case */ b = 10u;
3134-
/// /* 0: don't care */
3135-
/// /* 1: upper case */ b = 10U;
3136-
/// \endcode
3137-
int8_t SuffixCase;
3138-
3139-
bool operator==(const NumericLiteralCaseStyle &R) const {
3140-
return PrefixCase == R.PrefixCase && HexDigitCase == R.HexDigitCase &&
3141-
FloatExponentSeparatorCase == R.FloatExponentSeparatorCase &&
3142-
SuffixCase == R.SuffixCase;
3143-
}
3144-
};
3145-
3146-
/// Format numeric literals for languages that support flexible character case
3147-
/// in numeric literal constants.
3148-
/// \version 22
3149-
NumericLiteralCaseStyle NumericLiteralCase;
3150-
31513103
/// Separator format of integer literals of different bases.
31523104
///
31533105
/// If negative, remove separators. If ``0``, leave the literal as is. If
@@ -3606,6 +3558,76 @@ struct FormatStyle {
36063558
/// \version 9
36073559
std::vector<std::string> NamespaceMacros;
36083560

3561+
/// Control over each component in a numeric literal.
3562+
enum NumericLiteralComponentStyle : int8_t {
3563+
/// Leave this component of the literal as is.
3564+
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,
3569+
};
3570+
3571+
/// Character case format for different components of a numeric literal.
3572+
struct NumericLiteralCaseStyle {
3573+
/// Format floating point exponent separator character case.
3574+
/// \code{.text}
3575+
/// /* UpperCaseFloatExponentSeparator = Leave */
3576+
/// float a = 6.02e23 + 1.0E10;
3577+
/// /* UpperCaseFloatExponentSeparator = Always */
3578+
/// float a = 6.02E23 + 1.0E10;
3579+
/// /* UpperCaseFloatExponentSeparator = Never */
3580+
/// float a = 6.02e23 + 1.0e10;
3581+
/// \endcode
3582+
NumericLiteralComponentStyle UpperCaseFloatExponentSeparator;
3583+
/// Format hexadecimal digit case.
3584+
/// \code{.text}
3585+
/// /* UpperCaseHexDigit = Leave */
3586+
/// a = 0xaBcDeF;
3587+
/// /* UpperCaseHexDigit = Always */
3588+
/// a = 0xABCDEF;
3589+
/// /* UpperCaseHexDigit = Never */
3590+
/// a = 0xabcdef;
3591+
/// \endcode
3592+
NumericLiteralComponentStyle UpperCaseHexDigit;
3593+
/// Format integer prefix case.
3594+
/// \code{.text}
3595+
/// /* UpperCasePrefix = Leave */
3596+
/// a = 0XF0 | 0b1;
3597+
/// /* UpperCasePrefix = Always */
3598+
/// a = 0XF0 | 0B1;
3599+
/// /* UpperCasePrefix = Never */
3600+
/// a = 0xF0 | 0b1;
3601+
/// \endcode
3602+
NumericLiteralComponentStyle UpperCasePrefix;
3603+
/// Format suffix case. This option excludes case-specific reserved
3604+
/// suffixes, such as ``min`` in C++.
3605+
/// \code{.text}
3606+
/// /* UpperCaseSuffix = Leave */
3607+
/// a = 1uLL;
3608+
/// /* UpperCaseSuffix = Always */
3609+
/// a = 1ULL;
3610+
/// /* UpperCaseSuffix = Never */
3611+
/// a = 1ull;
3612+
/// \endcode
3613+
NumericLiteralComponentStyle UpperCaseSuffix;
3614+
3615+
bool operator==(const NumericLiteralCaseStyle &R) const {
3616+
return UpperCaseFloatExponentSeparator ==
3617+
R.UpperCaseFloatExponentSeparator &&
3618+
UpperCaseHexDigit == R.UpperCaseHexDigit &&
3619+
UpperCasePrefix == R.UpperCasePrefix &&
3620+
UpperCaseSuffix == R.UpperCaseSuffix;
3621+
}
3622+
bool operator!=(const NumericLiteralCaseStyle &R) const {
3623+
return !(*this == R);
3624+
}
3625+
};
3626+
3627+
/// Capitalization style for numeric literal constants.
3628+
/// \version 22
3629+
NumericLiteralCaseStyle NumericLiteralCase;
3630+
36093631
/// Controls bin-packing Objective-C protocol conformance list
36103632
/// items into as few lines as possible when they go over ``ColumnLimit``.
36113633
///
@@ -5472,7 +5494,6 @@ struct FormatStyle {
54725494
IndentWrappedFunctionNames == R.IndentWrappedFunctionNames &&
54735495
InsertBraces == R.InsertBraces &&
54745496
InsertNewlineAtEOF == R.InsertNewlineAtEOF &&
5475-
NumericLiteralCase == R.NumericLiteralCase &&
54765497
IntegerLiteralSeparator == R.IntegerLiteralSeparator &&
54775498
JavaImportGroups == R.JavaImportGroups &&
54785499
JavaScriptQuotes == R.JavaScriptQuotes &&
@@ -5487,6 +5508,7 @@ struct FormatStyle {
54875508
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
54885509
NamespaceIndentation == R.NamespaceIndentation &&
54895510
NamespaceMacros == R.NamespaceMacros &&
5511+
NumericLiteralCase == R.NumericLiteralCase &&
54905512
ObjCBinPackProtocolList == R.ObjCBinPackProtocolList &&
54915513
ObjCBlockIndentWidth == R.ObjCBlockIndentWidth &&
54925514
ObjCBreakBeforeNestedBlockParam ==

0 commit comments

Comments
 (0)