]> granicus.if.org Git - clang/commitdiff
[clang-format] Preserve spaces before a percent in (text) protos
authorKrasimir Georgiev <krasimir@google.com>
Thu, 5 Apr 2018 09:33:03 +0000 (09:33 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Thu, 5 Apr 2018 09:33:03 +0000 (09:33 +0000)
This makes sure that we do not change the meaning of pieces of text with
format specifiers.

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

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

index cdf921c3c659efbf51b4cbfdbb75f0d54318ad59..c2d6875e0c34cce695f15e148146e1eceeb90bb5 100644 (file)
@@ -2518,6 +2518,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     // A percent is probably part of a formatting specification, such as %lld.
     if (Left.is(tok::percent))
       return false;
+    // Preserve the existence of a space before a percent for cases like 0x%04x
+    // and "%d %d"
+    if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
+      return Right.WhitespaceRange.getEnd() != Right.WhitespaceRange.getBegin();
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
     if (Left.is(TT_JsFatArrow))
       return true;
index 672fd5914d077adb553ebf7f978672ccfa9afde0..4b1480b074aa76eb8ce80b30d884c821389758e3 100644 (file)
@@ -442,9 +442,11 @@ TEST_F(FormatTestProto, FormatsOptionsExtensions) {
                "};");
 }
 
-TEST_F(FormatTestProto, NoSpaceAfterPercent) {
+TEST_F(FormatTestProto, SpacesAroundPercents) {
   verifyFormat("option (MyProto.options) = {\n"
                "  key: %lld\n"
+               "  key: 0x%04x\n"
+               "  key: \"%d %d\"\n"
                "};");
 }
 
index 2cd7fb7bdf3051fe7bc96a94063e84d32d2fad94..4e4387b9d382654a46d1d101314cd2884e40d60f 100644 (file)
@@ -419,8 +419,10 @@ TEST_F(FormatTestTextProto, FormatsExtensions) {
       "}");
 }
 
-TEST_F(FormatTestTextProto, NoSpaceAfterPercent) {
+TEST_F(FormatTestTextProto, SpacesAroundPercents) {
   verifyFormat("key: %d");
+  verifyFormat("key: 0x%04x");
+  verifyFormat("key: \"%d %d\"");
 }
 
 TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {