[clang-format] calculate MaxInsertOffset in the original code correctly.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D27615

llvm-svn: 289203
This commit is contained in:
Eric Liu 2016-12-09 11:45:50 +00:00
parent 6bd372bae7
commit 21d1032855
2 changed files with 26 additions and 0 deletions

View File

@ -1677,7 +1677,9 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
unsigned MinInsertOffset =
getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style);
StringRef TrimmedCode = Code.drop_front(MinInsertOffset);
// Max insertion offset in the original code.
unsigned MaxInsertOffset =
MinInsertOffset +
getMaxHeaderInsertionOffset(FileName, TrimmedCode, Style);
SmallVector<StringRef, 32> Lines;
TrimmedCode.split(Lines, '\n');

View File

@ -916,6 +916,30 @@ TEST_F(CleanUpReplacementsTest, CanInsertAfterComment) {
EXPECT_EQ(Expected, apply(Code, Replaces));
}
TEST_F(CleanUpReplacementsTest, LongCommentsInTheBeginningOfFile) {
std::string Code = "// Loooooooooooooooooooooooooong comment\n"
"// Loooooooooooooooooooooooooong comment\n"
"// Loooooooooooooooooooooooooong comment\n"
"#include <string>\n"
"#include <vector>\n"
"\n"
"#include \"a.h\"\n"
"#include \"b.h\"\n";
std::string Expected = "// Loooooooooooooooooooooooooong comment\n"
"// Loooooooooooooooooooooooooong comment\n"
"// Loooooooooooooooooooooooooong comment\n"
"#include <string>\n"
"#include <vector>\n"
"\n"
"#include \"a.h\"\n"
"#include \"b.h\"\n"
"#include \"third.h\"\n";
tooling::Replacements Replaces =
toReplacements({createInsertion("#include \"third.h\"")});
Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
EXPECT_EQ(Expected, apply(Code, Replaces));
}
TEST_F(CleanUpReplacementsTest, CanDeleteAfterCode) {
std::string Code = "#include \"a.h\"\n"
"void f() {}\n"