This is an archive of the discontinued LLVM Phabricator instance.

[clang][Index] Visit the default parameter arguements in libindex.
ClosedPublic

Authored by hokein on Feb 14 2020, 6:03 AM.

Details

Summary

We are missing the default parmeter arguments when IndexFunctionLocals
is true.

Fixes https://github.com/clangd/clangd/issues/285.

Diff Detail

Event Timeline

hokein created this revision.Feb 14 2020, 6:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2020, 6:03 AM

Oh god, this code is a mess, so many knobs, and apparently I made it worst in the past sorry for that :D

I totally understand if you want to make minimal changes to the code like this, but it would be great to simplify it a little bit without regressing too much, currently there is a knob for function locals and then another for parameters in declarations, then there is an implicit conditioning for function definitions.
I would say it would be sensible to only have IndexFunctionLocals as an option and do full indexing of parameters, both in declarations and definitions if it is present (assuming this doesn't result in real regressions in the client code), but it is also OK to leave it as it is, since the mess is beyond the scope of this patch.

But if you leave it as it is, I believe we'll still be missing some references to decls inside default arguments in clangd, for example:

a.h:

int var = 2;
void foo(int x = var);

a.cc

void foo(int x) {
  va^r = x;
}

clangd won't have the reference occuring in default arg inside the index, as preambles are not indexed with IndexParametersInDeclarations option turned on.

clang/lib/Index/IndexDecl.cpp
102

this should move into the else clause down below, otherwise it would end up being indexed twice

hokein updated this revision to Diff 244935.Feb 17 2020, 3:36 AM
hokein marked 2 inline comments as done.

address comment

I totally understand if you want to make minimal changes to the code like this, but it would be great to simplify it a little bit without regressing too much, currently there is a knob for function locals and then another for parameters in declarations, then there is an implicit conditioning for function definitions.
I would say it would be sensible to only have IndexFunctionLocals as an option and do full indexing of parameters, both in declarations and definitions if it is present (assuming this doesn't result in real regressions in the client code), but it is also OK to leave it as it is, since the mess is beyond the scope of this patch.

removing the shouldIndexParametersInDeclarations seems good to me, looks like clangd is the only user of this flag. Regression is the concern -- as we are changing the behavior of libindex, and the libindex doesn't seem to have enough test coverage. I'd land this fix as it-is, and cleanup afterwards.

But if you leave it as it is, I believe we'll still be missing some references to decls inside default arguments in clangd, for example:

a.h:

int var = 2;
void foo(int x = var);
a.cc

void foo(int x) {

va^r = x;

}
clangd won't have the reference occuring in default arg inside the index, as preambles are not indexed with IndexParametersInDeclarations option turned on.

Are you talking about dynamic index? clangd doesn't record any refs in preamble by design -- as we skip function bodies in preamble

clang/lib/Index/IndexDecl.cpp
102

My reading to the source is that

  • IndexCtx.handleDecl(Parm) just reports the Parm decl occurrence, it doesn't traversal the default arguments unfortunately :(
  • the if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) here is only triggered for ObjC cases, C++ never run into this code path :(

a conservative fix is that we only need the change on Line 117. I removed the change in the current if clause.

hokein updated this revision to Diff 244968.Feb 17 2020, 7:02 AM

remove an accident change.

Looking the source code closely, we also have another code path of indexing the ParmVarDecl in clang/lib/Index/IndexTypeSourceInfo.cpp, it was called via IndexCtx.indexTypeSourceInfo in handleDeclarator.

In summary:

  1. default arguments will be visited if this is a non-definition decl, regardless the shouldIndexFunctionLocatl -- this is done in IndexTypeSourceInfo
  2. default arguments will be visited if this is a definition decl and shouldIndexFunctionLocal is fasle -- this is done in IndexDecl.
  3. default arguments will not be visited if this is a definition decl and shouldIndexFunctionLocal is true

This patch fixes 3).

kadircet accepted this revision.Feb 17 2020, 7:13 AM

LGTM,

just wondering though if you've tried the solution we discussed offline regarding updating indexTypeSourceInfo instead of this call site, could you mention the results in here?
So that the next soul that touches these lines has an idea about what's expecting them?

This revision is now accepted and ready to land.Feb 17 2020, 7:13 AM

LGTM,

just wondering though if you've tried the solution we discussed offline regarding updating indexTypeSourceInfo instead of this call site, could you mention the results in here?
So that the next soul that touches these lines has an idea about what's expecting them?

It seemed breaking a few existing tests...

This revision was automatically updated to reflect the committed changes.