diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index cdf921c3c6..c2d6875e0c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index 672fd5914d..4b1480b074 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -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" "};"); } diff --git a/unittests/Format/FormatTestTextProto.cpp b/unittests/Format/FormatTestTextProto.cpp index 2cd7fb7bdf..4e4387b9d3 100644 --- a/unittests/Format/FormatTestTextProto.cpp +++ b/unittests/Format/FormatTestTextProto.cpp @@ -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) {