Page MenuHomePhabricator

[clangd] Basic IncludeCleaner support for c/c++ standard library

Authored by sammccall on Nov 17 2021, 4:29 AM.



There are some limitations here, so this is behind a flag for now (in addition
to the config setting for the overall feature).

  • symbols without exactly one associated header aren't handled right
  • no macro support
  • referencing std::size_t usually doesn't leave any trace in the AST that the alias in std was used, so we associate with stddef.h instead of cstddef. (An AST issue not specific to stdlib, but much worse there)

Diff Detail

Event Timeline

sammccall created this revision.Nov 17 2021, 4:29 AM
sammccall requested review of this revision.Nov 17 2021, 4:29 AM

I can't remember how we discussed this offline, but my plan was to land this and address the limitations as followups rather than extend the scope of this patch.
So I think this is ready for review, though certainly not urgent.

Mostly LG, few nits.


Do we need a forward decl here?


nit: maybe mention the parameter name? it seems redundant but consistent with what we have around here.


What's the difference between header() and headers()? Without looking at the code below, I presume this is for symbols that could be defined in multiple headers?


maybe DenseMapInfo<unsigned>::getEmptyKey() and DenseMapInfo<unsigned>::getTombstoneKey() just like above?


Maybe pull this upstairs and use early return instead? It's either user code or Stdlib, so maybe add exclusively to one of the sets.


Not sure but: don't we want a config option instead? We can remove it just as easily since it's all "hidden" right now.

sammccall updated this revision to Diff 396635.Thu, Dec 30, 3:39 AM

(rebase before addressing comments)

sammccall updated this revision to Diff 396639.Thu, Dec 30, 3:57 AM
sammccall marked 4 inline comments as done.

address comments


Decl/NamespaceDecl are needed for the interface of the stdlib symbol recognizer, but we don't need to depend on any of the details of AST here.

Splitting the stdlib stuff into its own header seems possible if you'd prefer that?


empty/tombstone keys are reserved values, and we know what sensible reserved values are better than the traits for unsigned do.

I can fix the code above if you like.


I think we discussed this offline a bunch?

A config option is a bunch more plumbing, and we don't actually want to expose this option.

nridge added a subscriber: nridge.Sat, Jan 1, 5:02 PM
kbobyrev accepted this revision.Sun, Jan 2, 11:48 PM

LG with a nit, thanks!


Okay, that makes sense! Yes, that would be great if you could fix the code above for consistency :)


Oh, right, thanks for reminding!

This revision is now accepted and ready to land.Sun, Jan 2, 11:48 PM
This revision was automatically updated to reflect the committed changes.
sammccall marked 4 inline comments as done.

Breaks building on windows:


Fixed (I think) in a61f34ea2502d900c57a332174d4c103b6963c80.
Clang successfully emulated MSVC's misunderstanding of the code :-(


Done... yikes, the existing HeaderID empty key was zero... which is also the HeaderID for the main file!

Changed these to -1 and -2. Hopefully this is NFC, else maybe it fixed a scary bug...

I think the builder is stuck, that build is some hours old and doesn't have the fix in it.
(Which leaves the possibility that i broke the bot somehow, I'm poking at some other bots trying to see if any of them are more broken than normal...)

Thanks for noticing that. It's back and happy now.