diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -659,7 +659,7 @@ HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP); // Fill in value with evaluated initializer if possible. - if (const auto *Var = dyn_cast(D)) { + if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) { if (const Expr *Init = Var->getInit()) HI.Value = printExprValue(Init, Ctx); } else if (const auto *ECD = dyn_cast(D)) { diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -22,6 +22,7 @@ #include "gtest/gtest.h" #include +#include #include #include @@ -3722,6 +3723,34 @@ EXPECT_EQ(*HI->Value, "&bar"); } +TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) { + struct { + const char *const Code; + const std::optional HoverValue; + } Cases[] = { + {R"cpp( + // error-ok testing behavior on invalid decl + class Foo {}; + void foo(Foo p^aram = nullptr); + )cpp", + std::nullopt}, + {R"cpp( + class Foo {}; + void foo(Foo *p^aram = nullptr); + )cpp", + "nullptr"}, + }; + + for (const auto &C : Cases) { + Annotations T(C.Code); + TestTU TU = TestTU::withCode(T.code()); + auto AST = TU.build(); + auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr); + ASSERT_TRUE(HI); + ASSERT_EQ(HI->Value, C.HoverValue); + } +} + TEST(Hover, DisableShowAKA) { Annotations T(R"cpp( using m_int = int;