This is an archive of the discontinued LLVM Phabricator instance.

Move the isSelfContained function from clangd to libtooling.
ClosedPublic

Authored by hokein on Nov 9 2022, 1:12 AM.

Details

Summary

We plan to reuse it in the include-cleaner library, this patch moves
this functionality from clangd to libtooling, so that this piece of code can be
shared among all clang tools.

Diff Detail

Event Timeline

hokein created this revision.Nov 9 2022, 1:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 9 2022, 1:12 AM
Herald added a subscriber: arphaman. · View Herald Transcript
hokein requested review of this revision.Nov 9 2022, 1:12 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptNov 9 2022, 1:12 AM
kadircet added inline comments.Nov 10 2022, 7:37 AM
clang/include/clang/Tooling/Inclusions/Header.h
1 ↗(On Diff #474188)

rather than calling this Header can we call this HeaderAnalysis?

21 ↗(On Diff #474188)

let's mention being #importd as well

clang/lib/Tooling/Inclusions/Header.cpp
64 ↗(On Diff #474188)

translateFile actually does a linear scan over all the slocentries, so i think it's better for this API to be based on FileID. (later on we can easily get fileentry from fileid in constant time)

clang/unittests/Tooling/HeaderTest.cpp
21 ↗(On Diff #474188)

can you also add a test case for #import ?

hokein updated this revision to Diff 474707.Nov 11 2022, 2:52 AM
hokein marked 3 inline comments as done.

address comments:

  • mention #import case
  • rename file name
  • API: FileEntry => FileID
sammccall added inline comments.Nov 11 2022, 4:22 AM
clang/lib/Tooling/Inclusions/Header.cpp
64 ↗(On Diff #474188)

You can get content through the fileentry directly, no?

kadircet added inline comments.Nov 11 2022, 4:38 AM
clang/include/clang/Tooling/Inclusions/Header.h
21 ↗(On Diff #474188)

again comment seems to be the same.

clang/lib/Tooling/Inclusions/Header.cpp
64 ↗(On Diff #474188)

oh thanks for reminding that i forgot to mention it here, yes we can get that, but that would misbehave in the case of virtual/remapped files. so can you actually add a comment here about why we should get the contents from source manager?

clang/unittests/Tooling/HeaderTest.cpp
21 ↗(On Diff #474188)

this is marked as done, but i can't see any #import statements here. am i missing something?

sammccall added inline comments.Nov 11 2022, 4:41 AM
clang/lib/Tooling/Inclusions/Header.cpp
64 ↗(On Diff #474188)

But even when that remapping happens at the sourcemanager level, it happens on fileentries, not FileIDs (how would the latter even work?)

So SM.getMemoryBufferForFileOrNone(FileEntry)?

kadircet added inline comments.Nov 11 2022, 4:55 AM
clang/lib/Tooling/Inclusions/Header.cpp
64 ↗(On Diff #474188)

oh i didn't know about getMemoryBufferForFileOrNone, looks like it should do.

But even when that remapping happens at the sourcemanager level, it happens on fileentries, not FileIDs (how would the latter even work?)

i was just looking at contentcache, which is a 1:1 mapping for each FileID and has fileentries, one for the original file and the other for providing the contents (or a virtual buffer). i didn't notice the overrideinfo map was also exposed.

hokein updated this revision to Diff 474735.Nov 11 2022, 5:37 AM

getBufferData => getMemoryBufferForFileOrNone

clang/include/clang/Tooling/Inclusions/Header.h
21 ↗(On Diff #474188)

you're looking at the old version, this file has been renamed, please see the https://reviews.llvm.org/D137697.

kadircet accepted this revision.Nov 11 2022, 6:00 AM

thanks, lgtm!

This revision is now accepted and ready to land.Nov 11 2022, 6:00 AM
kadircet added inline comments.Nov 11 2022, 6:02 AM
clang/include/clang/Tooling/Inclusions/HeaderAnalysis.h
29

oops sorry, missed this part. the idea with using getmemorybuffer was to use fileentries here.

kadircet requested changes to this revision.Nov 11 2022, 6:02 AM
This revision now requires changes to proceed.Nov 11 2022, 6:02 AM
hokein updated this revision to Diff 474751.Nov 11 2022, 6:47 AM
hokein marked an inline comment as done.

use FileEntry in the API.

kadircet accepted this revision.Nov 11 2022, 7:07 AM

thanks!

This revision is now accepted and ready to land.Nov 11 2022, 7:07 AM
nridge added a subscriber: nridge.EditedNov 13 2022, 12:08 AM

nit: isSelfContained --> isSelfContainedHeader in commit message

This revision was landed with ongoing or failed builds.Nov 14 2022, 12:41 AM
This revision was automatically updated to reflect the committed changes.

@hokein it seems the unit test wasn't updated for the last-minute API change, which breaks check-all for me. I've posted https://reviews.llvm.org/D137927, could you take a look if that change is correct?

@hokein it seems the unit test wasn't updated for the last-minute API change, which breaks check-all for me. I've posted https://reviews.llvm.org/D137927, could you take a look if that change is correct?

oops, thanks, and your analysis is right! (I landed a fix in 5c4ae8a86a865e622e2c663666c0c139334b5da2 before reading your email, sorry).

No worries, thanks for fixing so quickly! I just happened to pull in the wrong moment...