[clang] [clang-format] java import sorting should ignore imports in comments and text blocks (PR #177326)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 22 01:46:31 PST 2026
https://github.com/nataliakokoromyti updated https://github.com/llvm/llvm-project/pull/177326
>From e281c4373cbb56cdd226a8bf7f9af681564d5c08 Mon Sep 17 00:00:00 2001
From: nataliakokoromyti <nataliakokoromyti at gmail.com>
Date: Thu, 22 Jan 2026 01:35:54 -0800
Subject: [PATCH] [clang-format] java import sorting should ignore imports in
comments and text blocks
---
clang/lib/Format/Format.cpp | 30 ++++++++++++++++++-
.../unittests/Format/SortImportsTestJava.cpp | 26 ++++++++++++++++
2 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f0e9aff2fd21a..58dfae897ed0f 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3757,6 +3757,8 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
SmallVector<StringRef> AssociatedCommentLines;
bool FormattingOff = false;
+ bool InBlockComment = false;
+ bool InTextBlock = false;
for (;;) {
auto Pos = Code.find('\n', SearchFrom);
@@ -3769,7 +3771,33 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
else if (isClangFormatOn(Trimmed))
FormattingOff = false;
- if (ImportRegex.match(Line, &Matches)) {
+ // Track block comments (/* ... */)
+ // Check if we're starting a block comment on this line
+ bool IsBlockComment = false;
+ if (Trimmed.starts_with("/*")) {
+ IsBlockComment = true;
+ if (!Trimmed.contains("*/"))
+ InBlockComment = true;
+ }
+ // Check if we're ending a block comment that started on a previous line
+ if (InBlockComment && Trimmed.contains("*/")) {
+ InBlockComment = false;
+ IsBlockComment = true;
+ }
+ // If we're in a multi-line block comment (not the first or last line)
+ if (InBlockComment && !Trimmed.starts_with("/*"))
+ IsBlockComment = true;
+
+ // Track Java text blocks (""" ... """)
+ size_t Count = 0;
+ size_t StartPos = 0;
+ while ((StartPos = Trimmed.find("\"\"\"", StartPos)) != StringRef::npos) {
+ ++Count;
+ StartPos += 3;
+ }
+ if (Count % 2 == 1)
+ InTextBlock = !InTextBlock;
+ if (!IsBlockComment && !InTextBlock && ImportRegex.match(Line, &Matches)) {
if (FormattingOff) {
// If at least one import line has formatting turned off, turn off
// formatting entirely.
diff --git a/clang/unittests/Format/SortImportsTestJava.cpp b/clang/unittests/Format/SortImportsTestJava.cpp
index 26674c75e97b1..9af9d8860fe73 100644
--- a/clang/unittests/Format/SortImportsTestJava.cpp
+++ b/clang/unittests/Format/SortImportsTestJava.cpp
@@ -349,6 +349,32 @@ TEST_F(SortImportsTestJava, NoReplacementsForValidImportsWindows) {
sortIncludes(FmtStyle, Code, GetCodeRange(Code), "input.java").empty());
}
+TEST_F(SortImportsTestJava, DoNotSortImportsInBlockComment) {
+ EXPECT_EQ("/* import org.d;\n"
+ "import org.c;\n"
+ "import org.b; */\n"
+ "import org.a;",
+ sort("/* import org.d;\n"
+ "import org.c;\n"
+ "import org.b; */\n"
+ "import org.a;"));
+}
+
+TEST_F(SortImportsTestJava, DoNotSortImportsInTextBlock) {
+ EXPECT_EQ("String code = \"\"\"\n"
+ " import org.c;\n"
+ " \\\"\"\"\n"
+ " import org.b;\n"
+ "\\\\\"\"\";\n"
+ "import org.a;",
+ sort("String code = \"\"\"\n"
+ " import org.c;\n"
+ " \\\"\"\"\n"
+ " import org.b;\n"
+ "\\\\\"\"\";\n"
+ "import org.a;"));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang
More information about the cfe-commits
mailing list