Skip to content

[clang-format] Add an option to format integer and float literal case #151590

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5076,6 +5076,113 @@ the configuration (without a prefix: ``Auto``).

For example: TESTSUITE

.. _NumericLiteralCase:

**NumericLiteralCase** (``NumericLiteralCaseStyle``) :versionbadge:`clang-format 22` :ref:`¶ <NumericLiteralCase>`
Capitalization style for numeric literal constants.

Nested configuration flags:

Separate control for each numeric literal component.

* ``NumericLiteralComponentStyle ExponentLetter``
Format floating point exponent separator letter case.

.. code-block:: c++

/* ExponentLetter = Leave */
float a = 6.02e23 + 1.0E10;
/* ExponentLetter = Upper */
float a = 6.02E23 + 1.0E10;
/* ExponentLetter = Lower */
float a = 6.02e23 + 1.0e10;

Possible values:

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

* ``NLCS_Upper`` (in configuration: ``Upper``)
Format this component with upper case characters.

* ``NLCS_Lower`` (in configuration: ``Lower``)
Format this component with lower case characters.


* ``NumericLiteralComponentStyle HexDigit``
Format hexadecimal digit case.

.. code-block:: c++

/* HexDigit = Leave */
a = 0xaBcDeF;
/* HexDigit = Upper */
a = 0xABCDEF;
/* HexDigit = Lower */
a = 0xabcdef;

Possible values:

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

* ``NLCS_Upper`` (in configuration: ``Upper``)
Format this component with upper case characters.

* ``NLCS_Lower`` (in configuration: ``Lower``)
Format this component with lower case characters.


* ``NumericLiteralComponentStyle Prefix``
Format integer prefix case.

.. code-block:: c++

/* Prefix = Leave */
a = 0XF0 | 0b1;
/* Prefix = Upper */
a = 0XF0 | 0B1;
/* Prefix = Lower */
a = 0xF0 | 0b1;

Possible values:

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

* ``NLCS_Upper`` (in configuration: ``Upper``)
Format this component with upper case characters.

* ``NLCS_Lower`` (in configuration: ``Lower``)
Format this component with lower case characters.


* ``NumericLiteralComponentStyle Suffix``
Format suffix case. This option excludes case-sensitive reserved
suffixes, such as ``min`` in C++.

.. code-block:: c++

/* Suffix = Leave */
a = 1uLL;
/* Suffix = Upper */
a = 1ULL;
/* Suffix = Lower */
a = 1ull;

Possible values:

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

* ``NLCS_Upper`` (in configuration: ``Upper``)
Format this component with upper case characters.

* ``NLCS_Lower`` (in configuration: ``Lower``)
Format this component with lower case characters.



.. _ObjCBinPackProtocolList:

**ObjCBinPackProtocolList** (``BinPackStyle``) :versionbadge:`clang-format 7` :ref:`¶ <ObjCBinPackProtocolList>`
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ AST Matchers

clang-format
------------
- Add ``NumericLiteralCase`` option for enforcing character case in numeric
literals.

libclang
--------
Expand Down
69 changes: 69 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3558,6 +3558,74 @@ struct FormatStyle {
/// \version 9
std::vector<std::string> NamespaceMacros;

/// Control over each component in a numeric literal.
enum NumericLiteralComponentStyle : int8_t {
/// Leave this component of the literal as is.
NLCS_Leave,
/// Format this component with upper case characters.
NLCS_Upper,
/// Format this component with lower case characters.
NLCS_Lower,
};

/// Separate control for each numeric literal component.
struct NumericLiteralCaseStyle {
/// Format floating point exponent separator letter case.
/// \code
/// /* ExponentLetter = Leave */
/// float a = 6.02e23 + 1.0E10;
/// /* ExponentLetter = Upper */
/// float a = 6.02E23 + 1.0E10;
/// /* ExponentLetter = Lower */
/// float a = 6.02e23 + 1.0e10;
/// \endcode
NumericLiteralComponentStyle ExponentLetter;
/// Format hexadecimal digit case.
/// \code
/// /* HexDigit = Leave */
/// a = 0xaBcDeF;
/// /* HexDigit = Upper */
/// a = 0xABCDEF;
/// /* HexDigit = Lower */
/// a = 0xabcdef;
/// \endcode
NumericLiteralComponentStyle HexDigit;
/// Format integer prefix case.
/// \code
/// /* Prefix = Leave */
/// a = 0XF0 | 0b1;
/// /* Prefix = Upper */
/// a = 0XF0 | 0B1;
/// /* Prefix = Lower */
/// a = 0xF0 | 0b1;
/// \endcode
NumericLiteralComponentStyle Prefix;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is BaseLetter better? I have no strong feeling either way. WDYT @HazardyKnusperkeks ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd stick with Prefix.

/// Format suffix case. This option excludes case-sensitive reserved
/// suffixes, such as ``min`` in C++.
/// \code
/// /* Suffix = Leave */
/// a = 1uLL;
/// /* Suffix = Upper */
/// a = 1ULL;
/// /* Suffix = Lower */
/// a = 1ull;
/// \endcode
NumericLiteralComponentStyle Suffix;

bool operator==(const NumericLiteralCaseStyle &R) const {
return ExponentLetter == R.ExponentLetter && HexDigit == R.HexDigit &&
Prefix == R.Prefix && Suffix == R.Suffix;
}

bool operator!=(const NumericLiteralCaseStyle &R) const {
return !(*this == R);
}
};

/// Capitalization style for numeric literal constants.
/// \version 22
NumericLiteralCaseStyle NumericLiteralCase;

/// Controls bin-packing Objective-C protocol conformance list
/// items into as few lines as possible when they go over ``ColumnLimit``.
///
Expand Down Expand Up @@ -5438,6 +5506,7 @@ struct FormatStyle {
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
NamespaceIndentation == R.NamespaceIndentation &&
NamespaceMacros == R.NamespaceMacros &&
NumericLiteralCase == R.NumericLiteralCase &&
ObjCBinPackProtocolList == R.ObjCBinPackProtocolList &&
ObjCBlockIndentWidth == R.ObjCBlockIndentWidth &&
ObjCBreakBeforeNestedBlockParam ==
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Format/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_clang_library(clangFormat
MatchFilePath.cpp
NamespaceEndCommentsFixer.cpp
NumericLiteralInfo.cpp
NumericLiteralCaseFixer.cpp
ObjCPropertyAttributeOrderFixer.cpp
QualifierAlignmentFixer.cpp
SortJavaScriptImports.cpp
Expand Down
35 changes: 33 additions & 2 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "DefinitionBlockSeparator.h"
#include "IntegerLiteralSeparatorFixer.h"
#include "NamespaceEndCommentsFixer.h"
#include "NumericLiteralCaseFixer.h"
#include "ObjCPropertyAttributeOrderFixer.h"
#include "QualifierAlignmentFixer.h"
#include "SortJavaScriptImports.h"
Expand Down Expand Up @@ -472,6 +473,25 @@ struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
}
};

template <>
struct ScalarEnumerationTraits<FormatStyle::NumericLiteralComponentStyle> {
static void enumeration(IO &IO,
FormatStyle::NumericLiteralComponentStyle &Value) {
IO.enumCase(Value, "Leave", FormatStyle::NLCS_Leave);
IO.enumCase(Value, "Upper", FormatStyle::NLCS_Upper);
IO.enumCase(Value, "Lower", FormatStyle::NLCS_Lower);
}
};

template <> struct MappingTraits<FormatStyle::NumericLiteralCaseStyle> {
static void mapping(IO &IO, FormatStyle::NumericLiteralCaseStyle &Base) {
IO.mapOptional("ExponentLetter", Base.ExponentLetter);
IO.mapOptional("HexDigit", Base.HexDigit);
IO.mapOptional("Prefix", Base.Prefix);
IO.mapOptional("Suffix", Base.Suffix);
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::OperandAlignmentStyle> {
static void enumeration(IO &IO, FormatStyle::OperandAlignmentStyle &Value) {
IO.enumCase(Value, "DontAlign", FormatStyle::OAS_DontAlign);
Expand Down Expand Up @@ -1110,6 +1130,7 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
IO.mapOptional("NumericLiteralCase", Style.NumericLiteralCase);
IO.mapOptional("ObjCBinPackProtocolList", Style.ObjCBinPackProtocolList);
IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
IO.mapOptional("ObjCBreakBeforeNestedBlockParam",
Expand Down Expand Up @@ -1635,6 +1656,10 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
LLVMStyle.MaxEmptyLinesToKeep = 1;
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.NumericLiteralCase = {/*ExponentLetter=*/FormatStyle::NLCS_Leave,
/*HexDigit=*/FormatStyle::NLCS_Leave,
/*Prefix=*/FormatStyle::NLCS_Leave,
/*Suffix=*/FormatStyle::NLCS_Leave};
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
LLVMStyle.ObjCBlockIndentWidth = 2;
LLVMStyle.ObjCBreakBeforeNestedBlockParam = true;
Expand Down Expand Up @@ -3872,6 +3897,12 @@ reformat(const FormatStyle &Style, StringRef Code,
return IntegerLiteralSeparatorFixer().process(Env, Expanded);
});

if (NumericLiteralCaseFixer::isActive(Style)) {
Passes.emplace_back([&](const Environment &Env) {
return NumericLiteralCaseFixer().process(Env, Expanded);
});
}

if (Style.isCpp()) {
if (Style.QualifierAlignment != FormatStyle::QAS_Leave)
addQualifierAlignmentFixerPasses(Expanded, Passes);
Expand Down Expand Up @@ -3983,8 +4014,8 @@ reformat(const FormatStyle &Style, StringRef Code,

if (Style.QualifierAlignment != FormatStyle::QAS_Leave) {
// Don't make replacements that replace nothing. QualifierAlignment can
// produce them if one of its early passes changes e.g. `const volatile` to
// `volatile const` and then a later pass changes it back again.
// produce them if one of its early passes changes e.g. `const volatile`
// to `volatile const` and then a later pass changes it back again.
tooling::Replacements NonNoOpFixes;
for (const tooling::Replacement &Fix : Fixes) {
StringRef OriginalCode = Code.substr(Fix.getOffset(), Fix.getLength());
Expand Down
Loading
Loading