[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 03:44:56 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL283210: [change-namespace] Fix a misplaced case when there is no trailing newline… (authored by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D25226?vs=73420&id=73447#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D25226

Files:
  clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
  clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp


Index: clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp
===================================================================
--- clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp
+++ clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp
@@ -513,6 +513,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: clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
===================================================================
--- clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
+++ clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
@@ -106,8 +106,9 @@
   // 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());
+  return SM.getLocForEndOfFile(LocInfo.first) == End ? End
+                                                     : 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.73447.patch
Type: text/x-patch
Size: 2034 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161004/a503b1e4/attachment.bin>


More information about the cfe-commits mailing list