-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang-format]: Add Custom
to ShortFunctionStyle
; add new AllowShortFunctionsOnASingleLineOptions for granular setup
#134337
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
df25a8b
e9e4e92
b1fd412
7f80e4a
bbe2273
5ff5810
70eafea
dd6493d
03fd9e9
3c0fa14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -613,15 +613,38 @@ template <> struct ScalarEnumerationTraits<FormatStyle::ShortBlockStyle> { | |
} | ||
}; | ||
|
||
template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { | ||
static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { | ||
IO.enumCase(Value, "None", FormatStyle::SFS_None); | ||
IO.enumCase(Value, "false", FormatStyle::SFS_None); | ||
IO.enumCase(Value, "All", FormatStyle::SFS_All); | ||
IO.enumCase(Value, "true", FormatStyle::SFS_All); | ||
IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); | ||
IO.enumCase(Value, "InlineOnly", FormatStyle::SFS_InlineOnly); | ||
IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); | ||
template <> struct MappingTraits<FormatStyle::ShortFunctionStyle> { | ||
static void enumInput(IO &IO, FormatStyle::ShortFunctionStyle &Value) { | ||
IO.enumCase(Value, "None", FormatStyle::ShortFunctionStyle({})); | ||
IO.enumCase(Value, "Empty", | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/false, | ||
/*Other=*/false})); | ||
IO.enumCase(Value, "Inline", | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/true, | ||
/*Other=*/false})); | ||
IO.enumCase(Value, "InlineOnly", | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/false, | ||
/*Inline=*/true, | ||
/*Other=*/false})); | ||
IO.enumCase(Value, "All", | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/true, | ||
/*Other=*/true})); | ||
|
||
// For backward compatibility. | ||
IO.enumCase(Value, "true", | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/true, | ||
/*Other=*/true})); | ||
IO.enumCase(Value, "false", FormatStyle::ShortFunctionStyle({})); | ||
} | ||
|
||
static void mapping(IO &IO, FormatStyle::ShortFunctionStyle &Value) { | ||
IO.mapOptional("Empty", Value.Empty); | ||
IO.mapOptional("Inline", Value.Inline); | ||
IO.mapOptional("Other", Value.Other); | ||
} | ||
}; | ||
|
||
|
@@ -1500,7 +1523,10 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { | |
LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; | ||
LLVMStyle.AllowShortCompoundRequirementOnASingleLine = true; | ||
LLVMStyle.AllowShortEnumsOnASingleLine = true; | ||
LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; | ||
LLVMStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please let me know if it is acceptable to make static functions like |
||
/*Inline=*/true, | ||
/*Other=*/true}); | ||
LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; | ||
LLVMStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; | ||
LLVMStyle.AllowShortLoopsOnASingleLine = false; | ||
|
@@ -1787,7 +1813,10 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { | |
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign; | ||
GoogleStyle.AlignTrailingComments = {}; | ||
GoogleStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Never; | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/false, | ||
/*Other=*/false}); | ||
GoogleStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; | ||
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; | ||
GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; | ||
|
@@ -1797,7 +1826,10 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { | |
} else if (Language == FormatStyle::LK_JavaScript) { | ||
GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | ||
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign; | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/false, | ||
/*Other=*/false}); | ||
// TODO: still under discussion whether to switch to SLS_All. | ||
GoogleStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; | ||
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; | ||
|
@@ -1814,7 +1846,10 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { | |
GoogleStyle.NamespaceIndentation = FormatStyle::NI_All; | ||
GoogleStyle.SpacesInContainerLiterals = false; | ||
} else if (Language == FormatStyle::LK_Proto) { | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/false, | ||
/*Other=*/false}); | ||
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; | ||
// This affects protocol buffer options specifications and text protos. | ||
// Text protos are currently mostly formatted inside C++ raw string literals | ||
|
@@ -1833,7 +1868,10 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { | |
GoogleStyle.IncludeStyle.IncludeBlocks = | ||
tooling::IncludeStyle::IBS_Preserve; | ||
} else if (Language == FormatStyle::LK_CSharp) { | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; | ||
GoogleStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/false, | ||
/*Other=*/false}); | ||
GoogleStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; | ||
GoogleStyle.BreakStringLiterals = false; | ||
GoogleStyle.ColumnLimit = 100; | ||
|
@@ -1892,7 +1930,10 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { | |
ChromiumStyle.AllowShortLoopsOnASingleLine = false; | ||
} else { | ||
ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; | ||
ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; | ||
ChromiumStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/true, | ||
/*Other=*/false}); | ||
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; | ||
ChromiumStyle.AllowShortLoopsOnASingleLine = false; | ||
ChromiumStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine; | ||
|
@@ -1906,7 +1947,10 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { | |
FormatStyle getMozillaStyle() { | ||
FormatStyle MozillaStyle = getLLVMStyle(); | ||
MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; | ||
MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; | ||
MozillaStyle.AllowShortFunctionsOnASingleLine = | ||
FormatStyle::ShortFunctionStyle({/*Empty=*/true, | ||
/*Inline=*/true, | ||
/*Other=*/false}); | ||
MozillaStyle.AlwaysBreakAfterDefinitionReturnType = | ||
FormatStyle::DRTBS_TopLevel; | ||
MozillaStyle.BinPackArguments = false; | ||
|
@@ -1988,7 +2032,7 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) { | |
Style.BraceWrapping.BeforeWhile = false; | ||
Style.PenaltyReturnTypeOnItsOwnLine = 1000; | ||
Style.AllowShortEnumsOnASingleLine = false; | ||
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; | ||
Style.AllowShortFunctionsOnASingleLine = FormatStyle::ShortFunctionStyle({}); | ||
Style.AllowShortCaseLabelsOnASingleLine = false; | ||
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; | ||
Style.AllowShortLoopsOnASingleLine = false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we expose this option to user? I am not sure what it means if we set only Other, but not Inline and not Empty - then C/C++ code will behave like SFS_All. Probably we should set it when user chooses 'All' in configuration.