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 @@ -981,10 +981,21 @@ const auto &OuterNode = N->outerImplicit(); if (!OuterNode.Parent) return; - const auto *CE = OuterNode.Parent->ASTNode.get(); - if (!CE) + + const FunctionDecl *FD = nullptr; + llvm::SmallVector Args; + + if (const auto *CE = OuterNode.Parent->ASTNode.get()) { + FD = CE->getDirectCallee(); + Args = {CE->arg_begin(), CE->arg_end()}; + } else if (const auto *CE = + OuterNode.Parent->ASTNode.get()) { + FD = CE->getConstructor(); + Args = {CE->arg_begin(), CE->arg_end()}; + } + if (!FD) return; - const FunctionDecl *FD = CE->getDirectCallee(); + // For non-function-call-like operatators (e.g. operator+, operator<<) it's // not immediattely obvious what the "passed as" would refer to and, given // fixed function signature, the value would be very low anyway, so we choose @@ -999,8 +1010,8 @@ auto Parameters = resolveForwardingParameters(FD); // Find argument index for N. - for (unsigned I = 0; I < CE->getNumArgs() && I < Parameters.size(); ++I) { - if (CE->getArg(I) != OuterNode.ASTNode.get()) + for (unsigned I = 0; I < Args.size() && I < Parameters.size(); ++I) { + if (Args[I] != OuterNode.ASTNode.get()) continue; // Extract matching argument from function declaration. 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 @@ -956,6 +956,29 @@ HI.CalleeArgInfo->Type = "const float &"; HI.CallPassType = HoverInfo::PassType{PassMode::Value, true}; }}, + { + R"cpp( + struct Foo { + explicit Foo(const float& arg) {} + }; + int main() { + int a = 0; + Foo foo([[^a]]); + } + )cpp", + [](HoverInfo &HI) { + HI.Name = "a"; + HI.Kind = index::SymbolKind::Variable; + HI.NamespaceScope = ""; + HI.Definition = "int a = 0"; + HI.LocalScope = "main::"; + HI.Value = "0"; + HI.Type = "int"; + HI.CalleeArgInfo.emplace(); + HI.CalleeArgInfo->Name = "arg"; + HI.CalleeArgInfo->Type = "const float &"; + HI.CallPassType = HoverInfo::PassType{PassMode::Value, true}; + }}, {// Literal passed to function call R"cpp( void fun(int arg_a, const int &arg_b) {}; @@ -1342,6 +1365,7 @@ CustomClass(const Base &x) {} CustomClass(int &x) {} CustomClass(float x) {} + CustomClass(int x, int y) {} }; void int_by_ref(int &x) {} @@ -1388,6 +1412,11 @@ {"base_by_ref([[^derived]]);", PassMode::Ref, false}, {"base_by_const_ref([[^derived]]);", PassMode::ConstRef, false}, {"base_by_value([[^derived]]);", PassMode::Value, false}, + // Custom class constructor tests + {"CustomClass c1([[^base]]);", PassMode::ConstRef, false}, + {"auto c2 = new CustomClass([[^base]]);", PassMode::ConstRef, false}, + {"CustomClass c3([[^int_x]]);", PassMode::Ref, false}, + {"CustomClass c3(int_x, [[^int_x]]);", PassMode::Value, false}, // Converted tests {"float_by_value([[^int_x]]);", PassMode::Value, true}, {"float_by_value([[^int_ref]]);", PassMode::Value, true},