Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 1 | //===-- SymbolIndexManager.cpp - Managing multiple SymbolIndices-*- C++ -*-===// |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +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 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 10 | #include "SymbolIndexManager.h" |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 11 | #include "find-all-symbols/SymbolInfo.h" |
Benjamin Kramer | 99985b8 | 2016-05-31 14:37:10 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/DenseMap.h" |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/Support/Debug.h" |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 16 | |
| 17 | #define DEBUG_TYPE "include-fixer" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace include_fixer { |
| 21 | |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 22 | using clang::find_all_symbols::SymbolInfo; |
| 23 | |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 24 | // Calculate a score based on whether we think the given header is closely |
| 25 | // related to the given source file. |
| 26 | static double similarityScore(llvm::StringRef FileName, |
| 27 | llvm::StringRef Header) { |
| 28 | // Compute the maximum number of common path segements between Header and |
| 29 | // a suffix of FileName. |
| 30 | // We do not do a full longest common substring computation, as Header |
| 31 | // specifies the path we would directly #include, so we assume it is rooted |
| 32 | // relatively to a subproject of the repository. |
| 33 | int MaxSegments = 1; |
| 34 | for (auto FileI = llvm::sys::path::begin(FileName), |
| 35 | FileE = llvm::sys::path::end(FileName); |
| 36 | FileI != FileE; ++FileI) { |
| 37 | int Segments = 0; |
| 38 | for (auto HeaderI = llvm::sys::path::begin(Header), |
| 39 | HeaderE = llvm::sys::path::end(Header), I = FileI; |
| 40 | HeaderI != HeaderE && *I == *HeaderI && I != FileE; ++I, ++HeaderI) { |
| 41 | ++Segments; |
| 42 | } |
| 43 | MaxSegments = std::max(Segments, MaxSegments); |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 44 | } |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 45 | return MaxSegments; |
| 46 | } |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 47 | |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 48 | static void rank(std::vector<SymbolInfo> &Symbols, |
| 49 | llvm::StringRef FileName) { |
| 50 | llvm::DenseMap<llvm::StringRef, double> Score; |
| 51 | for (const SymbolInfo &Symbol : Symbols) { |
| 52 | // Calculate a score from the similarity of the header the symbol is in |
| 53 | // with the current file and the popularity of the symbol. |
| 54 | double NewScore = similarityScore(FileName, Symbol.getFilePath()) * |
| 55 | (1.0 + std::log2(1 + Symbol.getNumOccurrences())); |
| 56 | double &S = Score[Symbol.getFilePath()]; |
| 57 | S = std::max(S, NewScore); |
| 58 | } |
| 59 | // Sort by the gathered scores. Use file name as a tie breaker so we can |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 60 | // deduplicate. |
| 61 | std::sort(Symbols.begin(), Symbols.end(), |
| 62 | [&](const SymbolInfo &A, const SymbolInfo &B) { |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 63 | auto AS = Score[A.getFilePath()]; |
| 64 | auto BS = Score[B.getFilePath()]; |
| 65 | if (AS != BS) |
| 66 | return AS > BS; |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 67 | return A.getFilePath() < B.getFilePath(); |
| 68 | }); |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 71 | std::vector<find_all_symbols::SymbolInfo> |
Haojian Wu | adedac6 | 2016-08-02 10:43:10 +0000 | [diff] [blame] | 72 | SymbolIndexManager::search(llvm::StringRef Identifier, |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 73 | bool IsNestedSearch, |
| 74 | llvm::StringRef FileName) const { |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 75 | // The identifier may be fully qualified, so split it and get all the context |
| 76 | // names. |
| 77 | llvm::SmallVector<llvm::StringRef, 8> Names; |
| 78 | Identifier.split(Names, "::"); |
| 79 | |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 80 | bool IsFullyQualified = false; |
| 81 | if (Identifier.startswith("::")) { |
| 82 | Names.erase(Names.begin()); // Drop first (empty) element. |
| 83 | IsFullyQualified = true; |
| 84 | } |
| 85 | |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 86 | // As long as we don't find a result keep stripping name parts from the end. |
| 87 | // This is to support nested classes which aren't recorded in the database. |
| 88 | // Eventually we will either hit a class (namespaces aren't in the database |
| 89 | // either) and can report that result. |
Benjamin Kramer | b53452b | 2016-06-03 14:07:38 +0000 | [diff] [blame] | 90 | bool TookPrefix = false; |
Eric Liu | a4be83a | 2016-06-13 19:05:07 +0000 | [diff] [blame] | 91 | std::vector<clang::find_all_symbols::SymbolInfo> MatchedSymbols; |
Haojian Wu | adedac6 | 2016-08-02 10:43:10 +0000 | [diff] [blame] | 92 | do { |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 93 | std::vector<clang::find_all_symbols::SymbolInfo> Symbols; |
| 94 | for (const auto &DB : SymbolIndices) { |
Benjamin Kramer | bdb2171 | 2017-01-09 15:18:28 +0000 | [diff] [blame] | 95 | auto Res = DB.get()->search(Names.back()); |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 96 | Symbols.insert(Symbols.end(), Res.begin(), Res.end()); |
| 97 | } |
| 98 | |
| 99 | DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got " |
| 100 | << Symbols.size() << " results...\n"); |
| 101 | |
| 102 | for (const auto &Symbol : Symbols) { |
| 103 | // Match the identifier name without qualifier. |
| 104 | if (Symbol.getName() == Names.back()) { |
| 105 | bool IsMatched = true; |
| 106 | auto SymbolContext = Symbol.getContexts().begin(); |
| 107 | auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name. |
| 108 | // Match the remaining context names. |
| 109 | while (IdentiferContext != Names.rend() && |
| 110 | SymbolContext != Symbol.getContexts().end()) { |
| 111 | if (SymbolContext->second == *IdentiferContext) { |
| 112 | ++IdentiferContext; |
| 113 | ++SymbolContext; |
| 114 | } else if (SymbolContext->first == |
| 115 | find_all_symbols::SymbolInfo::ContextType::EnumDecl) { |
| 116 | // Skip non-scoped enum context. |
| 117 | ++SymbolContext; |
| 118 | } else { |
| 119 | IsMatched = false; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 124 | // If the name was qualified we only want to add results if we evaluated |
| 125 | // all contexts. |
| 126 | if (IsFullyQualified) |
| 127 | IsMatched &= (SymbolContext == Symbol.getContexts().end()); |
| 128 | |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 129 | // FIXME: Support full match. At this point, we only find symbols in |
| 130 | // database which end with the same contexts with the identifier. |
| 131 | if (IsMatched && IdentiferContext == Names.rend()) { |
Benjamin Kramer | b53452b | 2016-06-03 14:07:38 +0000 | [diff] [blame] | 132 | // If we're in a situation where we took a prefix but the thing we |
| 133 | // found couldn't possibly have a nested member ignore it. |
| 134 | if (TookPrefix && |
| 135 | (Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Function || |
| 136 | Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Variable || |
| 137 | Symbol.getSymbolKind() == |
| 138 | SymbolInfo::SymbolKind::EnumConstantDecl || |
| 139 | Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Macro)) |
| 140 | continue; |
| 141 | |
Eric Liu | a4be83a | 2016-06-13 19:05:07 +0000 | [diff] [blame] | 142 | MatchedSymbols.push_back(Symbol); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 145 | } |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 146 | Names.pop_back(); |
Benjamin Kramer | b53452b | 2016-06-03 14:07:38 +0000 | [diff] [blame] | 147 | TookPrefix = true; |
Haojian Wu | adedac6 | 2016-08-02 10:43:10 +0000 | [diff] [blame] | 148 | } while (MatchedSymbols.empty() && !Names.empty() && IsNestedSearch); |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 149 | |
Manuel Klimek | a47515e | 2017-01-11 10:32:47 +0000 | [diff] [blame] | 150 | rank(MatchedSymbols, FileName); |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 151 | return MatchedSymbols; |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace include_fixer |
| 155 | } // namespace clang |