This is an archive of the discontinued LLVM Phabricator instance.

[clangd] IncludeCleaner: Complicated rules for enum usage
ClosedPublic

Authored by kbobyrev on Oct 21 2021, 3:30 AM.

Diff Detail

Event Timeline

kbobyrev created this revision.Oct 21 2021, 3:30 AM
kbobyrev requested review of this revision.Oct 21 2021, 3:30 AM
kbobyrev updated this revision to Diff 381195.Oct 21 2021, 3:32 AM

Remove unwanted (though good) formatting changes.

kbobyrev updated this revision to Diff 381198.Oct 21 2021, 3:35 AM

Add one more test.

sammccall added inline comments.Oct 25 2021, 7:22 AM
clang-tools-extra/clangd/IncludeCleaner.cpp
38

I don't understand this special case.
It seems you're trying to avoid requiring too many redecls of a referenced type. Why is this important, and different from e.g. Class?

83

This says what, rather than why.

Rather maybe:

Enums may be usefully forward-declared as *complete* types by specifying an underlying type.
In this case, the definition should see the declaration so they can be checked for compatibility.
86

D->isThisDeclarationADefinition() is more idiomatic for the first part

88

rather add(D) here?

kbobyrev updated this revision to Diff 381992.Oct 25 2021, 7:41 AM
kbobyrev marked 3 inline comments as done.

Resolve review comments.

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

Yes, this is for cases like

{
    "enum class Color;",
    "enum class Color {}; Color c;",
},

I think the problem here is that if we see the definition of the enum, it should be the "ground truth" for the usage, forward declarations are not really useful in this case. It's not much different from the class but I just wanted to handle it separately, not sure if it's OK to merge these two changes into one patch.

sammccall added inline comments.Oct 25 2021, 8:08 AM
clang-tools-extra/clangd/IncludeCleaner.cpp
38

The heuristic seems plausible, though there are counterexamples like:

"enum Color;"
"void foo(Color); enum Color {};"

My main argument would be that we have a general policy here: conservatively consider all redecls as used, rather than trying to work out which is best. It's not clear why enums are special enough that we should hack around them without rethinking the policy.

(I think VisitEnumDecl is different - our general policy is that decls *aren't* references to forward-decls, and in enums' case this is violates our meta-policy of "be conservative, consider everything that might be a use").

kbobyrev updated this revision to Diff 382063.Oct 25 2021, 11:13 AM
kbobyrev marked 2 inline comments as done.

Resolve review & offline comments.

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

This and our offline discussion makes total sense, I will delete it from the patch.

kbobyrev updated this revision to Diff 382065.Oct 25 2021, 11:14 AM

Delete duplicate tests.

sammccall accepted this revision.Oct 25 2021, 12:09 PM
This revision is now accepted and ready to land.Oct 25 2021, 12:09 PM
thakis added a subscriber: thakis.Oct 26 2021, 8:01 AM

Follow-up commit https://github.com/llvm/llvm-project/commit/1c2e249f938c50e1b331a1f7adc83c0a381f3897 (which doesn't seem to have its own phab link) broke clangd tests everywhere, e.g. http://45.33.8.238/linux/59134/step_9.txt

Please take a look.

This is like the third clangd-caused build failure in the last 12 h or so h :/

Yes, I'm aware and actively working on this one. I think I have the fix but
I'm trying to run all the tests (Clang etc) to make sure it doesn't break
anything else.

Hey Kirill,

If you mean removing traversal of the enumdecl->enumtype in
RecursiveASTVisitor, that's a subtle change that potentially affects a
lot of callers.
We'd need to be able to revert it, so we shouldn't rely on it to get
the tests back to green.

It looks like there's a single assertion failure, can you just change
the assertion to assert the (bad) current behavior?
(Or revert the patches)