Skip to content
Merged
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 @@ -5079,6 +5079,113 @@ the configuration (without a prefix: ``Auto``).

For example: TESTSUITE

.. _NumericLiteralCase:

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

Nested configuration flags:

Separate control for each numeric literal component.

For example, the config below will leave exponent letters alone, reformat
hexadecimal digits in lowercase, reformat numeric literal prefixes in
uppercase, and reformat suffixes in lowercase.

.. code-block:: c++

NumericLiteralCase:
ExponentLetter: Leave
HexDigit: Lower
Prefix: Upper
Suffix: Lower

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

.. code-block:: c++

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

Possible values:

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

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

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


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

.. code-block:: c++

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

Possible values:

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

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

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


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

.. code-block:: c++

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

Possible values:

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

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

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


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

.. code-block:: c++

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

Possible values:

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

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

* ``NLCS_Lower`` (in configuration: ``Lower``)
Format this component with lowercase 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 @@ -458,6 +458,8 @@ AST Matchers
clang-format
------------
- Add ``SpaceInEmptyBraces`` option and set it to ``Always`` for WebKit style.
- Add ``NumericLiteralCase`` option for enforcing character case in numeric
literals.

libclang
--------
Expand Down
68 changes: 68 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3558,6 +3558,73 @@ 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 uppercase characters.
NLCS_Upper,
/// Format this component with lowercase characters.
NLCS_Lower,
};

/// Separate control for each numeric literal component.
///
/// For example, the config below will leave exponent letters alone, reformat
/// hexadecimal digits in lowercase, reformat numeric literal prefixes in
/// uppercase, and reformat suffixes in lowercase.
/// \code
/// NumericLiteralCase:
/// ExponentLetter: Leave
/// HexDigit: Lower
/// Prefix: Upper
/// Suffix: Lower
/// \endcode
struct NumericLiteralCaseStyle {
/// Format floating point exponent separator letter case.
/// \code
/// float a = 6.02e23 + 1.0E10; // Leave
/// float a = 6.02E23 + 1.0E10; // Upper
/// float a = 6.02e23 + 1.0e10; // Lower
/// \endcode
NumericLiteralComponentStyle ExponentLetter;
/// Format hexadecimal digit case.
/// \code
/// a = 0xaBcDeF; // Leave
/// a = 0xABCDEF; // Upper
/// a = 0xabcdef; // Lower
/// \endcode
NumericLiteralComponentStyle HexDigit;
/// Format integer prefix case.
/// \code
/// a = 0XF0 | 0b1; // Leave
/// a = 0XF0 | 0B1; // Upper
/// a = 0xF0 | 0b1; // Lower
/// \endcode
NumericLiteralComponentStyle Prefix;
/// Format suffix case. This option excludes case-sensitive reserved
/// suffixes, such as ``min`` in C++.
/// \code
/// a = 1uLL; // Leave
/// a = 1ULL; // Upper
/// a = 1ull; // Lower
/// \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 literals.
/// \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 @@ -5469,6 +5536,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 @@ -13,6 +13,7 @@ add_clang_library(clangFormat
MacroExpander.cpp
MatchFilePath.cpp
NamespaceEndCommentsFixer.cpp
NumericLiteralCaseFixer.cpp
NumericLiteralInfo.cpp
ObjCPropertyAttributeOrderFixer.cpp
QualifierAlignmentFixer.cpp
Expand Down
29 changes: 29 additions & 0 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 &Value) {
IO.mapOptional("ExponentLetter", Value.ExponentLetter);
IO.mapOptional("HexDigit", Value.HexDigit);
IO.mapOptional("Prefix", Value.Prefix);
IO.mapOptional("Suffix", Value.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 @@ -1121,6 +1141,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 @@ -1653,6 +1674,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 @@ -3890,6 +3915,10 @@ reformat(const FormatStyle &Style, StringRef Code,
return IntegerLiteralSeparatorFixer().process(Env, Expanded);
});

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
Loading
Loading