This is an archive of the discontinued LLVM Phabricator instance.

Basic: Add hashing support for FileEntryRef and DirectoryEntryRef
ClosedPublic

Authored by dexonsmith on Dec 3 2020, 7:00 PM.

Details

Summary

Allow hashing FileEntryRef and DirectoryEntryRef via hash_value, and
use that to implement DenseMapInfo. This hash be equal whenever the
entry is the same (the name used to reference it is not relevant).

Also added DirectoryEntryRef::isSameRef as a drive-by to facilitate
testing.

Diff Detail

Event Timeline

dexonsmith created this revision.Dec 3 2020, 7:00 PM
dexonsmith requested review of this revision.Dec 3 2020, 7:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 3 2020, 7:00 PM
jansvoboda11 accepted this revision.Dec 8 2020, 5:00 AM

Left a couple of nits and a question, but LGTM overall.

clang/include/clang/Basic/DirectoryEntry.h
104

Nit: would it make sense to avoid copy-pasting the constructor logic here (llvm::DenseMapInfo<const MapEntry *>::getEmptyKey()) and call the constructor instead?
For example: isSameRef(DirectoryEntryRef(dense_map_empty_tag{})).

The same goes for FileEntryRef.

212

Nit: could use LHS.isSameRef(RHS) to keep this uniform.

clang/unittests/Basic/FileEntryTest.cpp
126

Question: do we prefer naming tests with the function/data structure under test, or using more descriptive names? (e.g. DenseMapFindsOldestEntry)

This revision is now accepted and ready to land.Dec 8 2020, 5:00 AM
arphaman accepted this revision.Dec 8 2020, 2:19 PM
dexonsmith added inline comments.Dec 8 2020, 3:16 PM
clang/include/clang/Basic/DirectoryEntry.h
104

Yes, that seems decent; I hadn't considered using isSameRef when the MapEntry is a bogus pointer, but that's probably better too. I'll update this and the instance of that below.

clang/unittests/Basic/FileEntryTest.cpp
126

Likely depends on the primary motivation of the test; I have a bit of tendency for the former in case expected behaviour changes, unless the whole point of the test is to test an interesting behaviour or corner case.

In this case, the main purpose of the test is to demonstrate that these can be used as DenseMap-family keys -- i.e., that the insert calls successfully compile. It's important that the behaviour is correct, too, of course.

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

Thanks for the reviews! Pushed 2878e965af27ce037378a4f0409e89039108c09f.

clang/include/clang/Basic/DirectoryEntry.h
104

Incorporated those suggestions (and the nit below) in the commit I pushed.