[PATCH] D75606: [clang-format] Improve identification of C# nullables

Phabricator via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 4 10:40:22 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGfe61bc1a0b5a: [clang-format] Improve identification of C# nullables (authored by Jonathan Coe <jbcoe at google.com>).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75606/new/

https://reviews.llvm.org/D75606

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
@@ -631,7 +631,17 @@
   FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
   Style.SpacesInSquareBrackets = false;
 
+  verifyFormat(R"(//
+public class A {
+  void foo() { int? value = some.bar(); }
+})",
+               Style); // int? is nullable not a conditional expression.
+
+  verifyFormat(R"(void foo(int? x, int? y, int? z) {})",
+               Style); // Nullables in function definitions.
+
   verifyFormat(R"(public float? Value;)", Style); // no space before `?`.
+
   verifyFormat(R"(int?[] arr = new int?[10];)",
                Style); // An array of a nullable type.
 }
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1011,7 +1011,12 @@
           Style.Language == FormatStyle::LK_JavaScript)
         break;
       if (Style.isCSharp()) {
-        if (Line.MustBeDeclaration && !Contexts.back().IsExpression) {
+        // `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)))) {
           Tok->Type = TT_CSharpNullable;
           break;
         }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75606.248242.patch
Type: text/x-patch
Size: 1634 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200304/d248e7aa/attachment.bin>


More information about the cfe-commits mailing list