From ef8a6dcd09a9c747032a609303878b2e06d1e9e2 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Tue, 4 Oct 2016 10:35:53 +0000 Subject: [PATCH] [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 llvm-svn: 283210 --- .../change-namespace/ChangeNamespace.cpp | 5 +++-- .../change-namespace/ChangeNamespaceTests.cpp | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.cpp b/clang-tools-extra/change-namespace/ChangeNamespace.cpp index 274c9ac..15ce4ed 100644 --- a/clang-tools-extra/change-namespace/ChangeNamespace.cpp +++ b/clang-tools-extra/change-namespace/ChangeNamespace.cpp @@ -106,8 +106,9 @@ SourceLocation getStartOfNextLine(SourceLocation Loc, const SourceManager &SM, // 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 diff --git a/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp b/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp index 4fe353b..04252d5 100644 --- a/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp +++ b/clang-tools-extra/unittests/change-namespace/ChangeNamespaceTests.cpp @@ -513,6 +513,27 @@ TEST_F(ChangeNamespaceTest, DoNotFixStaticVariableOfClass) { 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 -- 2.7.4