From 2704a8a0fd42eeded114d89abaea01319de6124c Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Wed, 14 Feb 2018 19:47:58 +0000 Subject: [PATCH] [clang-format] Recognize percents as format specifiers in protos Summary: 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. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43294 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325159 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 3 +++ unittests/Format/FormatTestProto.cpp | 6 ++++++ unittests/Format/FormatTestTextProto.cpp | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 248e2b5850..0d54208bbe 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2425,6 +2425,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 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; diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index 27ecde7f34..40a069b3f3 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -432,5 +432,11 @@ TEST_F(FormatTestProto, FormatsOptionsExtensions) { "};"); } +TEST_F(FormatTestProto, NoSpaceAfterPercent) { + verifyFormat("option (MyProto.options) = {\n" + " key: %lld\n" + "};"); +} + } // end namespace tooling } // end namespace clang diff --git a/unittests/Format/FormatTestTextProto.cpp b/unittests/Format/FormatTestTextProto.cpp index 8786b270ed..151774b9e2 100644 --- a/unittests/Format/FormatTestTextProto.cpp +++ b/unittests/Format/FormatTestTextProto.cpp @@ -386,5 +386,9 @@ TEST_F(FormatTestTextProto, FormatsExtensions) { " }\n" "}"); } + +TEST_F(FormatTestTextProto, NoSpaceAfterPercent) { + verifyFormat("key: %d"); +} } // end namespace tooling } // end namespace clang -- 2.40.0