diff options
author | Eric Liu <ioeric@google.com> | 2016-12-09 11:45:50 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2016-12-09 11:45:50 +0000 |
commit | 21d10328551893fc67d21501300e71573d085e26 (patch) | |
tree | dd0796ee48b9465269cffa0b233e05592ad572ad | |
parent | 6bd372bae7b71defe5944c05ef9ca6c29f43df19 (diff) | |
download | bcm5719-llvm-21d10328551893fc67d21501300e71573d085e26.tar.gz bcm5719-llvm-21d10328551893fc67d21501300e71573d085e26.zip |
[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
-rw-r--r-- | clang/lib/Format/Format.cpp | 2 | ||||
-rw-r--r-- | clang/unittests/Format/CleanupTest.cpp | 24 |
2 files changed, 26 insertions, 0 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 4b24b0b7336..bcea5ac411a 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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<StringRef, 32> Lines; TrimmedCode.split(Lines, '\n'); diff --git a/clang/unittests/Format/CleanupTest.cpp b/clang/unittests/Format/CleanupTest.cpp index c3e7e2b4c18..dbf3f070476 100644 --- a/clang/unittests/Format/CleanupTest.cpp +++ b/clang/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 <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" |