1 //===--- Trigram.h - Trigram generation for Fuzzy Matching ------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// Trigrams are attributes of the symbol unqualified name used to effectively 11 /// extract symbols which can be fuzzy-matched given user query from the 12 /// inverted index. To match query with the extracted set of trigrams Q, the set 13 /// of generated trigrams T for identifier (unqualified symbol name) should 14 /// contain all items of Q, i.e. Q ⊆ T. 15 /// 16 /// Trigram sets extracted from unqualified name and from query are different: 17 /// the set of query trigrams only contains consecutive sequences of three 18 /// characters (which is only a subset of all trigrams generated for an 19 /// identifier). 20 /// 21 //===----------------------------------------------------------------------===// 22 23 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DEX_TRIGRAM_H 24 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DEX_TRIGRAM_H 25 26 #include "Token.h" 27 #include "llvm/ADT/bit.h" 28 29 #include <string> 30 31 namespace clang { 32 namespace clangd { 33 namespace dex { 34 35 // Compact representation of a trigram (string with up to 3 characters). 36 // Trigram generation is the hot path of indexing, so Token is too wasteful. 37 class Trigram { 38 std::array<char, 4> Data; // Last element is length. 39 // Steal some invalid bit patterns for DenseMap sentinels. 40 enum class Sentinel { Tombstone = 4, Empty = 5 }; Trigram(Sentinel S)41 Trigram(Sentinel S) : Data{0, 0, 0, static_cast<char>(S)} {} id()42 uint32_t id() const { return llvm::bit_cast<uint32_t>(Data); } 43 44 public: Trigram()45 Trigram() : Data{0, 0, 0, 0} {} Trigram(char A)46 Trigram(char A) : Data{A, 0, 0, 1} {} Trigram(char A,char B)47 Trigram(char A, char B) : Data{A, B, 0, 2} {} Trigram(char A,char B,char C)48 Trigram(char A, char B, char C) : Data{A, B, C, 3} {} str()49 std::string str() const { return std::string(Data.data(), Data[3]); } 50 friend struct ::llvm::DenseMapInfo<Trigram>; 51 friend bool operator==(Trigram L, Trigram R) { return L.id() == R.id(); } 52 friend bool operator<(Trigram L, Trigram R) { return L.id() < R.id(); } 53 }; 54 55 /// Produces list of unique fuzzy-search trigrams from unqualified symbol. 56 /// The trigrams give the 3-character query substrings this symbol can match. 57 /// 58 /// The symbol's name is broken into segments, e.g. "FooBar" has two segments. 59 /// Trigrams can start at any character in the input. Then we can choose to move 60 /// to the next character or to the start of the next segment. 61 /// 62 /// Short trigrams (length 1-2) are used for short queries. These are: 63 /// - prefixes of the identifier, of length 1 and 2 64 /// - the first character + next head character 65 /// 66 /// For "FooBar" we get the following trigrams: 67 /// {f, fo, fb, foo, fob, fba, oob, oba, bar}. 68 /// 69 /// Trigrams are lowercase, as trigram matching is case-insensitive. 70 /// Trigrams in the list are deduplicated. 71 void generateIdentifierTrigrams(llvm::StringRef Identifier, 72 std::vector<Trigram> &Out); 73 74 /// Returns list of unique fuzzy-search trigrams given a query. 75 /// 76 /// Query is segmented using FuzzyMatch API and downcasted to lowercase. Then, 77 /// the simplest trigrams - sequences of three consecutive letters and digits 78 /// are extracted and returned after deduplication. 79 /// 80 /// For short queries (less than 3 characters with Head or Tail roles in Fuzzy 81 /// Matching segmentation) this returns a single trigram with the first 82 /// characters (up to 3) to perform prefix match. 83 std::vector<Token> generateQueryTrigrams(llvm::StringRef Query); 84 85 } // namespace dex 86 } // namespace clangd 87 } // namespace clang 88 89 namespace llvm { 90 template <> struct DenseMapInfo<clang::clangd::dex::Trigram> { 91 using Trigram = clang::clangd::dex::Trigram; 92 static inline Trigram getEmptyKey() { 93 return Trigram(Trigram::Sentinel::Empty); 94 } 95 static inline Trigram getTombstoneKey() { 96 return Trigram(Trigram::Sentinel::Tombstone); 97 } 98 static unsigned getHashValue(Trigram V) { 99 // Finalize step from MurmurHash3. 100 uint32_t X = V.id(); 101 X ^= X >> 16; 102 X *= uint32_t{0x85ebca6b}; 103 X ^= X >> 13; 104 X *= uint32_t{0xc2b2ae35}; 105 X ^= X >> 16; 106 return X; 107 } 108 static bool isEqual(const Trigram &LHS, const Trigram &RHS) { 109 return LHS == RHS; 110 } 111 }; 112 } // namespace llvm 113 114 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_DEX_TRIGRAM_H 115