blob: 831bb984eed3cdb3304bf567692b4ea351f37a36 [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"
Ilya Biryukovf0296462018-06-04 14:50:59 +000011#include "clang/AST/ASTContext.h"
12#include "clang/Basic/SourceManager.h"
Sam McCallc5707b62018-05-15 17:43:27 +000013#include "clang/Sema/CodeCompleteConsumer.h"
14#include "llvm/Support/FormatVariadic.h"
15#include "llvm/Support/MathExtras.h"
16#include "llvm/Support/raw_ostream.h"
17
18namespace clang {
19namespace clangd {
20using namespace llvm;
21
Ilya Biryukovf0296462018-06-04 14:50:59 +000022static bool hasDeclInMainFile(const Decl &D) {
23 auto &SourceMgr = D.getASTContext().getSourceManager();
24 for (auto *Redecl : D.redecls()) {
25 auto Loc = SourceMgr.getSpellingLoc(Redecl->getLocation());
26 if (SourceMgr.isWrittenInMainFile(Loc))
27 return true;
28 }
29 return false;
30}
31
Sam McCallc5707b62018-05-15 17:43:27 +000032void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) {
33 SemaCCPriority = SemaCCResult.Priority;
Sam McCallc5707b62018-05-15 17:43:27 +000034 if (SemaCCResult.Availability == CXAvailability_Deprecated)
35 Deprecated = true;
36}
37
38void SymbolQualitySignals::merge(const Symbol &IndexResult) {
39 References = std::max(IndexResult.References, References);
40}
41
42float SymbolQualitySignals::evaluate() const {
43 float Score = 1;
44
45 // This avoids a sharp gradient for tail symbols, and also neatly avoids the
46 // question of whether 0 references means a bad symbol or missing data.
47 if (References >= 3)
48 Score *= std::log(References);
49
50 if (SemaCCPriority)
51 // Map onto a 0-2 interval, so we don't reward/penalize non-Sema results.
52 // Priority 80 is a really bad score.
53 Score *= 2 - std::min<float>(80, SemaCCPriority) / 40;
54
55 if (Deprecated)
Aaron Ballman215e4712018-05-18 13:18:41 +000056 Score *= 0.1f;
Sam McCallc5707b62018-05-15 17:43:27 +000057
58 return Score;
59}
60
61raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) {
62 OS << formatv("=== Symbol quality: {0}\n", S.evaluate());
63 if (S.SemaCCPriority)
64 OS << formatv("\tSemaCCPriority: {0}\n", S.SemaCCPriority);
65 OS << formatv("\tReferences: {0}\n", S.References);
66 OS << formatv("\tDeprecated: {0}\n", S.Deprecated);
67 return OS;
68}
69
70void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
71 if (SemaCCResult.Availability == CXAvailability_NotAvailable ||
72 SemaCCResult.Availability == CXAvailability_NotAccessible)
73 Forbidden = true;
Ilya Biryukovf0296462018-06-04 14:50:59 +000074
75 if (SemaCCResult.Declaration) {
76 // We boost things that have decls in the main file.
77 // The real proximity scores would be more general when we have them.
78 float DeclProximity =
79 hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.0;
80 ProximityScore = std::max(DeclProximity, ProximityScore);
81 }
Sam McCallc5707b62018-05-15 17:43:27 +000082}
83
84float SymbolRelevanceSignals::evaluate() const {
85 if (Forbidden)
86 return 0;
Ilya Biryukovf0296462018-06-04 14:50:59 +000087
88 float Score = NameMatch;
89 // Proximity scores are [0,1] and we translate them into a multiplier in the
90 // range from 1 to 2.
91 Score *= 1 + ProximityScore;
92 return Score;
Sam McCallc5707b62018-05-15 17:43:27 +000093}
94raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) {
95 OS << formatv("=== Symbol relevance: {0}\n", S.evaluate());
96 OS << formatv("\tName match: {0}\n", S.NameMatch);
97 OS << formatv("\tForbidden: {0}\n", S.Forbidden);
98 return OS;
99}
100
101float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) {
102 return SymbolQuality * SymbolRelevance;
103}
104
105// Produces an integer that sorts in the same order as F.
106// That is: a < b <==> encodeFloat(a) < encodeFloat(b).
107static uint32_t encodeFloat(float F) {
108 static_assert(std::numeric_limits<float>::is_iec559, "");
109 constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1);
110
111 // Get the bits of the float. Endianness is the same as for integers.
112 uint32_t U = FloatToBits(F);
113 // IEEE 754 floats compare like sign-magnitude integers.
114 if (U & TopBit) // Negative float.
115 return 0 - U; // Map onto the low half of integers, order reversed.
116 return U + TopBit; // Positive floats map onto the high half of integers.
117}
118
119std::string sortText(float Score, llvm::StringRef Name) {
120 // We convert -Score to an integer, and hex-encode for readability.
121 // Example: [0.5, "foo"] -> "41000000foo"
122 std::string S;
123 llvm::raw_string_ostream OS(S);
124 write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower,
125 /*Width=*/2 * sizeof(Score));
126 OS << Name;
127 OS.flush();
128 return S;
129}
130
131} // namespace clangd
132} // namespace clang