Skip to content

Commit

Permalink
[clang-format] [PR41187] moves Java import statements to the wrong lo…
Browse files Browse the repository at this point in the history
…cation 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
  • Loading branch information
mydeveloperday committed Mar 30, 2019
1 parent 08a940d commit 88335c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/Format.cpp
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/SortImportsTestJava.cpp
Expand Up @@ -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"
Expand Down

0 comments on commit 88335c2

Please sign in to comment.