Skip to content

Commit cb133a4

Browse files
committed
[clangd] Hover: Add CalleeArgInfo for constructor expressions
Differential Revision: https://reviews.llvm.org/D147847
1 parent 81b1f47 commit cb133a4

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

clang-tools-extra/clangd/Hover.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,12 +981,23 @@ void maybeAddCalleeArgInfo(const SelectionTree::Node *N, HoverInfo &HI,
981981
const auto &OuterNode = N->outerImplicit();
982982
if (!OuterNode.Parent)
983983
return;
984-
const auto *CE = OuterNode.Parent->ASTNode.get<CallExpr>();
985-
if (!CE)
984+
985+
const FunctionDecl *FD = nullptr;
986+
llvm::ArrayRef<const Expr *> Args;
987+
988+
if (const auto *CE = OuterNode.Parent->ASTNode.get<CallExpr>()) {
989+
FD = CE->getDirectCallee();
990+
Args = {CE->getArgs(), CE->getNumArgs()};
991+
} else if (const auto *CE =
992+
OuterNode.Parent->ASTNode.get<CXXConstructExpr>()) {
993+
FD = CE->getConstructor();
994+
Args = {CE->getArgs(), CE->getNumArgs()};
995+
}
996+
if (!FD)
986997
return;
987-
const FunctionDecl *FD = CE->getDirectCallee();
988-
// For non-function-call-like operatators (e.g. operator+, operator<<) it's
989-
// not immediattely obvious what the "passed as" would refer to and, given
998+
999+
// For non-function-call-like operators (e.g. operator+, operator<<) it's
1000+
// not immediately obvious what the "passed as" would refer to and, given
9901001
// fixed function signature, the value would be very low anyway, so we choose
9911002
// to not support that.
9921003
// Both variadic functions and operator() (especially relevant for lambdas)
@@ -999,8 +1010,8 @@ void maybeAddCalleeArgInfo(const SelectionTree::Node *N, HoverInfo &HI,
9991010
auto Parameters = resolveForwardingParameters(FD);
10001011

10011012
// Find argument index for N.
1002-
for (unsigned I = 0; I < CE->getNumArgs() && I < Parameters.size(); ++I) {
1003-
if (CE->getArg(I) != OuterNode.ASTNode.get<Expr>())
1013+
for (unsigned I = 0; I < Args.size() && I < Parameters.size(); ++I) {
1014+
if (Args[I] != OuterNode.ASTNode.get<Expr>())
10041015
continue;
10051016

10061017
// Extract matching argument from function declaration.

clang-tools-extra/clangd/unittests/HoverTests.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,29 @@ class Foo final {})cpp";
956956
HI.CalleeArgInfo->Type = "const float &";
957957
HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
958958
}},
959+
{
960+
R"cpp(
961+
struct Foo {
962+
explicit Foo(const float& arg) {}
963+
};
964+
int main() {
965+
int a = 0;
966+
Foo foo([[^a]]);
967+
}
968+
)cpp",
969+
[](HoverInfo &HI) {
970+
HI.Name = "a";
971+
HI.Kind = index::SymbolKind::Variable;
972+
HI.NamespaceScope = "";
973+
HI.Definition = "int a = 0";
974+
HI.LocalScope = "main::";
975+
HI.Value = "0";
976+
HI.Type = "int";
977+
HI.CalleeArgInfo.emplace();
978+
HI.CalleeArgInfo->Name = "arg";
979+
HI.CalleeArgInfo->Type = "const float &";
980+
HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
981+
}},
959982
{// Literal passed to function call
960983
R"cpp(
961984
void fun(int arg_a, const int &arg_b) {};
@@ -1342,6 +1365,7 @@ class CustomClass {
13421365
CustomClass(const Base &x) {}
13431366
CustomClass(int &x) {}
13441367
CustomClass(float x) {}
1368+
CustomClass(int x, int y) {}
13451369
};
13461370
13471371
void int_by_ref(int &x) {}
@@ -1388,6 +1412,11 @@ void fun() {
13881412
{"base_by_ref([[^derived]]);", PassMode::Ref, false},
13891413
{"base_by_const_ref([[^derived]]);", PassMode::ConstRef, false},
13901414
{"base_by_value([[^derived]]);", PassMode::Value, false},
1415+
// Custom class constructor tests
1416+
{"CustomClass c1([[^base]]);", PassMode::ConstRef, false},
1417+
{"auto c2 = new CustomClass([[^base]]);", PassMode::ConstRef, false},
1418+
{"CustomClass c3([[^int_x]]);", PassMode::Ref, false},
1419+
{"CustomClass c3(int_x, [[^int_x]]);", PassMode::Value, false},
13911420
// Converted tests
13921421
{"float_by_value([[^int_x]]);", PassMode::Value, true},
13931422
{"float_by_value([[^int_ref]]);", PassMode::Value, true},

0 commit comments

Comments
 (0)