This is an archive of the discontinued LLVM Phabricator instance.

[SourceManager] Extract 'findFileIDsForFile' from 'translateFile' to allow mapping from one file entry to multiple FileIDs
AcceptedPublic

Authored by arphaman on Aug 17 2018, 2:18 PM.

Details

Reviewers
jkorous
ioeric
Summary

This patch extracts the code that searches for a file id from translateFile to findFileIDsForFile to allow the users to map from one file entry to multiple FileIDs.
Will be used as a basis for a clang-rename fix in https://reviews.llvm.org/D50801.

Diff Detail

Repository
rC Clang

Event Timeline

arphaman created this revision.Aug 17 2018, 2:18 PM
ioeric added inline comments.Aug 20 2018, 3:23 AM
include/clang/Basic/SourceManager.h
1555

nit: put this closer to the closely related translateFile?

1561

Callback pattern seems uncommon in LLVM/Clang. I'd suggest making this return a set of FileIDs and put the callback-based function as a helper (shared by this and translateFile)in the implementation.

arphaman updated this revision to Diff 161504.Aug 20 2018, 10:16 AM
arphaman marked 2 inline comments as done.

Address review comments.

include/clang/Basic/SourceManager.h
1561

I created a helper, but unfortunately it needs to be a member of SourceManager as FileID::get is private.

ioeric added inline comments.Aug 21 2018, 12:59 AM
lib/Basic/SourceManager.cpp
1701–1702

Do we also need this in findFileIDsForFile?

unittests/Basic/SourceManagerTest.cpp
428

Could you add a test case for getting file ID for main file, just to make sure we also covered cases handled by if (MainFileID.isValid()) {...} code path in translateFile()?

arphaman updated this revision to Diff 161847.Aug 21 2018, 4:49 PM
arphaman marked an inline comment as done.

Address review comments

lib/Basic/SourceManager.cpp
1701–1702

I don't really need this for my use case.

ioeric added inline comments.Aug 22 2018, 4:06 AM
lib/Basic/SourceManager.cpp
1701–1702

But it's not clear from the interface AFAICT. We should either handle this case (maybe controlled with a flag), or make it clear in the API (with a different name or documentation).

arphaman updated this revision to Diff 162106.Aug 22 2018, 4:47 PM

Address review comments.

arphaman marked an inline comment as done.Aug 22 2018, 4:47 PM
arphaman added inline comments.
lib/Basic/SourceManager.cpp
1701–1702

Hmm, I think that would be better. I pulled that code into the findFileIDsForFile helper function, so we can call it in two modes now. It's probably good to do that check just in case in the new API too, so it does that check as well.

ioeric added inline comments.Aug 27 2018, 4:46 AM
lib/Basic/SourceManager.cpp
1626

Should we check FileID::get(I) is valid?

1628

The conditions here are pretty hard to follow and reason about. Could we maybe split them (some documentation would also help)?

1628

In the original version, file system updates are checked last (after modules). Any reason to move it before modules?

Also, it seems that this code path could also be run when FileIDabove is invalid? So I wonder whether else if is correct here.

arphaman updated this revision to Diff 164734.Sep 10 2018, 12:59 PM
arphaman marked 3 inline comments as done.

Remove dead code for filesystem update fileID matching.

lib/Basic/SourceManager.cpp
1626

That's not really necessary. The FileID we get should be valid as a local SLoc entry should have a corresponding FileID. The SLoc 0 is not a File loc entry so we can never get FileID::get(0) here.

1628

I just realized that the original file system code has never been executed and was just dead code! If we take a look at this logic:

for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
    FileID IFileID;
    IFileID.ID = I;
    const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid);
    if (Invalid)
      return false;

You'll notice that the loop starts iterating at 0. Get SLocEntry is called with FileID 0, which sets the Invalid flag to true. Then we simply return, so the loop never reached the code below. Looks like it's a regression that happened years ago. I removed this code for now, but I'll reinstate it correctly in a follow-up patch.

jkorous accepted this revision.Nov 7 2018, 2:45 AM

LGTM and it seems like all of Eric's comments were answered too.

This revision is now accepted and ready to land.Nov 7 2018, 2:45 AM