r334179 - [clang-format] Consider tok::hashhash in python-style comments

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 7 02:46:24 PDT 2018


Author: krasimir
Date: Thu Jun  7 02:46:24 2018
New Revision: 334179

URL: http://llvm.org/viewvc/llvm-project?rev=334179&view=rev
Log:
[clang-format] Consider tok::hashhash in python-style comments

Summary: We were missing the case when python-style comments in text protos start with `##`.

Subscribers: cfe-commits

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

Modified:
    cfe/trunk/lib/Format/BreakableToken.cpp
    cfe/trunk/lib/Format/FormatTokenLexer.cpp
    cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Modified: cfe/trunk/lib/Format/BreakableToken.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/BreakableToken.cpp?rev=334179&r1=334178&r2=334179&view=diff
==============================================================================
--- cfe/trunk/lib/Format/BreakableToken.cpp (original)
+++ cfe/trunk/lib/Format/BreakableToken.cpp Thu Jun  7 02:46:24 2018
@@ -44,7 +44,8 @@ static StringRef getLineCommentIndentPre
                                             const FormatStyle &Style) {
   static const char *const KnownCStylePrefixes[] = {"///<", "//!<", "///", "//",
                                                     "//!"};
-  static const char *const KnownTextProtoPrefixes[] = {"//", "#"};
+  static const char *const KnownTextProtoPrefixes[] = {"//", "#", "##", "###",
+                                                       "####"};
   ArrayRef<const char *> KnownPrefixes(KnownCStylePrefixes);
   if (Style.Language == FormatStyle::LK_TextProto)
     KnownPrefixes = KnownTextProtoPrefixes;

Modified: cfe/trunk/lib/Format/FormatTokenLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatTokenLexer.cpp?rev=334179&r1=334178&r2=334179&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatTokenLexer.cpp (original)
+++ cfe/trunk/lib/Format/FormatTokenLexer.cpp Thu Jun  7 02:46:24 2018
@@ -334,7 +334,7 @@ void FormatTokenLexer::handleTemplateStr
 
 void FormatTokenLexer::tryParsePythonComment() {
   FormatToken *HashToken = Tokens.back();
-  if (HashToken->isNot(tok::hash))
+  if (!HashToken->isOneOf(tok::hash, tok::hashhash))
     return;
   // Turn the remainder of this line into a comment.
   const char *CommentBegin =

Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=334179&r1=334178&r2=334179&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Thu Jun  7 02:46:24 2018
@@ -347,6 +347,28 @@ TEST_F(FormatTestTextProto, KeepsComment
                "cccccccccccccccccccccccc: 3849");
 }
 
+TEST_F(FormatTestTextProto, UnderstandsHashHashComments) {
+  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"
+            "bb: 100\n"
+            "## another double-hash comment.\n"
+            "### a triple-hash comment\n"
+            "cc: 200\n"
+            "#### a quadriple-hash comment\n"
+            "dd: 100\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"
+                   "cc: 200\n"
+                   "#### a quadriple-hash comment\n"
+                   "dd: 100\n",
+                   Style));
+}
+
 TEST_F(FormatTestTextProto, FormatsExtensions) {
   verifyFormat("[type] { key: value }");
   verifyFormat("[type] {\n"




More information about the cfe-commits mailing list