This is an archive of the discontinued LLVM Phabricator instance.

[include-cleaner] Filter references to identity macros
ClosedPublic

Authored by kadircet on Aug 14 2023, 10:50 AM.

Details

Summary

Despite being true positives, these results just confuse users. So
filter them out.

Diff Detail

Event Timeline

kadircet created this revision.Aug 14 2023, 10:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 14 2023, 10:50 AM
kadircet requested review of this revision.Aug 14 2023, 10:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 14 2023, 10:50 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sammccall accepted this revision.Aug 14 2023, 12:55 PM
sammccall added inline comments.
clang-tools-extra/include-cleaner/lib/Analysis.cpp
39

shouldIgnoreMacroReference would better distinguish vs macro definition

45

Comment nit: I'm having trouble imagining cases that are actually*unrelated* to the stdlib.

If stderr is an impl-defined macro, then the only way to use the name to refer to something else is if it's not defined (inside ifndef stderr, or if you can be sure your TU doesn't include stdio first). Seems implausible...

That said I feel like we've had this conversation before and I've just forgotten the details.

46

Nit: "false positives" is a little unclear: positives for this function are negatives for walkAST and could be either for diagnostics.

Also, I think you mean *references* rather than declarations?

This revision is now accepted and ready to land.Aug 14 2023, 12:55 PM
kadircet updated this revision to Diff 550662.Aug 16 2023, 1:43 AM
kadircet marked an inline comment as done.
  • Rename helper, update comments.
kadircet marked an inline comment as done.Aug 16 2023, 1:43 AM
kadircet added inline comments.
clang-tools-extra/include-cleaner/lib/Analysis.cpp
45

If stderr is an impl-defined macro, then the only way to use the name to refer to something else is if it's not defined

That's absolutely right. The issue here is macro expansion triggers independent of the context, e.g.

#include <stdio.h>
namespace ns { void stderr(); }
void foo() { ns::stderr(); }

here we have a (well two) reference to stderr macro from stdandard library, which is not user's intent, but rather a quirk of the language.

46

this should've been "false negatives" from the "usedness perspective", we basically drop the reference to ns::stderr in above example, because the name is not spelled in the main file (but rather spelled inside a macro body).

sammccall accepted this revision.Aug 16 2023, 1:52 AM

still LG, comments are still confusing me a little

clang-tools-extra/include-cleaner/lib/Analysis.cpp
45

Sure, but this code is not valid C++ (since stderr is not guaranteed to expand to a single identifier). Is this actually a common/motivating case?

46

And now it's backwards if your perspective is this function itself!

just avoid "false positives"/"false negatives" terminology and describe the actual effect?

kadircet marked 2 inline comments as done.Aug 16 2023, 9:35 AM
kadircet added inline comments.
clang-tools-extra/include-cleaner/lib/Analysis.cpp
45

Sure, but this code is not valid C++ (since stderr is not guaranteed to expand to a single identifier).

That's true. Most of the standard library implementations I checked does that, but I also don't like that reliance, so making it a little bit more generic (which meant some more plumbing, sorry for the big diff :( ), by making sure that we apply this to all the macros that expand to themselves.

kadircet updated this revision to Diff 550790.Aug 16 2023, 9:35 AM
  • Apply filtering only to macros that expand to themselves
sammccall accepted this revision.Aug 17 2023, 9:37 AM
sammccall added inline comments.
clang-tools-extra/include-cleaner/lib/Analysis.cpp
44

still call out stdout/stderr?

This revision was automatically updated to reflect the committed changes.
kadircet marked an inline comment as done.