[clang-tools-extra] dde8a0f - [clangd] show underlying type in type hint for `decltype(expr)`

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 3 10:59:00 PST 2023


Author: v1nh1shungry
Date: 2023-01-03T13:58:39-05:00
New Revision: dde8a0fe91ccd4b54d5bd0c18043e0dd35652e47

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

LOG: [clangd] show underlying type in type hint for `decltype(expr)`

Reviewed By: nridge

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index 6bbad09b9f7bd..7af39658885be 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -662,7 +662,21 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
                  sourceLocToPosition(SM, Spelled->back().endLocation())};
   }
 
+  static bool shouldPrintCanonicalType(QualType QT) {
+    // The sugared type is more useful in some cases, and the canonical
+    // type in other cases. For now, prefer the sugared type unless
+    // we are printing `decltype(expr)`. This could be refined further
+    // (see https://github.com/clangd/clangd/issues/1298).
+    if (QT->isDecltypeType())
+      return true;
+    if (const AutoType *AT = QT->getContainedAutoType())
+      if (AT->getDeducedType()->isDecltypeType())
+        return true;
+    return false;
+  }
+
   void addTypeHint(SourceRange R, QualType T, llvm::StringRef Prefix) {
+    TypeHintPolicy.PrintCanonicalTypes = shouldPrintCanonicalType(T);
     addTypeHint(R, T, Prefix, TypeHintPolicy);
   }
 

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index fa90abec02cbc..53621f97a6049 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1364,7 +1364,6 @@ TEST(TypeHints, Aliased) {
 TEST(TypeHints, Decltype) {
   assertTypeHints(R"cpp(
     $a[[decltype(0)]] a;
-    // FIXME: will be nice to show `: int` instead
     $b[[decltype(a)]] b;
     const $c[[decltype(0)]] &c = b;
 
@@ -1377,11 +1376,13 @@ TEST(TypeHints, Decltype) {
 
     template <class, class> struct Foo;
     using G = Foo<$g[[decltype(0)]], float>;
+
+    auto $h[[h]] = $i[[decltype(0)]]{};
   )cpp",
-                  ExpectedHint{": int", "a"},
-                  ExpectedHint{": decltype(0)", "b"},
+                  ExpectedHint{": int", "a"}, ExpectedHint{": int", "b"},
                   ExpectedHint{": int", "c"}, ExpectedHint{": int", "e"},
-                  ExpectedHint{": int", "f"}, ExpectedHint{": int", "g"});
+                  ExpectedHint{": int", "f"}, ExpectedHint{": int", "g"},
+                  ExpectedHint{": int", "h"}, ExpectedHint{": int", "i"});
 }
 
 TEST(DesignatorHints, Basic) {


        


More information about the cfe-commits mailing list