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

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 20 07:54:32 PST 2024


================
@@ -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())) {
----------------
kadircet wrote:

we actually need `IE->getSourceRange()` to be inside the mainfile, this can still be `int x[] = EXPANDS_TO_CRAZY_LONG_INIT_DEFINED_OUTSIDE_THE_MAINFILE`. also you're just checking the first level of children now, we can have something like: `int **x = { {CRAZY_EXPANSION} };` which only has a single children at top level, but still has a ton nested underneath.

can we just have:
```
auto TotalChildrenInStmt = [&](const Stmt *S) {
  size_t res = 1;
  for(auto &C : S->children()) res += TotalChildrenInStmt(C);
  return res;
};
PP.SuppressInitializers = 200 < TB.expandedTokens(IE->getSourceRange()).size() || 100 < TotalChildrenInExpr(IE);
```

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


More information about the cfe-commits mailing list