[clang-tools-extra] r338223 - [clangd] Add command-line option
Author: rwols Date: Sun Jul 29 12:12:42 2018 New Revision: 338223 URL: http://llvm.org/viewvc/llvm-project?rev=338223&view=rev Log: [clangd] Add command-line option to suppress the space and the circular dot prepended in a completion label. Modified: clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp Modified: clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp?rev=338223&r1=338222&r2=338223&view=diff == --- clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp (original) +++ clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp Sun Jul 29 12:12:42 2018 @@ -12,13 +12,13 @@ #include "Path.h" #include "Trace.h" #include "index/SymbolYAML.h" +#include "clang/Basic/Version.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" -#include "clang/Basic/Version.h" #include #include #include @@ -157,6 +157,13 @@ static llvm::cl::opt llvm::cl::init(clangd::CodeCompleteOptions().ShowOrigins), llvm::cl::Hidden); +static llvm::cl::opt HeaderInsertionDecorators( +"header-insertion-decorators", +llvm::cl::desc("Prepend a circular dot or space before the completion " + "label, depending on wether " + "an include line will be inserted or not."), +llvm::cl::init(true)); + static llvm::cl::opt YamlSymbolFile( "yaml-symbol-file", llvm::cl::desc( @@ -276,6 +283,10 @@ int main(int argc, char *argv[]) { CCOpts.Limit = LimitResults; CCOpts.BundleOverloads = CompletionStyle != Detailed; CCOpts.ShowOrigins = ShowOrigins; + if (!HeaderInsertionDecorators) { +CCOpts.IncludeIndicator.Insert.clear(); +CCOpts.IncludeIndicator.NoInsert.clear(); + } // Initialize and run ClangdLSPServer. ClangdLSPServer LSPServer(Out, CCOpts, CompileCommandsDirPath, Opts); ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r320524 - [clangd] (Attempt to) read clang-format file for document formatting
Author: rwols Date: Tue Dec 12 12:25:06 2017 New Revision: 320524 URL: http://llvm.org/viewvc/llvm-project?rev=320524&view=rev Log: [clangd] (Attempt to) read clang-format file for document formatting Summary: Takes into account the clang-format file of the project, if any. Reverts to LLVM if nothing is found. Replies with an error if any error occured. For instance, a parse error in the clang-format YAML file. Reviewers: ilya-biryukov, sammccall, Nebiroth, malaperle, krasimir Reviewed By: sammccall Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D41031 Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.h Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=320524&r1=320523&r2=320524&view=diff == --- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Tue Dec 12 12:25:06 2017 @@ -17,18 +17,29 @@ using namespace clang; namespace { +TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R) { + Range ReplacementRange = { + offsetToPosition(Code, R.getOffset()), + offsetToPosition(Code, R.getOffset() + R.getLength())}; + return {ReplacementRange, R.getReplacementText()}; +} + std::vector replacementsToEdits(StringRef Code, const std::vector &Replacements) { // Turn the replacements into the format specified by the Language Server // Protocol. Fuse them into one big JSON array. std::vector Edits; - for (auto &R : Replacements) { -Range ReplacementRange = { -offsetToPosition(Code, R.getOffset()), -offsetToPosition(Code, R.getOffset() + R.getLength())}; -Edits.push_back({ReplacementRange, R.getReplacementText()}); - } + for (const auto &R : Replacements) +Edits.push_back(replacementToEdit(Code, R)); + return Edits; +} + +std::vector replacementsToEdits(StringRef Code, + const tooling::Replacements &Repls) { + std::vector Edits; + for (const auto &R : Repls) +Edits.push_back(replacementToEdit(Code, R)); return Edits; } @@ -153,23 +164,36 @@ void ClangdLSPServer::onDocumentOnTypeFo Ctx C, DocumentOnTypeFormattingParams &Params) { auto File = Params.textDocument.uri.file; std::string Code = Server.getDocument(File); - C.reply(json::ary( - replacementsToEdits(Code, Server.formatOnType(File, Params.position; + auto ReplacementsOrError = Server.formatOnType(Code, File, Params.position); + if (ReplacementsOrError) +C.reply(json::ary(replacementsToEdits(Code, ReplacementsOrError.get(; + else +C.replyError(ErrorCode::UnknownErrorCode, + llvm::toString(ReplacementsOrError.takeError())); } void ClangdLSPServer::onDocumentRangeFormatting( Ctx C, DocumentRangeFormattingParams &Params) { auto File = Params.textDocument.uri.file; std::string Code = Server.getDocument(File); - C.reply(json::ary( - replacementsToEdits(Code, Server.formatRange(File, Params.range; + auto ReplacementsOrError = Server.formatRange(Code, File, Params.range); + if (ReplacementsOrError) +C.reply(json::ary(replacementsToEdits(Code, ReplacementsOrError.get(; + else +C.replyError(ErrorCode::UnknownErrorCode, + llvm::toString(ReplacementsOrError.takeError())); } void ClangdLSPServer::onDocumentFormatting(Ctx C, DocumentFormattingParams &Params) { auto File = Params.textDocument.uri.file; std::string Code = Server.getDocument(File); - C.reply(json::ary(replacementsToEdits(Code, Server.formatFile(File; + auto ReplacementsOrError = Server.formatFile(Code, File); + if (ReplacementsOrError) +C.reply(json::ary(replacementsToEdits(Code, ReplacementsOrError.get(; + else +C.replyError(ErrorCode::UnknownErrorCode, + llvm::toString(ReplacementsOrError.takeError())); } void ClangdLSPServer::onCodeAction(Ctx C, CodeActionParams &Params) { Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=320524&r1=320523&r2=320524&view=diff == --- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Tue Dec 12 12:25:06 2017 @@ -38,16 +38,6 @@ private: std::promise &Promise; }; -std::vector formatCode(StringRef Code, StringRef Filename, - ArrayRef Ranges) { - // Call clang-format. - // FIXME: Don't ignore style. - format::FormatStyle Style = format::getLLVMStyle(); - aut
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746 >From 8187a249ac02d01b39bc583c37440a9eb6cbd7e7 Mon Sep 17 00:00:00 2001 From: Raoul Wols 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 (in)direct AST child nodes we'll assume it should render OK. --- clang-tools-extra/clangd/Hover.cpp| 14 - .../clangd/unittests/HoverTests.cpp | 51 +++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 06b949bc4a2b55..c3c5cf2732486d 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -136,6 +136,15 @@ std::string getNamespaceScope(const Decl *D) { return ""; } +/// Compute the number of child statements in this statement. Includes the +/// statement itself. +size_t totalChildrenInStmt(const Stmt *Statement) { + size_t Count = 1; + for (const auto &Child : Statement->children()) +Count += totalChildrenInStmt(Child); + return Count; +} + std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { if (auto *VD = llvm::dyn_cast(D)) { @@ -143,8 +152,9 @@ 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()) -PP.SuppressInitializers = true; + PP.SuppressInitializers = + 200 < TB.expandedTokens(IE->getSourceRange()).size() || + 100 < totalChildrenInStmt(IE); } } std::string Definition; diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 35db757b9c15b5..9b213da9ec844b 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -3956,6 +3956,57 @@ 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, HideBigNestedInitializersIncludedFromThePreamble) { + 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[1][1][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[1][1][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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
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 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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746 >From 1739d0a4fd079d2201e63166fbaba60644c52297 Mon Sep 17 00:00:00 2001 From: Raoul Wols 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 | 35 +++ 2 files changed, 39 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..c1c986d1db3769 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -3956,6 +3956,41 @@ 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); + 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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
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 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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
@@ -138,15 +138,9 @@ std::string getNamespaceScope(const Decl *D) { std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { - if (auto *VD = llvm::dyn_cast(D)) { -if (auto *IE = VD->getInit()) { - // 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()) -PP.SuppressInitializers = true; -} - } + // 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. + PP.EntireContentsOfLargeArray = false; rwols wrote: OK, I check the size of the children() now when the initializer expression is not in the main file, and added two unit tests that verify that for a large initializer list from a header file it won't render, while a "small" initializer list will still render. https://github.com/llvm/llvm-project/pull/79746 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
@@ -1720,6 +1720,12 @@ void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { OS << "{"; for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { if (i) OS << ", "; +// TODO: There is duplicated functionality in APValue::printPretty. +// Would be good to consolidate the two. +if (!Policy.EntireContentsOfLargeArray && i == 10) { rwols wrote: I reverted these changes in the clang namespace. https://github.com/llvm/llvm-project/pull/79746 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746 >From 06a84a493646b66a99a9e8e95a64ca09d952be28 Mon Sep 17 00:00:00 2001 From: Raoul Wols 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 (in)direct AST child nodes we'll assume it should render OK. --- clang-tools-extra/clangd/Hover.cpp| 14 - .../clangd/unittests/HoverTests.cpp | 51 +++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 06b949bc4a2b55..c3c5cf2732486d 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -136,6 +136,15 @@ std::string getNamespaceScope(const Decl *D) { return ""; } +/// Compute the number of child statements in this statement. Includes the +/// statement itself. +size_t totalChildrenInStmt(const Stmt *Statement) { + size_t Count = 1; + for (const auto &Child : Statement->children()) +Count += totalChildrenInStmt(Child); + return Count; +} + std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { if (auto *VD = llvm::dyn_cast(D)) { @@ -143,8 +152,9 @@ 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()) -PP.SuppressInitializers = true; + PP.SuppressInitializers = + 200 < TB.expandedTokens(IE->getSourceRange()).size() || + 100 < totalChildrenInStmt(IE); } } std::string Definition; diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 35db757b9c15b5..9b213da9ec844b 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -3956,6 +3956,57 @@ 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, HideBigNestedInitializersIncludedFromThePreamble) { + 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[1][1][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[1][1][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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746 >From 6d30615ba3fb59163938656827a60a838e160c31 Mon Sep 17 00:00:00 2001 From: Raoul Wols 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 (in)direct AST child nodes we'll assume it should render OK. --- clang-tools-extra/clangd/Hover.cpp| 14 - .../clangd/unittests/HoverTests.cpp | 51 +++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 06b949bc4a2b55..c3c5cf2732486d 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -136,6 +136,15 @@ std::string getNamespaceScope(const Decl *D) { return ""; } +/// Compute the number of child statements in this statement. Includes the +/// statement itself. +size_t totalChildrenInStmt(const Stmt *Statement) { + size_t Count = 1; + for (const auto &Child : Statement->children()) +Count += totalChildrenInStmt(Child); + return Count; +} + std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { if (auto *VD = llvm::dyn_cast(D)) { @@ -143,8 +152,9 @@ 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()) -PP.SuppressInitializers = true; + PP.SuppressInitializers = + 200 < TB.expandedTokens(IE->getSourceRange()).size() || + 100 < totalChildrenInStmt(IE); } } std::string Definition; diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 35db757b9c15b5..9b213da9ec844b 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -3956,6 +3956,57 @@ 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, HideBigNestedInitializersIncludedFromThePreamble) { + 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[1][1][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[1][1][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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
https://github.com/rwols created https://github.com/llvm/llvm-project/pull/79746 Previously we checked whether we were dealing with a large initializer using TokenBuffer::expandedTokens. However, TokenBuffer does not contain the tokens of the preamble. This causes large arrays imported from an #include to still print all their elements. The added unit test checks the case of a large array included from the preamble. >From 27bc0b64e17afce3196eb8d8415a8c61619dfae2 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Sun, 28 Jan 2024 14:14:29 +0100 Subject: [PATCH] [clangd] Prevent printing huge initializer lists in hover definitions --- clang-tools-extra/clangd/Hover.cpp| 12 +++ .../clangd/unittests/HoverTests.cpp | 21 ++- clang/lib/AST/StmtPrinter.cpp | 6 ++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 06b949bc4a2b552..ed70b3e97d39ccd 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -138,15 +138,9 @@ std::string getNamespaceScope(const Decl *D) { std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { - if (auto *VD = llvm::dyn_cast(D)) { -if (auto *IE = VD->getInit()) { - // 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()) -PP.SuppressInitializers = true; -} - } + // 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. + PP.EntireContentsOfLargeArray = false; std::string Definition; llvm::raw_string_ostream OS(Definition); D->print(OS, PP); diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 35db757b9c15b5d..c2d7dd406c1dc66 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -3953,9 +3953,28 @@ TEST(Hover, HideBigInitializers) { auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr); ASSERT_TRUE(H); - EXPECT_EQ(H->Definition, "int arr[]"); + EXPECT_EQ(H->Definition, "int arr[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}"); } +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] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}"); +} + + #if defined(__aarch64__) // FIXME: AARCH64 sanitizer buildbots are broken after 72142fbac4. #define PREDEFINEMACROS_TEST(x) DISABLED_##x diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp index 1df040e06db35e9..d2c0f1cce8afdb4 100644 --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -1720,6 +1720,12 @@ void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { OS << "{"; for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { if (i) OS << ", "; +// TODO: There is duplicated functionality in APValue::printPretty. +// Would be good to consolidate the two. +if (!Policy.EntireContentsOfLargeArray && i == 10) { + OS << "..."; + break; +} if (Node->getInit(i)) PrintExpr(Node->getInit(i)); else ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
https://github.com/rwols updated https://github.com/llvm/llvm-project/pull/79746 >From 6f95aba8dbdf2ac807216597e5ab7fc62af29770 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Sun, 28 Jan 2024 14:14:29 +0100 Subject: [PATCH] [clangd] Prevent printing huge initializer lists in hover definitions --- clang-tools-extra/clangd/Hover.cpp| 12 +++ .../clangd/unittests/HoverTests.cpp | 20 ++- clang/lib/AST/StmtPrinter.cpp | 6 ++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp index 06b949bc4a2b55..ed70b3e97d39cc 100644 --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -138,15 +138,9 @@ std::string getNamespaceScope(const Decl *D) { std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { - if (auto *VD = llvm::dyn_cast(D)) { -if (auto *IE = VD->getInit()) { - // 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()) -PP.SuppressInitializers = true; -} - } + // 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. + PP.EntireContentsOfLargeArray = false; std::string Definition; llvm::raw_string_ostream OS(Definition); D->print(OS, PP); diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 35db757b9c15b5..c2d4217230021d 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -3953,7 +3953,25 @@ TEST(Hover, HideBigInitializers) { auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr); ASSERT_TRUE(H); - EXPECT_EQ(H->Definition, "int arr[]"); + EXPECT_EQ(H->Definition, "int arr[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}"); +} + +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] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}"); } #if defined(__aarch64__) diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp index 1df040e06db35e..d2c0f1cce8afdb 100644 --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -1720,6 +1720,12 @@ void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { OS << "{"; for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { if (i) OS << ", "; +// TODO: There is duplicated functionality in APValue::printPretty. +// Would be good to consolidate the two. +if (!Policy.EntireContentsOfLargeArray && i == 10) { + OS << "..."; + break; +} if (Node->getInit(i)) PrintExpr(Node->getInit(i)); else ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
@@ -1720,6 +1720,12 @@ void StmtPrinter::VisitInitListExpr(InitListExpr* Node) { OS << "{"; for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { if (i) OS << ", "; +// TODO: There is duplicated functionality in APValue::printPretty. +// Would be good to consolidate the two. +if (!Policy.EntireContentsOfLargeArray && i == 10) { rwols wrote: I was also kind of unsure about the usage of `EntirecontentsOfLargeArray` here. I feel like StmtPrinter should always output valid C++ code but that's not possible using `EntireContentsOfLargeArray`. https://github.com/llvm/llvm-project/pull/79746 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang] [clangd] Prevent printing huge initializer lists in hover definitions (PR #79746)
@@ -138,15 +138,9 @@ std::string getNamespaceScope(const Decl *D) { std::string printDefinition(const Decl *D, PrintingPolicy PP, const syntax::TokenBuffer &TB) { - if (auto *VD = llvm::dyn_cast(D)) { -if (auto *IE = VD->getInit()) { - // 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()) -PP.SuppressInitializers = true; -} - } + // 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. + PP.EntireContentsOfLargeArray = false; rwols wrote: > a more concrete approach could be based on counting number of "total" > elements in the initializer list, and setting the suppression flag based on > that. i'd actually lean towards such a solution, WDYT? I agree, but don't know where to start on this. Since TokenBuffer doesn't have tokens from the preamble, what alternatives are there? https://github.com/llvm/llvm-project/pull/79746 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits