diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -4916,8 +4916,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) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/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) {