blob: 050b0bf6ef20089a8145ee0bf4faa560589e8510 [file] [log] [blame]
Sam McCallc5707b62018-05-15 17:43:27 +00001//===--- 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"
10#include "index/Index.h"
11#include "clang/Sema/CodeCompleteConsumer.h"
12#include "llvm/Support/FormatVariadic.h"
13#include "llvm/Support/MathExtras.h"
14#include "llvm/Support/raw_ostream.h"
15
16namespace clang {
17namespace clangd {
18using namespace llvm;
19
20void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) {
21 SemaCCPriority = SemaCCResult.Priority;
22
23 if (SemaCCResult.Availability == CXAvailability_Deprecated)
24 Deprecated = true;
25}
26
27void SymbolQualitySignals::merge(const Symbol &IndexResult) {
28 References = std::max(IndexResult.References, References);
29}
30
31float SymbolQualitySignals::evaluate() const {
32 float Score = 1;
33
34 // This avoids a sharp gradient for tail symbols, and also neatly avoids the
35 // question of whether 0 references means a bad symbol or missing data.
36 if (References >= 3)
37 Score *= std::log(References);
38
39 if (SemaCCPriority)
40 // Map onto a 0-2 interval, so we don't reward/penalize non-Sema results.
41 // Priority 80 is a really bad score.
42 Score *= 2 - std::min<float>(80, SemaCCPriority) / 40;
43
44 if (Deprecated)
Aaron Ballman215e4712018-05-18 13:18:41 +000045 Score *= 0.1f;
Sam McCallc5707b62018-05-15 17:43:27 +000046
47 return Score;
48}
49
50raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) {
51 OS << formatv("=== Symbol quality: {0}\n", S.evaluate());
52 if (S.SemaCCPriority)
53 OS << formatv("\tSemaCCPriority: {0}\n", S.SemaCCPriority);
54 OS << formatv("\tReferences: {0}\n", S.References);
55 OS << formatv("\tDeprecated: {0}\n", S.Deprecated);
56 return OS;
57}
58
59void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
60 if (SemaCCResult.Availability == CXAvailability_NotAvailable ||
61 SemaCCResult.Availability == CXAvailability_NotAccessible)
62 Forbidden = true;
63}
64
65float SymbolRelevanceSignals::evaluate() const {
66 if (Forbidden)
67 return 0;
68 return NameMatch;
69}
70raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) {
71 OS << formatv("=== Symbol relevance: {0}\n", S.evaluate());
72 OS << formatv("\tName match: {0}\n", S.NameMatch);
73 OS << formatv("\tForbidden: {0}\n", S.Forbidden);
74 return OS;
75}
76
77float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) {
78 return SymbolQuality * SymbolRelevance;
79}
80
81// Produces an integer that sorts in the same order as F.
82// That is: a < b <==> encodeFloat(a) < encodeFloat(b).
83static uint32_t encodeFloat(float F) {
84 static_assert(std::numeric_limits<float>::is_iec559, "");
85 constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1);
86
87 // Get the bits of the float. Endianness is the same as for integers.
88 uint32_t U = FloatToBits(F);
89 // IEEE 754 floats compare like sign-magnitude integers.
90 if (U & TopBit) // Negative float.
91 return 0 - U; // Map onto the low half of integers, order reversed.
92 return U + TopBit; // Positive floats map onto the high half of integers.
93}
94
95std::string sortText(float Score, llvm::StringRef Name) {
96 // We convert -Score to an integer, and hex-encode for readability.
97 // Example: [0.5, "foo"] -> "41000000foo"
98 std::string S;
99 llvm::raw_string_ostream OS(S);
100 write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower,
101 /*Width=*/2 * sizeof(Score));
102 OS << Name;
103 OS.flush();
104 return S;
105}
106
107} // namespace clangd
108} // namespace clang