Skip to content

Commit

Permalink
clang-format: Correctly calculate affected ranges when sorting #inclu…
Browse files Browse the repository at this point in the history
…des.

affectedRanges takes a start and an end offset, not offset and length.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@280165 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
djasper-gh committed Aug 30, 2016
1 parent b69026b commit e129378
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,10 +1263,10 @@ static void sortCppIncludes(const FormatStyle &Style,
ArrayRef<tooling::Range> Ranges, StringRef FileName,
tooling::Replacements &Replaces, unsigned *Cursor) {
unsigned IncludesBeginOffset = Includes.front().Offset;
unsigned IncludesBlockSize = Includes.back().Offset +
Includes.back().Text.size() -
IncludesBeginOffset;
if (!affectsRange(Ranges, IncludesBeginOffset, IncludesBlockSize))
unsigned IncludesEndOffset =
Includes.back().Offset + Includes.back().Text.size();
unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset;
if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset))
return;
SmallVector<unsigned, 16> Indices;
for (unsigned i = 0, e = Includes.size(); i != e; ++i)
Expand Down
16 changes: 14 additions & 2 deletions unittests/Format/SortIncludesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class SortIncludesTest : public ::testing::Test {
return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
}

std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
auto Ranges = GetCodeRange(Code);
std::string sort(StringRef Code, std::vector<tooling::Range> Ranges,
StringRef FileName = "input.cc") {
auto Replaces = sortIncludes(Style, Code, Ranges, FileName);
Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
auto Sorted = applyAllReplacements(Code, Replaces);
Expand All @@ -36,6 +36,10 @@ class SortIncludesTest : public ::testing::Test {
return *Result;
}

std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
return sort(Code, GetCodeRange(Code), FileName);
}

unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
sortIncludes(Style, Code, GetCodeRange(Code), "input.cpp", &Cursor);
return Cursor;
Expand All @@ -52,6 +56,14 @@ TEST_F(SortIncludesTest, BasicSorting) {
sort("#include \"a.h\"\n"
"#include \"c.h\"\n"
"#include \"b.h\"\n"));

EXPECT_EQ("// comment\n"
"#include <a>\n"
"#include <b>\n",
sort("// comment\n"
"#include <b>\n"
"#include <a>\n",
{tooling::Range(25, 1)}));
}

TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
Expand Down

0 comments on commit e129378

Please sign in to comment.