blob: 13ba8995f564da1c6b5019d449b50933f6ccd023 [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"
Sam McCall3f0243f2018-07-03 08:09:29 +000010#include "FileDistance.h"
Eric Liu09c3c372018-06-15 08:58:12 +000011#include "URI.h"
Sam McCallc5707b62018-05-15 17:43:27 +000012#include "index/Index.h"
Ilya Biryukovf0296462018-06-04 14:50:59 +000013#include "clang/AST/ASTContext.h"
Eric Liu8944f0e2018-07-05 08:14:04 +000014#include "clang/AST/DeclCXX.h"
Sam McCall4a3c69b2018-06-06 08:53:36 +000015#include "clang/AST/DeclVisitor.h"
Sam McCall3f0243f2018-07-03 08:09:29 +000016#include "clang/Basic/CharInfo.h"
Ilya Biryukovf0296462018-06-04 14:50:59 +000017#include "clang/Basic/SourceManager.h"
Sam McCallc5707b62018-05-15 17:43:27 +000018#include "clang/Sema/CodeCompleteConsumer.h"
Eric Liu8944f0e2018-07-05 08:14:04 +000019#include "llvm/Support/Casting.h"
Sam McCallc5707b62018-05-15 17:43:27 +000020#include "llvm/Support/FormatVariadic.h"
21#include "llvm/Support/MathExtras.h"
22#include "llvm/Support/raw_ostream.h"
Sam McCall3f0243f2018-07-03 08:09:29 +000023#include <cmath>
Sam McCallc5707b62018-05-15 17:43:27 +000024
25namespace clang {
26namespace clangd {
27using namespace llvm;
Sam McCalle018b362018-06-08 09:36:34 +000028static bool IsReserved(StringRef Name) {
29 // FIXME: Should we exclude _Bool and others recognized by the standard?
30 return Name.size() >= 2 && Name[0] == '_' &&
31 (isUppercase(Name[1]) || Name[1] == '_');
32}
Sam McCallc5707b62018-05-15 17:43:27 +000033
Ilya Biryukovf0296462018-06-04 14:50:59 +000034static bool hasDeclInMainFile(const Decl &D) {
35 auto &SourceMgr = D.getASTContext().getSourceManager();
36 for (auto *Redecl : D.redecls()) {
37 auto Loc = SourceMgr.getSpellingLoc(Redecl->getLocation());
38 if (SourceMgr.isWrittenInMainFile(Loc))
39 return true;
40 }
41 return false;
42}
43
Sam McCall4a3c69b2018-06-06 08:53:36 +000044static SymbolQualitySignals::SymbolCategory categorize(const NamedDecl &ND) {
45 class Switch
46 : public ConstDeclVisitor<Switch, SymbolQualitySignals::SymbolCategory> {
47 public:
48#define MAP(DeclType, Category) \
49 SymbolQualitySignals::SymbolCategory Visit##DeclType(const DeclType *) { \
50 return SymbolQualitySignals::Category; \
51 }
52 MAP(NamespaceDecl, Namespace);
53 MAP(NamespaceAliasDecl, Namespace);
54 MAP(TypeDecl, Type);
55 MAP(TypeAliasTemplateDecl, Type);
56 MAP(ClassTemplateDecl, Type);
57 MAP(ValueDecl, Variable);
58 MAP(VarTemplateDecl, Variable);
59 MAP(FunctionDecl, Function);
60 MAP(FunctionTemplateDecl, Function);
61 MAP(Decl, Unknown);
62#undef MAP
63 };
64 return Switch().Visit(&ND);
65}
66
Kirill Bobyrev7cf29bc2018-07-05 09:37:26 +000067static SymbolQualitySignals::SymbolCategory
68categorize(const CodeCompletionResult &R) {
Sam McCallc3b5bad2018-06-14 13:42:21 +000069 if (R.Declaration)
70 return categorize(*R.Declaration);
71 if (R.Kind == CodeCompletionResult::RK_Macro)
72 return SymbolQualitySignals::Macro;
73 // Everything else is a keyword or a pattern. Patterns are mostly keywords
74 // too, except a few which we recognize by cursor kind.
75 switch (R.CursorKind) {
Kirill Bobyrev7cf29bc2018-07-05 09:37:26 +000076 case CXCursor_CXXMethod:
77 return SymbolQualitySignals::Function;
78 case CXCursor_ModuleImportDecl:
79 return SymbolQualitySignals::Namespace;
80 case CXCursor_MacroDefinition:
81 return SymbolQualitySignals::Macro;
82 case CXCursor_TypeRef:
83 return SymbolQualitySignals::Type;
84 case CXCursor_MemberRef:
85 return SymbolQualitySignals::Variable;
86 default:
87 return SymbolQualitySignals::Keyword;
Sam McCallc3b5bad2018-06-14 13:42:21 +000088 }
89}
90
Sam McCall4a3c69b2018-06-06 08:53:36 +000091static SymbolQualitySignals::SymbolCategory
92categorize(const index::SymbolInfo &D) {
93 switch (D.Kind) {
Kirill Bobyrev7cf29bc2018-07-05 09:37:26 +000094 case index::SymbolKind::Namespace:
95 case index::SymbolKind::NamespaceAlias:
96 return SymbolQualitySignals::Namespace;
97 case index::SymbolKind::Macro:
98 return SymbolQualitySignals::Macro;
99 case index::SymbolKind::Enum:
100 case index::SymbolKind::Struct:
101 case index::SymbolKind::Class:
102 case index::SymbolKind::Protocol:
103 case index::SymbolKind::Extension:
104 case index::SymbolKind::Union:
105 case index::SymbolKind::TypeAlias:
106 return SymbolQualitySignals::Type;
107 case index::SymbolKind::Function:
108 case index::SymbolKind::ClassMethod:
109 case index::SymbolKind::InstanceMethod:
110 case index::SymbolKind::StaticMethod:
111 case index::SymbolKind::InstanceProperty:
112 case index::SymbolKind::ClassProperty:
113 case index::SymbolKind::StaticProperty:
114 case index::SymbolKind::Constructor:
115 case index::SymbolKind::Destructor:
116 case index::SymbolKind::ConversionFunction:
117 return SymbolQualitySignals::Function;
118 case index::SymbolKind::Variable:
119 case index::SymbolKind::Field:
120 case index::SymbolKind::EnumConstant:
121 case index::SymbolKind::Parameter:
122 return SymbolQualitySignals::Variable;
123 case index::SymbolKind::Using:
124 case index::SymbolKind::Module:
125 case index::SymbolKind::Unknown:
126 return SymbolQualitySignals::Unknown;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000127 }
Tim Northover0698e962018-06-06 13:28:49 +0000128 llvm_unreachable("Unknown index::SymbolKind");
Sam McCall4a3c69b2018-06-06 08:53:36 +0000129}
130
Sam McCallc5707b62018-05-15 17:43:27 +0000131void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) {
Sam McCallc5707b62018-05-15 17:43:27 +0000132 if (SemaCCResult.Availability == CXAvailability_Deprecated)
133 Deprecated = true;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000134
Sam McCallc3b5bad2018-06-14 13:42:21 +0000135 Category = categorize(SemaCCResult);
Sam McCalle018b362018-06-08 09:36:34 +0000136
137 if (SemaCCResult.Declaration) {
138 if (auto *ID = SemaCCResult.Declaration->getIdentifier())
139 ReservedName = ReservedName || IsReserved(ID->getName());
140 } else if (SemaCCResult.Kind == CodeCompletionResult::RK_Macro)
141 ReservedName = ReservedName || IsReserved(SemaCCResult.Macro->getName());
Sam McCallc5707b62018-05-15 17:43:27 +0000142}
143
144void SymbolQualitySignals::merge(const Symbol &IndexResult) {
145 References = std::max(IndexResult.References, References);
Sam McCall4a3c69b2018-06-06 08:53:36 +0000146 Category = categorize(IndexResult.SymInfo);
Sam McCalle018b362018-06-08 09:36:34 +0000147 ReservedName = ReservedName || IsReserved(IndexResult.Name);
Sam McCallc5707b62018-05-15 17:43:27 +0000148}
149
150float SymbolQualitySignals::evaluate() const {
151 float Score = 1;
152
153 // This avoids a sharp gradient for tail symbols, and also neatly avoids the
154 // question of whether 0 references means a bad symbol or missing data.
Eric Liucdc5f6a2018-06-28 16:51:12 +0000155 if (References >= 10)
156 Score *= std::log10(References);
Sam McCallc5707b62018-05-15 17:43:27 +0000157
Sam McCallc5707b62018-05-15 17:43:27 +0000158 if (Deprecated)
Aaron Ballman215e4712018-05-18 13:18:41 +0000159 Score *= 0.1f;
Sam McCalle018b362018-06-08 09:36:34 +0000160 if (ReservedName)
161 Score *= 0.1f;
Sam McCallc5707b62018-05-15 17:43:27 +0000162
Sam McCall4a3c69b2018-06-06 08:53:36 +0000163 switch (Category) {
Kirill Bobyrev7cf29bc2018-07-05 09:37:26 +0000164 case Keyword: // Often relevant, but misses most signals.
165 Score *= 4; // FIXME: important keywords should have specific boosts.
166 break;
167 case Type:
168 case Function:
169 case Variable:
170 Score *= 1.1f;
171 break;
172 case Namespace:
173 Score *= 0.8f;
174 break;
175 case Macro:
176 Score *= 0.2f;
177 break;
178 case Unknown:
179 break;
Sam McCall4a3c69b2018-06-06 08:53:36 +0000180 }
181
Sam McCallc5707b62018-05-15 17:43:27 +0000182 return Score;
183}
184
185raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) {
186 OS << formatv("=== Symbol quality: {0}\n", S.evaluate());
Sam McCallc5707b62018-05-15 17:43:27 +0000187 OS << formatv("\tReferences: {0}\n", S.References);
188 OS << formatv("\tDeprecated: {0}\n", S.Deprecated);
Sam McCalle018b362018-06-08 09:36:34 +0000189 OS << formatv("\tReserved name: {0}\n", S.ReservedName);
Sam McCall4a3c69b2018-06-06 08:53:36 +0000190 OS << formatv("\tCategory: {0}\n", static_cast<int>(S.Category));
Sam McCallc5707b62018-05-15 17:43:27 +0000191 return OS;
192}
193
Sam McCalld9b54f02018-06-05 16:30:25 +0000194static SymbolRelevanceSignals::AccessibleScope
Sam McCallabe37372018-06-27 11:43:54 +0000195ComputeScope(const NamedDecl *D) {
196 // Injected "Foo" within the class "Foo" has file scope, not class scope.
197 const DeclContext *DC = D->getDeclContext();
198 if (auto *R = dyn_cast_or_null<RecordDecl>(D))
199 if (R->isInjectedClassName())
200 DC = DC->getParent();
Eric Liu8944f0e2018-07-05 08:14:04 +0000201 // Class constructor should have the same scope as the class.
Simon Pilgrim4a032012018-07-05 09:35:12 +0000202 if (isa<CXXConstructorDecl>(D))
Eric Liu8944f0e2018-07-05 08:14:04 +0000203 DC = DC->getParent();
Sam McCall89f52932018-06-05 18:00:48 +0000204 bool InClass = false;
Sam McCallabe37372018-06-27 11:43:54 +0000205 for (; !DC->isFileContext(); DC = DC->getParent()) {
Sam McCalld9b54f02018-06-05 16:30:25 +0000206 if (DC->isFunctionOrMethod())
207 return SymbolRelevanceSignals::FunctionScope;
208 InClass = InClass || DC->isRecord();
209 }
210 if (InClass)
211 return SymbolRelevanceSignals::ClassScope;
212 // This threshold could be tweaked, e.g. to treat module-visible as global.
Sam McCallabe37372018-06-27 11:43:54 +0000213 if (D->getLinkageInternal() < ExternalLinkage)
Sam McCalld9b54f02018-06-05 16:30:25 +0000214 return SymbolRelevanceSignals::FileScope;
215 return SymbolRelevanceSignals::GlobalScope;
216}
217
218void SymbolRelevanceSignals::merge(const Symbol &IndexResult) {
219 // FIXME: Index results always assumed to be at global scope. If Scope becomes
220 // relevant to non-completion requests, we should recognize class members etc.
Eric Liu09c3c372018-06-15 08:58:12 +0000221
222 SymbolURI = IndexResult.CanonicalDeclaration.FileURI;
Sam McCalld9b54f02018-06-05 16:30:25 +0000223}
224
Sam McCallc5707b62018-05-15 17:43:27 +0000225void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
226 if (SemaCCResult.Availability == CXAvailability_NotAvailable ||
227 SemaCCResult.Availability == CXAvailability_NotAccessible)
228 Forbidden = true;
Ilya Biryukovf0296462018-06-04 14:50:59 +0000229
230 if (SemaCCResult.Declaration) {
Eric Liu09c3c372018-06-15 08:58:12 +0000231 // We boost things that have decls in the main file. We give a fixed score
232 // for all other declarations in sema as they are already included in the
233 // translation unit.
Ilya Biryukovf0296462018-06-04 14:50:59 +0000234 float DeclProximity =
Eric Liu09c3c372018-06-15 08:58:12 +0000235 hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.6;
236 SemaProximityScore = std::max(DeclProximity, SemaProximityScore);
Ilya Biryukovf0296462018-06-04 14:50:59 +0000237 }
Sam McCalld9b54f02018-06-05 16:30:25 +0000238
239 // Declarations are scoped, others (like macros) are assumed global.
Sam McCall661d89c2018-06-05 17:58:12 +0000240 if (SemaCCResult.Declaration)
Sam McCallabe37372018-06-27 11:43:54 +0000241 Scope = std::min(Scope, ComputeScope(SemaCCResult.Declaration));
Sam McCallc5707b62018-05-15 17:43:27 +0000242}
243
Sam McCall3f0243f2018-07-03 08:09:29 +0000244static std::pair<float, unsigned> proximityScore(llvm::StringRef SymbolURI,
245 URIDistance *D) {
246 if (!D || SymbolURI.empty())
247 return {0.f, 0u};
248 unsigned Distance = D->distance(SymbolURI);
249 // Assume approximately default options are used for sensible scoring.
250 return {std::exp(Distance * -0.4f / FileDistanceOptions().UpCost), Distance};
251}
252
Sam McCallc5707b62018-05-15 17:43:27 +0000253float SymbolRelevanceSignals::evaluate() const {
Sam McCalld9b54f02018-06-05 16:30:25 +0000254 float Score = 1;
255
Sam McCallc5707b62018-05-15 17:43:27 +0000256 if (Forbidden)
257 return 0;
Ilya Biryukovf0296462018-06-04 14:50:59 +0000258
Sam McCalld9b54f02018-06-05 16:30:25 +0000259 Score *= NameMatch;
260
Ilya Biryukovf0296462018-06-04 14:50:59 +0000261 // Proximity scores are [0,1] and we translate them into a multiplier in the
Sam McCall3f0243f2018-07-03 08:09:29 +0000262 // range from 1 to 3.
263 Score *= 1 + 2 * std::max(proximityScore(SymbolURI, FileProximityMatch).first,
264 SemaProximityScore);
Sam McCalld9b54f02018-06-05 16:30:25 +0000265
266 // Symbols like local variables may only be referenced within their scope.
267 // Conversely if we're in that scope, it's likely we'll reference them.
268 if (Query == CodeComplete) {
269 // The narrower the scope where a symbol is visible, the more likely it is
270 // to be relevant when it is available.
271 switch (Scope) {
272 case GlobalScope:
273 break;
274 case FileScope:
275 Score *= 1.5;
Sam McCallc22c9aa2018-06-07 08:16:36 +0000276 break;
Sam McCalld9b54f02018-06-05 16:30:25 +0000277 case ClassScope:
278 Score *= 2;
Sam McCallc22c9aa2018-06-07 08:16:36 +0000279 break;
Sam McCalld9b54f02018-06-05 16:30:25 +0000280 case FunctionScope:
281 Score *= 4;
Sam McCallc22c9aa2018-06-07 08:16:36 +0000282 break;
Sam McCalld9b54f02018-06-05 16:30:25 +0000283 }
284 }
285
Ilya Biryukovf0296462018-06-04 14:50:59 +0000286 return Score;
Sam McCallc5707b62018-05-15 17:43:27 +0000287}
Eric Liu09c3c372018-06-15 08:58:12 +0000288
Sam McCallc5707b62018-05-15 17:43:27 +0000289raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) {
290 OS << formatv("=== Symbol relevance: {0}\n", S.evaluate());
291 OS << formatv("\tName match: {0}\n", S.NameMatch);
292 OS << formatv("\tForbidden: {0}\n", S.Forbidden);
Eric Liu09c3c372018-06-15 08:58:12 +0000293 OS << formatv("\tSymbol URI: {0}\n", S.SymbolURI);
294 if (S.FileProximityMatch) {
Sam McCall3f0243f2018-07-03 08:09:29 +0000295 auto Score = proximityScore(S.SymbolURI, S.FileProximityMatch);
296 OS << formatv("\tIndex proximity: {0} (distance={1})\n", Score.first,
297 Score.second);
Eric Liu09c3c372018-06-15 08:58:12 +0000298 }
299 OS << formatv("\tSema proximity: {0}\n", S.SemaProximityScore);
Sam McCall661d89c2018-06-05 17:58:12 +0000300 OS << formatv("\tQuery type: {0}\n", static_cast<int>(S.Query));
301 OS << formatv("\tScope: {0}\n", static_cast<int>(S.Scope));
Sam McCallc5707b62018-05-15 17:43:27 +0000302 return OS;
303}
304
305float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) {
306 return SymbolQuality * SymbolRelevance;
307}
308
309// Produces an integer that sorts in the same order as F.
310// That is: a < b <==> encodeFloat(a) < encodeFloat(b).
311static uint32_t encodeFloat(float F) {
312 static_assert(std::numeric_limits<float>::is_iec559, "");
313 constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1);
314
315 // Get the bits of the float. Endianness is the same as for integers.
316 uint32_t U = FloatToBits(F);
317 // IEEE 754 floats compare like sign-magnitude integers.
318 if (U & TopBit) // Negative float.
319 return 0 - U; // Map onto the low half of integers, order reversed.
320 return U + TopBit; // Positive floats map onto the high half of integers.
321}
322
323std::string sortText(float Score, llvm::StringRef Name) {
324 // We convert -Score to an integer, and hex-encode for readability.
325 // Example: [0.5, "foo"] -> "41000000foo"
326 std::string S;
327 llvm::raw_string_ostream OS(S);
328 write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower,
329 /*Width=*/2 * sizeof(Score));
330 OS << Name;
331 OS.flush();
332 return S;
333}
334
335} // namespace clangd
336} // namespace clang