[clang] [clang-format] Don't sort includes for C# (PR #129369)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 23:52:15 PST 2025


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/129369

Fixes #106194

>From 38f6c5e4509ba3420aeb69661c7646666d037bc0 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 28 Feb 2025 23:50:04 -0800
Subject: [PATCH] [clang-format] Don't sort includes for C#

Fixes #106194
---
 clang/lib/Format/Format.cpp                 | 3 ++-
 clang/unittests/Format/SortIncludesTest.cpp | 9 +++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 92678a031178a..c699a96d3f45e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3547,7 +3547,8 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
     return sortJavaScriptImports(Style, Code, Ranges, FileName);
   if (Style.Language == FormatStyle::LanguageKind::LK_Java)
     return sortJavaImports(Style, Code, Ranges, FileName, Replaces);
-  sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
+  if (Style.isCpp())
+    sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
   return Replaces;
 }
 
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index cb3f8c73a0487..f20862f5f7461 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -1085,6 +1085,15 @@ TEST_F(SortIncludesTest, DoNotSortLikelyXml) {
                     "input.h", 0));
 }
 
+TEST_F(SortIncludesTest, DoNotSortCSharp) {
+  constexpr StringRef Code{"const string expectedDataStruct = @\"\n"
+                           "            #include <b.h>\n"
+                           "            #include <a.h>\n"
+                           "        \";"};
+  FmtStyle.Language = FormatStyle::LK_CSharp;
+  EXPECT_TRUE(sortIncludes(FmtStyle, Code, GetCodeRange(Code), "a.cs").empty());
+}
+
 TEST_F(SortIncludesTest, DoNotOutputReplacementsForSortedBlocksWithRegrouping) {
   Style.IncludeBlocks = Style.IBS_Regroup;
   StringRef Code = "#include \"b.h\"\n"



More information about the cfe-commits mailing list