Skip to content

Commit

Permalink
[clangd] Skip function parameter decls when evaluating variables on h…
Browse files Browse the repository at this point in the history
…over.

Differential Revision: https://reviews.llvm.org/D153015
  • Loading branch information
VitaNuo committed Jun 16, 2023
1 parent d0233cc commit c9888dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ HoverInfo getHoverContents(const NamedDecl *D, const PrintingPolicy &PP,
HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);

// Fill in value with evaluated initializer if possible.
if (const auto *Var = dyn_cast<VarDecl>(D)) {
if (const auto *Var = dyn_cast<VarDecl>(D); Var && !Var->isInvalidDecl()) {
if (const Expr *Init = Var->getInit())
HI.Value = printExprValue(Init, Ctx);
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
Expand Down
29 changes: 29 additions & 0 deletions clang-tools-extra/clangd/unittests/HoverTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "gtest/gtest.h"
#include <functional>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -3722,6 +3723,34 @@ TEST(Hover, ForwardStructNoCrash) {
EXPECT_EQ(*HI->Value, "&bar");
}

TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
struct {
const char *const Code;
const std::optional<std::string> 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;
Expand Down

0 comments on commit c9888dc

Please sign in to comment.