Author: Jonathan Coe Date: 2020-04-16T14:22:34+01:00 New Revision: 07c1978b15b4e9daefbf358e6fd185b5aa269f98
URL: https://github.com/llvm/llvm-project/commit/07c1978b15b4e9daefbf358e6fd185b5aa269f98 DIFF: https://github.com/llvm/llvm-project/commit/07c1978b15b4e9daefbf358e6fd185b5aa269f98.diff LOG: [clang-format] Do not interpret C# deconstruction in a foreach as a cast Reviewers: krasimir Reviewed By: krasimir Subscribers: cfe-commits, MyDeveloperDay Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D78295 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTestCSharp.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 8204623645a4..6532f8108f08 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1775,6 +1775,10 @@ class AnnotatingParser { if (Tok.Next->is(tok::question)) return false; + // `foreach((A a, B b) in someList)` should not be seen as a cast. + if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp()) + return false; + // Functions which end with decorations like volatile, noexcept are unlikely // to be casts. if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const, diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index b0e4e76cefe7..67571d3909bf 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -624,6 +624,7 @@ TEST_F(FormatTestCSharp, CSharpSpaces) { Style.SpaceBeforeCpp11BracedList = true; Style.Cpp11BracedListStyle = false; Style.SpacesInContainerLiterals = false; + Style.SpaceAfterCStyleCast = false; verifyFormat(R"(new Car { "Door", 0.1 })", Style); verifyFormat(R"(new Car { 0.1, "Door" })", Style); @@ -642,6 +643,12 @@ TEST_F(FormatTestCSharp, CSharpSpaces) { verifyFormat(R"(char[,,] rawCharArray = MakeCharacterGrid();)", Style); + // Not seen as a C-style cast. + verifyFormat(R"(// +foreach ((A a, B b) in someList) { +})", + Style); + Style.SpacesInSquareBrackets = true; verifyFormat(R"(private float[ , ] Values;)", Style); verifyFormat(R"(string dirPath = args?[ 0 ];)", Style); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits