Author: Emilia Dreamer Date: 2023-03-26T04:38:52+03:00 New Revision: a8d2bff290e1b86b4bca4007493205b4878c4f68
URL: https://github.com/llvm/llvm-project/commit/a8d2bff290e1b86b4bca4007493205b4878c4f68 DIFF: https://github.com/llvm/llvm-project/commit/a8d2bff290e1b86b4bca4007493205b4878c4f68.diff LOG: [clang-format] Don't wrap struct return types as structs 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 Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D146281 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index d7758e7d068d..dfde6e4e0163 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -4916,8 +4916,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 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) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index eb1b563b3d2c..80efa8c4babf 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3205,10 +3205,13 @@ TEST_F(FormatTest, MultiLineControlStatements) { 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 @@ TEST_F(FormatTest, MultiLineControlStatements) { " 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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits