[PATCH] D138300: [clangd] Support type hints for `decltype(expr)` in variable declarations

v1nh1shungry via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 18 08:13:24 PST 2022


v1nh1shungry updated this revision to Diff 476485.
v1nh1shungry added a comment.

Apply the comment's suggestion


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D138300

Files:
  clang-tools-extra/clangd/InlayHints.cpp
  clang-tools-extra/clangd/unittests/InlayHintTests.cpp


Index: clang-tools-extra/clangd/unittests/InlayHintTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1141,6 +1141,10 @@
                   ExpectedHint{": int &", "z"});
 }
 
+TEST(TypeHints, Decltype) {
+  assertTypeHints("decltype(0) $i[[i]];", ExpectedHint{": int", "i"});
+}
+
 TEST(TypeHints, NoQualifiers) {
   assertTypeHints(R"cpp(
     namespace A {
Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -296,17 +296,24 @@
       return true;
     }
 
-    if (D->getType()->getContainedAutoType()) {
-      if (!D->getType()->isDependentType()) {
+    auto T = D->getType();
+    if (T->getContainedAutoType()) {
+      if (!T->isDependentType()) {
         // Our current approach is to place the hint on the variable
         // and accordingly print the full type
         // (e.g. for `const auto& x = 42`, print `const int&`).
         // Alternatively, we could place the hint on the `auto`
         // (and then just print the type deduced for the `auto`).
-        addTypeHint(D->getLocation(), D->getType(), /*Prefix=*/": ");
+        addTypeHint(D->getLocation(), T, /*Prefix=*/": ");
       }
     }
 
+    // Show type hint for `decltype(expr)`, e.g.
+    // for `decltype(0) i;`, show `decltype(0) i: int;`
+    if (const auto *DT = llvm::dyn_cast<DecltypeType>(T))
+      addTypeHint(D->getLocation(), DT->getUnderlyingType(),
+                  /*Prefix=*/": ");
+
     // Handle templates like `int foo(auto x)` with exactly one instantiation.
     if (auto *PVD = llvm::dyn_cast<ParmVarDecl>(D)) {
       if (D->getIdentifier() && PVD->getType()->isDependentType() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138300.476485.patch
Type: text/x-patch
Size: 1895 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221118/c849c433/attachment-0001.bin>


More information about the cfe-commits mailing list