[PATCH] D43294: [clang-format] Recognize percents as format specifiers in protos

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 08:18:57 PST 2018


krasimir created this revision.
krasimir added a reviewer: djasper.
Herald added subscribers: cfe-commits, klimek.

Frequently, a percent in protos denotes a formatting specifier for string replacement.
Thus it is desirable to keep the percent together with what follows after it.


Repository:
  rC Clang

https://reviews.llvm.org/D43294

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestProto.cpp
  unittests/Format/FormatTestTextProto.cpp


Index: unittests/Format/FormatTestTextProto.cpp
===================================================================
--- unittests/Format/FormatTestTextProto.cpp
+++ unittests/Format/FormatTestTextProto.cpp
@@ -386,5 +386,9 @@
                "  }\n"
                "}");
 }
+
+TEST_F(FormatTestTextProto, NoSpaceAfterPercent) {
+  verifyFormat("key: %d");
+}
 } // end namespace tooling
 } // end namespace clang
Index: unittests/Format/FormatTestProto.cpp
===================================================================
--- unittests/Format/FormatTestProto.cpp
+++ unittests/Format/FormatTestProto.cpp
@@ -432,5 +432,11 @@
                "};");
 }
 
+TEST_F(FormatTestProto, NoSpaceAfterPercent) {
+  verifyFormat("option (MyProto.options) = {\n"
+               "  key: %lld\n"
+               "};");
+}
+
 } // end namespace tooling
 } // end namespace clang
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2425,6 +2425,9 @@
     if (Left.MatchingParen && Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
         Right.isOneOf(tok::l_brace, tok::less))
       return !Style.Cpp11BracedListStyle;
+    // A percent is probably part of a formatting specification, such as %lld.
+    if (Left.is(tok::percent))
+      return false;
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
     if (Left.is(TT_JsFatArrow))
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43294.134232.patch
Type: text/x-patch
Size: 1486 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180214/2034d459/attachment.bin>


More information about the cfe-commits mailing list