]> granicus.if.org Git - clang/commitdiff
[clang-format] Consider tok::hashhash in python-style comments
authorKrasimir Georgiev <krasimir@google.com>
Thu, 7 Jun 2018 09:46:24 +0000 (09:46 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Thu, 7 Jun 2018 09:46:24 +0000 (09:46 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@334179 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/BreakableToken.cpp
lib/Format/FormatTokenLexer.cpp
unittests/Format/FormatTestTextProto.cpp

index 933dce35403c938bf9edbf3fdca880a6236ae184..f727f8ddf8966de6f3051fc197d99454d35b7a96 100644 (file)
@@ -44,7 +44,8 @@ static StringRef getLineCommentIndentPrefix(StringRef Comment,
                                             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;
index fbd26965a6e789c07a1a0e713cfd3ef2f1d81856..c7f720a443d35415d22dadbb7dfd1f32269bf548 100644 (file)
@@ -334,7 +334,7 @@ void FormatTokenLexer::handleTemplateStrings() {
 
 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 =
index 050c319e4f722ad76ce12f5281c4bf43ed292c8e..fcf118eee8603d9bee11b9c7e09a56b41f687c20 100644 (file)
@@ -347,6 +347,28 @@ TEST_F(FormatTestTextProto, KeepsCommentsIndentedInList) {
                "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"