Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1 | //===--- Quality.cpp --------------------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | #include "Quality.h" |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 10 | #include "URI.h" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 11 | #include "index/Index.h" |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 13 | #include "clang/Basic/CharInfo.h" |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclVisitor.h" |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 17 | #include "llvm/Support/FormatVariadic.h" |
| 18 | #include "llvm/Support/MathExtras.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace clangd { |
| 23 | using namespace llvm; |
Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 24 | static bool IsReserved(StringRef Name) { |
| 25 | // FIXME: Should we exclude _Bool and others recognized by the standard? |
| 26 | return Name.size() >= 2 && Name[0] == '_' && |
| 27 | (isUppercase(Name[1]) || Name[1] == '_'); |
| 28 | } |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 29 | |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 30 | static bool hasDeclInMainFile(const Decl &D) { |
| 31 | auto &SourceMgr = D.getASTContext().getSourceManager(); |
| 32 | for (auto *Redecl : D.redecls()) { |
| 33 | auto Loc = SourceMgr.getSpellingLoc(Redecl->getLocation()); |
| 34 | if (SourceMgr.isWrittenInMainFile(Loc)) |
| 35 | return true; |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 40 | static SymbolQualitySignals::SymbolCategory categorize(const NamedDecl &ND) { |
| 41 | class Switch |
| 42 | : public ConstDeclVisitor<Switch, SymbolQualitySignals::SymbolCategory> { |
| 43 | public: |
| 44 | #define MAP(DeclType, Category) \ |
| 45 | SymbolQualitySignals::SymbolCategory Visit##DeclType(const DeclType *) { \ |
| 46 | return SymbolQualitySignals::Category; \ |
| 47 | } |
| 48 | MAP(NamespaceDecl, Namespace); |
| 49 | MAP(NamespaceAliasDecl, Namespace); |
| 50 | MAP(TypeDecl, Type); |
| 51 | MAP(TypeAliasTemplateDecl, Type); |
| 52 | MAP(ClassTemplateDecl, Type); |
| 53 | MAP(ValueDecl, Variable); |
| 54 | MAP(VarTemplateDecl, Variable); |
| 55 | MAP(FunctionDecl, Function); |
| 56 | MAP(FunctionTemplateDecl, Function); |
| 57 | MAP(Decl, Unknown); |
| 58 | #undef MAP |
| 59 | }; |
| 60 | return Switch().Visit(&ND); |
| 61 | } |
| 62 | |
Sam McCall | c3b5bad | 2018-06-14 13:42:21 +0000 | [diff] [blame] | 63 | static SymbolQualitySignals::SymbolCategory categorize(const CodeCompletionResult &R) { |
| 64 | if (R.Declaration) |
| 65 | return categorize(*R.Declaration); |
| 66 | if (R.Kind == CodeCompletionResult::RK_Macro) |
| 67 | return SymbolQualitySignals::Macro; |
| 68 | // Everything else is a keyword or a pattern. Patterns are mostly keywords |
| 69 | // too, except a few which we recognize by cursor kind. |
| 70 | switch (R.CursorKind) { |
| 71 | case CXCursor_CXXMethod: |
| 72 | return SymbolQualitySignals::Function; |
| 73 | case CXCursor_ModuleImportDecl: |
| 74 | return SymbolQualitySignals::Namespace; |
| 75 | case CXCursor_MacroDefinition: |
| 76 | return SymbolQualitySignals::Macro; |
| 77 | case CXCursor_TypeRef: |
| 78 | return SymbolQualitySignals::Type; |
| 79 | case CXCursor_MemberRef: |
| 80 | return SymbolQualitySignals::Variable; |
| 81 | default: |
| 82 | return SymbolQualitySignals::Keyword; |
| 83 | } |
| 84 | } |
| 85 | |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 86 | static SymbolQualitySignals::SymbolCategory |
| 87 | categorize(const index::SymbolInfo &D) { |
| 88 | switch (D.Kind) { |
| 89 | case index::SymbolKind::Namespace: |
| 90 | case index::SymbolKind::NamespaceAlias: |
| 91 | return SymbolQualitySignals::Namespace; |
| 92 | case index::SymbolKind::Macro: |
| 93 | return SymbolQualitySignals::Macro; |
| 94 | case index::SymbolKind::Enum: |
| 95 | case index::SymbolKind::Struct: |
| 96 | case index::SymbolKind::Class: |
| 97 | case index::SymbolKind::Protocol: |
| 98 | case index::SymbolKind::Extension: |
| 99 | case index::SymbolKind::Union: |
| 100 | case index::SymbolKind::TypeAlias: |
| 101 | return SymbolQualitySignals::Type; |
| 102 | case index::SymbolKind::Function: |
| 103 | case index::SymbolKind::ClassMethod: |
| 104 | case index::SymbolKind::InstanceMethod: |
| 105 | case index::SymbolKind::StaticMethod: |
| 106 | case index::SymbolKind::InstanceProperty: |
| 107 | case index::SymbolKind::ClassProperty: |
| 108 | case index::SymbolKind::StaticProperty: |
| 109 | case index::SymbolKind::Constructor: |
| 110 | case index::SymbolKind::Destructor: |
| 111 | case index::SymbolKind::ConversionFunction: |
| 112 | return SymbolQualitySignals::Function; |
| 113 | case index::SymbolKind::Variable: |
| 114 | case index::SymbolKind::Field: |
| 115 | case index::SymbolKind::EnumConstant: |
| 116 | case index::SymbolKind::Parameter: |
| 117 | return SymbolQualitySignals::Variable; |
| 118 | case index::SymbolKind::Using: |
| 119 | case index::SymbolKind::Module: |
| 120 | case index::SymbolKind::Unknown: |
| 121 | return SymbolQualitySignals::Unknown; |
| 122 | } |
Tim Northover | 0698e96 | 2018-06-06 13:28:49 +0000 | [diff] [blame] | 123 | llvm_unreachable("Unknown index::SymbolKind"); |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 126 | void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) { |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 127 | if (SemaCCResult.Availability == CXAvailability_Deprecated) |
| 128 | Deprecated = true; |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 129 | |
Sam McCall | c3b5bad | 2018-06-14 13:42:21 +0000 | [diff] [blame] | 130 | Category = categorize(SemaCCResult); |
Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 131 | |
| 132 | if (SemaCCResult.Declaration) { |
| 133 | if (auto *ID = SemaCCResult.Declaration->getIdentifier()) |
| 134 | ReservedName = ReservedName || IsReserved(ID->getName()); |
| 135 | } else if (SemaCCResult.Kind == CodeCompletionResult::RK_Macro) |
| 136 | ReservedName = ReservedName || IsReserved(SemaCCResult.Macro->getName()); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void SymbolQualitySignals::merge(const Symbol &IndexResult) { |
| 140 | References = std::max(IndexResult.References, References); |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 141 | Category = categorize(IndexResult.SymInfo); |
Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 142 | ReservedName = ReservedName || IsReserved(IndexResult.Name); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | float SymbolQualitySignals::evaluate() const { |
| 146 | float Score = 1; |
| 147 | |
| 148 | // This avoids a sharp gradient for tail symbols, and also neatly avoids the |
| 149 | // question of whether 0 references means a bad symbol or missing data. |
| 150 | if (References >= 3) |
| 151 | Score *= std::log(References); |
| 152 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 153 | if (Deprecated) |
Aaron Ballman | 215e471 | 2018-05-18 13:18:41 +0000 | [diff] [blame] | 154 | Score *= 0.1f; |
Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 155 | if (ReservedName) |
| 156 | Score *= 0.1f; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 157 | |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 158 | switch (Category) { |
Sam McCall | c3b5bad | 2018-06-14 13:42:21 +0000 | [diff] [blame] | 159 | case Keyword: // Usually relevant, but misses most signals. |
| 160 | Score *= 10; |
| 161 | break; |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 162 | case Type: |
| 163 | case Function: |
| 164 | case Variable: |
Simon Pilgrim | 0c9e1c8 | 2018-06-06 12:48:27 +0000 | [diff] [blame] | 165 | Score *= 1.1f; |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 166 | break; |
| 167 | case Namespace: |
Simon Pilgrim | 0c9e1c8 | 2018-06-06 12:48:27 +0000 | [diff] [blame] | 168 | Score *= 0.8f; |
Sam McCall | bc7cbb7 | 2018-06-06 12:38:37 +0000 | [diff] [blame] | 169 | break; |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 170 | case Macro: |
Simon Pilgrim | 0c9e1c8 | 2018-06-06 12:48:27 +0000 | [diff] [blame] | 171 | Score *= 0.2f; |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 172 | break; |
| 173 | case Unknown: |
| 174 | break; |
| 175 | } |
| 176 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 177 | return Score; |
| 178 | } |
| 179 | |
| 180 | raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) { |
| 181 | OS << formatv("=== Symbol quality: {0}\n", S.evaluate()); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 182 | OS << formatv("\tReferences: {0}\n", S.References); |
| 183 | OS << formatv("\tDeprecated: {0}\n", S.Deprecated); |
Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 184 | OS << formatv("\tReserved name: {0}\n", S.ReservedName); |
Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 185 | OS << formatv("\tCategory: {0}\n", static_cast<int>(S.Category)); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 186 | return OS; |
| 187 | } |
| 188 | |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 189 | /// Calculates a proximity score from \p From and \p To, which are URI strings |
| 190 | /// that have the same scheme. This does not parse URI. A URI (sans "<scheme>:") |
| 191 | /// is split into chunks by '/' and each chunk is considered a file/directory. |
| 192 | /// For example, "uri:///a/b/c" will be treated as /a/b/c |
| 193 | static float uriProximity(StringRef From, StringRef To) { |
| 194 | auto SchemeSplitFrom = From.split(':'); |
| 195 | auto SchemeSplitTo = To.split(':'); |
| 196 | assert((SchemeSplitFrom.first == SchemeSplitTo.first) && |
| 197 | "URIs must have the same scheme in order to compute proximity."); |
| 198 | auto Split = [](StringRef URIWithoutScheme) { |
| 199 | SmallVector<StringRef, 8> Split; |
| 200 | URIWithoutScheme.split(Split, '/', /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 201 | return Split; |
| 202 | }; |
| 203 | SmallVector<StringRef, 8> Fs = Split(SchemeSplitFrom.second); |
| 204 | SmallVector<StringRef, 8> Ts = Split(SchemeSplitTo.second); |
| 205 | auto F = Fs.begin(), T = Ts.begin(), FE = Fs.end(), TE = Ts.end(); |
| 206 | for (; F != FE && T != TE && *F == *T; ++F, ++T) { |
| 207 | } |
| 208 | // We penalize for traversing up and down from \p From to \p To but penalize |
| 209 | // less for traversing down because subprojects are more closely related than |
| 210 | // superprojects. |
| 211 | int UpDist = FE - F; |
| 212 | int DownDist = TE - T; |
| 213 | return std::pow(0.7, UpDist + DownDist/2); |
| 214 | } |
| 215 | |
| 216 | FileProximityMatcher::FileProximityMatcher(ArrayRef<StringRef> ProximityPaths) |
| 217 | : ProximityPaths(ProximityPaths.begin(), ProximityPaths.end()) {} |
| 218 | |
| 219 | float FileProximityMatcher::uriProximity(StringRef SymbolURI) const { |
| 220 | float Score = 0; |
| 221 | if (!ProximityPaths.empty() && !SymbolURI.empty()) { |
| 222 | for (const auto &Path : ProximityPaths) |
| 223 | // Only calculate proximity score for two URIs with the same scheme so |
| 224 | // that the computation can be purely text-based and thus avoid expensive |
| 225 | // URI encoding/decoding. |
| 226 | if (auto U = URI::create(Path, SymbolURI.split(':').first)) { |
| 227 | Score = std::max(Score, clangd::uriProximity(U->toString(), SymbolURI)); |
| 228 | } else { |
| 229 | llvm::consumeError(U.takeError()); |
| 230 | } |
| 231 | } |
| 232 | return Score; |
| 233 | } |
| 234 | |
| 235 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 236 | const FileProximityMatcher &M) { |
| 237 | OS << formatv("File proximity matcher: "); |
Eric Liu | ffaaf7d | 2018-06-21 09:51:28 +0000 | [diff] [blame^] | 238 | OS << formatv("ProximityPaths[{0}]", llvm::join(M.ProximityPaths.begin(), |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 239 | M.ProximityPaths.end(), ",")); |
| 240 | return OS; |
| 241 | } |
| 242 | |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 243 | static SymbolRelevanceSignals::AccessibleScope |
| 244 | ComputeScope(const NamedDecl &D) { |
Sam McCall | 89f5293 | 2018-06-05 18:00:48 +0000 | [diff] [blame] | 245 | bool InClass = false; |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 246 | for (const DeclContext *DC = D.getDeclContext(); !DC->isFileContext(); |
| 247 | DC = DC->getParent()) { |
| 248 | if (DC->isFunctionOrMethod()) |
| 249 | return SymbolRelevanceSignals::FunctionScope; |
| 250 | InClass = InClass || DC->isRecord(); |
| 251 | } |
| 252 | if (InClass) |
| 253 | return SymbolRelevanceSignals::ClassScope; |
| 254 | // This threshold could be tweaked, e.g. to treat module-visible as global. |
| 255 | if (D.getLinkageInternal() < ExternalLinkage) |
| 256 | return SymbolRelevanceSignals::FileScope; |
| 257 | return SymbolRelevanceSignals::GlobalScope; |
| 258 | } |
| 259 | |
| 260 | void SymbolRelevanceSignals::merge(const Symbol &IndexResult) { |
| 261 | // FIXME: Index results always assumed to be at global scope. If Scope becomes |
| 262 | // relevant to non-completion requests, we should recognize class members etc. |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 263 | |
| 264 | SymbolURI = IndexResult.CanonicalDeclaration.FileURI; |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 267 | void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) { |
| 268 | if (SemaCCResult.Availability == CXAvailability_NotAvailable || |
| 269 | SemaCCResult.Availability == CXAvailability_NotAccessible) |
| 270 | Forbidden = true; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 271 | |
| 272 | if (SemaCCResult.Declaration) { |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 273 | // We boost things that have decls in the main file. We give a fixed score |
| 274 | // for all other declarations in sema as they are already included in the |
| 275 | // translation unit. |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 276 | float DeclProximity = |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 277 | hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.6; |
| 278 | SemaProximityScore = std::max(DeclProximity, SemaProximityScore); |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 279 | } |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 280 | |
| 281 | // Declarations are scoped, others (like macros) are assumed global. |
Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame] | 282 | if (SemaCCResult.Declaration) |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 283 | Scope = std::min(Scope, ComputeScope(*SemaCCResult.Declaration)); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | float SymbolRelevanceSignals::evaluate() const { |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 287 | float Score = 1; |
| 288 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 289 | if (Forbidden) |
| 290 | return 0; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 291 | |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 292 | Score *= NameMatch; |
| 293 | |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 294 | float IndexProximityScore = |
| 295 | FileProximityMatch ? FileProximityMatch->uriProximity(SymbolURI) : 0; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 296 | // Proximity scores are [0,1] and we translate them into a multiplier in the |
| 297 | // range from 1 to 2. |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 298 | Score *= 1 + std::max(IndexProximityScore, SemaProximityScore); |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 299 | |
| 300 | // Symbols like local variables may only be referenced within their scope. |
| 301 | // Conversely if we're in that scope, it's likely we'll reference them. |
| 302 | if (Query == CodeComplete) { |
| 303 | // The narrower the scope where a symbol is visible, the more likely it is |
| 304 | // to be relevant when it is available. |
| 305 | switch (Scope) { |
| 306 | case GlobalScope: |
| 307 | break; |
| 308 | case FileScope: |
| 309 | Score *= 1.5; |
Sam McCall | c22c9aa | 2018-06-07 08:16:36 +0000 | [diff] [blame] | 310 | break; |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 311 | case ClassScope: |
| 312 | Score *= 2; |
Sam McCall | c22c9aa | 2018-06-07 08:16:36 +0000 | [diff] [blame] | 313 | break; |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 314 | case FunctionScope: |
| 315 | Score *= 4; |
Sam McCall | c22c9aa | 2018-06-07 08:16:36 +0000 | [diff] [blame] | 316 | break; |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 320 | return Score; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 321 | } |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 322 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 323 | raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) { |
| 324 | OS << formatv("=== Symbol relevance: {0}\n", S.evaluate()); |
| 325 | OS << formatv("\tName match: {0}\n", S.NameMatch); |
| 326 | OS << formatv("\tForbidden: {0}\n", S.Forbidden); |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 327 | OS << formatv("\tSymbol URI: {0}\n", S.SymbolURI); |
| 328 | if (S.FileProximityMatch) { |
Eric Liu | ffaaf7d | 2018-06-21 09:51:28 +0000 | [diff] [blame^] | 329 | OS << "\tIndex proximity: " |
| 330 | << S.FileProximityMatch->uriProximity(S.SymbolURI) << " (" |
| 331 | << *S.FileProximityMatch << ")\n"; |
Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 332 | } |
| 333 | OS << formatv("\tSema proximity: {0}\n", S.SemaProximityScore); |
Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame] | 334 | OS << formatv("\tQuery type: {0}\n", static_cast<int>(S.Query)); |
| 335 | OS << formatv("\tScope: {0}\n", static_cast<int>(S.Scope)); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 336 | return OS; |
| 337 | } |
| 338 | |
| 339 | float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) { |
| 340 | return SymbolQuality * SymbolRelevance; |
| 341 | } |
| 342 | |
| 343 | // Produces an integer that sorts in the same order as F. |
| 344 | // That is: a < b <==> encodeFloat(a) < encodeFloat(b). |
| 345 | static uint32_t encodeFloat(float F) { |
| 346 | static_assert(std::numeric_limits<float>::is_iec559, ""); |
| 347 | constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1); |
| 348 | |
| 349 | // Get the bits of the float. Endianness is the same as for integers. |
| 350 | uint32_t U = FloatToBits(F); |
| 351 | // IEEE 754 floats compare like sign-magnitude integers. |
| 352 | if (U & TopBit) // Negative float. |
| 353 | return 0 - U; // Map onto the low half of integers, order reversed. |
| 354 | return U + TopBit; // Positive floats map onto the high half of integers. |
| 355 | } |
| 356 | |
| 357 | std::string sortText(float Score, llvm::StringRef Name) { |
| 358 | // We convert -Score to an integer, and hex-encode for readability. |
| 359 | // Example: [0.5, "foo"] -> "41000000foo" |
| 360 | std::string S; |
| 361 | llvm::raw_string_ostream OS(S); |
| 362 | write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower, |
| 363 | /*Width=*/2 * sizeof(Score)); |
| 364 | OS << Name; |
| 365 | OS.flush(); |
| 366 | return S; |
| 367 | } |
| 368 | |
| 369 | } // namespace clangd |
| 370 | } // namespace clang |