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 | |
| 70 | void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) { |
| 71 | if (SemaCCResult.Availability == CXAvailability_NotAvailable || |
| 72 | SemaCCResult.Availability == CXAvailability_NotAccessible) |
| 73 | Forbidden = true; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 74 | |
| 75 | if (SemaCCResult.Declaration) { |
| 76 | // We boost things that have decls in the main file. |
| 77 | // The real proximity scores would be more general when we have them. |
| 78 | float DeclProximity = |
| 79 | hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.0; |
| 80 | ProximityScore = std::max(DeclProximity, ProximityScore); |
| 81 | } |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | float SymbolRelevanceSignals::evaluate() const { |
| 85 | if (Forbidden) |
| 86 | return 0; |
Ilya Biryukov | f029646 | 2018-06-04 14:50:59 +0000 | [diff] [blame] | 87 | |
| 88 | float Score = NameMatch; |
| 89 | // Proximity scores are [0,1] and we translate them into a multiplier in the |
| 90 | // range from 1 to 2. |
| 91 | Score *= 1 + ProximityScore; |
| 92 | return Score; |
Sam McCall | c5707b6 | 2018-05-15 17:43:27 +0000 | [diff] [blame] | 93 | } |
| 94 | raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) { |
| 95 | OS << formatv("=== Symbol relevance: {0}\n", S.evaluate()); |
| 96 | OS << formatv("\tName match: {0}\n", S.NameMatch); |
| 97 | OS << formatv("\tForbidden: {0}\n", S.Forbidden); |
| 98 | return OS; |
| 99 | } |
| 100 | |
| 101 | float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) { |
| 102 | return SymbolQuality * SymbolRelevance; |
| 103 | } |
| 104 | |
| 105 | // Produces an integer that sorts in the same order as F. |
| 106 | // That is: a < b <==> encodeFloat(a) < encodeFloat(b). |
| 107 | static uint32_t encodeFloat(float F) { |
| 108 | static_assert(std::numeric_limits<float>::is_iec559, ""); |
| 109 | constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1); |
| 110 | |
| 111 | // Get the bits of the float. Endianness is the same as for integers. |
| 112 | uint32_t U = FloatToBits(F); |
| 113 | // IEEE 754 floats compare like sign-magnitude integers. |
| 114 | if (U & TopBit) // Negative float. |
| 115 | return 0 - U; // Map onto the low half of integers, order reversed. |
| 116 | return U + TopBit; // Positive floats map onto the high half of integers. |
| 117 | } |
| 118 | |
| 119 | std::string sortText(float Score, llvm::StringRef Name) { |
| 120 | // We convert -Score to an integer, and hex-encode for readability. |
| 121 | // Example: [0.5, "foo"] -> "41000000foo" |
| 122 | std::string S; |
| 123 | llvm::raw_string_ostream OS(S); |
| 124 | write_hex(OS, encodeFloat(-Score), llvm::HexPrintStyle::Lower, |
| 125 | /*Width=*/2 * sizeof(Score)); |
| 126 | OS << Name; |
| 127 | OS.flush(); |
| 128 | return S; |
| 129 | } |
| 130 | |
| 131 | } // namespace clangd |
| 132 | } // namespace clang |