This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Avoid expensive checks of buffer names in IncludeCleaner
ClosedPublic

Authored by sammccall on Oct 27 2021, 12:20 PM.

Details

Summary

This changes the handling of special buffers (<command-line> etc) that
SourceManager treats as files but FileManager does not.

We now include them in findReferencedFiles() and drop them as part of
translateToHeaderIDs(). This pairs more naturally with the data representations
we're using, and so avoids a bunch of converting between representations for
filtering.

Diff Detail

Event Timeline

sammccall created this revision.Oct 27 2021, 12:20 PM
sammccall requested review of this revision.Oct 27 2021, 12:20 PM

Thank you for taking care of this!

clang-tools-extra/clangd/IncludeCleaner.cpp
261

Implementation-wise, this seems like the right place to drop nonexistent "headers". However, this is a very surprising behavior from something like "translateToHeaderIDs" for two reasons:

  • As the name states, it used to be a simple "translation" process, now it's also filtering.
  • TranslatedHeaderIDs.size() == Files.size() can be rather unexpected

We never had 1:1 order etc translation assumptions but this is certainly the new behavior we want to document.

261

Thinking out loud: now I'm also thinking about the "handle non self-contained files" + "drop warnings for files without include warnings"

clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
275

nit: Drop the parenthesis?

sammccall added inline comments.Oct 27 2021, 1:44 PM
clang-tools-extra/clangd/IncludeCleaner.cpp
261

This function is mapping from a domain (FileIDs excluding macros) where special buffers exist to a codomain (HeaderIDs) where they don't. Some sort of filtering/handling is implied by the signature!
So I think it's not surprising at least if you think about it that way :-) But this definitely needs to be documented, I'll do that.

Thinking out loud: now I'm also thinking about the "handle non self-contained files" + "drop warnings for files without include warnings"

There may some small efficiency from mashing those into this loop as well, but I don't think it's worth the confusion.
The reason I put this one inline instead of as a separate filtering step is that the check we'd be doing is a natural part of the conversion. It's basically a form of parse, don't validate

sammccall updated this revision to Diff 382779.Oct 27 2021, 1:49 PM

Add more docs

sammccall marked 3 inline comments as done.Oct 27 2021, 1:50 PM
kbobyrev accepted this revision.Oct 27 2021, 10:04 PM

LGTM, thanks!

This revision is now accepted and ready to land.Oct 27 2021, 10:04 PM