r297028 - [clang-format] Make NamespaceEndCommentFixer add at most one comment

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 6 08:44:45 PST 2017


Author: krasimir
Date: Mon Mar  6 10:44:45 2017
New Revision: 297028

URL: http://llvm.org/viewvc/llvm-project?rev=297028&view=rev
Log:
[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

Modified:
    cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
    cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp

Modified: cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp?rev=297028&r1=297027&r2=297028&view=diff
==============================================================================
--- cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp (original)
+++ cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp Mon Mar  6 10:44:45 2017
@@ -135,7 +135,10 @@ tooling::Replacements NamespaceEndCommen
       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 &&

Modified: cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp?rev=297028&r1=297027&r2=297028&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp (original)
+++ cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp Mon Mar  6 10:44:45 2017
@@ -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,




More information about the cfe-commits mailing list