From 47d2e226a05a51f0b54b3630a038ba2f8cd4f6d5 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Mon, 6 Mar 2017 16:44:45 +0000 Subject: [PATCH] [clang-format] Make NamespaceEndCommentFixer add at most one comment Summary: Until now, NamespaceEndCommentFixer was adding missing comments for every run, which results in multiple end comments for: ``` namespace { int i; int j; } #if A int a = 1; #else int a = 2; #endif ``` result before: ``` namespace { int i; int j; }// namespace // namespace #if A int a = 1; #else int a = 2; #endif ``` result after: ``` namespace { int i; int j; }// namespace #if A int a = 1; #else int a = 2; #endif ``` Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D30659 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@297028 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/NamespaceEndCommentsFixer.cpp | 5 ++++- .../Format/NamespaceEndCommentsFixerTest.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Format/NamespaceEndCommentsFixer.cpp b/lib/Format/NamespaceEndCommentsFixer.cpp index 578dcf7b67..d0b5919c9b 100644 --- a/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/lib/Format/NamespaceEndCommentsFixer.cpp @@ -135,7 +135,10 @@ tooling::Replacements NamespaceEndCommentsFixer::analyze( NamespaceTok = NamespaceTok->getNextNonComment(); if (NamespaceTok->isNot(tok::kw_namespace)) continue; - const FormatToken *RBraceTok = EndLine->First; + FormatToken *RBraceTok = EndLine->First; + if (RBraceTok->Finalized) + continue; + RBraceTok->Finalized = true; const std::string NamespaceName = computeName(NamespaceTok); bool AddNewline = (I + 1 < E) && AnnotatedLines[I + 1]->First->NewlinesBefore == 0 && diff --git a/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/unittests/Format/NamespaceEndCommentsFixerTest.cpp index 28212f8524..0341fd7ef4 100644 --- a/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ b/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -388,6 +388,24 @@ TEST_F(NamespaceEndCommentsFixerTest, " int i;\n" "}\n" "}\n")); + EXPECT_EQ("namespace {\n" + " int i;\n" + " int j;\n" + "}// namespace\n" + "#if A\n" + " int i;\n" + "#else\n" + " int j;\n" + "#endif", + fixNamespaceEndComments("namespace {\n" + " int i;\n" + " int j;\n" + "}\n" + "#if A\n" + " int i;\n" + "#else\n" + " int j;\n" + "#endif")); } TEST_F(NamespaceEndCommentsFixerTest, -- 2.40.0