[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)

Raoul Wols via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 10 12:11:20 PST 2024


https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746

>From 3ad404a10c3def9f92f399774f9f1507442bca1b Mon Sep 17 00:00:00 2001
From: Raoul Wols <raoulwols at gmail.com>
Date: Sat, 10 Feb 2024 20:52:03 +0100
Subject: [PATCH] [clangd] Do not render large initializer expressions from the
 preamble

An attempt is made to estimate the size of the initializer expression.
If it has less than 100 direct AST child nodes we'll assume it should render
OK.
---
 clang-tools-extra/clangd/Hover.cpp            | 10 +++++-
 .../clangd/unittests/HoverTests.cpp           | 34 +++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index 06b949bc4a2b55..b39c2b40468cd1 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -143,8 +143,16 @@ std::string printDefinition(const Decl *D, PrintingPolicy PP,
       // Initializers might be huge and result in lots of memory allocations in
       // some catostrophic cases. Such long lists are not useful in hover cards
       // anyway.
-      if (200 < TB.expandedTokens(IE->getSourceRange()).size())
+      const auto &SM = VD->getASTContext().getSourceManager();
+      if (!SM.isInMainFile(VD->getLocation())) {
+        const auto &Children = IE->children();
+        const size_t Length = std::distance(Children.begin(), Children.end());
+        if (100 < Length) {
+          PP.SuppressInitializers = true;
+        }
+      } else if (200 < TB.expandedTokens(IE->getSourceRange()).size()) {
         PP.SuppressInitializers = true;
+      }
     }
   }
   std::string Definition;
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 35db757b9c15b5..a77b2d66d9e7fa 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3956,6 +3956,40 @@ TEST(Hover, HideBigInitializers) {
   EXPECT_EQ(H->Definition, "int arr[]");
 }
 
+TEST(Hover, HideBigInitializersIncludedFromThePreamble) {
+  Annotations T(R"cpp(
+  #include "hugearray.h"
+  auto x = a^rr;
+  )cpp");
+  TestTU TU = TestTU::withCode(T.code());
+  TU.AdditionalFiles["hugearray.h"] = R"cpp(
+    #define A(x) x, x, x, x
+    #define B(x) A(A(A(A(x))))
+    int arr[256] = {B(0)};
+  )cpp";
+  auto AST = TU.build();
+  auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(H);
+  EXPECT_EQ(H->Definition, "int arr[256]");
+}
+
+TEST(Hover, DoNotHideSmallInitializersIncludedFromThePreamble) {
+  Annotations T(R"cpp(
+  #include "smallarray.h"
+  auto x = a^rr;
+  )cpp");
+  TestTU TU = TestTU::withCode(T.code());
+  TU.AdditionalFiles["smallarray.h"] = R"cpp(
+    #define A(x) x, x
+    #define B(x) A(A(x))
+    int arr[4] = {B(0)};
+  )cpp";
+  auto AST = TU.build();
+  auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(H);
+  EXPECT_EQ(H->Definition, "int arr[4] = {0, 0, 0, 0}");
+}
+
 #if defined(__aarch64__)
 // FIXME: AARCH64 sanitizer buildbots are broken after 72142fbac4.
 #define PREDEFINEMACROS_TEST(x) DISABLED_##x



More information about the cfe-commits mailing list