Skip to content

Commit 3ccdaeb

Browse files
author
Ivan Rymarchyk
committed
PR-133598: [clang-format]: Add StaticInlineOnly and StaticInline options to ShortFunctionStyle
* fixed small PR requests such as grammar + added additional unit tests to test with attribute in different places
1 parent cc9c8d7 commit 3ccdaeb

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

clang/include/clang/Format/Format.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ struct FormatStyle {
870870
/// void f() {}
871871
/// \endcode
872872
SFS_Inline,
873-
/// Only merge functions defined as static inline. Implies ``empty``.
873+
/// Merge functions defined as static inline, also merge empty functions.
874874
/// \code
875875
/// void f5(void) {}
876876
/// static inline int f6(int a, int b) { return a + b; }

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,21 @@ class LineJoiner {
344344
const auto *FirstNonCommentToken =
345345
TheLine ? TheLine->getFirstNonComment() : nullptr;
346346

347-
// Look for 'static' and 'inline' keywords in any order
347+
// Look for 'static' and 'inline' keywords in any order.
348348
bool HasStatic = false;
349349
bool HasInline = false;
350350
const FormatToken *Tok = FirstNonCommentToken;
351351

352-
while (Tok && !Tok->is(TT_FunctionLBrace)) {
352+
while (Tok && !Tok->is(TT_FunctionDeclarationName) &&
353+
(!HasStatic || !HasInline)) {
353354
if (Tok->is(tok::kw_static))
354355
HasStatic = true;
355356
if (Tok->is(tok::kw_inline))
356357
HasInline = true;
357358
Tok = Tok->Next;
358359
}
359360

360-
// If we found both static and inline, allow merging
361+
// If we found both static and inline, allow merging.
361362
if (HasStatic && HasInline)
362363
return true;
363364
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15197,6 +15197,56 @@ TEST_F(FormatTest, PullStaticInlineFunctionDefinitionsIntoSingleLine) {
1519715197
"{\n"
1519815198
"}",
1519915199
MergeStaticInline);
15200+
15201+
// additional attribute tests
15202+
verifyFormat("inline static __attribute__((unused)) int f() { return 42; }",
15203+
"inline static __attribute__((unused)) int f() \n"
15204+
"{\n"
15205+
" return 42; \n"
15206+
"}",
15207+
MergeStaticInline);
15208+
verifyFormat("__attribute__((unused)) inline static int f() { return 42; }",
15209+
"__attribute__((unused)) inline static int f() \n"
15210+
"{\n"
15211+
" return 42; \n"
15212+
"}",
15213+
MergeStaticInline);
15214+
verifyFormat("inline static int f() __attribute__((unused)) { return 42; }",
15215+
"inline static int f() \n"
15216+
"__attribute__((unused)) \n"
15217+
"{\n"
15218+
" return 42; \n"
15219+
"}",
15220+
MergeStaticInline);
15221+
verifyFormat("__attribute__((unused)) inline static int f() { return 42; }",
15222+
"__attribute__((unused)) inline static int f() \n"
15223+
"{\n"
15224+
" return 42; \n"
15225+
"}",
15226+
MergeStaticInline);
15227+
15228+
verifyFormat("inline static const int f() { return 42; }",
15229+
"inline static const int f() \n"
15230+
"{\n"
15231+
" return 42; \n"
15232+
"}",
15233+
MergeStaticInline);
15234+
15235+
verifyFormat("_Noreturn static inline auto f() { return 42; }",
15236+
"_Noreturn static inline auto f() \n"
15237+
"{\n"
15238+
" return 42; \n"
15239+
"}",
15240+
MergeStaticInline);
15241+
15242+
verifyFormat("constexpr auto f() {\n"
15243+
" return 42;\n"
15244+
"}",
15245+
"constexpr auto f() \n"
15246+
"{\n"
15247+
" return 42; \n"
15248+
"}",
15249+
MergeStaticInline);
1520015250
}
1520115251

1520215252
TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {

0 commit comments

Comments
 (0)