Skip to content

Commit

Permalink
[clang-format][NFC] Extend isProto() to also cover LK_TextProto (#73582)
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca committed Nov 29, 2023
1 parent efa1d0d commit 4c17452
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 47 deletions.
4 changes: 3 additions & 1 deletion clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3034,7 +3034,9 @@ struct FormatStyle {
bool isJson() const { return Language == LK_Json; }
bool isJavaScript() const { return Language == LK_JavaScript; }
bool isVerilog() const { return Language == LK_Verilog; }
bool isProto() const { return Language == LK_Proto; }
bool isProto() const {
return Language == LK_Proto || Language == LK_TextProto;
}

/// Language, this format style is targeted at.
/// \version 3.5
Expand Down
12 changes: 3 additions & 9 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return CurrentState.Indent;
}
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
(Current.is(tok::greater) &&
(Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto))) &&
(Current.is(tok::greater) && Style.isProto())) &&
State.Stack.size() > 1) {
if (Current.closesBlockOrBlockTypeList(Style))
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
Expand Down Expand Up @@ -1278,9 +1276,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
if (Current.is(tok::identifier) && Current.Next &&
(!Style.isVerilog() || Current.Next->is(tok::colon)) &&
(Current.Next->is(TT_DictLiteral) ||
((Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) &&
Current.Next->isOneOf(tok::less, tok::l_brace)))) {
(Style.isProto() && Current.Next->isOneOf(tok::less, tok::l_brace)))) {
return CurrentState.Indent;
}
if (NextNonComment->is(TT_ObjCStringLiteral) &&
Expand Down Expand Up @@ -1798,9 +1794,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
Current.MatchingParen->Previous &&
Current.MatchingParen->Previous->is(tok::comma);
AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto ||
!Style.BinPackArguments ||
Style.isProto() || !Style.BinPackArguments ||
(NextNonComment && NextNonComment->isOneOf(
TT_DesignatedInitializerPeriod,
TT_DesignatedInitializerLSquare));
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ bool FormatToken::opensBlockOrBlockTypeList(const FormatStyle &Style) const {
(is(tok::l_brace) &&
(getBlockKind() == BK_Block || is(TT_DictLiteral) ||
(!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
(is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto));
(is(tok::less) && Style.isProto());
}

TokenRole::~TokenRole() {}
Expand Down
5 changes: 1 addition & 4 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,11 +1313,8 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
}
}

if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) &&
Tok.is(tok::char_constant)) {
if ((Style.isJavaScript() || Style.isProto()) && Tok.is(tok::char_constant))
Tok.Tok.setKind(tok::string_literal);
}

if (Tok.is(tok::comment) && isClangFormatOn(Tok.TokenText))
FormattingDisabled = false;
Expand Down
41 changes: 13 additions & 28 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ class AnnotatingParser {
}
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
(CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
!Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
Style.Language != FormatStyle::LK_TextProto)) {
!Style.isCSharp() && !Style.isProto())) {
return false;
}
// If a && or || is found and interpreted as a binary operator, this set
Expand Down Expand Up @@ -678,8 +677,7 @@ class AnnotatingParser {
} else if (CurrentToken->is(tok::r_square) && Parent &&
Parent->is(TT_TemplateCloser)) {
Left->setType(TT_ArraySubscriptLSquare);
} else if (Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) {
} else if (Style.isProto()) {
// Square braces in LK_Proto can either be message field attributes:
//
// optional Aaa aaa = 1 [
Expand Down Expand Up @@ -906,8 +904,7 @@ class AnnotatingParser {
Previous = Previous->getPreviousNonComment();
if ((CurrentToken->is(tok::colon) &&
(!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) {
Style.isProto()) {
OpeningBrace.setType(TT_DictLiteral);
if (Previous->Tok.getIdentifierInfo() ||
Previous->is(tok::string_literal)) {
Expand Down Expand Up @@ -1049,9 +1046,7 @@ class AnnotatingParser {
Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
Tok->setType(TT_ModulePartitionColon);
} else if (Contexts.back().ColonIsDictLiteral ||
Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) {
} else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
Tok->setType(TT_DictLiteral);
if (Style.Language == FormatStyle::LK_TextProto) {
if (FormatToken *Previous = Tok->getPreviousNonComment())
Expand Down Expand Up @@ -1290,10 +1285,8 @@ class AnnotatingParser {
Tok->SpacesRequiredBefore = 1;
break;
case tok::kw_operator:
if (Style.Language == FormatStyle::LK_TextProto ||
Style.Language == FormatStyle::LK_Proto) {
if (Style.isProto())
break;
}
while (CurrentToken &&
!CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
if (CurrentToken->isOneOf(tok::star, tok::amp))
Expand Down Expand Up @@ -2877,9 +2870,7 @@ class ExpressionParser {
return prec::Conditional;
if (NextNonComment && Current->is(TT_SelectorName) &&
(NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
((Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) &&
NextNonComment->is(tok::less)))) {
(Style.isProto() && NextNonComment->is(tok::less)))) {
return prec::Assignment;
}
if (Current->is(TT_JsComputedPropertyName))
Expand Down Expand Up @@ -3740,7 +3731,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
// Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
if (Left.opensScope() && Right.closesScope())
return 200;
} else if (Style.isProto()) {
} else if (Style.Language == FormatStyle::LK_Proto) {
if (Right.is(tok::l_square))
return 1;
if (Right.is(tok::period))
Expand Down Expand Up @@ -4194,9 +4185,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
const auto SpaceRequiredForArrayInitializerLSquare =
[](const FormatToken &LSquareTok, const FormatStyle &Style) {
return Style.SpacesInContainerLiterals ||
((Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) &&
!Style.Cpp11BracedListStyle &&
(Style.isProto() && !Style.Cpp11BracedListStyle &&
LSquareTok.endsSequence(tok::l_square, tok::colon,
TT_SelectorName));
};
Expand Down Expand Up @@ -4462,8 +4451,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Right.is(TT_TemplateOpener)) {
return true;
}
} else if (Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) {
} else if (Style.isProto()) {
if (Right.is(tok::period) &&
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
Keywords.kw_repeated, Keywords.kw_extend)) {
Expand Down Expand Up @@ -5085,8 +5073,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
} else if (Style.BreakAdjacentStringLiterals &&
(Style.isCpp() || Style.isProto() ||
Style.Language == FormatStyle::LK_TableGen ||
Style.Language == FormatStyle::LK_TextProto)) {
Style.Language == FormatStyle::LK_TableGen)) {
if (Left.isStringLiteral() && Right.isStringLiteral())
return true;
}
Expand Down Expand Up @@ -5338,9 +5325,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
// together.
//
// We ensure elsewhere that extensions are always on their own line.
if ((Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) &&
Right.is(TT_SelectorName) && Right.isNot(tok::r_square) && Right.Next) {
if (Style.isProto() && Right.is(TT_SelectorName) &&
Right.isNot(tok::r_square) && Right.Next) {
// Keep `@submessage` together in:
// @submessage { key: value }
if (Left.is(tok::at))
Expand Down Expand Up @@ -5554,8 +5540,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return false;
}
if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
if (Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) {
if (Style.isProto()) {
if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
return false;
// Prevent cases like:
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
[[fallthrough]];
}
case tok::kw_case:
if (Style.isProto() || Style.isVerilog() ||
if (Style.Language == FormatStyle::LK_Proto || Style.isVerilog() ||
(Style.isJavaScript() && Line->MustBeDeclaration)) {
// Proto: there are no switch/case statements
// Verilog: Case labels don't have this word. We handle case
Expand Down Expand Up @@ -1527,7 +1527,7 @@ void UnwrappedLineParser::parseStructuralElement(
break;
case tok::kw_case:
// Proto: there are no switch/case statements.
if (Style.isProto()) {
if (Style.Language == FormatStyle::LK_Proto) {
nextToken();
return;
}
Expand Down Expand Up @@ -2034,7 +2034,7 @@ void UnwrappedLineParser::parseStructuralElement(
break;
case tok::kw_case:
// Proto: there are no switch/case statements.
if (Style.isProto()) {
if (Style.Language == FormatStyle::LK_Proto) {
nextToken();
return;
}
Expand Down

0 comments on commit 4c17452

Please sign in to comment.