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" |
| 12 | #include "llvm/ADT/SmallVector.h" |
| 13 | #include "llvm/Support/Debug.h" |
| 14 | |
| 15 | #define DEBUG_TYPE "include-fixer" |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace include_fixer { |
| 19 | |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame^] | 20 | using clang::find_all_symbols::SymbolInfo; |
| 21 | |
| 22 | /// Sorts and uniques SymbolInfos based on the popularity info in SymbolInfo. |
| 23 | static void rankByPopularity(std::vector<SymbolInfo> &Symbols) { |
| 24 | // First collect occurrences per header file. |
| 25 | std::map<llvm::StringRef, unsigned> HeaderPopularity; |
| 26 | for (const SymbolInfo &Symbol : Symbols) { |
| 27 | unsigned &Popularity = HeaderPopularity[Symbol.getFilePath()]; |
| 28 | Popularity = std::max(Popularity, Symbol.getNumOccurrences()); |
| 29 | } |
| 30 | |
| 31 | // Sort by the gathered popularities. Use file name as a tie breaker so we can |
| 32 | // deduplicate. |
| 33 | std::sort(Symbols.begin(), Symbols.end(), |
| 34 | [&](const SymbolInfo &A, const SymbolInfo &B) { |
| 35 | auto APop = HeaderPopularity[A.getFilePath()]; |
| 36 | auto BPop = HeaderPopularity[B.getFilePath()]; |
| 37 | if (APop != BPop) |
| 38 | return APop > BPop; |
| 39 | return A.getFilePath() < B.getFilePath(); |
| 40 | }); |
| 41 | |
| 42 | // Deduplicate based on the file name. They will have the same popularity and |
| 43 | // we don't want to suggest the same header twice. |
| 44 | Symbols.erase(std::unique(Symbols.begin(), Symbols.end(), |
| 45 | [](const SymbolInfo &A, const SymbolInfo &B) { |
| 46 | return A.getFilePath() == B.getFilePath(); |
| 47 | }), |
| 48 | Symbols.end()); |
| 49 | } |
| 50 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 51 | std::vector<std::string> |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 52 | SymbolIndexManager::search(llvm::StringRef Identifier) const { |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 53 | // The identifier may be fully qualified, so split it and get all the context |
| 54 | // names. |
| 55 | llvm::SmallVector<llvm::StringRef, 8> Names; |
| 56 | Identifier.split(Names, "::"); |
| 57 | |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 58 | bool IsFullyQualified = false; |
| 59 | if (Identifier.startswith("::")) { |
| 60 | Names.erase(Names.begin()); // Drop first (empty) element. |
| 61 | IsFullyQualified = true; |
| 62 | } |
| 63 | |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 64 | // As long as we don't find a result keep stripping name parts from the end. |
| 65 | // This is to support nested classes which aren't recorded in the database. |
| 66 | // Eventually we will either hit a class (namespaces aren't in the database |
| 67 | // either) and can report that result. |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 68 | std::vector<std::string> Results; |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 69 | while (Results.empty() && !Names.empty()) { |
| 70 | std::vector<clang::find_all_symbols::SymbolInfo> Symbols; |
| 71 | for (const auto &DB : SymbolIndices) { |
| 72 | auto Res = DB->search(Names.back().str()); |
| 73 | Symbols.insert(Symbols.end(), Res.begin(), Res.end()); |
| 74 | } |
| 75 | |
| 76 | DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got " |
| 77 | << Symbols.size() << " results...\n"); |
| 78 | |
Benjamin Kramer | 658d280 | 2016-05-31 14:33:28 +0000 | [diff] [blame^] | 79 | rankByPopularity(Symbols); |
| 80 | |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 81 | for (const auto &Symbol : Symbols) { |
| 82 | // Match the identifier name without qualifier. |
| 83 | if (Symbol.getName() == Names.back()) { |
| 84 | bool IsMatched = true; |
| 85 | auto SymbolContext = Symbol.getContexts().begin(); |
| 86 | auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name. |
| 87 | // Match the remaining context names. |
| 88 | while (IdentiferContext != Names.rend() && |
| 89 | SymbolContext != Symbol.getContexts().end()) { |
| 90 | if (SymbolContext->second == *IdentiferContext) { |
| 91 | ++IdentiferContext; |
| 92 | ++SymbolContext; |
| 93 | } else if (SymbolContext->first == |
| 94 | find_all_symbols::SymbolInfo::ContextType::EnumDecl) { |
| 95 | // Skip non-scoped enum context. |
| 96 | ++SymbolContext; |
| 97 | } else { |
| 98 | IsMatched = false; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 103 | // If the name was qualified we only want to add results if we evaluated |
| 104 | // all contexts. |
| 105 | if (IsFullyQualified) |
| 106 | IsMatched &= (SymbolContext == Symbol.getContexts().end()); |
| 107 | |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 108 | // FIXME: Support full match. At this point, we only find symbols in |
| 109 | // database which end with the same contexts with the identifier. |
| 110 | if (IsMatched && IdentiferContext == Names.rend()) { |
| 111 | // FIXME: file path should never be in the form of <...> or "...", but |
| 112 | // the unit test with fixed database use <...> file path, which might |
| 113 | // need to be changed. |
| 114 | // FIXME: if the file path is a system header name, we want to use |
| 115 | // angle brackets. |
| 116 | std::string FilePath = Symbol.getFilePath().str(); |
| 117 | Results.push_back((FilePath[0] == '"' || FilePath[0] == '<') |
| 118 | ? FilePath |
| 119 | : "\"" + FilePath + "\""); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 122 | } |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 123 | Names.pop_back(); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 124 | } |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame] | 125 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 126 | return Results; |
| 127 | } |
| 128 | |
| 129 | } // namespace include_fixer |
| 130 | } // namespace clang |