Skip to content

Commit 4c17452

Browse files
authored
[clang-format][NFC] Extend isProto() to also cover LK_TextProto (#73582)
1 parent efa1d0d commit 4c17452

File tree

6 files changed

+24
-47
lines changed

6 files changed

+24
-47
lines changed

clang/include/clang/Format/Format.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3034,7 +3034,9 @@ struct FormatStyle {
30343034
bool isJson() const { return Language == LK_Json; }
30353035
bool isJavaScript() const { return Language == LK_JavaScript; }
30363036
bool isVerilog() const { return Language == LK_Verilog; }
3037-
bool isProto() const { return Language == LK_Proto; }
3037+
bool isProto() const {
3038+
return Language == LK_Proto || Language == LK_TextProto;
3039+
}
30383040

30393041
/// Language, this format style is targeted at.
30403042
/// \version 3.5

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,9 +1229,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
12291229
return CurrentState.Indent;
12301230
}
12311231
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
1232-
(Current.is(tok::greater) &&
1233-
(Style.Language == FormatStyle::LK_Proto ||
1234-
Style.Language == FormatStyle::LK_TextProto))) &&
1232+
(Current.is(tok::greater) && Style.isProto())) &&
12351233
State.Stack.size() > 1) {
12361234
if (Current.closesBlockOrBlockTypeList(Style))
12371235
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
@@ -1278,9 +1276,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
12781276
if (Current.is(tok::identifier) && Current.Next &&
12791277
(!Style.isVerilog() || Current.Next->is(tok::colon)) &&
12801278
(Current.Next->is(TT_DictLiteral) ||
1281-
((Style.Language == FormatStyle::LK_Proto ||
1282-
Style.Language == FormatStyle::LK_TextProto) &&
1283-
Current.Next->isOneOf(tok::less, tok::l_brace)))) {
1279+
(Style.isProto() && Current.Next->isOneOf(tok::less, tok::l_brace)))) {
12841280
return CurrentState.Indent;
12851281
}
12861282
if (NextNonComment->is(TT_ObjCStringLiteral) &&
@@ -1798,9 +1794,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
17981794
Current.MatchingParen->Previous &&
17991795
Current.MatchingParen->Previous->is(tok::comma);
18001796
AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
1801-
Style.Language == FormatStyle::LK_Proto ||
1802-
Style.Language == FormatStyle::LK_TextProto ||
1803-
!Style.BinPackArguments ||
1797+
Style.isProto() || !Style.BinPackArguments ||
18041798
(NextNonComment && NextNonComment->isOneOf(
18051799
TT_DesignatedInitializerPeriod,
18061800
TT_DesignatedInitializerLSquare));

clang/lib/Format/FormatToken.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ bool FormatToken::opensBlockOrBlockTypeList(const FormatStyle &Style) const {
100100
(is(tok::l_brace) &&
101101
(getBlockKind() == BK_Block || is(TT_DictLiteral) ||
102102
(!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
103-
(is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
104-
Style.Language == FormatStyle::LK_TextProto));
103+
(is(tok::less) && Style.isProto());
105104
}
106105

107106
TokenRole::~TokenRole() {}

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,11 +1313,8 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
13131313
}
13141314
}
13151315

1316-
if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Proto ||
1317-
Style.Language == FormatStyle::LK_TextProto) &&
1318-
Tok.is(tok::char_constant)) {
1316+
if ((Style.isJavaScript() || Style.isProto()) && Tok.is(tok::char_constant))
13191317
Tok.Tok.setKind(tok::string_literal);
1320-
}
13211318

13221319
if (Tok.is(tok::comment) && isClangFormatOn(Tok.TokenText))
13231320
FormattingDisabled = false;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ class AnnotatingParser {
227227
}
228228
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
229229
(CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
230-
!Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
231-
Style.Language != FormatStyle::LK_TextProto)) {
230+
!Style.isCSharp() && !Style.isProto())) {
232231
return false;
233232
}
234233
// If a && or || is found and interpreted as a binary operator, this set
@@ -678,8 +677,7 @@ class AnnotatingParser {
678677
} else if (CurrentToken->is(tok::r_square) && Parent &&
679678
Parent->is(TT_TemplateCloser)) {
680679
Left->setType(TT_ArraySubscriptLSquare);
681-
} else if (Style.Language == FormatStyle::LK_Proto ||
682-
Style.Language == FormatStyle::LK_TextProto) {
680+
} else if (Style.isProto()) {
683681
// Square braces in LK_Proto can either be message field attributes:
684682
//
685683
// optional Aaa aaa = 1 [
@@ -906,8 +904,7 @@ class AnnotatingParser {
906904
Previous = Previous->getPreviousNonComment();
907905
if ((CurrentToken->is(tok::colon) &&
908906
(!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
909-
Style.Language == FormatStyle::LK_Proto ||
910-
Style.Language == FormatStyle::LK_TextProto) {
907+
Style.isProto()) {
911908
OpeningBrace.setType(TT_DictLiteral);
912909
if (Previous->Tok.getIdentifierInfo() ||
913910
Previous->is(tok::string_literal)) {
@@ -1049,9 +1046,7 @@ class AnnotatingParser {
10491046
Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
10501047
Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
10511048
Tok->setType(TT_ModulePartitionColon);
1052-
} else if (Contexts.back().ColonIsDictLiteral ||
1053-
Style.Language == FormatStyle::LK_Proto ||
1054-
Style.Language == FormatStyle::LK_TextProto) {
1049+
} else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
10551050
Tok->setType(TT_DictLiteral);
10561051
if (Style.Language == FormatStyle::LK_TextProto) {
10571052
if (FormatToken *Previous = Tok->getPreviousNonComment())
@@ -1290,10 +1285,8 @@ class AnnotatingParser {
12901285
Tok->SpacesRequiredBefore = 1;
12911286
break;
12921287
case tok::kw_operator:
1293-
if (Style.Language == FormatStyle::LK_TextProto ||
1294-
Style.Language == FormatStyle::LK_Proto) {
1288+
if (Style.isProto())
12951289
break;
1296-
}
12971290
while (CurrentToken &&
12981291
!CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
12991292
if (CurrentToken->isOneOf(tok::star, tok::amp))
@@ -2877,9 +2870,7 @@ class ExpressionParser {
28772870
return prec::Conditional;
28782871
if (NextNonComment && Current->is(TT_SelectorName) &&
28792872
(NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
2880-
((Style.Language == FormatStyle::LK_Proto ||
2881-
Style.Language == FormatStyle::LK_TextProto) &&
2882-
NextNonComment->is(tok::less)))) {
2873+
(Style.isProto() && NextNonComment->is(tok::less)))) {
28832874
return prec::Assignment;
28842875
}
28852876
if (Current->is(TT_JsComputedPropertyName))
@@ -3740,7 +3731,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
37403731
// Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
37413732
if (Left.opensScope() && Right.closesScope())
37423733
return 200;
3743-
} else if (Style.isProto()) {
3734+
} else if (Style.Language == FormatStyle::LK_Proto) {
37443735
if (Right.is(tok::l_square))
37453736
return 1;
37463737
if (Right.is(tok::period))
@@ -4194,9 +4185,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
41944185
const auto SpaceRequiredForArrayInitializerLSquare =
41954186
[](const FormatToken &LSquareTok, const FormatStyle &Style) {
41964187
return Style.SpacesInContainerLiterals ||
4197-
((Style.Language == FormatStyle::LK_Proto ||
4198-
Style.Language == FormatStyle::LK_TextProto) &&
4199-
!Style.Cpp11BracedListStyle &&
4188+
(Style.isProto() && !Style.Cpp11BracedListStyle &&
42004189
LSquareTok.endsSequence(tok::l_square, tok::colon,
42014190
TT_SelectorName));
42024191
};
@@ -4462,8 +4451,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
44624451
Right.is(TT_TemplateOpener)) {
44634452
return true;
44644453
}
4465-
} else if (Style.Language == FormatStyle::LK_Proto ||
4466-
Style.Language == FormatStyle::LK_TextProto) {
4454+
} else if (Style.isProto()) {
44674455
if (Right.is(tok::period) &&
44684456
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
44694457
Keywords.kw_repeated, Keywords.kw_extend)) {
@@ -5085,8 +5073,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
50855073
return true;
50865074
} else if (Style.BreakAdjacentStringLiterals &&
50875075
(Style.isCpp() || Style.isProto() ||
5088-
Style.Language == FormatStyle::LK_TableGen ||
5089-
Style.Language == FormatStyle::LK_TextProto)) {
5076+
Style.Language == FormatStyle::LK_TableGen)) {
50905077
if (Left.isStringLiteral() && Right.isStringLiteral())
50915078
return true;
50925079
}
@@ -5338,9 +5325,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
53385325
// together.
53395326
//
53405327
// We ensure elsewhere that extensions are always on their own line.
5341-
if ((Style.Language == FormatStyle::LK_Proto ||
5342-
Style.Language == FormatStyle::LK_TextProto) &&
5343-
Right.is(TT_SelectorName) && Right.isNot(tok::r_square) && Right.Next) {
5328+
if (Style.isProto() && Right.is(TT_SelectorName) &&
5329+
Right.isNot(tok::r_square) && Right.Next) {
53445330
// Keep `@submessage` together in:
53455331
// @submessage { key: value }
53465332
if (Left.is(tok::at))
@@ -5554,8 +5540,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
55545540
return false;
55555541
}
55565542
if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
5557-
if (Style.Language == FormatStyle::LK_Proto ||
5558-
Style.Language == FormatStyle::LK_TextProto) {
5543+
if (Style.isProto()) {
55595544
if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
55605545
return false;
55615546
// Prevent cases like:

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
438438
[[fallthrough]];
439439
}
440440
case tok::kw_case:
441-
if (Style.isProto() || Style.isVerilog() ||
441+
if (Style.Language == FormatStyle::LK_Proto || Style.isVerilog() ||
442442
(Style.isJavaScript() && Line->MustBeDeclaration)) {
443443
// Proto: there are no switch/case statements
444444
// Verilog: Case labels don't have this word. We handle case
@@ -1527,7 +1527,7 @@ void UnwrappedLineParser::parseStructuralElement(
15271527
break;
15281528
case tok::kw_case:
15291529
// Proto: there are no switch/case statements.
1530-
if (Style.isProto()) {
1530+
if (Style.Language == FormatStyle::LK_Proto) {
15311531
nextToken();
15321532
return;
15331533
}
@@ -2034,7 +2034,7 @@ void UnwrappedLineParser::parseStructuralElement(
20342034
break;
20352035
case tok::kw_case:
20362036
// Proto: there are no switch/case statements.
2037-
if (Style.isProto()) {
2037+
if (Style.Language == FormatStyle::LK_Proto) {
20382038
nextToken();
20392039
return;
20402040
}

0 commit comments

Comments
 (0)