Skip to content

Commit 09c8cfe

Browse files
authored
[clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (#135466)
Also remove redundant name qualifiers format::, FormatStyle::, and LanguageKind::.
1 parent bbc5d20 commit 09c8cfe

File tree

10 files changed

+86
-115
lines changed

10 files changed

+86
-115
lines changed

clang/include/clang/Format/Format.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,11 +3383,11 @@ struct FormatStyle {
33833383
}
33843384
bool isCSharp() const { return Language == LK_CSharp; }
33853385
bool isJson() const { return Language == LK_Json; }
3386+
bool isJava() const { return Language == LK_Java; }
33863387
bool isJavaScript() const { return Language == LK_JavaScript; }
33873388
bool isVerilog() const { return Language == LK_Verilog; }
3388-
bool isProto() const {
3389-
return Language == LK_Proto || Language == LK_TextProto;
3390-
}
3389+
bool isTextProto() const { return Language == LK_TextProto; }
3390+
bool isProto() const { return Language == LK_Proto || isTextProto(); }
33913391
bool isTableGen() const { return Language == LK_TableGen; }
33923392

33933393
/// The language that this format style targets.
@@ -5482,9 +5482,9 @@ struct FormatStyle {
54825482
// The memory management and ownership reminds of a birds nest: chicks
54835483
// leaving the nest take photos of the nest with them.
54845484
struct FormatStyleSet {
5485-
typedef std::map<FormatStyle::LanguageKind, FormatStyle> MapType;
5485+
typedef std::map<LanguageKind, FormatStyle> MapType;
54865486

5487-
std::optional<FormatStyle> Get(FormatStyle::LanguageKind Language) const;
5487+
std::optional<FormatStyle> Get(LanguageKind Language) const;
54885488

54895489
// Adds \p Style to this FormatStyleSet. Style must not have an associated
54905490
// FormatStyleSet.
@@ -5516,8 +5516,8 @@ struct FormatStyle {
55165516

55175517
/// Returns a format style complying with the LLVM coding standards:
55185518
/// http://llvm.org/docs/CodingStandards.html.
5519-
FormatStyle getLLVMStyle(
5520-
FormatStyle::LanguageKind Language = FormatStyle::LanguageKind::LK_Cpp);
5519+
FormatStyle
5520+
getLLVMStyle(FormatStyle::LanguageKind Language = FormatStyle::LK_Cpp);
55215521

55225522
/// Returns a format style complying with one of Google's style guides:
55235523
/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.

clang/lib/Format/BreakableToken.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static StringRef getLineCommentIndentPrefix(StringRef Comment,
4646
static constexpr StringRef KnownTextProtoPrefixes[] = {"####", "###", "##",
4747
"//", "#"};
4848
ArrayRef<StringRef> KnownPrefixes(KnownCStylePrefixes);
49-
if (Style.Language == FormatStyle::LK_TextProto)
49+
if (Style.isTextProto())
5050
KnownPrefixes = KnownTextProtoPrefixes;
5151

5252
assert(
@@ -576,7 +576,7 @@ BreakableBlockComment::BreakableBlockComment(
576576
IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
577577

578578
// Detect a multiline jsdoc comment and set DelimitersOnNewline in that case.
579-
if (Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) {
579+
if (Style.isJavaScript() || Style.isJava()) {
580580
if ((Lines[0] == "*" || Lines[0].starts_with("* ")) && Lines.size() > 1) {
581581
// This is a multiline jsdoc comment.
582582
DelimitersOnNewline = true;
@@ -694,7 +694,7 @@ const llvm::StringSet<>
694694
};
695695

696696
unsigned BreakableBlockComment::getContentIndent(unsigned LineIndex) const {
697-
if (Style.Language != FormatStyle::LK_Java && !Style.isJavaScript())
697+
if (!Style.isJava() && !Style.isJavaScript())
698698
return 0;
699699
// The content at LineIndex 0 of a comment like:
700700
// /** line 0 */

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static bool opensProtoMessageField(const FormatToken &LessTok,
158158
const FormatStyle &Style) {
159159
if (LessTok.isNot(tok::less))
160160
return false;
161-
return Style.Language == FormatStyle::LK_TextProto ||
161+
return Style.isTextProto() ||
162162
(Style.Language == FormatStyle::LK_Proto &&
163163
(LessTok.NestingLevel > 0 ||
164164
(LessTok.Previous && LessTok.Previous->is(tok::equal))));
@@ -281,7 +281,7 @@ LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
281281
State.LowestLevelOnLine = 0;
282282
State.IgnoreStackForComparison = false;
283283

284-
if (Style.Language == FormatStyle::LK_TextProto) {
284+
if (Style.isTextProto()) {
285285
// We need this in order to deal with the bin packing of text fields at
286286
// global scope.
287287
auto &CurrentState = State.Stack.back();
@@ -924,7 +924,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
924924
CurrentState.ContainsUnwrappedBuilder = true;
925925
}
926926

927-
if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
927+
if (Current.is(TT_LambdaArrow) && Style.isJava())
928928
CurrentState.NoLineBreak = true;
929929
if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
930930
(Previous.MatchingParen &&
@@ -1315,7 +1315,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
13151315
NextNonComment = &Current;
13161316

13171317
// Java specific bits.
1318-
if (Style.Language == FormatStyle::LK_Java &&
1318+
if (Style.isJava() &&
13191319
Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends)) {
13201320
return std::max(CurrentState.LastSpace,
13211321
CurrentState.Indent + Style.ContinuationIndentWidth);
@@ -1806,7 +1806,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
18061806
(Style.AlignOperands != FormatStyle::OAS_DontAlign ||
18071807
PrecedenceLevel < prec::Assignment) &&
18081808
(!Previous || Previous->isNot(tok::kw_return) ||
1809-
(Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
1809+
(!Style.isJava() && PrecedenceLevel > 0)) &&
18101810
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
18111811
PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
18121812
(!Style.isTableGen() ||
@@ -2453,8 +2453,8 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
24532453
? 0
24542454
: Current.UnbreakableTailLength;
24552455

2456-
if (Style.isVerilog() || Style.Language == FormatStyle::LK_Java ||
2457-
Style.isJavaScript() || Style.isCSharp()) {
2456+
if (Style.isVerilog() || Style.isJava() || Style.isJavaScript() ||
2457+
Style.isCSharp()) {
24582458
BreakableStringLiteralUsingOperators::QuoteStyleType QuoteStyle;
24592459
if (Style.isJavaScript() && Text.starts_with("'") &&
24602460
Text.ends_with("'")) {

clang/lib/Format/Format.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,13 +3591,12 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
35913591
return Replaces;
35923592
if (isLikelyXml(Code))
35933593
return Replaces;
3594-
if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
3595-
isMpegTS(Code)) {
3596-
return Replaces;
3597-
}
3598-
if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
3594+
if (Style.isJavaScript()) {
3595+
if (isMpegTS(Code))
3596+
return Replaces;
35993597
return sortJavaScriptImports(Style, Code, Ranges, FileName);
3600-
if (Style.Language == FormatStyle::LanguageKind::LK_Java)
3598+
}
3599+
if (Style.isJava())
36013600
return sortJavaImports(Style, Code, Ranges, FileName, Replaces);
36023601
if (Style.isCpp())
36033602
sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
@@ -3777,7 +3776,7 @@ reformat(const FormatStyle &Style, StringRef Code,
37773776
return {tooling::Replacements(), 0};
37783777
if (isLikelyXml(Code))
37793778
return {tooling::Replacements(), 0};
3780-
if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
3779+
if (Expanded.isJavaScript() && isMpegTS(Code))
37813780
return {tooling::Replacements(), 0};
37823781

37833782
// JSON only needs the formatting passing.

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
8989
tryParseJSRegexLiteral();
9090
handleTemplateStrings();
9191
}
92-
if (Style.Language == FormatStyle::LK_TextProto)
92+
if (Style.isTextProto())
9393
tryParsePythonComment();
9494
tryMergePreviousTokens();
9595
if (Style.isCSharp()) {
@@ -207,7 +207,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
207207
return;
208208
}
209209

210-
if (Style.Language == FormatStyle::LK_Java) {
210+
if (Style.isJava()) {
211211
static const tok::TokenKind JavaRightLogicalShiftAssign[] = {
212212
tok::greater, tok::greater, tok::greaterequal};
213213
if (tryMergeTokens(JavaRightLogicalShiftAssign, TT_BinaryOperator))
@@ -1239,8 +1239,8 @@ FormatToken *FormatTokenLexer::getNextToken() {
12391239
// finds comments that contain a backslash followed by a line break, truncates
12401240
// the comment token at the backslash, and resets the lexer to restart behind
12411241
// the backslash.
1242-
if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
1243-
FormatTok->is(tok::comment) && FormatTok->TokenText.starts_with("//")) {
1242+
if ((Style.isJavaScript() || Style.isJava()) && FormatTok->is(tok::comment) &&
1243+
FormatTok->TokenText.starts_with("//")) {
12441244
size_t BackslashPos = FormatTok->TokenText.find('\\');
12451245
while (BackslashPos != StringRef::npos) {
12461246
if (BackslashPos + 1 < FormatTok->TokenText.size() &&
@@ -1302,7 +1302,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
13021302
IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
13031303
FormatTok->Tok.setIdentifierInfo(&Info);
13041304
FormatTok->Tok.setKind(Info.getTokenID());
1305-
if (Style.Language == FormatStyle::LK_Java &&
1305+
if (Style.isJava() &&
13061306
FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete,
13071307
tok::kw_operator)) {
13081308
FormatTok->Tok.setKind(tok::identifier);

0 commit comments

Comments
 (0)