[PATCH] D75983: [clang-format] Improved identification of C# nullables
Jonathan B Coe via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 11 05:09:03 PDT 2020
jbcoe created this revision.
jbcoe added a reviewer: krasimir.
jbcoe added a project: clang-format.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Allow '?' inside C# generics.
Do not mistake casts like (Type?) as conditional operators.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D75983
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestCSharp.cpp
Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -650,6 +650,10 @@
verifyFormat(R"(int?[] arr = new int?[10];)",
Style); // An array of a nullable type.
+
+ verifyFormat(R"(var x = (int?)y;)", Style); // Cast to a nullable type.
+
+ verifyFormat(R"(var x = new MyContainer<int?>();)", Style); // Generics.
}
TEST_F(FormatTestCSharp, CSharpArraySubscripts) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -131,7 +131,7 @@
}
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
(CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
- Style.Language != FormatStyle::LK_Proto &&
+ !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
Style.Language != FormatStyle::LK_TextProto))
return false;
// If a && or || is found and interpreted as a binary operator, this set
@@ -1013,12 +1013,13 @@
Style.Language == FormatStyle::LK_JavaScript)
break;
if (Style.isCSharp()) {
- // `Type? name;` and `Type? name =` can only be nullable types.
+ // `Type?)`, `Type?>`, `Type? name;`and `Type? name =` can only be
+ // nullable types.
// Line.MustBeDeclaration will be true for `Type? name;`.
- if (!Contexts.back().IsExpression &&
- (Line.MustBeDeclaration ||
- (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
- Tok->Next->Next->is(tok::equal)))) {
+ if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) ||
+ (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) ||
+ (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
+ Tok->Next->Next->is(tok::equal))) {
Tok->Type = TT_CSharpNullable;
break;
}
@@ -2961,10 +2962,12 @@
if (Right.is(TT_CSharpNullable))
return false;
- // Require space after ? in nullable types.
- if (Left.is(TT_CSharpNullable))
+ // Require space after ? in nullable types except in generics and casts.
+ if (Left.is(TT_CSharpNullable)) {
+ if (Right.isOneOf(TT_TemplateCloser, tok::r_paren))
+ return false;
return true;
-
+ }
// No space before or after '?.'.
if (Left.is(TT_CSharpNullConditional) || Right.is(TT_CSharpNullConditional))
return false;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75983.249589.patch
Type: text/x-patch
Size: 2711 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200311/389f1529/attachment.bin>
More information about the cfe-commits
mailing list