r325252 - [clang-format] Support repeated field lists in protos
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 15 07:30:22 PST 2018
Author: krasimir
Date: Thu Feb 15 07:30:22 2018
New Revision: 325252
URL: http://llvm.org/viewvc/llvm-project?rev=325252&view=rev
Log:
[clang-format] Support repeated field lists in protos
Summary:
This patch adds support for list initialization of proto repeated fields:
```
keys: [1, 2, 3]
```
Reviewers: djasper
Reviewed By: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D43298
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestProto.cpp
cfe/trunk/unittests/Format/FormatTestTextProto.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=325252&r1=325251&r2=325252&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Feb 15 07:30:22 2018
@@ -383,11 +383,18 @@ private:
// }
// }
//
- // In the first case we want to spread the contents inside the square
- // braces; in the second we want to keep them inline.
+ // or repeated fields (in options):
+ //
+ // option (Aaa.options) = {
+ // keys: [ 1, 2, 3 ]
+ // }
+ //
+ // In the first and the third case we want to spread the contents inside
+ // the square braces; in the second we want to keep them inline.
Left->Type = TT_ArrayInitializerLSquare;
if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
- tok::equal)) {
+ tok::equal) &&
+ !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
Left->Type = TT_ProtoExtensionLSquare;
BindingIncrease = 10;
}
@@ -2331,16 +2338,26 @@ bool TokenAnnotator::spaceRequiredBetwee
!Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
if (Right.is(tok::star) && Left.is(tok::l_paren))
return false;
+ const auto SpaceRequiredForArrayInitializerLSquare =
+ [](const FormatToken &LSquareTok, const FormatStyle &Style) {
+ return Style.SpacesInContainerLiterals ||
+ ((Style.Language == FormatStyle::LK_Proto ||
+ Style.Language == FormatStyle::LK_TextProto) &&
+ !Style.Cpp11BracedListStyle &&
+ LSquareTok.endsSequence(tok::l_square, tok::colon,
+ TT_SelectorName));
+ };
if (Left.is(tok::l_square))
- return (Left.is(TT_ArrayInitializerLSquare) &&
- Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
+ return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
+ SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
(Left.isOneOf(TT_ArraySubscriptLSquare,
TT_StructuredBindingLSquare) &&
Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
if (Right.is(tok::r_square))
return Right.MatchingParen &&
- ((Style.SpacesInContainerLiterals &&
- Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
+ ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
+ SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
+ Style)) ||
(Style.SpacesInSquareBrackets &&
Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
TT_StructuredBindingLSquare)));
Modified: cfe/trunk/unittests/Format/FormatTestProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestProto.cpp?rev=325252&r1=325251&r2=325252&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestProto.cpp Thu Feb 15 07:30:22 2018
@@ -438,5 +438,35 @@ TEST_F(FormatTestProto, NoSpaceAfterPerc
"};");
}
+TEST_F(FormatTestProto, FormatsRepeatedListInitializersInOptions) {
+ verifyFormat("option (MyProto.options) = {\n"
+ " key: item\n"
+ " keys: [\n"
+ " 'ala',\n"
+ " 'bala',\n"
+ " 'porto',\n"
+ " 'kala',\n"
+ " 'too',\n"
+ " 'long',\n"
+ " 'long',\n"
+ " 'long'\n"
+ " ]\n"
+ " key: [ item ]\n"
+ " msg {\n"
+ " key: item\n"
+ " keys: [\n"
+ " 'ala',\n"
+ " 'bala',\n"
+ " 'porto',\n"
+ " 'kala',\n"
+ " 'too',\n"
+ " 'long',\n"
+ " 'long'\n"
+ " ]\n"
+ " }\n"
+ " key: value\n"
+ "};");
+}
+
} // end namespace tooling
} // end namespace clang
Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=325252&r1=325251&r2=325252&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Thu Feb 15 07:30:22 2018
@@ -31,14 +31,18 @@ protected:
return *Result;
}
- static std::string format(llvm::StringRef Code) {
- FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
- Style.ColumnLimit = 60; // To make writing tests easier.
+ static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
return format(Code, 0, Code.size(), Style);
}
+ static void verifyFormat(llvm::StringRef Code, const FormatStyle &Style) {
+ EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
+ }
+
static void verifyFormat(llvm::StringRef Code) {
- EXPECT_EQ(Code.str(), format(test::messUp(Code)));
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+ Style.ColumnLimit = 60; // To make writing tests easier.
+ verifyFormat(Code, Style);
}
};
@@ -390,5 +394,43 @@ TEST_F(FormatTestTextProto, FormatsExten
TEST_F(FormatTestTextProto, NoSpaceAfterPercent) {
verifyFormat("key: %d");
}
+
+TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {
+ verifyFormat("keys: []");
+ verifyFormat("keys: [ 1 ]");
+ verifyFormat("keys: [ 'ala', 'bala' ]");
+ verifyFormat("keys:\n"
+ " [ 'ala', 'bala', 'porto', 'kala', 'too', 'long', 'ng' ]");
+ verifyFormat("key: item\n"
+ "keys: [\n"
+ " 'ala',\n"
+ " 'bala',\n"
+ " 'porto',\n"
+ " 'kala',\n"
+ " 'too',\n"
+ " 'long',\n"
+ " 'long',\n"
+ " 'long'\n"
+ "]\n"
+ "key: item\n"
+ "msg {\n"
+ " key: item\n"
+ " keys: [\n"
+ " 'ala',\n"
+ " 'bala',\n"
+ " 'porto',\n"
+ " 'kala',\n"
+ " 'too',\n"
+ " 'long',\n"
+ " 'long'\n"
+ " ]\n"
+ "}\n"
+ "key: value"
+ );
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+ Style.ColumnLimit = 60; // To make writing tests easier.
+ Style.Cpp11BracedListStyle = true;
+ verifyFormat("keys: [1]", Style);
+}
} // end namespace tooling
} // end namespace clang
More information about the cfe-commits
mailing list