| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H |
| 28 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H |
| 29 |
|
| 30 |
#include "FileDistance.h" |
| 31 |
#include "clang/Sema/CodeCompleteConsumer.h" |
| 32 |
#include "llvm/ADT/StringRef.h" |
| 33 |
#include "llvm/ADT/StringSet.h" |
| 34 |
#include <algorithm> |
| 35 |
#include <functional> |
| 36 |
#include <optional> |
| 37 |
#include <vector> |
| 38 |
|
| 39 |
namespace llvm { |
| 40 |
class raw_ostream; |
| 41 |
} |
| 42 |
|
| 43 |
namespace clang { |
| 44 |
class CodeCompletionResult; |
| 45 |
|
| 46 |
namespace clangd { |
| 47 |
struct ASTSignals; |
| 48 |
struct Symbol; |
| 49 |
class URIDistance; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
struct SymbolQualitySignals { |
| 57 |
bool Deprecated = false; |
| 58 |
bool ReservedName = false; |
| 59 |
|
| 60 |
bool ImplementationDetail = false; |
| 61 |
unsigned References = 0; |
| 62 |
|
| 63 |
enum SymbolCategory { |
| 64 |
Unknown = 0, |
| 65 |
Variable, |
| 66 |
Macro, |
| 67 |
Type, |
| 68 |
Function, |
| 69 |
Constructor, |
| 70 |
Destructor, |
| 71 |
Namespace, |
| 72 |
Keyword, |
| 73 |
Operator, |
| 74 |
} Category = Unknown; |
| 75 |
|
| 76 |
void merge(const CodeCompletionResult &SemaCCResult); |
| 77 |
void merge(const Symbol &IndexResult); |
| 78 |
|
| 79 |
|
| 80 |
float evaluateHeuristics() const; |
| 81 |
}; |
| 82 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &, |
| 83 |
const SymbolQualitySignals &); |
| 84 |
|
| 85 |
|
| 86 |
struct SymbolRelevanceSignals { |
| 87 |
|
| 88 |
llvm::StringRef Name; |
| 89 |
|
| 90 |
float NameMatch = 1; |
| 91 |
|
| 92 |
llvm::StringSet<>* ContextWords = nullptr; |
| 93 |
bool Forbidden = false; |
| 94 |
|
| 95 |
bool NeedsFixIts = false; |
| 96 |
bool InBaseClass = false; |
| 97 |
|
| 98 |
URIDistance *FileProximityMatch = nullptr; |
| 99 |
|
| 100 |
|
| 101 |
llvm::StringRef SymbolURI; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
float SemaFileProximityScore = 0; |
| 106 |
|
| 107 |
|
| 108 |
ScopeDistance *ScopeProximityMatch = nullptr; |
| 109 |
std::optional<llvm::StringRef> SymbolScope; |
| 110 |
|
| 111 |
bool SemaSaysInScope = false; |
| 112 |
|
| 113 |
|
| 114 |
enum AccessibleScope { |
| 115 |
FunctionScope, |
| 116 |
ClassScope, |
| 117 |
FileScope, |
| 118 |
GlobalScope, |
| 119 |
} Scope = GlobalScope; |
| 120 |
|
| 121 |
enum QueryType { |
| 122 |
CodeComplete, |
| 123 |
Generic, |
| 124 |
} Query = Generic; |
| 125 |
|
| 126 |
CodeCompletionContext::Kind Context = CodeCompletionContext::CCC_Other; |
| 127 |
|
| 128 |
|
| 129 |
bool IsInstanceMember = false; |
| 130 |
|
| 131 |
|
| 132 |
bool HadContextType = false; |
| 133 |
|
| 134 |
bool HadSymbolType = false; |
| 135 |
|
| 136 |
bool TypeMatchesPreferred = false; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
unsigned FilterLength = 0; |
| 141 |
|
| 142 |
const ASTSignals *MainFileSignals = nullptr; |
| 143 |
|
| 144 |
unsigned MainFileRefs = 0; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
unsigned ScopeRefsInFile = 0; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
struct DerivedSignals { |
| 153 |
|
| 154 |
bool NameMatchesContext = false; |
| 155 |
|
| 156 |
unsigned FileProximityDistance = FileDistance::Unreachable; |
| 157 |
|
| 158 |
unsigned ScopeProximityDistance = FileDistance::Unreachable; |
| 159 |
}; |
| 160 |
|
| 161 |
DerivedSignals calculateDerivedSignals() const; |
| 162 |
|
| 163 |
void merge(const CodeCompletionResult &SemaResult); |
| 164 |
void merge(const Symbol &IndexResult); |
| 165 |
void computeASTSignals(const CodeCompletionResult &SemaResult); |
| 166 |
|
| 167 |
|
| 168 |
float evaluateHeuristics() const; |
| 169 |
}; |
| 170 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &, |
| 171 |
const SymbolRelevanceSignals &); |
| 172 |
|
| 173 |
|
| 174 |
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
struct DecisionForestScores { |
| 180 |
float Total = 0.f; |
| 181 |
float ExcludingName = 0.f; |
| 182 |
}; |
| 183 |
|
| 184 |
DecisionForestScores |
| 185 |
evaluateDecisionForest(const SymbolQualitySignals &Quality, |
| 186 |
const SymbolRelevanceSignals &Relevance, float Base); |
| 187 |
|
| 188 |
|
| 189 |
template <typename T, typename Compare = std::greater<T>> class TopN { |
| 190 |
public: |
| 191 |
using value_type = T; |
| 192 |
TopN(size_t N, Compare Greater = Compare()) |
| 193 |
: N(N), Greater(std::move(Greater)) {} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
bool push(value_type &&V) { |
| 198 |
bool Dropped = false; |
| 199 |
if (Heap.size() >= N) { |
| 200 |
Dropped = true; |
| 201 |
if (N > 0 && Greater(V, Heap.front())) { |
| 202 |
std::pop_heap(Heap.begin(), Heap.end(), Greater); |
| 203 |
Heap.back() = std::move(V); |
| 204 |
std::push_heap(Heap.begin(), Heap.end(), Greater); |
| 205 |
} |
| 206 |
} else { |
| 207 |
Heap.push_back(std::move(V)); |
| 208 |
std::push_heap(Heap.begin(), Heap.end(), Greater); |
| 209 |
} |
| 210 |
assert(Heap.size() <= N); |
| 211 |
assert(std::is_heap(Heap.begin(), Heap.end(), Greater)); |
| 212 |
return Dropped; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
std::vector<value_type> items() && { |
| 217 |
std::sort_heap(Heap.begin(), Heap.end(), Greater); |
| 218 |
assert(Heap.size() <= N); |
| 219 |
return std::move(Heap); |
| 220 |
} |
| 221 |
|
| 222 |
private: |
| 223 |
const size_t N; |
| 224 |
std::vector<value_type> Heap; |
| 225 |
Compare Greater; |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
std::string sortText(float Score, llvm::StringRef Tiebreak = ""); |
| 231 |
|
| 232 |
struct SignatureQualitySignals { |
| 233 |
uint32_t NumberOfParameters = 0; |
| 234 |
uint32_t NumberOfOptionalParameters = 0; |
| 235 |
CodeCompleteConsumer::OverloadCandidate::CandidateKind Kind = |
| 236 |
CodeCompleteConsumer::OverloadCandidate::CandidateKind::CK_Function; |
| 237 |
}; |
| 238 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &, |
| 239 |
const SignatureQualitySignals &); |
| 240 |
|
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#endif |