[clang] 2f209cc - [clang-format] C# formatting a class with inheritance followed by an attribute specifier assume its a braces initializer

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 20 09:07:36 PST 2019


Author: mydeveloperday
Date: 2019-12-20T17:07:00Z
New Revision: 2f209ccfbe5e6b33088763b1e022ba876fb0f35c

URL: https://github.com/llvm/llvm-project/commit/2f209ccfbe5e6b33088763b1e022ba876fb0f35c
DIFF: https://github.com/llvm/llvm-project/commit/2f209ccfbe5e6b33088763b1e022ba876fb0f35c.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTestCSharp.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 1e27eab1e744..ead6b4743207 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 a2f381078d42..d8311d269647 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) {


        


More information about the cfe-commits mailing list