[PATCH] D146281: [clang-format] Don't wrap struct return types as structs
Emilia Dreamer via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 17 03:30:06 PDT 2023
rymiel created this revision.
rymiel added a project: clang-format.
rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay.
Herald added a project: All.
rymiel requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
When using BraceWrapping.AfterClass or BraceWrapping.AfterStruct, the
token annotator relies on the first token of the line to determine if
we're dealing with a struct or class, however, this check is faulty if
it's actually a function with an elaborated struct/class return type, as
is common in C.
This patch skips the check if the brace is already annotated as
FunctionLBrace, in which case we already know it's a function and should
be treated as such.
Fixes https://github.com/llvm/llvm-project/issues/58527
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D146281
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -3205,10 +3205,13 @@
format("try{foo();}catch(...){baz();}", Style));
Style.BraceWrapping.AfterFunction = true;
+ Style.BraceWrapping.AfterStruct = false;
Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
Style.ColumnLimit = 80;
verifyFormat("void shortfunction() { bar(); }", Style);
+ verifyFormat("struct T shortfunction() { return bar(); }", Style);
+ verifyFormat("struct T {};", Style);
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
verifyFormat("void shortfunction()\n"
@@ -3216,6 +3219,36 @@
" bar();\n"
"}",
Style);
+ verifyFormat("struct T shortfunction()\n"
+ "{\n"
+ " return bar();\n"
+ "}",
+ Style);
+ verifyFormat("struct T {};", Style);
+
+ Style.BraceWrapping.AfterFunction = false;
+ Style.BraceWrapping.AfterStruct = true;
+ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
+ verifyFormat("void shortfunction() { bar(); }", Style);
+ verifyFormat("struct T shortfunction() { return bar(); }", Style);
+ verifyFormat("struct T\n"
+ "{\n"
+ "};",
+ Style);
+
+ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
+ verifyFormat("void shortfunction() {\n"
+ " bar();\n"
+ "}",
+ Style);
+ verifyFormat("struct T shortfunction() {\n"
+ " return bar();\n"
+ "}",
+ Style);
+ verifyFormat("struct T\n"
+ "{\n"
+ "};",
+ Style);
}
TEST_F(FormatTest, BeforeWhile) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -4881,8 +4881,13 @@
return true;
}
- return (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
- (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
+ // Don't attempt to interpret struct return types as structs.
+ if (Right.isNot(TT_FunctionLBrace)) {
+ return (Line.startsWith(tok::kw_class) &&
+ Style.BraceWrapping.AfterClass) ||
+ (Line.startsWith(tok::kw_struct) &&
+ Style.BraceWrapping.AfterStruct);
+ }
}
if (Left.is(TT_ObjCBlockLBrace) &&
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146281.506031.patch
Type: text/x-patch
Size: 2709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230317/c87185cb/attachment-0001.bin>
More information about the cfe-commits
mailing list