[clang-format] Preserve spaces before a percent in (text) protos

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
This commit is contained in:
Krasimir Georgiev 2018-04-05 09:33:03 +00:00
Родитель d8ad1e8b85
Коммит 734dd1682c
3 изменённых файлов: 10 добавлений и 2 удалений

Просмотреть файл

@ -2518,6 +2518,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
// A percent is probably part of a formatting specification, such as %lld. // A percent is probably part of a formatting specification, such as %lld.
if (Left.is(tok::percent)) if (Left.is(tok::percent))
return false; 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) { } else if (Style.Language == FormatStyle::LK_JavaScript) {
if (Left.is(TT_JsFatArrow)) if (Left.is(TT_JsFatArrow))
return true; return true;

Просмотреть файл

@ -442,9 +442,11 @@ TEST_F(FormatTestProto, FormatsOptionsExtensions) {
"};"); "};");
} }
TEST_F(FormatTestProto, NoSpaceAfterPercent) { TEST_F(FormatTestProto, SpacesAroundPercents) {
verifyFormat("option (MyProto.options) = {\n" verifyFormat("option (MyProto.options) = {\n"
" key: %lld\n" " key: %lld\n"
" key: 0x%04x\n"
" key: \"%d %d\"\n"
"};"); "};");
} }

Просмотреть файл

@ -419,8 +419,10 @@ TEST_F(FormatTestTextProto, FormatsExtensions) {
"}"); "}");
} }
TEST_F(FormatTestTextProto, NoSpaceAfterPercent) { TEST_F(FormatTestTextProto, SpacesAroundPercents) {
verifyFormat("key: %d"); verifyFormat("key: %d");
verifyFormat("key: 0x%04x");
verifyFormat("key: \"%d %d\"");
} }
TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) { TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {