blob: dd8256a32a1e3e234b4ebe264b4b7769bbb229e8 [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"
Eric Liu09c3c372018-06-15 08:58:12 +000010#include "URI.h"
Sam McCallc5707b62018-05-15 17:43:27 +000011#include "index/Index.h"
Ilya Biryukovf0296462018-06-04 14:50:59 +000012#include "clang/AST/ASTContext.h"
Sam McCalle018b362018-06-08 09:36:34 +000013#include "clang/Basic/CharInfo.h"
Sam McCall4a3c69b2018-06-06 08:53:36 +000014#include "clang/AST/DeclVisitor.h"
Ilya Biryukovf0296462018-06-04 14:50:59 +000015#include "clang/Basic/SourceManager.h"
Sam McCallc5707b62018-05-15 17:43:27 +000016#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
21namespace clang {
22namespace clangd {
23using namespace llvm;
Sam McCalle018b362018-06-08 09:36:34 +000024static 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 McCallc5707b62018-05-15 17:43:27 +000029
Ilya Biryukovf0296462018-06-04 14:50:59 +000030static 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 McCall4a3c69b2018-06-06 08:53:36 +000040static 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 McCallc3b5bad2018-06-14 13:42:21 +000063static 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 McCall4a3c69b2018-06-06 08:53:36 +000086static SymbolQualitySignals::SymbolCategory
87categorize(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 Northover0698e962018-06-06 13:28:49 +0000123 llvm_unreachable("Unknown index::SymbolKind");
Sam McCall4a3c69b2018-06-06 08:53:36 +0000124}
125
Sam McCallc5707b62018-05-15 17:43:27 +0000126void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) {
Sam McCallc5707b62018-05-15 17:43:27 +0000127 if (SemaCCResult.Availability == CXAvailability_Deprecated)
128 Deprecated = true;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000129
Sam McCallc3b5bad2018-06-14 13:42:21 +0000130 Category = categorize(SemaCCResult);
Sam McCalle018b362018-06-08 09:36:34 +0000131
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 McCallc5707b62018-05-15 17:43:27 +0000137}
138
139void SymbolQualitySignals::merge(const Symbol &IndexResult) {
140 References = std::max(IndexResult.References, References);
Sam McCall4a3c69b2018-06-06 08:53:36 +0000141 Category = categorize(IndexResult.SymInfo);
Sam McCalle018b362018-06-08 09:36:34 +0000142 ReservedName = ReservedName || IsReserved(IndexResult.Name);
Sam McCallc5707b62018-05-15 17:43:27 +0000143}
144
145float 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 McCallc5707b62018-05-15 17:43:27 +0000153 if (Deprecated)
Aaron Ballman215e4712018-05-18 13:18:41 +0000154 Score *= 0.1f;
Sam McCalle018b362018-06-08 09:36:34 +0000155 if (ReservedName)
156 Score *= 0.1f;
Sam McCallc5707b62018-05-15 17:43:27 +0000157
Sam McCall4a3c69b2018-06-06 08:53:36 +0000158 switch (Category) {
Sam McCallc3b5bad2018-06-14 13:42:21 +0000159 case Keyword: // Usually relevant, but misses most signals.
160 Score *= 10;
161 break;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000162 case Type:
163 case Function:
164 case Variable:
Simon Pilgrim0c9e1c82018-06-06 12:48:27 +0000165 Score *= 1.1f;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000166 break;
167 case Namespace:
Simon Pilgrim0c9e1c82018-06-06 12:48:27 +0000168 Score *= 0.8f;
Sam McCallbc7cbb72018-06-06 12:38:37 +0000169 break;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000170 case Macro:
Simon Pilgrim0c9e1c82018-06-06 12:48:27 +0000171 Score *= 0.2f;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000172 break;
173 case Unknown:
174 break;
175 }
176
Sam McCallc5707b62018-05-15 17:43:27 +0000177 return Score;
178}
179
180raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) {
181 OS << formatv("=== Symbol quality: {0}\n", S.evaluate());
Sam McCallc5707b62018-05-15 17:43:27 +0000182 OS << formatv("\tReferences: {0}\n", S.References);
183 OS << formatv("\tDeprecated: {0}\n", S.Deprecated);
Sam McCalle018b362018-06-08 09:36:34 +0000184 OS << formatv("\tReserved name: {0}\n", S.ReservedName);
Sam McCall4a3c69b2018-06-06 08:53:36 +0000185 OS << formatv("\tCategory: {0}\n", static_cast<int>(S.Category));
Sam McCallc5707b62018-05-15 17:43:27 +0000186 return OS;
187}
188
Eric Liu09c3c372018-06-15 08:58:12 +0000189/// 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
193static 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
216FileProximityMatcher::FileProximityMatcher(ArrayRef<StringRef> ProximityPaths)
217 : ProximityPaths(ProximityPaths.begin(), ProximityPaths.end()) {}
218
219float 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
235llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
236 const FileProximityMatcher &M) {
237 OS << formatv("File proximity matcher: ");
Eric Liuffaaf7d2018-06-21 09:51:28 +0000238 OS << formatv("ProximityPaths[{0}]", llvm::join(M.ProximityPaths.begin(),
Eric Liu09c3c372018-06-15 08:58:12 +0000239 M.ProximityPaths.end(), ","));
240 return OS;
241}
242
Sam McCalld9b54f02018-06-05 16:30:25 +0000243static SymbolRelevanceSignals::AccessibleScope
244ComputeScope(const NamedDecl &D) {
Sam McCall89f52932018-06-05 18:00:48 +0000245 bool InClass = false;
Sam McCalld9b54f02018-06-05 16:30:25 +0000246 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
260void 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 Liu09c3c372018-06-15 08:58:12 +0000263
264 SymbolURI = IndexResult.CanonicalDeclaration.FileURI;
Sam McCalld9b54f02018-06-05 16:30:25 +0000265}
266
Sam McCallc5707b62018-05-15 17:43:27 +0000267void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
268 if (SemaCCResult.Availability == CXAvailability_NotAvailable ||
269 SemaCCResult.Availability == CXAvailability_NotAccessible)
270 Forbidden = true;
Ilya Biryukovf0296462018-06-04 14:50:59 +0000271
272 if (SemaCCResult.Declaration) {
Eric Liu09c3c372018-06-15 08:58:12 +0000273 // 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 Biryukovf0296462018-06-04 14:50:59 +0000276 float DeclProximity =
Eric Liu09c3c372018-06-15 08:58:12 +0000277 hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.6;
278 SemaProximityScore = std::max(DeclProximity, SemaProximityScore);
Ilya Biryukovf0296462018-06-04 14:50:59 +0000279 }
Sam McCalld9b54f02018-06-05 16:30:25 +0000280
281 // Declarations are scoped, others (like macros) are assumed global.
Sam McCall661d89c2018-06-05 17:58:12 +0000282 if (SemaCCResult.Declaration)
Sam McCalld9b54f02018-06-05 16:30:25 +0000283 Scope = std::min(Scope, ComputeScope(*SemaCCResult.Declaration));
Sam McCallc5707b62018-05-15 17:43:27 +0000284}
285
286float SymbolRelevanceSignals::evaluate() const {
Sam McCalld9b54f02018-06-05 16:30:25 +0000287 float Score = 1;
288
Sam McCallc5707b62018-05-15 17:43:27 +0000289 if (Forbidden)
290 return 0;
Ilya Biryukovf0296462018-06-04 14:50:59 +0000291
Sam McCalld9b54f02018-06-05 16:30:25 +0000292 Score *= NameMatch;
293
Eric Liu09c3c372018-06-15 08:58:12 +0000294 float IndexProximityScore =
295 FileProximityMatch ? FileProximityMatch->uriProximity(SymbolURI) : 0;
Ilya Biryukovf0296462018-06-04 14:50:59 +0000296 // Proximity scores are [0,1] and we translate them into a multiplier in the
297 // range from 1 to 2.
Eric Liu09c3c372018-06-15 08:58:12 +0000298 Score *= 1 + std::max(IndexProximityScore, SemaProximityScore);
Sam McCalld9b54f02018-06-05 16:30:25 +0000299
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 McCallc22c9aa2018-06-07 08:16:36 +0000310 break;
Sam McCalld9b54f02018-06-05 16:30:25 +0000311 case ClassScope:
312 Score *= 2;
Sam McCallc22c9aa2018-06-07 08:16:36 +0000313 break;
Sam McCalld9b54f02018-06-05 16:30:25 +0000314 case FunctionScope:
315 Score *= 4;
Sam McCallc22c9aa2018-06-07 08:16:36 +0000316 break;
Sam McCalld9b54f02018-06-05 16:30:25 +0000317 }
318 }
319
Ilya Biryukovf0296462018-06-04 14:50:59 +0000320 return Score;
Sam McCallc5707b62018-05-15 17:43:27 +0000321}
Eric Liu09c3c372018-06-15 08:58:12 +0000322
Sam McCallc5707b62018-05-15 17:43:27 +0000323raw_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 Liu09c3c372018-06-15 08:58:12 +0000327 OS << formatv("\tSymbol URI: {0}\n", S.SymbolURI);
328 if (S.FileProximityMatch) {
Eric Liuffaaf7d2018-06-21 09:51:28 +0000329 OS << "\tIndex proximity: "
330 << S.FileProximityMatch->uriProximity(S.SymbolURI) << " ("
331 << *S.FileProximityMatch << ")\n";
Eric Liu09c3c372018-06-15 08:58:12 +0000332 }
333 OS << formatv("\tSema proximity: {0}\n", S.SemaProximityScore);
Sam McCall661d89c2018-06-05 17:58:12 +0000334 OS << formatv("\tQuery type: {0}\n", static_cast<int>(S.Query));
335 OS << formatv("\tScope: {0}\n", static_cast<int>(S.Scope));
Sam McCallc5707b62018-05-15 17:43:27 +0000336 return OS;
337}
338
339float 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).
345static 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
357std::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