Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 1 | //===--- 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 Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/Basic/SourceManager.h" |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 13 | #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 | |
| 18 | namespace clang { |
| 19 | namespace clangd { |
| 20 | using namespace llvm; |
| 21 | |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 22 | static 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 McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 32 | void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) { |
| 33 | SemaCCPriority = SemaCCResult.Priority; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 34 | if (SemaCCResult.Availability == CXAvailability_Deprecated) |
| 35 | Deprecated = true; |
| 36 | } |
| 37 | |
| 38 | void SymbolQualitySignals::merge(const Symbol &IndexResult) { |
| 39 | References = std::max(IndexResult.References, References); |
| 40 | } |
| 41 | |
| 42 | float 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 Ballman | 215e471 | 2018-05-18 13:18:41 +0000 | [diff] [blame] | 56 | Score *= 0.1f; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 57 | |
| 58 | return Score; |
| 59 | } |
| 60 | |
| 61 | raw_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 McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 70 | static SymbolRelevanceSignals::AccessibleScope |
| 71 | ComputeScope(const NamedDecl &D) { |
Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame^] | 72 | bool InClass = true; |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 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 | |
| 87 | void 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 McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 92 | void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) { |
| 93 | if (SemaCCResult.Availability == CXAvailability_NotAvailable || |
| 94 | SemaCCResult.Availability == CXAvailability_NotAccessible) |
| 95 | Forbidden = true; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 96 | |
| 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 McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 104 | |
| 105 | // Declarations are scoped, others (like macros) are assumed global. |
Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame^] | 106 | if (SemaCCResult.Declaration) |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 107 | Scope = std::min(Scope, ComputeScope(*SemaCCResult.Declaration)); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | float SymbolRelevanceSignals::evaluate() const { |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 111 | float Score = 1; |
| 112 | |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 113 | if (Forbidden) |
| 114 | return 0; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 115 | |
Sam McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 116 | Score *= NameMatch; |
| 117 | |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 118 | // 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 McCall | d9b54f0 | 2018-06-05 16:30:25 +0000 | [diff] [blame] | 121 | |
| 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 Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 139 | return Score; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 140 | } |
| 141 | raw_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); |
Sam McCall | 661d89c | 2018-06-05 17:58:12 +0000 | [diff] [blame^] | 145 | OS << formatv("\tProximity: {0}\n", S.ProximityScore); |
| 146 | OS << formatv("\tQuery type: {0}\n", static_cast<int>(S.Query)); |
| 147 | OS << formatv("\tScope: {0}\n", static_cast<int>(S.Scope)); |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 148 | return OS; |
| 149 | } |
| 150 | |
| 151 | float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) { |
| 152 | return SymbolQuality * SymbolRelevance; |
| 153 | } |
| 154 | |
| 155 | // Produces an integer that sorts in the same order as F. |
| 156 | // That is: a < b <==> encodeFloat(a) < encodeFloat(b). |
| 157 | static uint32_t encodeFloat(float F) { |
| 158 | static_assert(std::numeric_limits<float>::is_iec559, ""); |
| 159 | constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1); |
| 160 | |
| 161 | // Get the bits of the float. Endianness is the same as for integers. |
| 162 | uint32_t U = FloatToBits(F); |
| 163 | // IEEE 754 floats compare like sign-magnitude integers. |
| 164 | if (U & TopBit) // Negative float. |
| 165 | return 0 - U; // Map onto the low half of integers, order reversed. |
| 166 | return U + TopBit; // Positive floats map onto the high half of integers. |
| 167 | } |
| 168 | |
| 169 | std::string sortText(float Score, llvm::StringRef Name) { |
| 170 | // We convert -Score to an integer, and hex-encode for readability. |
| 171 | // Example: [0.5, "foo"] -> "41000000foo" |
| 172 | std::string S; |
| 173 | llvm::raw_string_ostream OS(S); |
| 174 | write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower, |
| 175 | /*Width=*/2 * sizeof(Score)); |
| 176 | OS << Name; |
| 177 | OS.flush(); |
| 178 | return S; |
| 179 | } |
| 180 | |
| 181 | } // namespace clangd |
| 182 | } // namespace clang |