Skip to content

Commit 1e0174a

Browse files
committed
Treat C# using as a control statement
Contributed by jbcoe! Summary: Unless SpaceBeforeParensOptions is set to SBPO_Never, a space will be put between `using` and `(` in C# code. Reviewers: klimek, MyDeveloperDay, krasimir Reviewed By: krasimir Subscribers: MyDeveloperDay, cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D72144
1 parent aa0f37e commit 1e0174a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2869,7 +2869,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
28692869
// space between keywords and paren e.g. "using ("
28702870
if (Right.is(tok::l_paren))
28712871
if (Left.is(tok::kw_using))
2872-
return spaceRequiredBeforeParens(Left);
2872+
return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements ||
2873+
spaceRequiredBeforeParens(Right);
28732874
} else if (Style.Language == FormatStyle::LK_JavaScript) {
28742875
if (Left.is(TT_JsFatArrow))
28752876
return true;

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,46 @@ TEST_F(FormatTestCSharp, CSharpUsing) {
235235
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
236236
verifyFormat("public void foo () {\n"
237237
" using (StreamWriter sw = new StreamWriter (filenameA)) {}\n"
238+
" using () {}\n"
238239
"}",
239240
Style);
240241

242+
// Ensure clang-format affects top-level snippets correctly.
241243
verifyFormat("using (StreamWriter sw = new StreamWriter (filenameB)) {}",
242244
Style);
243245

244246
Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
245247
verifyFormat("public void foo() {\n"
246248
" using(StreamWriter sw = new StreamWriter(filenameB)) {}\n"
249+
" using() {}\n"
247250
"}",
248251
Style);
249252

253+
// Ensure clang-format affects top-level snippets correctly.
250254
verifyFormat("using(StreamWriter sw = new StreamWriter(filenameB)) {}",
251255
Style);
256+
257+
Style.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
258+
verifyFormat("public void foo() {\n"
259+
" using (StreamWriter sw = new StreamWriter(filenameA)) {}\n"
260+
" using () {}\n"
261+
"}",
262+
Style);
263+
264+
// Ensure clang-format affects top-level snippets correctly.
265+
verifyFormat("using (StreamWriter sw = new StreamWriter(filenameB)) {}",
266+
Style);
267+
268+
Style.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
269+
verifyFormat("public void foo() {\n"
270+
" using (StreamWriter sw = new StreamWriter (filenameA)) {}\n"
271+
" using() {}\n"
272+
"}",
273+
Style);
274+
275+
// Ensure clang-format affects top-level snippets correctly.
276+
verifyFormat("using (StreamWriter sw = new StreamWriter (filenameB)) {}",
277+
Style);
252278
}
253279

254280
TEST_F(FormatTestCSharp, CSharpRegions) {

0 commit comments

Comments
 (0)