[clang] Don't preserve the typo expr in the recovery expr for invalid VarDecls (#90948)
With the commit d5308949cf884d8e4b971d51a8b4f73584c4adec, we now
preserve the initializer for invalid decls with the recovery-expr.
However there is a chance that the original init expr is a typo-expr, we
should not preserve it in the final AST, as typo-expr is an internal AST
node. We should use the one after the typo correction.
This is spotted by a clangd hover crash on the testcase.
GitOrigin-RevId: fc866fd2a2cfca6d62f48dcf83778959fd24f559
diff --git a/clangd/unittests/HoverTests.cpp b/clangd/unittests/HoverTests.cpp
index 5ead747..28df24f 100644
--- a/clangd/unittests/HoverTests.cpp
+++ b/clangd/unittests/HoverTests.cpp
@@ -965,6 +965,19 @@
// Bindings are in theory public members of an anonymous struct.
HI.AccessSpecifier = "public";
}},
+ {// Don't crash on invalid decl with invalid init expr.
+ R"cpp(
+ Unknown [[^abc]] = invalid;
+ // error-ok
+ )cpp",
+ [](HoverInfo &HI) {
+ HI.Name = "abc";
+ HI.Kind = index::SymbolKind::Variable;
+ HI.NamespaceScope = "";
+ HI.Definition = "int abc = <recovery - expr>()";
+ HI.Type = "int";
+ HI.AccessSpecifier = "public";
+ }},
{// Extra info for function call.
R"cpp(
void fun(int arg_a, int &arg_b) {};