]> granicus.if.org Git - clang/commitdiff
[clang-format] Make NamespaceEndCommentFixer add at most one comment
authorKrasimir Georgiev <krasimir@google.com>
Mon, 6 Mar 2017 16:44:45 +0000 (16:44 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Mon, 6 Mar 2017 16:44:45 +0000 (16:44 +0000)
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
unittests/Format/NamespaceEndCommentsFixerTest.cpp

index 578dcf7b6700cc7e263da847ac46cd6c968e67f3..d0b5919c9bd558db31458e994495753256896028 100644 (file)
@@ -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 &&
index 28212f8524d02f07675b7a6bae107d17adba5a6f..0341fd7ef44f99824848662294a61ceec9d5d0af 100644 (file)
@@ -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,