[clang-tools-extra] r283210 - [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:35:54 PDT 2016


Author: hokein
Date: Tue Oct  4 05:35:53 2016
New Revision: 283210

URL: http://llvm.org/viewvc/llvm-project?rev=283210&view=rev
Log:
[change-namespace] Fix a misplaced case when there is no trailing newline character at the end of the file.

Reviewers: ioeric

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25226

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

Modified: clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp?rev=283210&r1=283209&r2=283210&view=diff
==============================================================================
--- clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp (original)
+++ clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp Tue Oct  4 05:35:53 2016
@@ -106,8 +106,9 @@ SourceLocation getStartOfNextLine(Source
   // 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

Modified: clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp?rev=283210&r1=283209&r2=283210&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp Tue Oct  4 05:35:53 2016
@@ -513,6 +513,27 @@ TEST_F(ChangeNamespaceTest, DoNotFixStat
   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




More information about the cfe-commits mailing list