This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Not collect include headers for dynamic index for now.
ClosedPublic

Authored by ioeric on Feb 21 2018, 2:19 AM.

Details

Summary

The new behaviors introduced by this patch:
o When include collection is enabled, we always set IncludeHeader field in Symbol
even if it's the same as FileURI in decl.
o Disable include collection in FileIndex which is currently only used to build
dynamic index. We should revisit when we actually want to use FileIndex to global
index.
o Code-completion only uses IncludeHeader to insert headers but not FileURI in
CanonicalDeclaration. This ensures that inserted headers are always canonicalized.
Note that include insertion can still be triggered for symbols that are already
included if they are merged from dynamic index and static index, but we would
only use includes that are already canonicalized (e.g. from static index).

Reason for change:
Collecting header includes in dynamic index enables inserting includes for headers
that are not indexed but opened in the editor. Comparing to inserting includes for
symbols in global/static index, this is nice-to-have but would probably require
non-trivial amount of work to get right. For example:
o Currently it's not easy to fully support CanonicalIncludes in dynamic index, given the way
we run dynamic index.
o It's also harder to reason about the correctness of include canonicalization for dynamic index
(i.e. symbols in the current file/TU) than static index where symbols are collected
offline and sanity check is possible before shipping to production.
o We have less control/flexibility over symbol info in the dynamic index
(e.g. URIs, path normalization), which could be used to help make decision when inserting includes.

As header collection (especially canonicalization) is relatively new, and enabling
it for dynamic index would immediately affect current users with only dynamic
index support, I propose we disable it for dynamic index for now to avoid
compromising other hot features like code completion and only support it for
static index where include insertion would likely to bring more value.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric created this revision.Feb 21 2018, 2:19 AM

Is there a way to still enable include insertion but in a restricted manner, e.g. only for the current project?
File of canonical declaration is usually a pretty good guess and it would be nice to have it. Maybe we could allowing to disable the include insertion via a flag or config parameter rather than disabling it for dynamic index entirely.

It bugs me that internal STL headers are added all the time, but it also lets me test the feature without building the static index. I find the second more valuable, given that we're still actively working on it.

clangd/index/FileIndex.cpp
18 ↗(On Diff #135210)

Should we also remove the mutex from CanonicalIncludes now?

clangd/index/SymbolCollector.cpp
172 ↗(On Diff #135210)

Maybe simplify to return toURI(...)?
Or is there a reason why it doesn't work?

Is there a way to still enable include insertion but in a restricted manner, e.g. only for the current project?

I'm not sure if this is currently supported, as we don't have a good definition of a "project" yet. Could you provide a use case?

File of canonical declaration is usually a pretty good guess and it would be nice to have it. Maybe we could allowing to disable the include insertion via a flag or config parameter rather than disabling it for dynamic index entirely.

Yes, it is a nice-to-have, when it works :) Without proper canonicalization, this could easily go wrong (e.g. we don't want to insert gtest/gmock internal headers guarded by IWYU pragma).

Do you think it's beneficial to collect headers in dynamic index even when it doesn't work correctly? Again, I'm not arguing that collecting includes in dynamic index is not worth doing; I just think it's a rabbit hole that we don't need to chase down at this point :)

It bugs me that internal STL headers are added all the time, but it also lets me test the feature without building the static index. I find the second more valuable, given that we're still actively working on it.

I think we can test the feature in a safer approach (e.g. build a small static index offline) while not bugging normal users with the wrong includes, which IMO would hurt code completion experience very badly... In general, I think user experience is much more valuable than us being able to test features.

ilya-biryukov accepted this revision.Feb 21 2018, 4:27 AM

As discussed offline, properly supporting IWYU pragma in the dynamic index seems like too much design work for now and it's not gonna get fixed soon.
So opting for disabling the feature seems like the right approach.

Therefore, LGTM. (There are a few nits in inline comments from my previous reply that you might want to look at before landing this)

This revision is now accepted and ready to land.Feb 21 2018, 4:27 AM
ioeric updated this revision to Diff 135244.Feb 21 2018, 6:18 AM
ioeric marked 2 inline comments as done.

Properly use toURI.

clangd/index/FileIndex.cpp
18 ↗(On Diff #135210)

I am inclined to keeping it as it brings thread-safety to CanonicalIncludes (which we wouldn't need to worry about in the future), and the current users are not in performance critical code path anyway.

clangd/index/SymbolCollector.cpp
172 ↗(On Diff #135210)

No reason... #this is what happened when copy-paste code...

sammccall accepted this revision.Feb 22 2018, 2:07 AM
This revision was automatically updated to reflect the committed changes.