-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-format]: Add StaticInlineOnly and StaticInline options to ShortFunctionStyle
#133598
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 1 commit
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -300,8 +300,9 @@ class LineJoiner { | |||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| if (Style.AllowShortFunctionsOnASingleLine & | ||||||
| FormatStyle::SFS_InlineOnly) { | ||||||
| if (Style.AllowShortFunctionsOnASingleLine == | ||||||
| FormatStyle::SFS_InlineOnly || | ||||||
| Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline) { | ||||||
| // Just checking TheLine->Level != 0 is not enough, because it | ||||||
| // provokes treating functions inside indented namespaces as short. | ||||||
| if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace)) | ||||||
|
|
@@ -335,6 +336,32 @@ class LineJoiner { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (Style.AllowShortFunctionsOnASingleLine == | ||||||
| FormatStyle::SFS_StaticInlineOnly || | ||||||
| Style.AllowShortFunctionsOnASingleLine == | ||||||
| FormatStyle::SFS_StaticInline) { | ||||||
| // Check if the current line belongs to a static inline function | ||||||
| const auto *FirstNonCommentToken = | ||||||
| TheLine ? TheLine->getFirstNonComment() : nullptr; | ||||||
|
|
||||||
| // Look for 'static' and 'inline' keywords in any order | ||||||
|
||||||
| // Look for 'static' and 'inline' keywords in any order | |
| // Look for 'static' and 'inline' keywords in any order. |
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.
Thanks, fixed
Outdated
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.
You really don't need to go until the brace, the paren would suffice, or even better the name?
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.
Seems like the name is sufficient. Thanks for idea. I've made a fix; please take a look.
Outdated
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.
| // If we found both static and inline, allow merging | |
| // If we found both static and inline, allow merging. |
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.
Thanks, fixed
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.
Move this check into the ifs of the loop, exit early.
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.
Added additional check in while(). Please take a look.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15120,6 +15120,85 @@ TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { | |
| MergeInlineOnly); | ||
| } | ||
|
|
||
| TEST_F(FormatTest, PullStaticInlineFunctionDefinitionsIntoSingleLine) { | ||
| FormatStyle MergeStaticInlineOnly = getLLVMStyle(); | ||
| MergeStaticInlineOnly.AllowShortFunctionsOnASingleLine = | ||
| FormatStyle::SFS_StaticInlineOnly; | ||
| verifyFormat("static inline int f() { return 42; }", | ||
| "static inline int f() {\n" | ||
| " return 42; \n" | ||
| "}", | ||
| MergeStaticInlineOnly); | ||
| verifyFormat("inline static int f() { return 42; }", | ||
| "inline static int f() \n" | ||
| "{\n" | ||
| " return 42; \n" | ||
| "}", | ||
| MergeStaticInlineOnly); | ||
| verifyNoChange("int f() {\n" | ||
| " return 42;\n" | ||
| "}", | ||
| MergeStaticInlineOnly); | ||
|
|
||
| verifyFormat("void f1(void) {\n" | ||
| "}", | ||
| "void f1(void)\n" | ||
| "{\n" | ||
| "}", | ||
| MergeStaticInlineOnly); | ||
|
|
||
| verifyNoChange("int f2(int a, int b) {\n" | ||
| " return a + b;\n" | ||
| "}", | ||
| MergeStaticInlineOnly); | ||
|
|
||
| verifyNoChange("static void f3(void) {\n" | ||
| "}\n", | ||
| MergeStaticInlineOnly); | ||
|
|
||
| verifyFormat("static int f4(int a, int b) {\n" | ||
| " return a + b;\n" | ||
| "}\n", | ||
| MergeStaticInlineOnly); | ||
|
|
||
| verifyNoChange("static inline void f5(void) {}", MergeStaticInlineOnly); | ||
|
|
||
| verifyFormat("static inline int f6(int a, int b) { return a + b; }", | ||
| "static inline int f6(int a, int b) \n" | ||
| "{ return a + b; }", | ||
| MergeStaticInlineOnly); | ||
|
|
||
| verifyFormat("int f(int a, int b) {\n" | ||
| " return a + b;\n" | ||
| "}", | ||
| "int f(int a, int b) { return a + b; }", MergeStaticInlineOnly); | ||
|
|
||
| FormatStyle MergeStaticInline = getLLVMStyle(); | ||
| MergeStaticInline.AllowShortFunctionsOnASingleLine = | ||
| FormatStyle::SFS_StaticInline; | ||
| verifyFormat("static inline int f() { return 42; }", | ||
| "static inline int f() {\n" | ||
| " return 42; \n" | ||
| "}", | ||
| MergeStaticInline); | ||
| verifyFormat("inline static int f() { return 42; }", | ||
| "inline static int f() \n" | ||
| "{\n" | ||
| " return 42; \n" | ||
| "}", | ||
| MergeStaticInline); | ||
| verifyNoChange("int f() {\n" | ||
| " return 42;\n" | ||
| "}", | ||
| MergeStaticInline); | ||
|
|
||
| verifyFormat("void f1(void) {}", | ||
| "void f1(void)\n" | ||
| "{\n" | ||
| "}", | ||
| MergeStaticInline); | ||
| } | ||
|
Contributor
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 add some tests with attributes.
Author
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. I've added some tests with different attributes in different places. If you have any idea what should be added - small example (just code line) would be appreciated.
Contributor
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. How about
Author
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. I have no preference regarding constexpr because it was not mentioned in 'Inline' or 'InlineOnly' options before. Right now it won't be formatted if StaticInline is specified (I added unit test to show this). Please let me know if I need to change this. |
||
|
|
||
| TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { | ||
| FormatStyle MergeInlineOnly = getLLVMStyle(); | ||
| MergeInlineOnly.AllowShortFunctionsOnASingleLine = | ||
|
|
||
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.
This should have a different text.
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.
Thanks, fixed. Now it mention that empty function also merged.