r280165 - clang-format: Correctly calculate affected ranges when sorting #includes.
Daniel Jasper via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 30 14:33:41 PDT 2016
Author: djasper
Date: Tue Aug 30 16:33:41 2016
New Revision: 280165
URL: http://llvm.org/viewvc/llvm-project?rev=280165&view=rev
Log:
clang-format: Correctly calculate affected ranges when sorting #includes.
affectedRanges takes a start and an end offset, not offset and length.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/SortIncludesTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=280165&r1=280164&r2=280165&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Aug 30 16:33:41 2016
@@ -1263,10 +1263,10 @@ static void sortCppIncludes(const Format
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)
Modified: cfe/trunk/unittests/Format/SortIncludesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortIncludesTest.cpp?rev=280165&r1=280164&r2=280165&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/SortIncludesTest.cpp (original)
+++ cfe/trunk/unittests/Format/SortIncludesTest.cpp Tue Aug 30 16:33:41 2016
@@ -24,8 +24,8 @@ protected:
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);
@@ -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 <a>\n"
+ "#include <b>\n",
+ sort("// comment\n"
+ "#include <b>\n"
+ "#include <a>\n",
+ {tooling::Range(25, 1)}));
}
TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
More information about the cfe-commits
mailing list