[clang] [clang-format] Add an option to format numeric literal case (PR #151590)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 12 01:07:10 PDT 2025
================
@@ -0,0 +1,176 @@
+//===--- NumericLiteralCaseFixer.cpp ----------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements NumericLiteralCaseFixer that standardizes character
+/// case within numeric literals.
+///
+//===----------------------------------------------------------------------===//
+
+#include "NumericLiteralCaseFixer.h"
+#include "NumericLiteralInfo.h"
+
+#include "llvm/ADT/StringExtras.h"
+
+#include <algorithm>
+
+namespace clang {
+namespace format {
+
+static bool isNumericLiteralCaseFixerNeeded(const FormatStyle &Style) {
+ // Check if language is supported.
+ switch (Style.Language) {
+ case FormatStyle::LK_C:
+ case FormatStyle::LK_Cpp:
+ case FormatStyle::LK_ObjC:
+ case FormatStyle::LK_CSharp:
+ case FormatStyle::LK_Java:
+ case FormatStyle::LK_JavaScript:
+ break;
+ default:
+ return false;
+ }
+
+ // Check if style options are set.
+ const auto &Option = Style.NumericLiteralCase;
+ const auto Leave = FormatStyle::NLCS_Leave;
+ return Option.Prefix != Leave || Option.HexDigit != Leave ||
+ Option.ExponentLetter != Leave || Option.Suffix != Leave;
+}
+
+static std::string
+transformComponent(StringRef Component,
+ FormatStyle::NumericLiteralComponentStyle ConfigValue) {
+ switch (ConfigValue) {
+ case FormatStyle::NLCS_Upper:
+ return Component.upper();
+ case FormatStyle::NLCS_Lower:
+ return Component.lower();
+ default:
+ // Covers FormatStyle::NLCS_Leave.
+ return Component.str();
+ }
+}
+
+/// Test if Suffix matches a C++ literal reserved by the library.
+/// Matches against all suffixes reserved in the C++23 standard.
+static bool matchesReservedSuffix(StringRef Suffix) {
+ static constexpr std::array<StringRef, 11> SortedReservedSuffixes = {
+ "d", "h", "i", "if", "il", "min", "ms", "ns", "s", "us", "y"};
----------------
owenca wrote:
```suggestion
"d", "h", "i", "if", "il", "min", "ms", "ns", "s", "us", "y",
};
```
https://github.com/llvm/llvm-project/pull/151590
More information about the cfe-commits
mailing list