[clang-format] Correctly detect `!` as TT_NonNullAssertion after `default`.
authorMarek Kurdej <marek.kurdej+llvm.org@gmail.com>
Mon, 7 Mar 2022 17:48:24 +0000 (18:48 +0100)
committerMarek Kurdej <marek.kurdej+llvm.org@gmail.com>
Tue, 8 Mar 2022 12:35:26 +0000 (13:35 +0100)
Fixes https://github.com/llvm/llvm-project/issues/53153.

Depends on D121132.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D121136

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestCSharp.cpp

index 236e9a7..8f5f4e0 100644 (file)
@@ -1650,9 +1650,9 @@ private:
                 : Current.Previous->is(tok::identifier);
         if (IsIdentifier ||
             Current.Previous->isOneOf(
-                tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace,
-                tok::kw_false, tok::kw_true, Keywords.kw_type, Keywords.kw_get,
-                Keywords.kw_init, Keywords.kw_set) ||
+                tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
+                tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
+                Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
             Current.Previous->Tok.isLiteral()) {
           Current.setType(TT_NonNullAssertion);
           return;
index 2a76420..aa0304f 100644 (file)
@@ -1078,6 +1078,26 @@ public class SaleItem
                MicrosoftStyle);
 }
 
+TEST_F(FormatTestCSharp, DefaultLiteral) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
+
+  verifyFormat(
+      "T[] InitializeArray<T>(int length, T initialValue = default) {}", Style);
+  verifyFormat("System.Numerics.Complex fillValue = default;", Style);
+  verifyFormat("int Value { get } = default;", Style);
+  verifyFormat("int Value { get } = default!;", Style);
+  verifyFormat(R"(//
+public record Person {
+  public string GetInit { get; init; } = default!;
+};)",
+               Style);
+  verifyFormat(R"(//
+public record Person {
+  public string GetSet { get; set; } = default!;
+};)",
+               Style);
+}
+
 TEST_F(FormatTestCSharp, CSharpSpaces) {
   FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
   Style.SpaceBeforeSquareBrackets = false;