From 2f209ccfbe5e6b33088763b1e022ba876fb0f35c Mon Sep 17 00:00:00 2001 From: mydeveloperday Date: Fri, 20 Dec 2019 17:04:49 +0000 Subject: [PATCH] [clang-format] C# formatting a class with inheritance followed by an attribute specifier assume its a braces initializer Summary: https://bugs.llvm.org/show_bug.cgi?id=44340 The rule that prevents `... {} [[....]]` being treated as a braced initializer for C++ causes problems for C# with attributes, causing it to be incorrectly classified and then messing up the subsequent formatting. (see bug for details of formatting) Reviewers: mitchell-stellar, klimek, sammccall Reviewed By: mitchell-stellar Subscribers: cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D71769 --- clang/lib/Format/UnwrappedLineParser.cpp | 2 +- clang/unittests/Format/FormatTestCSharp.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 1e27eab..ead6b47 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -466,7 +466,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { (NextTok->is(tok::semi) && (!ExpectClassBody || LBraceStack.size() != 1)) || (NextTok->isBinaryOperator() && !NextIsObjCMethod); - if (NextTok->is(tok::l_square)) { + if (!Style.isCSharp() && NextTok->is(tok::l_square)) { // We can have an array subscript after a braced init // list, but C++11 attributes are expected after blocks. NextTok = Tokens->getNextToken(); diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index a2f3810..d8311d2 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -316,6 +316,27 @@ TEST_F(FormatTestCSharp, AttributesIndentation) { " }\n" "}\n", Style); + + verifyFormat("public A : Base\n" + "{\n" + "}\n" + "[Test]\n" + "public Foo()\n" + "{\n" + "}\n", + Style); + + verifyFormat("namespace\n" + "{\n" + "public A : Base\n" + "{\n" + "}\n" + "[Test]\n" + "public Foo()\n" + "{\n" + "}\n" + "}\n", + Style); } TEST_F(FormatTestCSharp, CSharpSpaceBefore) { -- 2.7.4