MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: djasper, klimek, mprobst, reuk, JonasToth.
MyDeveloperDay added a project: clang-tools-extra.

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;
    }
  }


https://reviews.llvm.org/D59684

Files:
  clang/lib/Format/Format.cpp
  clang/unittests/Format/SortImportsTestJava.cpp


Index: clang/unittests/Format/SortImportsTestJava.cpp
===================================================================
--- clang/unittests/Format/SortImportsTestJava.cpp
+++ clang/unittests/Format/SortImportsTestJava.cpp
@@ -262,6 +262,21 @@
                  "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"
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1960,7 +1960,7 @@
 namespace {
 
 const char JavaImportRegexPattern[] =
-    "^[\t ]*import[\t ]*(static[\t ]*)?([^\t ]*)[\t ]*;";
+    "^[\t ]*import[\t ]+(static[\t ]*)?([^\t ]*)[\t ]*;";
 
 } // anonymous namespace
 


Index: clang/unittests/Format/SortImportsTestJava.cpp
===================================================================
--- clang/unittests/Format/SortImportsTestJava.cpp
+++ clang/unittests/Format/SortImportsTestJava.cpp
@@ -262,6 +262,21 @@
                  "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"
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1960,7 +1960,7 @@
 namespace {
 
 const char JavaImportRegexPattern[] =
-    "^[\t ]*import[\t ]*(static[\t ]*)?([^\t ]*)[\t ]*;";
+    "^[\t ]*import[\t ]+(static[\t ]*)?([^\t ]*)[\t ]*;";
 
 } // anonymous namespace
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to