diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -18,6 +18,7 @@ #include "URI.h" #include "index/Index.h" #include "index/Merge.h" +#include "index/Relation.h" #include "index/SymbolCollector.h" #include "index/SymbolLocation.h" #include "clang/AST/ASTContext.h" @@ -1107,7 +1108,7 @@ const SymbolIndex *Index, int Levels, PathRef TUPath) { RelationsRequest Req; Req.Subjects.insert(ID); - Req.Predicate = index::SymbolRole::RelationBaseOf; + Req.Predicate = RelationKind::BaseOf; Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) { if (Optional ChildSym = symbolToTypeHierarchyItem(Object, Index, TUPath)) { diff --git a/clang-tools-extra/clangd/index/Index.h b/clang-tools-extra/clangd/index/Index.h --- a/clang-tools-extra/clangd/index/Index.h +++ b/clang-tools-extra/clangd/index/Index.h @@ -75,7 +75,7 @@ struct RelationsRequest { llvm::DenseSet Subjects; - index::SymbolRole Predicate; + RelationKind Predicate; /// If set, limit the number of relations returned from the index. llvm::Optional Limit; }; diff --git a/clang-tools-extra/clangd/index/MemIndex.h b/clang-tools-extra/clangd/index/MemIndex.h --- a/clang-tools-extra/clangd/index/MemIndex.h +++ b/clang-tools-extra/clangd/index/MemIndex.h @@ -27,8 +27,9 @@ for (const std::pair> &R : Refs) this->Refs.try_emplace(R.first, R.second.begin(), R.second.end()); for (const Relation &R : Relations) - this->Relations[std::make_pair(R.Subject, R.Predicate)].push_back( - R.Object); + this->Relations[std::make_pair(R.Subject, + static_cast(R.Predicate))] + .push_back(R.Object); } // Symbols are owned by BackingData, Index takes ownership. template > Refs; // A map from (subject, predicate) pair to objects. - llvm::DenseMap, std::vector> - Relations; + static_assert(sizeof(RelationKind) == sizeof(uint8_t), + "RelationKind should be of same size as a uint8_t"); + llvm::DenseMap, std::vector> Relations; std::shared_ptr KeepAlive; // poor man's move-only std::any // Size of memory retained by KeepAlive. size_t BackingDataSize = 0; diff --git a/clang-tools-extra/clangd/index/MemIndex.cpp b/clang-tools-extra/clangd/index/MemIndex.cpp --- a/clang-tools-extra/clangd/index/MemIndex.cpp +++ b/clang-tools-extra/clangd/index/MemIndex.cpp @@ -92,7 +92,8 @@ Req.Limit.getValueOr(std::numeric_limits::max()); for (const SymbolID &Subject : Req.Subjects) { LookupRequest LookupReq; - auto It = Relations.find(std::make_pair(Subject, Req.Predicate)); + auto It = Relations.find( + std::make_pair(Subject, static_cast(Req.Predicate))); if (It != Relations.end()) { for (const auto &Obj : It->second) { if (Remaining > 0) { diff --git a/clang-tools-extra/clangd/index/Relation.h b/clang-tools-extra/clangd/index/Relation.h --- a/clang-tools-extra/clangd/index/Relation.h +++ b/clang-tools-extra/clangd/index/Relation.h @@ -19,12 +19,16 @@ namespace clang { namespace clangd { +enum class RelationKind : uint8_t { + BaseOf, +}; + /// Represents a relation between two symbols. /// For an example "A is a base class of B" may be represented -/// as { Subject = A, Predicate = RelationBaseOf, Object = B }. +/// as { Subject = A, Predicate = BaseOf, Object = B }. struct Relation { SymbolID Subject; - index::SymbolRole Predicate; + RelationKind Predicate; SymbolID Object; bool operator==(const Relation &Other) const { @@ -59,7 +63,7 @@ /// Lookup all relations matching the given subject and predicate. llvm::iterator_range lookup(const SymbolID &Subject, - index::SymbolRole Predicate) const; + RelationKind Predicate) const; /// RelationSlab::Builder is a mutable container that can 'freeze' to /// RelationSlab. @@ -85,31 +89,4 @@ } // namespace clangd } // namespace clang -namespace llvm { - -// Support index::SymbolRole as a DenseMap key for the purpose of looking up -// relations. -template <> struct DenseMapInfo { - static inline clang::index::SymbolRole getEmptyKey() { - // Choose an enumerator that's not a relation. - return clang::index::SymbolRole::Declaration; - } - - static inline clang::index::SymbolRole getTombstoneKey() { - // Choose another enumerator that's not a relation. - return clang::index::SymbolRole::Definition; - } - - static unsigned getHashValue(const clang::index::SymbolRole &Key) { - return hash_value(Key); - } - - static bool isEqual(const clang::index::SymbolRole &LHS, - const clang::index::SymbolRole &RHS) { - return LHS == RHS; - } -}; - -} // namespace llvm - #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RELATION_H diff --git a/clang-tools-extra/clangd/index/Relation.cpp b/clang-tools-extra/clangd/index/Relation.cpp --- a/clang-tools-extra/clangd/index/Relation.cpp +++ b/clang-tools-extra/clangd/index/Relation.cpp @@ -14,8 +14,7 @@ namespace clangd { llvm::iterator_range -RelationSlab::lookup(const SymbolID &Subject, - index::SymbolRole Predicate) const { +RelationSlab::lookup(const SymbolID &Subject, RelationKind Predicate) const { auto IterPair = std::equal_range(Relations.begin(), Relations.end(), Relation{Subject, Predicate, SymbolID{}}, [](const Relation &A, const Relation &B) { diff --git a/clang-tools-extra/clangd/index/Serialization.h b/clang-tools-extra/clangd/index/Serialization.h --- a/clang-tools-extra/clangd/index/Serialization.h +++ b/clang-tools-extra/clangd/index/Serialization.h @@ -83,11 +83,6 @@ std::unique_ptr loadIndex(llvm::StringRef Filename, bool UseDex = true); -// Used for serializing SymbolRole as used in Relation. -enum class RelationKind : uint8_t { BaseOf }; -RelationKind symbolRoleToRelationKind(index::SymbolRole); -index::SymbolRole relationKindToSymbolRole(RelationKind); - } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/index/Serialization.cpp b/clang-tools-extra/clangd/index/Serialization.cpp --- a/clang-tools-extra/clangd/index/Serialization.cpp +++ b/clang-tools-extra/clangd/index/Serialization.cpp @@ -29,29 +29,6 @@ return llvm::make_error(Msg, llvm::inconvertibleErrorCode()); } -} // namespace - -RelationKind symbolRoleToRelationKind(index::SymbolRole Role) { - // SymbolRole is used to record relations in the index. - // Only handle the relations we actually store currently. - // If we start storing more relations, this list can be expanded. - switch (Role) { - case index::SymbolRole::RelationBaseOf: - return RelationKind::BaseOf; - default: - llvm_unreachable("Unsupported symbol role"); - } -} - -index::SymbolRole relationKindToSymbolRole(RelationKind Kind) { - switch (Kind) { - case RelationKind::BaseOf: - return index::SymbolRole::RelationBaseOf; - } - llvm_unreachable("Invalid relation kind"); -} - -namespace { // IO PRIMITIVES // We use little-endian 32 bit ints, sometimes with variable-length encoding. @@ -395,15 +372,13 @@ void writeRelation(const Relation &R, llvm::raw_ostream &OS) { OS << R.Subject.raw(); - RelationKind Kind = symbolRoleToRelationKind(R.Predicate); - OS.write(static_cast(Kind)); + OS.write(static_cast(R.Predicate)); OS << R.Object.raw(); } Relation readRelation(Reader &Data) { SymbolID Subject = Data.consumeID(); - index::SymbolRole Predicate = - relationKindToSymbolRole(static_cast(Data.consume8())); + RelationKind Predicate = static_cast(Data.consume8()); SymbolID Object = Data.consumeID(); return {Subject, Predicate, Object}; } diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -445,8 +445,7 @@ // in the index and find nothing, but that's a situation they // probably need to handle for other reasons anyways. // We currently do (B) because it's simpler. - this->Relations.insert( - Relation{ID, index::SymbolRole::RelationBaseOf, *ObjectID}); + this->Relations.insert(Relation{ID, RelationKind::BaseOf, *ObjectID}); } } diff --git a/clang-tools-extra/clangd/index/YAMLSerialization.cpp b/clang-tools-extra/clangd/index/YAMLSerialization.cpp --- a/clang-tools-extra/clangd/index/YAMLSerialization.cpp +++ b/clang-tools-extra/clangd/index/YAMLSerialization.cpp @@ -282,14 +282,11 @@ struct NormalizedSymbolRole { NormalizedSymbolRole(IO &) {} - NormalizedSymbolRole(IO &IO, SymbolRole R) { - Kind = static_cast(clang::clangd::symbolRoleToRelationKind(R)); + NormalizedSymbolRole(IO &IO, RelationKind R) { + Kind = static_cast(R); } - SymbolRole denormalize(IO &IO) { - return clang::clangd::relationKindToSymbolRole( - static_cast(Kind)); - } + RelationKind denormalize(IO &IO) { return static_cast(Kind); } uint8_t Kind = 0; }; @@ -303,7 +300,7 @@ template <> struct MappingTraits { static void mapping(IO &IO, Relation &Relation) { - MappingNormalization NRole( + MappingNormalization NRole( IO, Relation.Predicate); IO.mapRequired("Subject", Relation.Subject); IO.mapRequired("Predicate", NRole->Kind); diff --git a/clang-tools-extra/clangd/index/dex/Dex.h b/clang-tools-extra/clangd/index/dex/Dex.h --- a/clang-tools-extra/clangd/index/dex/Dex.h +++ b/clang-tools-extra/clangd/index/dex/Dex.h @@ -26,6 +26,7 @@ #include "Trigram.h" #include "index/Index.h" #include "index/MemIndex.h" +#include "index/Relation.h" #include "index/SymbolCollector.h" namespace clang { @@ -49,8 +50,9 @@ for (auto &&Ref : Refs) this->Refs.try_emplace(Ref.first, Ref.second); for (auto &&Rel : Relations) - this->Relations[std::make_pair(Rel.Subject, Rel.Predicate)].push_back( - Rel.Object); + this->Relations[std::make_pair(Rel.Subject, + static_cast(Rel.Predicate))] + .push_back(Rel.Object); buildIndex(); } // Symbols and Refs are owned by BackingData, Index takes ownership. @@ -106,8 +108,9 @@ llvm::DenseMap InvertedIndex; dex::Corpus Corpus; llvm::DenseMap> Refs; - llvm::DenseMap, std::vector> - Relations; + static_assert(sizeof(RelationKind) == sizeof(uint8_t), + "RelationKind should be of same size as a uint8_t"); + llvm::DenseMap, std::vector> Relations; std::shared_ptr KeepAlive; // poor man's move-only std::any // Size of memory retained by KeepAlive. size_t BackingDataSize = 0; diff --git a/clang-tools-extra/clangd/index/dex/Dex.cpp b/clang-tools-extra/clangd/index/dex/Dex.cpp --- a/clang-tools-extra/clangd/index/dex/Dex.cpp +++ b/clang-tools-extra/clangd/index/dex/Dex.cpp @@ -271,7 +271,8 @@ Req.Limit.getValueOr(std::numeric_limits::max()); for (const SymbolID &Subject : Req.Subjects) { LookupRequest LookupReq; - auto It = Relations.find(std::make_pair(Subject, Req.Predicate)); + auto It = Relations.find( + std::make_pair(Subject, static_cast(Req.Predicate))); if (It != Relations.end()) { for (const auto &Object : It->second) { if (Remaining > 0) { diff --git a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp @@ -239,9 +239,8 @@ // containing the definition of the subject (A_CC) SymbolID A = findSymbol(*ShardHeader->Symbols, "A_CC").ID; SymbolID B = findSymbol(*ShardSource->Symbols, "B_CC").ID; - EXPECT_THAT( - *ShardHeader->Relations, - UnorderedElementsAre(Relation{A, index::SymbolRole::RelationBaseOf, B})); + EXPECT_THAT(*ShardHeader->Relations, + UnorderedElementsAre(Relation{A, RelationKind::BaseOf, B})); // (and not in the file containing the definition of the object (B_CC)). EXPECT_EQ(ShardSource->Relations->size(), 0u); } diff --git a/clang-tools-extra/clangd/unittests/DexTests.cpp b/clang-tools-extra/clangd/unittests/DexTests.cpp --- a/clang-tools-extra/clangd/unittests/DexTests.cpp +++ b/clang-tools-extra/clangd/unittests/DexTests.cpp @@ -705,16 +705,15 @@ std::vector Symbols{Parent, Child1, Child2}; - std::vector Relations{ - {Parent.ID, index::SymbolRole::RelationBaseOf, Child1.ID}, - {Parent.ID, index::SymbolRole::RelationBaseOf, Child2.ID}}; + std::vector Relations{{Parent.ID, RelationKind::BaseOf, Child1.ID}, + {Parent.ID, RelationKind::BaseOf, Child2.ID}}; Dex I{Symbols, RefSlab(), Relations}; std::vector Results; RelationsRequest Req; Req.Subjects.insert(Parent.ID); - Req.Predicate = index::SymbolRole::RelationBaseOf; + Req.Predicate = RelationKind::BaseOf; I.relations(Req, [&](const SymbolID &Subject, const Symbol &Object) { Results.push_back(Object.ID); }); diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp @@ -364,7 +364,7 @@ uint32_t Results = 0; RelationsRequest Req; Req.Subjects.insert(A); - Req.Predicate = index::SymbolRole::RelationBaseOf; + Req.Predicate = RelationKind::BaseOf; Index.relations(Req, [&](const SymbolID &, const Symbol &) { ++Results; }); EXPECT_EQ(Results, 1u); } diff --git a/clang-tools-extra/clangd/unittests/IndexTests.cpp b/clang-tools-extra/clangd/unittests/IndexTests.cpp --- a/clang-tools-extra/clangd/unittests/IndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/IndexTests.cpp @@ -83,20 +83,15 @@ SymbolID D{"D"}; RelationSlab::Builder Builder; - Builder.insert(Relation{A, index::SymbolRole::RelationBaseOf, B}); - Builder.insert(Relation{A, index::SymbolRole::RelationBaseOf, C}); - Builder.insert(Relation{B, index::SymbolRole::RelationBaseOf, D}); - Builder.insert(Relation{C, index::SymbolRole::RelationBaseOf, D}); - Builder.insert(Relation{B, index::SymbolRole::RelationChildOf, A}); - Builder.insert(Relation{C, index::SymbolRole::RelationChildOf, A}); - Builder.insert(Relation{D, index::SymbolRole::RelationChildOf, B}); - Builder.insert(Relation{D, index::SymbolRole::RelationChildOf, C}); + Builder.insert(Relation{A, RelationKind::BaseOf, B}); + Builder.insert(Relation{A, RelationKind::BaseOf, C}); + Builder.insert(Relation{B, RelationKind::BaseOf, D}); + Builder.insert(Relation{C, RelationKind::BaseOf, D}); RelationSlab Slab = std::move(Builder).build(); - EXPECT_THAT( - Slab.lookup(A, index::SymbolRole::RelationBaseOf), - UnorderedElementsAre(Relation{A, index::SymbolRole::RelationBaseOf, B}, - Relation{A, index::SymbolRole::RelationBaseOf, C})); + EXPECT_THAT(Slab.lookup(A, RelationKind::BaseOf), + UnorderedElementsAre(Relation{A, RelationKind::BaseOf, B}, + Relation{A, RelationKind::BaseOf, C})); } TEST(RelationSlab, Duplicates) { @@ -105,14 +100,13 @@ SymbolID C{"C"}; RelationSlab::Builder Builder; - Builder.insert(Relation{A, index::SymbolRole::RelationBaseOf, B}); - Builder.insert(Relation{A, index::SymbolRole::RelationBaseOf, C}); - Builder.insert(Relation{A, index::SymbolRole::RelationBaseOf, B}); + Builder.insert(Relation{A, RelationKind::BaseOf, B}); + Builder.insert(Relation{A, RelationKind::BaseOf, C}); + Builder.insert(Relation{A, RelationKind::BaseOf, B}); RelationSlab Slab = std::move(Builder).build(); - EXPECT_THAT(Slab, UnorderedElementsAre( - Relation{A, index::SymbolRole::RelationBaseOf, B}, - Relation{A, index::SymbolRole::RelationBaseOf, C})); + EXPECT_THAT(Slab, UnorderedElementsAre(Relation{A, RelationKind::BaseOf, B}, + Relation{A, RelationKind::BaseOf, C})); } TEST(SwapIndexTest, OldIndexRecycled) { diff --git a/clang-tools-extra/clangd/unittests/SerializationTests.cpp b/clang-tools-extra/clangd/unittests/SerializationTests.cpp --- a/clang-tools-extra/clangd/unittests/SerializationTests.cpp +++ b/clang-tools-extra/clangd/unittests/SerializationTests.cpp @@ -152,9 +152,9 @@ SymbolID Base = cantFail(SymbolID::fromStr("6481EE7AF2841756")); SymbolID Derived = cantFail(SymbolID::fromStr("6512AEC512EA3A2D")); ASSERT_TRUE(bool(ParsedYAML->Relations)); - EXPECT_THAT(*ParsedYAML->Relations, - UnorderedElementsAre( - Relation{Base, index::SymbolRole::RelationBaseOf, Derived})); + EXPECT_THAT( + *ParsedYAML->Relations, + UnorderedElementsAre(Relation{Base, RelationKind::BaseOf, Derived})); } std::vector YAMLFromSymbols(const SymbolSlab &Slab) { diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp --- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp +++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp @@ -673,8 +673,7 @@ const Symbol &Base = findSymbol(Symbols, "Base"); const Symbol &Derived = findSymbol(Symbols, "Derived"); EXPECT_THAT(Relations, - Contains(Relation{Base.ID, index::SymbolRole::RelationBaseOf, - Derived.ID})); + Contains(Relation{Base.ID, RelationKind::BaseOf, Derived.ID})); } TEST_F(SymbolCollectorTest, References) { diff --git a/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp b/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp --- a/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp +++ b/clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp @@ -482,7 +482,7 @@ std::vector Result; RelationsRequest Req; Req.Subjects.insert(Subject); - Req.Predicate = index::SymbolRole::RelationBaseOf; + Req.Predicate = RelationKind::BaseOf; Index->relations(Req, [&Result](const SymbolID &Subject, const Symbol &Object) { Result.push_back(Object.ID); diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h --- a/llvm/include/llvm/ADT/DenseMapInfo.h +++ b/llvm/include/llvm/ADT/DenseMapInfo.h @@ -67,6 +67,17 @@ } }; +// Provide DenseMapInfo for unsigned chars. +template <> struct DenseMapInfo { + static inline unsigned char getEmptyKey() { return ~0; } + static inline unsigned char getTombstoneKey() { return ~0 - 1; } + static unsigned getHashValue(const unsigned char &Val) { return Val * 37U; } + + static bool isEqual(const unsigned char &LHS, const unsigned char &RHS) { + return LHS == RHS; + } +}; + // Provide DenseMapInfo for unsigned shorts. template <> struct DenseMapInfo { static inline unsigned short getEmptyKey() { return 0xFFFF; }