clang-format: Only try to find the "main" include in the first block of

includes.

llvm-svn: 256153
This commit is contained in:
Daniel Jasper 2015-12-21 13:40:49 +00:00
parent 5da2f6cd03
commit 32d75fa293
2 changed files with 17 additions and 1 deletions

View File

@ -1814,6 +1814,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
FileName.endswith(".cxx") || FileName.endswith(".m") ||
FileName.endswith(".mm");
StringRef FileStem = llvm::sys::path::stem(FileName);
bool FirstIncludeBlock = true;
// Create pre-compiled regular expressions for the #include categories.
SmallVector<llvm::Regex, 4> CategoryRegexs;
@ -1843,7 +1844,8 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
break;
}
}
if (IsSource && Category > 0 && IncludeName.startswith("\"")) {
if (IsSource && Category > 0 && FirstIncludeBlock &&
IncludeName.startswith("\"")) {
StringRef HeaderStem =
llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
if (FileStem.startswith(HeaderStem))
@ -1854,6 +1856,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
Cursor);
IncludesInBlock.clear();
FirstIncludeBlock = false;
}
Prev = Pos + 1;
}

View File

@ -191,6 +191,19 @@ TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
"#include \"c.h\"\n"
"#include \"b.h\"\n",
"a.h"));
// Only do this in the first #include block.
EXPECT_EQ("#include <a>\n"
"\n"
"#include \"b.h\"\n"
"#include \"c.h\"\n"
"#include \"llvm/a.h\"\n",
sort("#include <a>\n"
"\n"
"#include \"llvm/a.h\"\n"
"#include \"c.h\"\n"
"#include \"b.h\"\n",
"a.cc"));
}
TEST_F(SortIncludesTest, NegativePriorities) {