]> granicus.if.org Git - clang/commitdiff
[clang-format] calculate MaxInsertOffset in the original code correctly.
authorEric Liu <ioeric@google.com>
Fri, 9 Dec 2016 11:45:50 +0000 (11:45 +0000)
committerEric Liu <ioeric@google.com>
Fri, 9 Dec 2016 11:45:50 +0000 (11:45 +0000)
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
unittests/Format/CleanupTest.cpp

index 4b24b0b7336b0667e9ad298989c0dabc1113b352..bcea5ac411af2ee75a2a5e1efea862e98a51803b 100644 (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');
index c3e7e2b4c184c7bff04a72a6f2a8871ec9dc2b81..dbf3f0704768d77bca33e1337ecb2b7237359c0a 100644 (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"