From 10bd02d9c35d8ecac8469dcb1fd2912a24b15f10 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Fri, 9 Dec 2016 11:45:50 +0000 Subject: [PATCH] [clang-format] calculate MaxInsertOffset in the original code correctly. Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D27615 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289203 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 2 ++ unittests/Format/CleanupTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 4b24b0b733..bcea5ac411 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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 Lines; TrimmedCode.split(Lines, '\n'); diff --git a/unittests/Format/CleanupTest.cpp b/unittests/Format/CleanupTest.cpp index c3e7e2b4c1..dbf3f07047 100644 --- a/unittests/Format/CleanupTest.cpp +++ b/unittests/Format/CleanupTest.cpp @@ -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 \n" + "#include \n" + "\n" + "#include \"a.h\"\n" + "#include \"b.h\"\n"; + std::string Expected = "// Loooooooooooooooooooooooooong comment\n" + "// Loooooooooooooooooooooooooong comment\n" + "// Loooooooooooooooooooooooooong comment\n" + "#include \n" + "#include \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" -- 2.50.1