blob: 9ac9955e723af5fcd0443f59bfc3ef855c3a2608 [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
Sam McCalld9b54f02018-06-05 16:30:25 +000070static SymbolRelevanceSignals::AccessibleScope
71ComputeScope(const NamedDecl &D) {
72 bool InClass;
73 for (const DeclContext *DC = D.getDeclContext(); !DC->isFileContext();
74 DC = DC->getParent()) {
75 if (DC->isFunctionOrMethod())
76 return SymbolRelevanceSignals::FunctionScope;
77 InClass = InClass || DC->isRecord();
78 }
79 if (InClass)
80 return SymbolRelevanceSignals::ClassScope;
81 // This threshold could be tweaked, e.g. to treat module-visible as global.
82 if (D.getLinkageInternal() < ExternalLinkage)
83 return SymbolRelevanceSignals::FileScope;
84 return SymbolRelevanceSignals::GlobalScope;
85}
86
87void SymbolRelevanceSignals::merge(const Symbol &IndexResult) {
88 // FIXME: Index results always assumed to be at global scope. If Scope becomes
89 // relevant to non-completion requests, we should recognize class members etc.
90}
91
Sam McCallc5707b62018-05-15 17:43:27 +000092void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
93 if (SemaCCResult.Availability == CXAvailability_NotAvailable ||
94 SemaCCResult.Availability == CXAvailability_NotAccessible)
95 Forbidden = true;
Ilya Biryukovf0296462018-06-04 14:50:59 +000096
97 if (SemaCCResult.Declaration) {
98 // We boost things that have decls in the main file.
99 // The real proximity scores would be more general when we have them.
100 float DeclProximity =
101 hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.0;
102 ProximityScore = std::max(DeclProximity, ProximityScore);
103 }
Sam McCalld9b54f02018-06-05 16:30:25 +0000104
105 // Declarations are scoped, others (like macros) are assumed global.
106 if (SemaCCResult.Kind == CodeCompletionResult::RK_Declaration)
107 Scope = std::min(Scope, ComputeScope(*SemaCCResult.Declaration));
Sam McCallc5707b62018-05-15 17:43:27 +0000108}
109
110float SymbolRelevanceSignals::evaluate() const {
Sam McCalld9b54f02018-06-05 16:30:25 +0000111 float Score = 1;
112
Sam McCallc5707b62018-05-15 17:43:27 +0000113 if (Forbidden)
114 return 0;
Ilya Biryukovf0296462018-06-04 14:50:59 +0000115
Sam McCalld9b54f02018-06-05 16:30:25 +0000116 Score *= NameMatch;
117
Ilya Biryukovf0296462018-06-04 14:50:59 +0000118 // Proximity scores are [0,1] and we translate them into a multiplier in the
119 // range from 1 to 2.
120 Score *= 1 + ProximityScore;
Sam McCalld9b54f02018-06-05 16:30:25 +0000121
122 // Symbols like local variables may only be referenced within their scope.
123 // Conversely if we're in that scope, it's likely we'll reference them.
124 if (Query == CodeComplete) {
125 // The narrower the scope where a symbol is visible, the more likely it is
126 // to be relevant when it is available.
127 switch (Scope) {
128 case GlobalScope:
129 break;
130 case FileScope:
131 Score *= 1.5;
132 case ClassScope:
133 Score *= 2;
134 case FunctionScope:
135 Score *= 4;
136 }
137 }
138
Ilya Biryukovf0296462018-06-04 14:50:59 +0000139 return Score;
Sam McCallc5707b62018-05-15 17:43:27 +0000140}
141raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) {
142 OS << formatv("=== Symbol relevance: {0}\n", S.evaluate());
143 OS << formatv("\tName match: {0}\n", S.NameMatch);
144 OS << formatv("\tForbidden: {0}\n", S.Forbidden);
145 return OS;
146}
147
148float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) {
149 return SymbolQuality * SymbolRelevance;
150}
151
152// Produces an integer that sorts in the same order as F.
153// That is: a < b <==> encodeFloat(a) < encodeFloat(b).
154static uint32_t encodeFloat(float F) {
155 static_assert(std::numeric_limits<float>::is_iec559, "");
156 constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1);
157
158 // Get the bits of the float. Endianness is the same as for integers.
159 uint32_t U = FloatToBits(F);
160 // IEEE 754 floats compare like sign-magnitude integers.
161 if (U & TopBit) // Negative float.
162 return 0 - U; // Map onto the low half of integers, order reversed.
163 return U + TopBit; // Positive floats map onto the high half of integers.
164}
165
166std::string sortText(float Score, llvm::StringRef Name) {
167 // We convert -Score to an integer, and hex-encode for readability.
168 // Example: [0.5, "foo"] -> "41000000foo"
169 std::string S;
170 llvm::raw_string_ostream OS(S);
171 write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower,
172 /*Width=*/2 * sizeof(Score));
173 OS << Name;
174 OS.flush();
175 return S;
176}
177
178} // namespace clangd
179} // namespace clang