From e12937859f69f56f7674fcdf8075cb34f87eca07 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 30 Aug 2016 21:33:41 +0000 Subject: [PATCH] clang-format: Correctly calculate affected ranges when sorting #includes. 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 --- lib/Format/Format.cpp | 8 ++++---- unittests/Format/SortIncludesTest.cpp | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index b7d3c55061..86f4ae1f19 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1263,10 +1263,10 @@ static void sortCppIncludes(const FormatStyle &Style, ArrayRef 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 Indices; for (unsigned i = 0, e = Includes.size(); i != e; ++i) diff --git a/unittests/Format/SortIncludesTest.cpp b/unittests/Format/SortIncludesTest.cpp index b6ee2ddf66..c3c56a8130 100644 --- a/unittests/Format/SortIncludesTest.cpp +++ b/unittests/Format/SortIncludesTest.cpp @@ -24,8 +24,8 @@ protected: return std::vector(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 Ranges, + StringRef FileName = "input.cc") { auto Replaces = sortIncludes(Style, Code, Ranges, FileName); Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges); auto Sorted = applyAllReplacements(Code, Replaces); @@ -36,6 +36,10 @@ protected: 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; @@ -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 \n" + "#include \n", + sort("// comment\n" + "#include \n" + "#include \n", + {tooling::Range(25, 1)})); } TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) { -- 2.40.0