diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 5be398565c96..55ab654f9603 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2611,6 +2611,10 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, return Style.Language == FormatStyle::LK_JavaScript || !Left.TokenText.endswith("=*/"); if (Right.is(tok::l_paren)) { + // using (FileStream fs... + if (Style.isCSharp() && Left.is(tok::kw_using) && + Style.SpaceBeforeParens != FormatStyle::SBPO_Never) + return true; if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) || (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) return true; diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index 801adb28bd91..7fda9566dc8f 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -165,6 +165,21 @@ TEST_F(FormatTestCSharp, Attributes) { "public string Host {\n set;\n get;\n}"); } +TEST_F(FormatTestCSharp, CSharpUsing) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp); + Style.SpaceBeforeParens = FormatStyle::SBPO_Always; + verifyFormat("public void foo() {\n" + " using (StreamWriter sw = new StreamWriter (filenameA)) {}\n" + "}", + Style); + + Style.SpaceBeforeParens = FormatStyle::SBPO_Never; + verifyFormat("public void foo() {\n" + " using(StreamWriter sw = new StreamWriter(filenameB)) {}\n" + "}", + Style); +} + TEST_F(FormatTestCSharp, CSharpRegions) { verifyFormat("#region aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa " "aaaaaaaaaaaaaaa long region");