This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Skip function parameter decls when evaluating variables on hover.
ClosedPublic

Authored by VitaNuo on Jun 15 2023, 5:44 AM.

Diff Detail

Event Timeline

VitaNuo created this revision.Jun 15 2023, 5:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 5:44 AM
VitaNuo requested review of this revision.Jun 15 2023, 5:44 AM
VitaNuo updated this revision to Diff 531718.Jun 15 2023, 5:51 AM

Simplify.

thanks.

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

I believe this case should trigger a crash, but I don't see the crash with a trunk-built clangd, do we miss something here?

VitaNuo marked an inline comment as done.Jun 15 2023, 7:26 AM

thanks!

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

It crashes on invalid code. I've inspected two user workspaces:

class Foo{};
void foo(Foo param = nullptr);
void foo(ClassName param = functionReturningObjectOfSimilarSoundingButUnrelatedClass());

I guess clangd is not even expected to act soundly in the face of non-compiling code.
But since these crashes are easy to get rid of, and evaluating parameters in function declarations is pointless anyways, we can just avoid these crashes at no extra cost. I cannot use non-compiling code in a hover test, though.

hokein added inline comments.Jun 15 2023, 9:55 AM
clang-tools-extra/clangd/Hover.cpp
663

We're ignoring all ParmVarDecl cases,

The crash here is for broken code. For the crash case, the AST node looks like

`-ParmVarDecl 0x563b7cc853c0 <col:10, col:20> col:14 invalid param 'Foo':'Foo' cinit
    `-OpaqueValueExpr 0x563b7cc854a0 <col:20> 'Foo':'Foo'

One alternative is to exclude invalid VarDecl, then the Hover feature still keep working on valid ParmVarDecl.

if (const auto* Var = dyn_cast<VarDecl>(D); Var && !Var->isInvalidDecl()) {
   ...
}
clang-tools-extra/clangd/unittests/HoverTests.cpp
3727

thanks for the clarification. That makes sense.

We can still use these broken code (the first one should be good enough) in the HoverTest, we need to add a magic comment /* error-ok */.

VitaNuo updated this revision to Diff 532042.Jun 16 2023, 1:28 AM

Address review comments.

VitaNuo updated this revision to Diff 532044.Jun 16 2023, 1:30 AM
VitaNuo marked 2 inline comments as done.

Simplify.

Thanks for the review!

clang-tools-extra/clangd/Hover.cpp
663

Why would you do that?

663

Sorry this was an unintended comment.

hokein accepted this revision.Jun 16 2023, 2:27 AM

Thanks, looks good, just one nit to simplify the unittest.

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

nit: instead of creating a completely-new TEST, it seems simpler to just add a testcase in the existing TEST(Hover, All).

{R"cpp(// Should not crash on an invalid param decl.
       class Foo {};
       // error-ok
       void foo(Foo [[fo^o]] = nullptr);
       )cpp",
    [](HoverInfo &HI) {
      HI.Name = "foo";
      HI.Type = "Foo";
      HI.Kind = index::SymbolKind::Parameter;
      HI.NamespaceScope = "";
      HI.LocalScope = "foo::";
      HI.Definition = "Foo foo = <null expr>";
    }},
This revision is now accepted and ready to land.Jun 16 2023, 2:27 AM
This revision was automatically updated to reflect the committed changes.
VitaNuo marked an inline comment as done.

Thanks!

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

I'm not sure I agree. The Hover, All test takes already about 5 seconds to scroll through on the screen :)

On top of that, other test cases that are a result of crash fixes seem to be separate too.
Therefore, I'd prefer to keep it as a separate test case for now.

nridge added a subscriber: nridge.Jun 17 2023, 8:57 PM