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

- fix qualifiers
- fix recursive `decltype` (partly)
- support return type


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,29 @@
                   ExpectedHint{": int &", "z"});
 }
 
+TEST(TypeHints, Decltype) {
+  assertTypeHints(R"cpp(
+    decltype(0) $a[[a]];
+    decltype(a) $b[[b]];
+    const decltype(0) $c[[c]] = 0;
+  )cpp",
+                  ExpectedHint{": int", "a"}, ExpectedHint{": int", "b"},
+                  ExpectedHint{": const int", "c"});
+  assertTypeHints(R"cpp(
+    decltype(0) $i[[i]] = 0;
+    // FIXME: Nice to show `: int &`
+    decltype((i)) $a[[a]] = i;
+  )cpp",
+                  ExpectedHint{": int", "i"},
+                  ExpectedHint{": decltype(0) &", "a"});
+  // FIXME: Support type hints for l/r reference
+  assertTypeHints(R"cpp(
+    int i = 0;
+    decltype(0) &$a[[a]] = i;
+    decltype(0) &&$b[[b]] = 0;
+  )cpp");
+}
+
 TEST(TypeHints, NoQualifiers) {
   assertTypeHints(R"cpp(
     namespace A {
@@ -1274,6 +1297,8 @@
 
     auto f5($noreturn[[)]] {}
 
+    decltype(0) f6($ret3[[)]];
+
     // `auto` conversion operator
     struct A {
       operator auto($retConv[[)]] { return 42; }
@@ -1287,7 +1312,7 @@
   )cpp",
       ExpectedHint{"-> int", "ret1a"}, ExpectedHint{"-> int", "ret1b"},
       ExpectedHint{"-> int &", "ret2"}, ExpectedHint{"-> void", "noreturn"},
-      ExpectedHint{"-> int", "retConv"});
+      ExpectedHint{"-> int", "ret3"}, ExpectedHint{"-> int", "retConv"});
 }
 
 TEST(TypeHints, DependentType) {
Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -279,10 +279,15 @@
   }
 
   void addReturnTypeHint(FunctionDecl *D, SourceRange Range) {
-    auto *AT = D->getReturnType()->getContainedAutoType();
-    if (!AT || AT->getDeducedType().isNull())
-      return;
-    addTypeHint(Range, D->getReturnType(), /*Prefix=*/"-> ");
+    auto RT = D->getReturnType();
+
+    if (auto *AT = RT->getContainedAutoType();
+        AT && !AT->getDeducedType().isNull())
+      addTypeHint(Range, RT, /*Prefix=*/"-> ");
+
+    if (llvm::isa<DecltypeType>(RT))
+      if (auto UT = getUnderlyingType(RT); !UT->isDependentType())
+        addTypeHint(Range, UT, /*Prefix=*/"-> ");
   }
 
   bool VisitVarDecl(VarDecl *D) {
@@ -296,14 +301,24 @@
       return true;
     }
 
-    if (D->getType()->getContainedAutoType()) {
-      if (!D->getType()->isDependentType()) {
+    auto T = D->getType();
+
+    // Show type hint for `decltype(expr)`, e.g.
+    // for `decltype(0) i;`, show `decltype(0) i: int;`
+    if (llvm::isa<DecltypeType>(T)) {
+      if (auto UT = getUnderlyingType(T); !UT->isDependentType())
+        addTypeHint(D->getLocation(), UT, /*Prefix=*/": ");
+      return true;
+    }
+
+    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=*/": ");
       }
     }
 
@@ -375,6 +390,14 @@
 private:
   using NameVec = SmallVector<StringRef, 8>;
 
+  static QualType getUnderlyingType(QualType T) {
+    auto LFQ = T.getLocalFastQualifiers();
+    while (const auto *DT = dyn_cast<DecltypeType>(T))
+      T = DT->getUnderlyingType();
+    T.setLocalFastQualifiers(LFQ);
+    return T;
+  }
+
   void processCall(const FunctionDecl *Callee,
                    llvm::ArrayRef<const Expr *> Args) {
     if (!Cfg.InlayHints.Parameters || Args.size() == 0 || !Callee)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to