Skip to content

[clang-format][NFC] Add isJava() and isTextProto() in FormatStyle #135466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3383,11 +3383,11 @@ struct FormatStyle {
}
bool isCSharp() const { return Language == LK_CSharp; }
bool isJson() const { return Language == LK_Json; }
bool isJava() const { return Language == LK_Java; }
bool isJavaScript() const { return Language == LK_JavaScript; }
bool isVerilog() const { return Language == LK_Verilog; }
bool isProto() const {
return Language == LK_Proto || Language == LK_TextProto;
}
bool isTextProto() const { return Language == LK_TextProto; }
bool isProto() const { return Language == LK_Proto || isTextProto(); }
bool isTableGen() const { return Language == LK_TableGen; }

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

std::optional<FormatStyle> Get(FormatStyle::LanguageKind Language) const;
std::optional<FormatStyle> Get(LanguageKind Language) const;

// Adds \p Style to this FormatStyleSet. Style must not have an associated
// FormatStyleSet.
Expand Down Expand Up @@ -5516,8 +5516,8 @@ struct FormatStyle {

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

/// Returns a format style complying with one of Google's style guides:
/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Format/BreakableToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static StringRef getLineCommentIndentPrefix(StringRef Comment,
static constexpr StringRef KnownTextProtoPrefixes[] = {"####", "###", "##",
"//", "#"};
ArrayRef<StringRef> KnownPrefixes(KnownCStylePrefixes);
if (Style.Language == FormatStyle::LK_TextProto)
if (Style.isTextProto())
KnownPrefixes = KnownTextProtoPrefixes;

assert(
Expand Down Expand Up @@ -576,7 +576,7 @@ BreakableBlockComment::BreakableBlockComment(
IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());

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

unsigned BreakableBlockComment::getContentIndent(unsigned LineIndex) const {
if (Style.Language != FormatStyle::LK_Java && !Style.isJavaScript())
if (!Style.isJava() && !Style.isJavaScript())
return 0;
// The content at LineIndex 0 of a comment like:
// /** line 0 */
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static bool opensProtoMessageField(const FormatToken &LessTok,
const FormatStyle &Style) {
if (LessTok.isNot(tok::less))
return false;
return Style.Language == FormatStyle::LK_TextProto ||
return Style.isTextProto() ||
(Style.Language == FormatStyle::LK_Proto &&
(LessTok.NestingLevel > 0 ||
(LessTok.Previous && LessTok.Previous->is(tok::equal))));
Expand Down Expand Up @@ -281,7 +281,7 @@ LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
State.LowestLevelOnLine = 0;
State.IgnoreStackForComparison = false;

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

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

// Java specific bits.
if (Style.Language == FormatStyle::LK_Java &&
if (Style.isJava() &&
Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends)) {
return std::max(CurrentState.LastSpace,
CurrentState.Indent + Style.ContinuationIndentWidth);
Expand Down Expand Up @@ -1806,7 +1806,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
(Style.AlignOperands != FormatStyle::OAS_DontAlign ||
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
(Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
(!Style.isJava() && PrecedenceLevel > 0)) &&
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
Expand Down Expand Up @@ -2453,8 +2453,8 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
? 0
: Current.UnbreakableTailLength;

if (Style.isVerilog() || Style.Language == FormatStyle::LK_Java ||
Style.isJavaScript() || Style.isCSharp()) {
if (Style.isVerilog() || Style.isJava() || Style.isJavaScript() ||
Style.isCSharp()) {
BreakableStringLiteralUsingOperators::QuoteStyleType QuoteStyle;
if (Style.isJavaScript() && Text.starts_with("'") &&
Text.ends_with("'")) {
Expand Down
13 changes: 6 additions & 7 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,13 +3591,12 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
return Replaces;
if (isLikelyXml(Code))
return Replaces;
if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript &&
isMpegTS(Code)) {
return Replaces;
}
if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript)
if (Style.isJavaScript()) {
if (isMpegTS(Code))
return Replaces;
return sortJavaScriptImports(Style, Code, Ranges, FileName);
if (Style.Language == FormatStyle::LanguageKind::LK_Java)
}
if (Style.isJava())
return sortJavaImports(Style, Code, Ranges, FileName, Replaces);
if (Style.isCpp())
sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor);
Expand Down Expand Up @@ -3777,7 +3776,7 @@ reformat(const FormatStyle &Style, StringRef Code,
return {tooling::Replacements(), 0};
if (isLikelyXml(Code))
return {tooling::Replacements(), 0};
if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code))
if (Expanded.isJavaScript() && isMpegTS(Code))
return {tooling::Replacements(), 0};

// JSON only needs the formatting passing.
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
tryParseJSRegexLiteral();
handleTemplateStrings();
}
if (Style.Language == FormatStyle::LK_TextProto)
if (Style.isTextProto())
tryParsePythonComment();
tryMergePreviousTokens();
if (Style.isCSharp()) {
Expand Down Expand Up @@ -207,7 +207,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
return;
}

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