Skip to content

Commit 88335c2

Browse files
[clang-format] [PR41187] moves Java import statements to the wrong location if code contains statements that start with the word import
Summary: Import sorting of java file, incorrectly move import statement to after a function beginning with the word import. Make 1 character change to regular expression to ensure there is always at least one space/tab after the word import Previously clang-format --style="LLVM" would format ``` import X; class C { void m() { importFile(); } } ``` as ``` class C { void m() { importFile(); import X; } } ``` Reviewers: djasper, klimek, reuk, JonasToth Reviewed By: klimek Subscribers: cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D59684 llvm-svn: 357345
1 parent 08a940d commit 88335c2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ static void sortJavaImports(const FormatStyle &Style,
19831983
namespace {
19841984

19851985
const char JavaImportRegexPattern[] =
1986-
"^[\t ]*import[\t ]*(static[\t ]*)?([^\t ]*)[\t ]*;";
1986+
"^[\t ]*import[\t ]+(static[\t ]*)?([^\t ]*)[\t ]*;";
19871987

19881988
} // anonymous namespace
19891989

clang/unittests/Format/SortImportsTestJava.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ TEST_F(SortImportsTestJava, NoNewlineAtEnd) {
262262
"import org.a;"));
263263
}
264264

265+
TEST_F(SortImportsTestJava, ImportNamedFunction) {
266+
EXPECT_EQ("import X;\n"
267+
"class C {\n"
268+
" void m() {\n"
269+
" importFile();\n"
270+
" }\n"
271+
"}\n",
272+
sort("import X;\n"
273+
"class C {\n"
274+
" void m() {\n"
275+
" importFile();\n"
276+
" }\n"
277+
"}\n"));
278+
}
279+
265280
TEST_F(SortImportsTestJava, NoReplacementsForValidImports) {
266281
// Identical #includes have led to a failure with an unstable sort.
267282
std::string Code = "import org.a;\n"

0 commit comments

Comments
 (0)