[PATCH] D93163: [clang-format] Fix handling of ## comments in TextProto

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 12 08:49:03 PST 2020


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

As @krasimir said in D92257 <https://reviews.llvm.org/D92257> the old behavior is buggy.

- Added checks for `##`, `###`, and `####`.
- Adapted the test.
- Removed the check for TextProto, which is already in `getLineCommentIndentPrefix`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93163

Files:
  clang/lib/Format/BreakableToken.cpp
  clang/unittests/Format/FormatTestTextProto.cpp


Index: clang/unittests/Format/FormatTestTextProto.cpp
===================================================================
--- clang/unittests/Format/FormatTestTextProto.cpp
+++ clang/unittests/Format/FormatTestTextProto.cpp
@@ -380,25 +380,29 @@
                "cccccccccccccccccccccccc: 3849");
 }
 
-TEST_F(FormatTestTextProto, UnderstandsHashHashComments) {
+TEST_F(FormatTestTextProto, UnderstandsHashComments) {
   FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
   Style.ColumnLimit = 60; // To make writing tests easier.
   EXPECT_EQ("aaa: 100\n"
-            "##this is a double-hash comment.\n"
+            "## this is a double-hash comment.\n"
             "bb: 100\n"
             "## another double-hash comment.\n"
             "### a triple-hash comment\n"
             "cc: 200\n"
+            "### another triple-hash comment\n"
             "#### a quadriple-hash comment\n"
-            "dd: 100\n",
+            "dd: 100\n"
+            "#### another quadriple-hash comment\n",
             format("aaa: 100\n"
                    "##this is a double-hash comment.\n"
                    "bb: 100\n"
                    "## another double-hash comment.\n"
-                   "### a triple-hash comment\n"
+                   "###a triple-hash comment\n"
                    "cc: 200\n"
-                   "#### a quadriple-hash comment\n"
-                   "dd: 100\n",
+                   "### another triple-hash comment\n"
+                   "####a quadriple-hash comment\n"
+                   "dd: 100\n"
+                   "#### another quadriple-hash comment\n",
                    Style));
 }
 
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -789,9 +789,14 @@
           Prefix[i] = "///< ";
         else if (Prefix[i] == "//!<")
           Prefix[i] = "//!< ";
-        else if (Prefix[i] == "#" &&
-                 Style.Language == FormatStyle::LK_TextProto)
+        else if (Prefix[i] == "#")
           Prefix[i] = "# ";
+        else if (Prefix[i] == "##")
+          Prefix[i] = "## ";
+        else if (Prefix[i] == "###")
+          Prefix[i] = "### ";
+        else if (Prefix[i] == "####")
+          Prefix[i] = "#### ";
       }
 
       Tokens[i] = LineTok;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93163.311394.patch
Type: text/x-patch
Size: 2373 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201212/47c3a043/attachment-0001.bin>


More information about the cfe-commits mailing list