summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2017-03-06 16:44:45 +0000
committerKrasimir Georgiev <krasimir@google.com>2017-03-06 16:44:45 +0000
commitbda77397db14ebf2d4688fae28226dcdf35bda1e (patch)
tree6136c1ddf3564b900b64440e1679f959f66a779c
parentadacd8f6176b90935f21f3df34cfcb8cca4e6dbf (diff)
downloadbcm5719-llvm-bda77397db14ebf2d4688fae28226dcdf35bda1e.tar.gz
bcm5719-llvm-bda77397db14ebf2d4688fae28226dcdf35bda1e.zip
[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 llvm-svn: 297028
-rw-r--r--clang/lib/Format/NamespaceEndCommentsFixer.cpp5
-rw-r--r--clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp18
2 files changed, 22 insertions, 1 deletions
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 578dcf7b670..d0b5919c9bd 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/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/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index 28212f8524d..0341fd7ef44 100644
--- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/clang/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,
OpenPOWER on IntegriCloud