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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rGdde8a0fe91cc: [clangd] show underlying type in type hint for `decltype(expr)` (authored by v1nh1shungry, committed by nridge).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D140814

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
@@ -1364,7 +1364,6 @@
 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 @@
 
     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) {
Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -662,7 +662,21 @@
                  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);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140814.486031.patch
Type: text/x-patch
Size: 2189 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230103/b93a8194/attachment.bin>


More information about the cfe-commits mailing list