[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 11:53:06 PST 2024
https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746
>From 8c850241cedeaad1bcc91c68ad7558f485d212e8 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
---
clang-tools-extra/clangd/Hover.cpp | 5 ++-
.../clangd/unittests/HoverTests.cpp | 34 +++++++++++++++++++
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index 06b949bc4a2b55..2ff2416c3d68a8 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -143,8 +143,11 @@ 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()) ||
+ 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..e591f246457bc1 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]");
+}
+
+// FIXME(rwols): Enable this test.
+TEST(Hover, DISABLED_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);
+}
+
#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