This is an archive of the discontinued LLVM Phabricator instance.

[clangd] IncludeCleaner: Do not require forward declarations of RecordDecls when definition is available
ClosedPublic

Authored by kbobyrev on Dec 2 2021, 5:42 AM.

Details

Summary

This makes IncludeCleaner more useful in the presense of a large number of
forward declarations. If the definition is already in the Translation Unit and
visible to the Main File, forward declarations have no effect.

The original patch D112707 was split in two: D114864 and this one.

Diff Detail

Event Timeline

kbobyrev created this revision.Dec 2 2021, 5:42 AM
kbobyrev requested review of this revision.Dec 2 2021, 5:42 AM
kadircet added inline comments.Dec 2 2021, 6:33 AM
clang-tools-extra/clangd/IncludeCleaner.cpp
140

if we made it here, we already know definition wasn't visible in the current TU.

maybe just something like:

// Special case RecordDecls, as it is common for them to be forward declared multiple times.
// The most common two cases are:
// - definition available in TU, only mark that one as usage. rest is likely to be unnecessary. this might result in false positives when an internal definition is visible.
// - there's a forward declaration in the main file, no need for other redecls.
if (auto *RD = ...) {
  if (auto *Def) {
    insert that;
    return;
  }
  if (mostRecentInMainFile) {
    return;
  }
}

If we really want to keep a note around nested decls, we might say something like:
Forward decls/definitions of nested recorddecls can only be nested inside other recorddecls, and the definition for outer recorddecl has to be visible, and we always preserve definitions.

But I don't think all the additional information provides much value, rather creates confusion and forces one to think more about the details.

clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
66–67

you mind changing these into functions as well?

kbobyrev updated this revision to Diff 391393.Dec 2 2021, 10:55 AM
kbobyrev marked 2 inline comments as done.

Resolve review comments.

kadircet accepted this revision.Dec 3 2021, 12:06 AM

thanks!

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

i know it was me who didn't capitalize, but can you capitalize the first words :D
s/rest/Rest
s/this/This (line below)

This revision is now accepted and ready to land.Dec 3 2021, 12:06 AM

thanks!

Aww, sorry, I capitalized/reformatted parts of them but missed the rest :D

kbobyrev updated this revision to Diff 391566.Dec 3 2021, 12:34 AM

Fix typos.

kbobyrev updated this revision to Diff 391567.Dec 3 2021, 12:35 AM
kbobyrev marked an inline comment as done.

Improve wording.

This revision was landed with ongoing or failed builds.Dec 3 2021, 12:37 AM
This revision was automatically updated to reflect the committed changes.