Skip to content

[clang-format] Disable IntegerLiteralSeparator for C++ before c++14 #151273

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

Merged
merged 2 commits into from
Jul 30, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jul 30, 2025

Fixes #151102

@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #151102


Full diff: https://github.com/llvm/llvm-project/pull/151273.diff

2 Files Affected:

  • (modified) clang/lib/Format/IntegerLiteralSeparatorFixer.cpp (+7-4)
  • (modified) clang/unittests/Format/IntegerLiteralSeparatorTest.cpp (+3)
diff --git a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
index 80487fa673bf0..7772a5619c4bd 100644
--- a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
+++ b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
@@ -45,15 +45,18 @@ std::pair<tooling::Replacements, unsigned>
 IntegerLiteralSeparatorFixer::process(const Environment &Env,
                                       const FormatStyle &Style) {
   switch (Style.Language) {
-  case FormatStyle::LK_Cpp:
-  case FormatStyle::LK_ObjC:
-    Separator = '\'';
-    break;
   case FormatStyle::LK_CSharp:
   case FormatStyle::LK_Java:
   case FormatStyle::LK_JavaScript:
     Separator = '_';
     break;
+  case FormatStyle::LK_Cpp:
+  case FormatStyle::LK_ObjC:
+    if (Style.Standard >= FormatStyle::LS_Cpp14) {
+      Separator = '\'';
+      break;
+    }
+    [[fallthrough]];
   default:
     return {};
   }
diff --git a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
index 8681c3d2f89ce..53b6dd8efadff 100644
--- a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
+++ b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
@@ -83,6 +83,9 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) {
                "d = 5678_km;\n"
                "h = 0xDEF_u16;",
                Style);
+
+  Style.Standard = FormatStyle::LS_Cpp11;
+  verifyFormat("ld = 1234L;", Style);
 }
 
 TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) {

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Jul 30, 2025
@owenca owenca changed the title [clang-format] Disalbe IntegerLiteralSeparator for C++ before c++14 [clang-format] Disable IntegerLiteralSeparator for C++ before c++14 Jul 30, 2025
@owenca owenca merged commit 5fc482c into llvm:main Jul 30, 2025
11 of 12 checks passed
@owenca owenca deleted the 151102 branch July 30, 2025 16:43
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Jul 30, 2025
@owenca
Copy link
Contributor Author

owenca commented Jul 30, 2025

/cherry-pick 5fc482c

@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

/pull-request #151362

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

Clang format produces code which does not compile with clang
3 participants