| Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- Quality.cpp ---------------------------------------------*- C++-*-===// | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 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 | // | 
| Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 9 | #include "Quality.h" | 
| Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 10 | #include "AST.h" | 
| Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 11 | #include "FileDistance.h" | 
| Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 12 | #include "URI.h" | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 13 | #include "index/Index.h" | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" | 
| Eric Liu | 8944f0e | 2018-07-05 08:14:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" | 
| Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CodeCompleteConsumer.h" | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" | 
|  | 23 | #include "llvm/ADT/SmallString.h" | 
|  | 24 | #include "llvm/ADT/SmallVector.h" | 
|  | 25 | #include "llvm/ADT/StringExtras.h" | 
|  | 26 | #include "llvm/ADT/StringRef.h" | 
| Eric Liu | 8944f0e | 2018-07-05 08:14:04 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Casting.h" | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FormatVariadic.h" | 
|  | 29 | #include "llvm/Support/MathExtras.h" | 
|  | 30 | #include "llvm/Support/raw_ostream.h" | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 31 | #include <algorithm> | 
| Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 32 | #include <cmath> | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | namespace clang { | 
|  | 35 | namespace clangd { | 
|  | 36 | using namespace llvm; | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 37 | static bool isReserved(StringRef Name) { | 
| Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 38 | // FIXME: Should we exclude _Bool and others recognized by the standard? | 
|  | 39 | return Name.size() >= 2 && Name[0] == '_' && | 
|  | 40 | (isUppercase(Name[1]) || Name[1] == '_'); | 
|  | 41 | } | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 42 |  | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 43 | static bool hasDeclInMainFile(const Decl &D) { | 
|  | 44 | auto &SourceMgr = D.getASTContext().getSourceManager(); | 
|  | 45 | for (auto *Redecl : D.redecls()) { | 
|  | 46 | auto Loc = SourceMgr.getSpellingLoc(Redecl->getLocation()); | 
|  | 47 | if (SourceMgr.isWrittenInMainFile(Loc)) | 
|  | 48 | return true; | 
|  | 49 | } | 
|  | 50 | return false; | 
|  | 51 | } | 
|  | 52 |  | 
| Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 53 | static bool hasUsingDeclInMainFile(const CodeCompletionResult &R) { | 
|  | 54 | const auto &Context = R.Declaration->getASTContext(); | 
|  | 55 | const auto &SourceMgr = Context.getSourceManager(); | 
|  | 56 | if (R.ShadowDecl) { | 
|  | 57 | const auto Loc = SourceMgr.getExpansionLoc(R.ShadowDecl->getLocation()); | 
|  | 58 | if (SourceMgr.isWrittenInMainFile(Loc)) | 
|  | 59 | return true; | 
|  | 60 | } | 
|  | 61 | return false; | 
|  | 62 | } | 
|  | 63 |  | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 64 | static SymbolQualitySignals::SymbolCategory categorize(const NamedDecl &ND) { | 
|  | 65 | class Switch | 
|  | 66 | : public ConstDeclVisitor<Switch, SymbolQualitySignals::SymbolCategory> { | 
|  | 67 | public: | 
|  | 68 | #define MAP(DeclType, Category)                                                \ | 
|  | 69 | SymbolQualitySignals::SymbolCategory Visit##DeclType(const DeclType *) {     \ | 
|  | 70 | return SymbolQualitySignals::Category;                                     \ | 
|  | 71 | } | 
|  | 72 | MAP(NamespaceDecl, Namespace); | 
|  | 73 | MAP(NamespaceAliasDecl, Namespace); | 
|  | 74 | MAP(TypeDecl, Type); | 
|  | 75 | MAP(TypeAliasTemplateDecl, Type); | 
|  | 76 | MAP(ClassTemplateDecl, Type); | 
| Eric Liu | d7de811 | 2018-07-24 08:51:52 +0000 | [diff] [blame] | 77 | MAP(CXXConstructorDecl, Constructor); | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 78 | MAP(ValueDecl, Variable); | 
|  | 79 | MAP(VarTemplateDecl, Variable); | 
|  | 80 | MAP(FunctionDecl, Function); | 
|  | 81 | MAP(FunctionTemplateDecl, Function); | 
|  | 82 | MAP(Decl, Unknown); | 
|  | 83 | #undef MAP | 
|  | 84 | }; | 
|  | 85 | return Switch().Visit(&ND); | 
|  | 86 | } | 
|  | 87 |  | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 88 | static SymbolQualitySignals::SymbolCategory | 
|  | 89 | categorize(const CodeCompletionResult &R) { | 
| Sam McCall | c3b5bad | 2018-06-14 13:42:21 +0000 | [diff] [blame] | 90 | if (R.Declaration) | 
|  | 91 | return categorize(*R.Declaration); | 
|  | 92 | if (R.Kind == CodeCompletionResult::RK_Macro) | 
|  | 93 | return SymbolQualitySignals::Macro; | 
|  | 94 | // Everything else is a keyword or a pattern. Patterns are mostly keywords | 
|  | 95 | // too, except a few which we recognize by cursor kind. | 
|  | 96 | switch (R.CursorKind) { | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 97 | case CXCursor_CXXMethod: | 
|  | 98 | return SymbolQualitySignals::Function; | 
|  | 99 | case CXCursor_ModuleImportDecl: | 
|  | 100 | return SymbolQualitySignals::Namespace; | 
|  | 101 | case CXCursor_MacroDefinition: | 
|  | 102 | return SymbolQualitySignals::Macro; | 
|  | 103 | case CXCursor_TypeRef: | 
|  | 104 | return SymbolQualitySignals::Type; | 
|  | 105 | case CXCursor_MemberRef: | 
|  | 106 | return SymbolQualitySignals::Variable; | 
| Eric Liu | d7de811 | 2018-07-24 08:51:52 +0000 | [diff] [blame] | 107 | case CXCursor_Constructor: | 
|  | 108 | return SymbolQualitySignals::Constructor; | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 109 | default: | 
|  | 110 | return SymbolQualitySignals::Keyword; | 
| Sam McCall | c3b5bad | 2018-06-14 13:42:21 +0000 | [diff] [blame] | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 114 | static SymbolQualitySignals::SymbolCategory | 
|  | 115 | categorize(const index::SymbolInfo &D) { | 
|  | 116 | switch (D.Kind) { | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 117 | case index::SymbolKind::Namespace: | 
|  | 118 | case index::SymbolKind::NamespaceAlias: | 
|  | 119 | return SymbolQualitySignals::Namespace; | 
|  | 120 | case index::SymbolKind::Macro: | 
|  | 121 | return SymbolQualitySignals::Macro; | 
|  | 122 | case index::SymbolKind::Enum: | 
|  | 123 | case index::SymbolKind::Struct: | 
|  | 124 | case index::SymbolKind::Class: | 
|  | 125 | case index::SymbolKind::Protocol: | 
|  | 126 | case index::SymbolKind::Extension: | 
|  | 127 | case index::SymbolKind::Union: | 
|  | 128 | case index::SymbolKind::TypeAlias: | 
|  | 129 | return SymbolQualitySignals::Type; | 
|  | 130 | case index::SymbolKind::Function: | 
|  | 131 | case index::SymbolKind::ClassMethod: | 
|  | 132 | case index::SymbolKind::InstanceMethod: | 
|  | 133 | case index::SymbolKind::StaticMethod: | 
|  | 134 | case index::SymbolKind::InstanceProperty: | 
|  | 135 | case index::SymbolKind::ClassProperty: | 
|  | 136 | case index::SymbolKind::StaticProperty: | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 137 | case index::SymbolKind::Destructor: | 
|  | 138 | case index::SymbolKind::ConversionFunction: | 
|  | 139 | return SymbolQualitySignals::Function; | 
| Eric Liu | d7de811 | 2018-07-24 08:51:52 +0000 | [diff] [blame] | 140 | case index::SymbolKind::Constructor: | 
|  | 141 | return SymbolQualitySignals::Constructor; | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 142 | case index::SymbolKind::Variable: | 
|  | 143 | case index::SymbolKind::Field: | 
|  | 144 | case index::SymbolKind::EnumConstant: | 
|  | 145 | case index::SymbolKind::Parameter: | 
|  | 146 | return SymbolQualitySignals::Variable; | 
|  | 147 | case index::SymbolKind::Using: | 
|  | 148 | case index::SymbolKind::Module: | 
|  | 149 | case index::SymbolKind::Unknown: | 
|  | 150 | return SymbolQualitySignals::Unknown; | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 151 | } | 
| Tim Northover | 0698e96 | 2018-06-06 13:28:49 +0000 | [diff] [blame] | 152 | llvm_unreachable("Unknown index::SymbolKind"); | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 155 | static bool isInstanceMember(const NamedDecl *ND) { | 
|  | 156 | if (!ND) | 
|  | 157 | return false; | 
|  | 158 | if (const auto *TP = dyn_cast<FunctionTemplateDecl>(ND)) | 
|  | 159 | ND = TP->TemplateDecl::getTemplatedDecl(); | 
|  | 160 | if (const auto *CM = dyn_cast<CXXMethodDecl>(ND)) | 
|  | 161 | return !CM->isStatic(); | 
|  | 162 | return isa<FieldDecl>(ND); // Note that static fields are VarDecl. | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | static bool isInstanceMember(const index::SymbolInfo &D) { | 
|  | 166 | switch (D.Kind) { | 
|  | 167 | case index::SymbolKind::InstanceMethod: | 
|  | 168 | case index::SymbolKind::InstanceProperty: | 
|  | 169 | case index::SymbolKind::Field: | 
|  | 170 | return true; | 
|  | 171 | default: | 
|  | 172 | return false; | 
|  | 173 | } | 
|  | 174 | } | 
|  | 175 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 176 | void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) { | 
| Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 177 | Deprecated |= (SemaCCResult.Availability == CXAvailability_Deprecated); | 
| Sam McCall | c3b5bad | 2018-06-14 13:42:21 +0000 | [diff] [blame] | 178 | Category = categorize(SemaCCResult); | 
| Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 179 |  | 
|  | 180 | if (SemaCCResult.Declaration) { | 
| Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 181 | ImplementationDetail |= isImplementationDetail(SemaCCResult.Declaration); | 
| Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 182 | if (auto *ID = SemaCCResult.Declaration->getIdentifier()) | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 183 | ReservedName = ReservedName || isReserved(ID->getName()); | 
| Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 184 | } else if (SemaCCResult.Kind == CodeCompletionResult::RK_Macro) | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 185 | ReservedName = ReservedName || isReserved(SemaCCResult.Macro->getName()); | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
|  | 188 | void SymbolQualitySignals::merge(const Symbol &IndexResult) { | 
| Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 189 | Deprecated |= (IndexResult.Flags & Symbol::Deprecated); | 
| Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 190 | ImplementationDetail |= (IndexResult.Flags & Symbol::ImplementationDetail); | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 191 | References = std::max(IndexResult.References, References); | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 192 | Category = categorize(IndexResult.SymInfo); | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 193 | ReservedName = ReservedName || isReserved(IndexResult.Name); | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
|  | 196 | float SymbolQualitySignals::evaluate() const { | 
|  | 197 | float Score = 1; | 
|  | 198 |  | 
|  | 199 | // This avoids a sharp gradient for tail symbols, and also neatly avoids the | 
|  | 200 | // question of whether 0 references means a bad symbol or missing data. | 
| Eric Liu | 84bd5db | 2018-07-25 11:26:35 +0000 | [diff] [blame] | 201 | if (References >= 10) { | 
|  | 202 | // Use a sigmoid style boosting function, which flats out nicely for large | 
|  | 203 | // numbers (e.g. 2.58 for 1M refererences). | 
|  | 204 | // The following boosting function is equivalent to: | 
|  | 205 | //   m = 0.06 | 
|  | 206 | //   f = 12.0 | 
|  | 207 | //   boost = f * sigmoid(m * std::log(References)) - 0.5 * f + 0.59 | 
|  | 208 | // Sample data points: (10, 1.00), (100, 1.41), (1000, 1.82), | 
|  | 209 | //                     (10K, 2.21), (100K, 2.58), (1M, 2.94) | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 210 | float S = std::pow(References, -0.06); | 
|  | 211 | Score *= 6.0 * (1 - S) / (1 + S) + 0.59; | 
| Eric Liu | 84bd5db | 2018-07-25 11:26:35 +0000 | [diff] [blame] | 212 | } | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 213 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 214 | if (Deprecated) | 
| Aaron Ballman | 215e471 | 2018-05-18 13:18:41 +0000 | [diff] [blame] | 215 | Score *= 0.1f; | 
| Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 216 | if (ReservedName) | 
|  | 217 | Score *= 0.1f; | 
| Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 218 | if (ImplementationDetail) | 
|  | 219 | Score *= 0.2f; | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 220 |  | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 221 | switch (Category) { | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 222 | case Keyword: // Often relevant, but misses most signals. | 
|  | 223 | Score *= 4; // FIXME: important keywords should have specific boosts. | 
|  | 224 | break; | 
|  | 225 | case Type: | 
|  | 226 | case Function: | 
|  | 227 | case Variable: | 
|  | 228 | Score *= 1.1f; | 
|  | 229 | break; | 
|  | 230 | case Namespace: | 
|  | 231 | Score *= 0.8f; | 
|  | 232 | break; | 
|  | 233 | case Macro: | 
| Eric Liu | f592d28 | 2018-09-05 07:40:38 +0000 | [diff] [blame] | 234 | Score *= 0.5f; | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 235 | break; | 
|  | 236 | case Unknown: | 
| Eric Liu | d7de811 | 2018-07-24 08:51:52 +0000 | [diff] [blame] | 237 | case Constructor: // No boost constructors so they are after class types. | 
| Kirill Bobyrev | 7cf29bc | 2018-07-05 09:37:26 +0000 | [diff] [blame] | 238 | break; | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 241 | return Score; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) { | 
|  | 245 | OS << formatv("=== Symbol quality: {0}\n", S.evaluate()); | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 246 | OS << formatv("\tReferences: {0}\n", S.References); | 
|  | 247 | OS << formatv("\tDeprecated: {0}\n", S.Deprecated); | 
| Sam McCall | e018b36 | 2018-06-08 09:36:34 +0000 | [diff] [blame] | 248 | OS << formatv("\tReserved name: {0}\n", S.ReservedName); | 
| Sam McCall | 4a3c69b | 2018-06-06 08:53:36 +0000 | [diff] [blame] | 249 | OS << formatv("\tCategory: {0}\n", static_cast<int>(S.Category)); | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 250 | return OS; | 
|  | 251 | } | 
|  | 252 |  | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 253 | static SymbolRelevanceSignals::AccessibleScope | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 254 | computeScope(const NamedDecl *D) { | 
| Sam McCall | abe3737 | 2018-06-27 11:43:54 +0000 | [diff] [blame] | 255 | // Injected "Foo" within the class "Foo" has file scope, not class scope. | 
|  | 256 | const DeclContext *DC = D->getDeclContext(); | 
|  | 257 | if (auto *R = dyn_cast_or_null<RecordDecl>(D)) | 
|  | 258 | if (R->isInjectedClassName()) | 
|  | 259 | DC = DC->getParent(); | 
| Eric Liu | 8944f0e | 2018-07-05 08:14:04 +0000 | [diff] [blame] | 260 | // Class constructor should have the same scope as the class. | 
| Simon Pilgrim | 4a03201 | 2018-07-05 09:35:12 +0000 | [diff] [blame] | 261 | if (isa<CXXConstructorDecl>(D)) | 
| Eric Liu | 8944f0e | 2018-07-05 08:14:04 +0000 | [diff] [blame] | 262 | DC = DC->getParent(); | 
| Sam McCall | 89f5293 | 2018-06-05 18:00:48 +0000 | [diff] [blame] | 263 | bool InClass = false; | 
| Sam McCall | abe3737 | 2018-06-27 11:43:54 +0000 | [diff] [blame] | 264 | for (; !DC->isFileContext(); DC = DC->getParent()) { | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 265 | if (DC->isFunctionOrMethod()) | 
|  | 266 | return SymbolRelevanceSignals::FunctionScope; | 
|  | 267 | InClass = InClass || DC->isRecord(); | 
|  | 268 | } | 
|  | 269 | if (InClass) | 
|  | 270 | return SymbolRelevanceSignals::ClassScope; | 
|  | 271 | // This threshold could be tweaked, e.g. to treat module-visible as global. | 
| Sam McCall | abe3737 | 2018-06-27 11:43:54 +0000 | [diff] [blame] | 272 | if (D->getLinkageInternal() < ExternalLinkage) | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 273 | return SymbolRelevanceSignals::FileScope; | 
|  | 274 | return SymbolRelevanceSignals::GlobalScope; | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | void SymbolRelevanceSignals::merge(const Symbol &IndexResult) { | 
|  | 278 | // FIXME: Index results always assumed to be at global scope. If Scope becomes | 
|  | 279 | // relevant to non-completion requests, we should recognize class members etc. | 
| Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 280 |  | 
|  | 281 | SymbolURI = IndexResult.CanonicalDeclaration.FileURI; | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 282 | SymbolScope = IndexResult.Scope; | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 283 | IsInstanceMember |= isInstanceMember(IndexResult.SymInfo); | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 284 | } | 
|  | 285 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 286 | void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) { | 
|  | 287 | if (SemaCCResult.Availability == CXAvailability_NotAvailable || | 
|  | 288 | SemaCCResult.Availability == CXAvailability_NotAccessible) | 
|  | 289 | Forbidden = true; | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 290 |  | 
|  | 291 | if (SemaCCResult.Declaration) { | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 292 | SemaSaysInScope = true; | 
| Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 293 | // We boost things that have decls in the main file. We give a fixed score | 
|  | 294 | // for all other declarations in sema as they are already included in the | 
|  | 295 | // translation unit. | 
| Kirill Bobyrev | 47d7f52 | 2018-07-11 14:49:49 +0000 | [diff] [blame] | 296 | float DeclProximity = (hasDeclInMainFile(*SemaCCResult.Declaration) || | 
|  | 297 | hasUsingDeclInMainFile(SemaCCResult)) | 
|  | 298 | ? 1.0 | 
|  | 299 | : 0.6; | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 300 | SemaFileProximityScore = std::max(DeclProximity, SemaFileProximityScore); | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 301 | IsInstanceMember |= isInstanceMember(SemaCCResult.Declaration); | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 302 | } | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 303 |  | 
|  | 304 | // Declarations are scoped, others (like macros) are assumed global. | 
| Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame] | 305 | if (SemaCCResult.Declaration) | 
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 306 | Scope = std::min(Scope, computeScope(SemaCCResult.Declaration)); | 
| Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 307 |  | 
|  | 308 | NeedsFixIts = !SemaCCResult.FixIts.empty(); | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 311 | static std::pair<float, unsigned> uriProximity(llvm::StringRef SymbolURI, | 
|  | 312 | URIDistance *D) { | 
| Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 313 | if (!D || SymbolURI.empty()) | 
|  | 314 | return {0.f, 0u}; | 
|  | 315 | unsigned Distance = D->distance(SymbolURI); | 
|  | 316 | // Assume approximately default options are used for sensible scoring. | 
|  | 317 | return {std::exp(Distance * -0.4f / FileDistanceOptions().UpCost), Distance}; | 
|  | 318 | } | 
|  | 319 |  | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 320 | static float scopeBoost(ScopeDistance &Distance, | 
|  | 321 | llvm::Optional<llvm::StringRef> SymbolScope) { | 
|  | 322 | if (!SymbolScope) | 
|  | 323 | return 1; | 
|  | 324 | auto D = Distance.distance(*SymbolScope); | 
|  | 325 | if (D == FileDistance::Unreachable) | 
|  | 326 | return 0.4; | 
|  | 327 | return std::max(0.5, 2.0 * std::pow(0.6, D / 2.0)); | 
|  | 328 | } | 
|  | 329 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 330 | float SymbolRelevanceSignals::evaluate() const { | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 331 | float Score = 1; | 
|  | 332 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 333 | if (Forbidden) | 
|  | 334 | return 0; | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 335 |  | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 336 | Score *= NameMatch; | 
|  | 337 |  | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 338 | // File proximity scores are [0,1] and we translate them into a multiplier in | 
|  | 339 | // the range from 1 to 3. | 
|  | 340 | Score *= 1 + 2 * std::max(uriProximity(SymbolURI, FileProximityMatch).first, | 
|  | 341 | SemaFileProximityScore); | 
|  | 342 |  | 
|  | 343 | if (ScopeProximityMatch) | 
|  | 344 | // Use a constant scope boost for sema results, as scopes of sema results | 
|  | 345 | // can be tricky (e.g. class/function scope). Set to the max boost as we | 
|  | 346 | // don't load top-level symbols from the preamble and sema results are | 
|  | 347 | // always in the accessible scope. | 
|  | 348 | Score *= | 
|  | 349 | SemaSaysInScope ? 2.0 : scopeBoost(*ScopeProximityMatch, SymbolScope); | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 350 |  | 
|  | 351 | // Symbols like local variables may only be referenced within their scope. | 
|  | 352 | // Conversely if we're in that scope, it's likely we'll reference them. | 
|  | 353 | if (Query == CodeComplete) { | 
|  | 354 | // The narrower the scope where a symbol is visible, the more likely it is | 
|  | 355 | // to be relevant when it is available. | 
|  | 356 | switch (Scope) { | 
|  | 357 | case GlobalScope: | 
|  | 358 | break; | 
|  | 359 | case FileScope: | 
|  | 360 | Score *= 1.5; | 
| Sam McCall | c22c9aa | 2018-06-07 08:16:36 +0000 | [diff] [blame] | 361 | break; | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 362 | case ClassScope: | 
|  | 363 | Score *= 2; | 
| Sam McCall | c22c9aa | 2018-06-07 08:16:36 +0000 | [diff] [blame] | 364 | break; | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 365 | case FunctionScope: | 
|  | 366 | Score *= 4; | 
| Sam McCall | c22c9aa | 2018-06-07 08:16:36 +0000 | [diff] [blame] | 367 | break; | 
| Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 371 | // Penalize non-instance members when they are accessed via a class instance. | 
|  | 372 | if (!IsInstanceMember && | 
|  | 373 | (Context == CodeCompletionContext::CCC_DotMemberAccess || | 
|  | 374 | Context == CodeCompletionContext::CCC_ArrowMemberAccess)) { | 
|  | 375 | Score *= 0.5; | 
|  | 376 | } | 
|  | 377 |  | 
| Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 378 | // Penalize for FixIts. | 
|  | 379 | if (NeedsFixIts) | 
|  | 380 | Score *= 0.5; | 
|  | 381 |  | 
| Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 382 | return Score; | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 383 | } | 
| Eric Liu | 09c3c37 | 2018-06-15 08:58:12 +0000 | [diff] [blame] | 384 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 385 | raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) { | 
|  | 386 | OS << formatv("=== Symbol relevance: {0}\n", S.evaluate()); | 
|  | 387 | OS << formatv("\tName match: {0}\n", S.NameMatch); | 
|  | 388 | OS << formatv("\tForbidden: {0}\n", S.Forbidden); | 
| Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 389 | OS << formatv("\tNeedsFixIts: {0}\n", S.NeedsFixIts); | 
| Eric Liu | 5d2a807 | 2018-07-23 10:56:37 +0000 | [diff] [blame] | 390 | OS << formatv("\tIsInstanceMember: {0}\n", S.IsInstanceMember); | 
|  | 391 | OS << formatv("\tContext: {0}\n", getCompletionKindString(S.Context)); | 
| Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame] | 392 | OS << formatv("\tQuery type: {0}\n", static_cast<int>(S.Query)); | 
|  | 393 | OS << formatv("\tScope: {0}\n", static_cast<int>(S.Scope)); | 
| Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 394 |  | 
|  | 395 | OS << formatv("\tSymbol URI: {0}\n", S.SymbolURI); | 
|  | 396 | OS << formatv("\tSymbol scope: {0}\n", | 
|  | 397 | S.SymbolScope ? *S.SymbolScope : "<None>"); | 
|  | 398 |  | 
|  | 399 | if (S.FileProximityMatch) { | 
|  | 400 | auto Score = uriProximity(S.SymbolURI, S.FileProximityMatch); | 
|  | 401 | OS << formatv("\tIndex URI proximity: {0} (distance={1})\n", Score.first, | 
|  | 402 | Score.second); | 
|  | 403 | } | 
|  | 404 | OS << formatv("\tSema file proximity: {0}\n", S.SemaFileProximityScore); | 
|  | 405 |  | 
|  | 406 | OS << formatv("\tSema says in scope: {0}\n", S.SemaSaysInScope); | 
|  | 407 | if (S.ScopeProximityMatch) | 
|  | 408 | OS << formatv("\tIndex scope boost: {0}\n", | 
|  | 409 | scopeBoost(*S.ScopeProximityMatch, S.SymbolScope)); | 
|  | 410 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 411 | return OS; | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) { | 
|  | 415 | return SymbolQuality * SymbolRelevance; | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | // Produces an integer that sorts in the same order as F. | 
|  | 419 | // That is: a < b <==> encodeFloat(a) < encodeFloat(b). | 
|  | 420 | static uint32_t encodeFloat(float F) { | 
|  | 421 | static_assert(std::numeric_limits<float>::is_iec559, ""); | 
|  | 422 | constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1); | 
|  | 423 |  | 
|  | 424 | // Get the bits of the float. Endianness is the same as for integers. | 
|  | 425 | uint32_t U = FloatToBits(F); | 
|  | 426 | // IEEE 754 floats compare like sign-magnitude integers. | 
|  | 427 | if (U & TopBit)    // Negative float. | 
|  | 428 | return 0 - U;    // Map onto the low half of integers, order reversed. | 
|  | 429 | return U + TopBit; // Positive floats map onto the high half of integers. | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | std::string sortText(float Score, llvm::StringRef Name) { | 
|  | 433 | // We convert -Score to an integer, and hex-encode for readability. | 
|  | 434 | // Example: [0.5, "foo"] -> "41000000foo" | 
|  | 435 | std::string S; | 
|  | 436 | llvm::raw_string_ostream OS(S); | 
|  | 437 | write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower, | 
|  | 438 | /*Width=*/2 * sizeof(Score)); | 
|  | 439 | OS << Name; | 
|  | 440 | OS.flush(); | 
|  | 441 | return S; | 
|  | 442 | } | 
|  | 443 |  | 
| Kadir Cetinkaya | e486e37 | 2018-08-13 08:40:05 +0000 | [diff] [blame] | 444 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, | 
|  | 445 | const SignatureQualitySignals &S) { | 
|  | 446 | OS << formatv("=== Signature Quality:\n"); | 
|  | 447 | OS << formatv("\tNumber of parameters: {0}\n", S.NumberOfParameters); | 
|  | 448 | OS << formatv("\tNumber of optional parameters: {0}\n", | 
|  | 449 | S.NumberOfOptionalParameters); | 
|  | 450 | OS << formatv("\tContains active parameter: {0}\n", | 
|  | 451 | S.ContainsActiveParameter); | 
|  | 452 | OS << formatv("\tKind: {0}\n", S.Kind); | 
|  | 453 | return OS; | 
|  | 454 | } | 
|  | 455 |  | 
| Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 456 | } // namespace clangd | 
|  | 457 | } // namespace clang |