[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
This commit is contained in:
Paul Hoad 2019-03-30 13:05:40 +00:00
parent 08a940d629
commit 88335c21a4
2 changed files with 16 additions and 1 deletions

View File

@ -1983,7 +1983,7 @@ static void sortJavaImports(const FormatStyle &Style,
namespace { namespace {
const char JavaImportRegexPattern[] = const char JavaImportRegexPattern[] =
"^[\t ]*import[\t ]*(static[\t ]*)?([^\t ]*)[\t ]*;"; "^[\t ]*import[\t ]+(static[\t ]*)?([^\t ]*)[\t ]*;";
} // anonymous namespace } // anonymous namespace

View File

@ -262,6 +262,21 @@ TEST_F(SortImportsTestJava, NoNewlineAtEnd) {
"import org.a;")); "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) { TEST_F(SortImportsTestJava, NoReplacementsForValidImports) {
// Identical #includes have led to a failure with an unstable sort. // Identical #includes have led to a failure with an unstable sort.
std::string Code = "import org.a;\n" std::string Code = "import org.a;\n"