[PATCH] D25226: [change-namespace] Fix a misplaced case when there is no trailing newline character at the end of the file.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 4 01:11:14 PDT 2016


hokein created this revision.
hokein added a reviewer: ioeric.
hokein added a subscriber: cfe-commits.

https://reviews.llvm.org/D25226

Files:
  change-namespace/ChangeNamespace.cpp
  unittests/change-namespace/ChangeNamespaceTests.cpp


Index: unittests/change-namespace/ChangeNamespaceTests.cpp
===================================================================
--- unittests/change-namespace/ChangeNamespaceTests.cpp
+++ unittests/change-namespace/ChangeNamespaceTests.cpp
@@ -447,6 +447,27 @@
   EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code));
 }
 
+TEST_F(ChangeNamespaceTest, NoMisplaceAtEOF) {
+  std::string Code = "namespace na {\n"
+                     "namespace nb {\n"
+                     "class A;\n"
+                     "class B {};\n"
+                     "}"
+                     "}";
+  std::string Expected = "namespace na {\n"
+                         "namespace nb {\n"
+                         "class A;\n"
+                         "}\n"
+                         "}\n"
+                         "namespace x {\n"
+                         "namespace y {\n"
+                         "\n"
+                         "class B {};\n"
+                         "} // namespace y\n"
+                         "} // namespace x\n";
+  EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code));
+}
+
 } // anonymous namespace
 } // namespace change_namespace
 } // namespace clang
Index: change-namespace/ChangeNamespace.cpp
===================================================================
--- change-namespace/ChangeNamespace.cpp
+++ change-namespace/ChangeNamespace.cpp
@@ -106,8 +106,10 @@
   // FIXME: this is a bit hacky to get ReadToEndOfLine work.
   Lex.setParsingPreprocessorDirective(true);
   Lex.ReadToEndOfLine(&Line);
-  // FIXME: should not +1 at EOF.
-  return Loc.getLocWithOffset(Line.size() + 1);
+  auto End = Loc.getLocWithOffset(Line.size());
+  if (SM.getLocForEndOfFile(LocInfo.first) == End)
+    return End;
+  return End.getLocWithOffset(1);
 }
 
 // Returns `R` with new range that refers to code after `Replaces` being


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25226.73420.patch
Type: text/x-patch
Size: 1854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161004/fae4529d/attachment-0001.bin>


More information about the cfe-commits mailing list