Author: paulhoad Date: Sat Mar 30 06:05:40 2019 New Revision: 357345 URL: http://llvm.org/viewvc/llvm-project?rev=357345&view=rev Log: [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 Modified: cfe/trunk/lib/Format/Format.cpp cfe/trunk/unittests/Format/SortImportsTestJava.cpp Modified: cfe/trunk/lib/Format/Format.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=357345&r1=357344&r2=357345&view=diff ============================================================================== --- cfe/trunk/lib/Format/Format.cpp (original) +++ cfe/trunk/lib/Format/Format.cpp Sat Mar 30 06:05:40 2019 @@ -1983,7 +1983,7 @@ static void sortJavaImports(const Format namespace { const char JavaImportRegexPattern[] = - "^[\t ]*import[\t ]*(static[\t ]*)?([^\t ]*)[\t ]*;"; + "^[\t ]*import[\t ]+(static[\t ]*)?([^\t ]*)[\t ]*;"; } // anonymous namespace Modified: cfe/trunk/unittests/Format/SortImportsTestJava.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortImportsTestJava.cpp?rev=357345&r1=357344&r2=357345&view=diff ============================================================================== --- cfe/trunk/unittests/Format/SortImportsTestJava.cpp (original) +++ cfe/trunk/unittests/Format/SortImportsTestJava.cpp Sat Mar 30 06:05:40 2019 @@ -262,6 +262,21 @@ TEST_F(SortImportsTestJava, NoNewlineAtE "import org.a;")); } +TEST_F(SortImportsTestJava, ImportNamedFunction) { + EXPECT_EQ("import X;\n" + "class C {\n" + " void m() {\n" + " importFile();\n" + " }\n" + "}\n", + sort("import X;\n" + "class C {\n" + " void m() {\n" + " importFile();\n" + " }\n" + "}\n")); +} + TEST_F(SortImportsTestJava, NoReplacementsForValidImports) { // Identical #includes have led to a failure with an unstable sort. std::string Code = "import org.a;\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits