[clang-tools-extra] 5321900 - [clang][clangd] Desugar array type.

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 9 04:19:52 PST 2021


Author: lh123
Date: 2021-12-09T20:12:48+08:00
New Revision: 53219009aaebd2c26028c1df05550183a94c489c

URL: https://github.com/llvm/llvm-project/commit/53219009aaebd2c26028c1df05550183a94c489c
DIFF: https://github.com/llvm/llvm-project/commit/53219009aaebd2c26028c1df05550183a94c489c.diff

LOG: [clang][clangd] Desugar array type.

Desugar array type.

Reviewed By: sammccall

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/unittests/HoverTests.cpp
    clang/lib/AST/ASTDiagnostic.cpp
    clang/test/Misc/diag-aka-types.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 7ec69d5d00ac7..740c907d7e4e4 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -914,7 +914,7 @@ class Foo {})cpp";
        [](HoverInfo &HI) {
          HI.Name = "arr";
          HI.Kind = index::SymbolKind::Variable;
-         HI.Type = "m_int[Size]";
+         HI.Type = {"m_int[Size]", "int[Size]"};
          HI.NamespaceScope = "";
          HI.Definition = "template <int Size> m_int arr[Size]";
          HI.TemplateParameters = {{{"int"}, {"Size"}, llvm::None}};
@@ -930,7 +930,7 @@ class Foo {})cpp";
        [](HoverInfo &HI) {
          HI.Name = "arr<4>";
          HI.Kind = index::SymbolKind::Variable;
-         HI.Type = "m_int[4]";
+         HI.Type = {"m_int[4]", "int[4]"};
          HI.NamespaceScope = "";
          HI.Definition = "m_int arr[4]";
        }},
@@ -998,6 +998,52 @@ class Foo {})cpp";
          HI.Definition = "template <typename T> using AA = A<T>";
          HI.Type = {"A<T>", "type-parameter-0-0"}; // FIXME: should be 'T'
          HI.TemplateParameters = {{{"typename"}, std::string("T"), llvm::None}};
+       }},
+      {// Constant array
+       R"cpp(
+          using m_int = int;
+
+          m_int ^[[arr]][10];
+         )cpp",
+       [](HoverInfo &HI) {
+         HI.Name = "arr";
+         HI.NamespaceScope = "";
+         HI.LocalScope = "";
+         HI.Kind = index::SymbolKind::Variable;
+         HI.Definition = "m_int arr[10]";
+         HI.Type = {"m_int[10]", "int[10]"};
+       }},
+      {// Incomplete array
+       R"cpp(
+          using m_int = int;
+
+          extern m_int ^[[arr]][];
+         )cpp",
+       [](HoverInfo &HI) {
+         HI.Name = "arr";
+         HI.NamespaceScope = "";
+         HI.LocalScope = "";
+         HI.Kind = index::SymbolKind::Variable;
+         HI.Definition = "extern m_int arr[]";
+         HI.Type = {"m_int[]", "int[]"};
+       }},
+      {// Dependent size array
+       R"cpp(
+          using m_int = int;
+
+          template<int Size>
+          struct Test {
+            m_int ^[[arr]][Size];
+          };
+         )cpp",
+       [](HoverInfo &HI) {
+         HI.Name = "arr";
+         HI.NamespaceScope = "";
+         HI.LocalScope = "Test<Size>::";
+         HI.AccessSpecifier = "public";
+         HI.Kind = index::SymbolKind::Field;
+         HI.Definition = "m_int arr[Size]";
+         HI.Type = {"m_int[Size]", "int[Size]"};
        }}};
   for (const auto &Case : Cases) {
     SCOPED_TRACE(Case.Code);

diff  --git a/clang/lib/AST/ASTDiagnostic.cpp b/clang/lib/AST/ASTDiagnostic.cpp
index e1b517db28ac9..4865f3f706b3f 100644
--- a/clang/lib/AST/ASTDiagnostic.cpp
+++ b/clang/lib/AST/ASTDiagnostic.cpp
@@ -131,6 +131,29 @@ QualType clang::desugarForDiagnostic(ASTContext &Context, QualType QT,
       }
     }
 
+    if (const auto *AT = dyn_cast<ArrayType>(Ty)) {
+      QualType ElementTy =
+          desugarForDiagnostic(Context, AT->getElementType(), ShouldAKA);
+      if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
+        QT = Context.getConstantArrayType(
+            ElementTy, CAT->getSize(), CAT->getSizeExpr(),
+            CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
+      else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
+        QT = Context.getVariableArrayType(
+            ElementTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
+            VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange());
+      else if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(AT))
+        QT = Context.getDependentSizedArrayType(
+            ElementTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
+            DSAT->getIndexTypeCVRQualifiers(), DSAT->getBracketsRange());
+      else if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT))
+        QT = Context.getIncompleteArrayType(ElementTy, IAT->getSizeModifier(),
+                                            IAT->getIndexTypeCVRQualifiers());
+      else
+        llvm_unreachable("Unhandled array type");
+      break;
+    }
+
     // Don't desugar magic Objective-C types.
     if (QualType(Ty,0) == Context.getObjCIdType() ||
         QualType(Ty,0) == Context.getObjCClassType() ||

diff  --git a/clang/test/Misc/diag-aka-types.cpp b/clang/test/Misc/diag-aka-types.cpp
index 3e051bdddec66..493038d0262c4 100644
--- a/clang/test/Misc/diag-aka-types.cpp
+++ b/clang/test/Misc/diag-aka-types.cpp
@@ -62,3 +62,9 @@ decltype(void()) (&f2)(int) = 0; // expected-error{{non-const lvalue reference t
 void (&f3)(decltype(1 + 2)) = 0; // expected-error{{non-const lvalue reference to type 'void (decltype(1 + 2))' (aka 'void (int)') cannot bind to a temporary of type 'int'}}
 decltype(1+2) (&f4)(double, decltype(1 + 2)) = 0; // expected-error{{non-const lvalue reference to type 'decltype(1 + 2) (double, decltype(1 + 2))' (aka 'int (double, int)') cannot bind to a temporary of type 'int'}}
 auto (&f5)() -> decltype(1+2) = 0; // expected-error{{non-const lvalue reference to type 'auto () -> decltype(1 + 2)' (aka 'auto () -> int') cannot bind to a temporary of type 'int'}}
+
+using C = decltype(1+2);;
+C a6[10];
+extern C a8[];
+int a9 = a6; // expected-error{{cannot initialize a variable of type 'int' with an lvalue of type 'C[10]' (aka 'int[10]')}}
+int a10 = a8; // expected-error{{cannot initialize a variable of type 'int' with an lvalue of type 'C[]' (aka 'int[]')}}


        


More information about the cfe-commits mailing list