[clang] [clang-format]: Add `Custom` to `ShortFunctionStyle`; add new AllowShortFunctionsOnASingleLineOptions for granular setup (PR #134337)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 4 18:31:59 PDT 2025
================
@@ -871,13 +871,81 @@ struct FormatStyle {
/// void f() { bar(); }
/// \endcode
SFS_All,
+ /// Configure merge behavior using AllowShortFunctionsOnASingleLineOptions
+ SFS_Custom,
};
/// Dependent on the value, ``int f() { return 0; }`` can be put on a
/// single line.
/// \version 3.5
ShortFunctionStyle AllowShortFunctionsOnASingleLine;
+ /// Precise control over merging short functions
+ /// \code
+ /// # Should be declared this way:
+ /// AllowShortFunctionsOnASingleLine: Custom
+ /// AllowShortFunctionsOnASingleLineOptions:
+ /// Empty: false
+ /// Inline: true
+ /// All: false
+ /// \endcode
+ struct ShortFunctionMergeFlags {
+ /// Only merge empty functions.
+ /// \code
+ /// void f() {}
+ /// void f2() {
+ /// bar2();
+ /// }
+ /// \endcode
+ bool Empty;
+ /// Only merge functions defined inside a class.
+ /// \code
+ /// class Foo {
+ /// void f() { foo(); }
+ /// };
+ /// void f() {
+ /// foo();
+ /// }
+ /// void f() {}
+ /// \endcode
+ bool Inline;
+ /// Merge all functions fitting on a single line.
+ /// \code
+ /// class Foo {
+ /// void f() { foo(); }
+ /// };
+ /// void f() { bar(); }
+ /// \endcode
+ bool All;
----------------
irymarchyk wrote:
That was my understanding too, but I have to set only 'All/Other' to true when SFS_All was specified to pass unit tests.
There seems to be JS dependency on SFS_Inline/SFS_InlineOnly:
https://github.com/llvm/llvm-project/blob/78905ce6cbd3fa8f8b467e7cad0e9a093c1b1c44/clang/lib/Format/TokenAnnotator.cpp#L5697
(in my PR it was changed a little bit)
And this is unit test which fails if I set every boolean to true when SFS_All is set:
https://github.com/llvm/llvm-project/blob/78905ce6cbd3fa8f8b467e7cad0e9a093c1b1c44/clang/unittests/Format/FormatTestJS.cpp#L1141
Seems like the code only works for Inline cases, but not for SFS_All case... So I have to set Inline to false when SFS_All (and Empty too to be consistent).
```
/Users/irymarchyk/dev/llvm-project/clang/unittests/Format/FormatTestJS.cpp:51: Failure
Expected equality of these values:
Code.str()
Which is: "var func = function() { return 1; };"
format(Code, Style)
Which is: "var func = function() {\n return 1;\n};"
With diff:
@@ -1,1 +1,3 @@
-var func = function() { return 1; };
+var func = function() {
+ return 1;
+};
Expected code is not stable
/Users/irymarchyk/dev/llvm-project/clang/unittests/Format/FormatTestJS.cpp:53: Failure
Expected equality of these values:
Code.str()
Which is: "var func = function() { return 1; };"
Result
Which is: "var func = function() {\n return 1;\n};"
With diff:
@@ -1,1 +1,3 @@
-var func = function() { return 1; };
+var func = function() {
+ return 1;
+};
Formatted:
var func = function() {
return 1;
};
/Users/irymarchyk/dev/llvm-project/clang/unittests/Format/FormatTestJS.cpp:51: Failure
Expected equality of these values:
Code.str()
Which is: "var outer = function() { var inner = function() { return 1; } };"
format(Code, Style)
Which is: "var outer = function() {\n var inner = function() { return 1; }\n};"
With diff:
@@ -1,1 +1,3 @@
-var outer = function() { var inner = function() { return 1; } };
+var outer = function() {
+ var inner = function() { return 1; }
+};
Expected code is not stable
/Users/irymarchyk/dev/llvm-project/clang/unittests/Format/FormatTestJS.cpp:53: Failure
Expected equality of these values:
Code.str()
Which is: "var outer = function() { var inner = function() { return 1; } };"
Result
Which is: "var outer = function() {\n var inner = function() { return 1; }\n};"
With diff:
@@ -1,1 +1,3 @@
-var outer = function() { var inner = function() { return 1; } };
+var outer = function() {
+ var inner = function() { return 1; }
+};
Formatted:
var outer = function() {
var inner = function() { return 1; }
};
[ FAILED ] FormatTestJS.InliningFunctionLiterals (24 ms)
```
https://github.com/llvm/llvm-project/pull/134337
More information about the cfe-commits
mailing list