[clang] 717cd16 - [clang-format] Fix namespace end comments fixer with anonymous namespaces.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 14 02:41:20 PST 2022


Author: Marek Kurdej
Date: 2022-01-14T11:41:14+01:00
New Revision: 717cd16e85faa6498cf6c46838241b0e7ddaaaba

URL: https://github.com/llvm/llvm-project/commit/717cd16e85faa6498cf6c46838241b0e7ddaaaba
DIFF: https://github.com/llvm/llvm-project/commit/717cd16e85faa6498cf6c46838241b0e7ddaaaba.diff

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

Previously, a strange trailing comment was produced:
```
namespace out { namespace {
}} // namespace out::
```
(mind the "out::").

Reviewed By: MyDeveloperDay, owenpan

Differential Revision: https://reviews.llvm.org/D117289

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 951a982580519..0c34c6126c214 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -261,7 +261,8 @@ std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
           updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
         }
         ++CompactedNamespacesCount;
-        AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
+        if (!NamespaceName.empty())
+          AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
         continue;
       }
       NamespaceName += AllNamespaceNames;

diff  --git a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index 3afe35e7ea34f..925b0e3b2e4c9 100644
--- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -256,6 +256,15 @@ TEST_F(NamespaceEndCommentsFixerTest, AddsEndComment) {
                                     "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 @@ TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndLineComment) {
                                     "}// 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) {


        


More information about the cfe-commits mailing list