Commit cc9c8d7
Ivan Rymarchyk
[clang-format]: Add
Currently, the `ShortFunctionStyle` option in clang-format lacks the granularity to specifically control the single-line formatting of `static inline` C functions independently from other function types like regular empty functions.
**Problem:**
Users may want to enforce a style where:
1. **Only `static inline` functions** are allowed on a single line (if they fit), forcing all other functions (including empty ones) onto multiple lines. This is useful for keeping utility/helper functions concise while maintaining a consistent multi-line format for primary function definitions.
2. **`static inline` functions *or* empty functions** are allowed on a single line (if they fit), while other non-empty, non-`static inline` functions are forced onto multiple lines. This is a slightly less strict variation.
The existing `ShortFunctionStyle` options do not cover these specific C use cases adequately:
* `None`: Forces all functions multi-line.
* `Empty`: Allows *any* empty function on one line, not just `static inline` ones.
* `All`: Allows any short function on one line.
* `Inline`/`InlineOnly`: Primarily target C++ member functions or C++ free inline functions, not specifically the C `static inline` pattern.
**Proposed Solution:**
Introduce two new values for the `ShortFunctionStyle` enum (currently named `ShortFunctionStyle` internally, likely `SFS_...` values):
1. **`StaticInlineOnly`**
* **Configuration Name:** `StaticInlineOnly`
* **Internal Enum Value (Suggestion):** `SFS_StaticInlineOnly`
* **Behavior:** Allows *only* functions declared with both `static` and `inline` specifiers to be formatted on a single line, provided they fit within the `ColumnLimit`. All other functions (regular, static non-inline, inline non-static, empty or not) must be formatted across multiple lines.
2. **`StaticInline`**
* **Configuration Name:** `StaticInline`
* **Internal Enum Value (Suggestion):** `SFS_StaticInline`
* **Behavior:** Allows functions declared with both `static` and `inline` specifiers *or* functions with an empty body (`{}`) to be formatted on a single line, provided they fit within the `ColumnLimit`. Non-empty functions that are *not* `static inline` must be formatted across multiple lines. This effectively combines the `SFS_Empty` behavior with allowing non-empty `static inline` functions.
**Expected Formatting:**
* **With `ShortFunctionStyle: StaticInlineOnly`**
```c
void f1(void) // Multi-line (not static inline)
{
}
int f2(int a, int b) // Multi-line (not static inline)
{
return a + b;
}
static void f3(void) // Multi-line (not static inline)
{
}
static int f4(int a, int b) // Multi-line (not static inline)
{
return a + b;
}
static inline void f5(void) {} // Single-line allowed
static inline int f6(int a, int b) { return a + b; } // Single-line allowed (if fits)
inline void f7(void) // Multi-line (not static inline)
{
}
```
* **With `ShortFunctionStyle: StaticInline`** (Implies Empty)
```c
void f1(void) {} // Single-line allowed (empty)
int f2(int a, int b) // Multi-line (non-empty, not static inline)
{
return a + b;
}
static void f3(void) {} // Single-line allowed (empty)
static int f4(int a, int b) // Multi-line (non-empty, not static inline)
{
return a + b;
}
static inline void f5(void) {} // Single-line allowed (static inline and empty)
static inline int f6(int a, int b) { return a + b; } // Single-line allowed (static inline, if fits)
inline void f7(void) {} // Single-line allowed (empty)
```StaticInlineOnly and StaticInline options to ShortFunctionStyle
1 parent 8ea3f81 commit cc9c8d7
File tree
7 files changed
+150
-4
lines changed- clang
- docs
- include/clang/Format
- lib/Format
- unittests/Format
7 files changed
+150
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1926 | 1926 | | |
1927 | 1927 | | |
1928 | 1928 | | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
1929 | 1938 | | |
1930 | 1939 | | |
1931 | 1940 | | |
| |||
1949 | 1958 | | |
1950 | 1959 | | |
1951 | 1960 | | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
1952 | 1969 | | |
1953 | 1970 | | |
1954 | 1971 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
844 | 844 | | |
845 | 845 | | |
846 | 846 | | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
847 | 854 | | |
848 | 855 | | |
849 | 856 | | |
| |||
863 | 870 | | |
864 | 871 | | |
865 | 872 | | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
866 | 879 | | |
867 | 880 | | |
868 | 881 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
622 | 622 | | |
623 | 623 | | |
624 | 624 | | |
| 625 | + | |
| 626 | + | |
625 | 627 | | |
626 | 628 | | |
627 | 629 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5690 | 5690 | | |
5691 | 5691 | | |
5692 | 5692 | | |
5693 | | - | |
5694 | | - | |
| 5693 | + | |
| 5694 | + | |
| 5695 | + | |
| 5696 | + | |
5695 | 5697 | | |
5696 | 5698 | | |
5697 | 5699 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
303 | | - | |
304 | | - | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
305 | 306 | | |
306 | 307 | | |
307 | 308 | | |
| |||
335 | 336 | | |
336 | 337 | | |
337 | 338 | | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
338 | 365 | | |
339 | 366 | | |
340 | 367 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
620 | 620 | | |
621 | 621 | | |
622 | 622 | | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
623 | 629 | | |
624 | 630 | | |
625 | 631 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15120 | 15120 | | |
15121 | 15121 | | |
15122 | 15122 | | |
| 15123 | + | |
| 15124 | + | |
| 15125 | + | |
| 15126 | + | |
| 15127 | + | |
| 15128 | + | |
| 15129 | + | |
| 15130 | + | |
| 15131 | + | |
| 15132 | + | |
| 15133 | + | |
| 15134 | + | |
| 15135 | + | |
| 15136 | + | |
| 15137 | + | |
| 15138 | + | |
| 15139 | + | |
| 15140 | + | |
| 15141 | + | |
| 15142 | + | |
| 15143 | + | |
| 15144 | + | |
| 15145 | + | |
| 15146 | + | |
| 15147 | + | |
| 15148 | + | |
| 15149 | + | |
| 15150 | + | |
| 15151 | + | |
| 15152 | + | |
| 15153 | + | |
| 15154 | + | |
| 15155 | + | |
| 15156 | + | |
| 15157 | + | |
| 15158 | + | |
| 15159 | + | |
| 15160 | + | |
| 15161 | + | |
| 15162 | + | |
| 15163 | + | |
| 15164 | + | |
| 15165 | + | |
| 15166 | + | |
| 15167 | + | |
| 15168 | + | |
| 15169 | + | |
| 15170 | + | |
| 15171 | + | |
| 15172 | + | |
| 15173 | + | |
| 15174 | + | |
| 15175 | + | |
| 15176 | + | |
| 15177 | + | |
| 15178 | + | |
| 15179 | + | |
| 15180 | + | |
| 15181 | + | |
| 15182 | + | |
| 15183 | + | |
| 15184 | + | |
| 15185 | + | |
| 15186 | + | |
| 15187 | + | |
| 15188 | + | |
| 15189 | + | |
| 15190 | + | |
| 15191 | + | |
| 15192 | + | |
| 15193 | + | |
| 15194 | + | |
| 15195 | + | |
| 15196 | + | |
| 15197 | + | |
| 15198 | + | |
| 15199 | + | |
| 15200 | + | |
| 15201 | + | |
15123 | 15202 | | |
15124 | 15203 | | |
15125 | 15204 | | |
| |||
0 commit comments