[PATCH] D117289: [clang-format] Fix namespace end comments fixer with anonymous namespaces.

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 14 00:35:25 PST 2022


curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Previously, a strange trailing comment was produced:

  namespace out { namespace {
  }} // namespace out::

(mind the "out::").


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117289

Files:
  clang/lib/Format/NamespaceEndCommentsFixer.cpp
  clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===================================================================
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -256,6 +256,15 @@
                                     "int j;\n"
                                     "};}",
                                     CompactNamespacesStyle));
+  EXPECT_EQ("namespace out { namespace {\n"
+            "int i;\n"
+            "int j;\n"
+            "}}// namespace out",
+            fixNamespaceEndComments("namespace out { namespace {\n"
+                                    "int i;\n"
+                                    "int j;\n"
+                                    "}}",
+                                    CompactNamespacesStyle));
 
   // Adds an end comment after a semicolon.
   EXPECT_EQ("namespace {\n"
@@ -609,6 +618,11 @@
                                     "}// banamespace in\n"
                                     "} // namespace out",
                                     CompactNamespacesStyle));
+  EXPECT_EQ("namespace out { namespace {\n"
+            "}} // namespace out",
+            fixNamespaceEndComments("namespace out { namespace {\n"
+                                    "}} // namespace out::",
+                                    CompactNamespacesStyle));
 }
 
 TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidMacroEndLineComment) {
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===================================================================
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -261,7 +261,8 @@
           updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
         }
         ++CompactedNamespacesCount;
-        AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
+        if (!NamespaceName.empty())
+          AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
         continue;
       }
       NamespaceName += AllNamespaceNames;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117289.399919.patch
Type: text/x-patch
Size: 2090 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220114/a922c147/attachment-0001.bin>


More information about the cfe-commits mailing list