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