]> granicus.if.org Git - clang/commitdiff
[clang-format] [PR41187] moves Java import statements to the wrong location if code...
authorPaul Hoad <mydeveloperday@gmail.com>
Sat, 30 Mar 2019 13:05:40 +0000 (13:05 +0000)
committerPaul Hoad <mydeveloperday@gmail.com>
Sat, 30 Mar 2019 13:05:40 +0000 (13:05 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@357345 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/SortImportsTestJava.cpp

index da8523997612f9d940376bbdadf91186e9984b1a..119be9fae678990895630293767317a44f2e5f7f 100644 (file)
@@ -1983,7 +1983,7 @@ static void sortJavaImports(const FormatStyle &Style,
 namespace {
 
 const char JavaImportRegexPattern[] =
-    "^[\t ]*import[\t ]*(static[\t ]*)?([^\t ]*)[\t ]*;";
+    "^[\t ]*import[\t ]+(static[\t ]*)?([^\t ]*)[\t ]*;";
 
 } // anonymous namespace
 
index fbd349ca47f0f9742bbfca8db9f25779d032b318..d2826a2107cda3d2f24a7391db8eb27bde9562f7 100644 (file)
@@ -262,6 +262,21 @@ TEST_F(SortImportsTestJava, NoNewlineAtEnd) {
                  "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"