[PATCH] D115107: [clang][clangd] Desugar array type.

2021-12-09 Thread liu hui via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG53219009aaeb: [clang][clangd] Desugar array type. (authored 
by lh123).

Changed prior to commit:
  https://reviews.llvm.org/D115107?vs=391884=393110#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D115107

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

Index: clang/test/Misc/diag-aka-types.cpp
===
--- clang/test/Misc/diag-aka-types.cpp
+++ clang/test/Misc/diag-aka-types.cpp
@@ -62,3 +62,9 @@
 void ()(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) ()(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 ()() -> 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[]')}}
Index: clang/lib/AST/ASTDiagnostic.cpp
===
--- clang/lib/AST/ASTDiagnostic.cpp
+++ clang/lib/AST/ASTDiagnostic.cpp
@@ -131,6 +131,29 @@
   }
 }
 
+if (const auto *AT = dyn_cast(Ty)) {
+  QualType ElementTy =
+  desugarForDiagnostic(Context, AT->getElementType(), ShouldAKA);
+  if (const auto *CAT = dyn_cast(AT))
+QT = Context.getConstantArrayType(
+ElementTy, CAT->getSize(), CAT->getSizeExpr(),
+CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
+  else if (const auto *VAT = dyn_cast(AT))
+QT = Context.getVariableArrayType(
+ElementTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
+VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange());
+  else if (const auto *DSAT = dyn_cast(AT))
+QT = Context.getDependentSizedArrayType(
+ElementTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
+DSAT->getIndexTypeCVRQualifiers(), DSAT->getBracketsRange());
+  else if (const auto *IAT = dyn_cast(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() ||
Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -914,7 +914,7 @@
[](HoverInfo ) {
  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  m_int arr[Size]";
  HI.TemplateParameters = {{{"int"}, {"Size"}, llvm::None}};
@@ -930,7 +930,7 @@
[](HoverInfo ) {
  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 @@
  HI.Definition = "template  using AA = A";
  HI.Type = {"A", "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.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.Name = "arr";
+ HI.NamespaceScope = "";
+ HI.LocalScope = "";
+ HI.Kind = index::SymbolKind::Variable;
+ HI.Definition = "extern m_int arr[]";
+ HI.Type = {"m_int[]", 

[PATCH] D115107: [clang][clangd] Desugar array type.

2021-12-07 Thread Sam McCall via Phabricator via cfe-commits
sammccall accepted this revision.
sammccall added a comment.
This revision is now accepted and ready to land.

Nice!




Comment at: clang/lib/AST/ASTDiagnostic.cpp:134
 
+if (const auto *CAT = dyn_cast(Ty)) {
+  QualType ElementTy =

this doesn't directly express the idea "we handle all array types". You *could* 
write:

```
if (const auto *AT = dyn_cast(Ty)) {
 QualType ElementTy = desugar(...);
 switch(AT->getTypeClass()) {
case Type::ConstantArray:
  QT = Context.getConstantArrayType(...);
  break;
...
default:
  llvm_unreachable("Unhandled array type");
 }
 break;
}
```

Up to you whether you think this is clearer.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D115107

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115107: [clang][clangd] Desugar array type.

2021-12-04 Thread liu hui via Phabricator via cfe-commits
lh123 created this revision.
lh123 added reviewers: sammccall, kadircet.
lh123 added projects: clang, clang-tools-extra.
Herald added subscribers: usaxena95, arphaman.
lh123 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.

Desugar array type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115107

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

Index: clang/test/Misc/diag-aka-types.cpp
===
--- clang/test/Misc/diag-aka-types.cpp
+++ clang/test/Misc/diag-aka-types.cpp
@@ -62,3 +62,9 @@
 void ()(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) ()(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 ()() -> 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[]')}}
Index: clang/lib/AST/ASTDiagnostic.cpp
===
--- clang/lib/AST/ASTDiagnostic.cpp
+++ clang/lib/AST/ASTDiagnostic.cpp
@@ -131,6 +131,38 @@
   }
 }
 
+if (const auto *CAT = dyn_cast(Ty)) {
+  QualType ElementTy =
+  desugarForDiagnostic(Context, CAT->getElementType(), ShouldAKA);
+  QT = Context.getConstantArrayType(
+  ElementTy, CAT->getSize(), CAT->getSizeExpr(), CAT->getSizeModifier(),
+  CAT->getIndexTypeCVRQualifiers());
+  break;
+}
+if (const auto *VAT = dyn_cast(Ty)) {
+  QualType ElementTy =
+  desugarForDiagnostic(Context, VAT->getElementType(), ShouldAKA);
+  QT = Context.getVariableArrayType(
+  ElementTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
+  VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange());
+  break;
+}
+if (const auto *DSAT = dyn_cast(Ty)) {
+  QualType ElementTy =
+  desugarForDiagnostic(Context, DSAT->getElementType(), ShouldAKA);
+  QT = Context.getDependentSizedArrayType(
+  ElementTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
+  DSAT->getIndexTypeCVRQualifiers(), DSAT->getBracketsRange());
+  break;
+}
+if (const auto *IAT = dyn_cast(Ty)) {
+  QualType ElementTy =
+  desugarForDiagnostic(Context, IAT->getElementType(), ShouldAKA);
+  QT = Context.getIncompleteArrayType(ElementTy, IAT->getSizeModifier(),
+  IAT->getIndexTypeCVRQualifiers());
+  break;
+}
+
 // Don't desugar magic Objective-C types.
 if (QualType(Ty,0) == Context.getObjCIdType() ||
 QualType(Ty,0) == Context.getObjCClassType() ||
Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -969,6 +969,52 @@
  HI.Definition = "template  using AA = A";
  HI.Type = {"A", "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.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.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
+  struct Test {
+m_int ^[[arr]][Size];
+  };
+ )cpp",
+   [](HoverInfo ) {
+ HI.Name = "arr";
+ HI.NamespaceScope = "";
+ HI.LocalScope = "Test::";
+ HI.AccessSpecifier = "public";
+ HI.Kind = index::SymbolKind::Field;
+