[clang-tools-extra] c2810f2 - [clangd] Omit type hints that are too long

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 6 23:33:44 PDT 2021


Author: Nathan Ridge
Date: 2021-09-07T02:33:58-04:00
New Revision: c2810f2c1655593a48791327e9563417caf2e261

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

LOG: [clangd] Omit type hints that are too long

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

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 7c3c6a2421d83..bea5306daeb7e 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -331,8 +331,9 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
     if (!T.getTypePtrOrNull())
       return;
 
-    addInlayHint(R, InlayHintKind::TypeHint,
-                 std::string(Prefix) + T.getAsString(TypeHintPolicy));
+    std::string TypeName = T.getAsString(TypeHintPolicy);
+    if (TypeName.length() < TypeNameLimit)
+      addInlayHint(R, InlayHintKind::TypeHint, std::string(Prefix) + TypeName);
   }
 
   std::vector<InlayHint> &Results;
@@ -341,6 +342,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
   StringRef MainFileBuf;
   const HeuristicResolver *Resolver;
   PrintingPolicy TypeHintPolicy;
+
+  static const size_t TypeNameLimit = 32;
 };
 
 std::vector<InlayHint> inlayHints(ParsedAST &AST) {

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 6796a8ce70fff..62930d1f42b12 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -591,6 +591,17 @@ TEST(TypeHints, DependentType) {
   )cpp");
 }
 
+TEST(TypeHints, LongTypeName) {
+  assertTypeHints(R"cpp(
+    template <typename, typename, typename>
+    struct A {};
+    struct MultipleWords {};
+    A<MultipleWords, MultipleWords, MultipleWords> foo();
+    // Omit type hint past a certain length (currently 32)
+    auto var = foo();
+  )cpp");
+}
+
 // FIXME: Low-hanging fruit where we could omit a type hint:
 //  - auto x = TypeName(...);
 //  - auto x = (TypeName) (...);


        


More information about the cfe-commits mailing list