blob: 970a6125569ddc56b008afab436baa0f14f62c5e [file] [log] [blame]
Benjamin Kramera3d82332016-05-13 09:27:54 +00001//===-- SymbolIndexManager.cpp - Managing multiple SymbolIndices-*- C++ -*-===//
Eric Liu692aca62016-05-04 08:22:35 +00002//
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 Kramera3d82332016-05-13 09:27:54 +000010#include "SymbolIndexManager.h"
Eric Liu692aca62016-05-04 08:22:35 +000011#include "find-all-symbols/SymbolInfo.h"
Benjamin Kramer99985b82016-05-31 14:37:10 +000012#include "llvm/ADT/DenseMap.h"
Eric Liu692aca62016-05-04 08:22:35 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/Support/Debug.h"
15
16#define DEBUG_TYPE "include-fixer"
17
18namespace clang {
19namespace include_fixer {
20
Benjamin Kramer658d2802016-05-31 14:33:28 +000021using clang::find_all_symbols::SymbolInfo;
22
Haojian Wu627ca962016-07-08 09:10:29 +000023/// Sorts SymbolInfos based on the popularity info in SymbolInfo.
Benjamin Kramer658d2802016-05-31 14:33:28 +000024static void rankByPopularity(std::vector<SymbolInfo> &Symbols) {
25 // First collect occurrences per header file.
Benjamin Kramer99985b82016-05-31 14:37:10 +000026 llvm::DenseMap<llvm::StringRef, unsigned> HeaderPopularity;
Benjamin Kramer658d2802016-05-31 14:33:28 +000027 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 Kramer658d2802016-05-31 14:33:28 +000042}
43
Haojian Wu627ca962016-07-08 09:10:29 +000044std::vector<find_all_symbols::SymbolInfo>
Benjamin Kramera3d82332016-05-13 09:27:54 +000045SymbolIndexManager::search(llvm::StringRef Identifier) const {
Eric Liu692aca62016-05-04 08:22:35 +000046 // 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 Kramer9b15b6f2016-05-19 12:41:56 +000051 bool IsFullyQualified = false;
52 if (Identifier.startswith("::")) {
53 Names.erase(Names.begin()); // Drop first (empty) element.
54 IsFullyQualified = true;
55 }
56
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000057 // 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 Kramerb53452b2016-06-03 14:07:38 +000061 bool TookPrefix = false;
Eric Liua4be83a2016-06-13 19:05:07 +000062 std::vector<clang::find_all_symbols::SymbolInfo> MatchedSymbols;
63 while (MatchedSymbols.empty() && !Names.empty()) {
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000064 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 Kramer9b15b6f2016-05-19 12:41:56 +000095 // 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 Kramer5e6b35f2016-05-18 16:42:38 +0000100 // 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 Kramerb53452b2016-06-03 14:07:38 +0000103 // 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 Liua4be83a2016-06-13 19:05:07 +0000113 MatchedSymbols.push_back(Symbol);
Eric Liu692aca62016-05-04 08:22:35 +0000114 }
115 }
Eric Liu692aca62016-05-04 08:22:35 +0000116 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000117 Names.pop_back();
Benjamin Kramerb53452b2016-06-03 14:07:38 +0000118 TookPrefix = true;
Eric Liu692aca62016-05-04 08:22:35 +0000119 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000120
Eric Liua4be83a2016-06-13 19:05:07 +0000121 rankByPopularity(MatchedSymbols);
Haojian Wu627ca962016-07-08 09:10:29 +0000122 return MatchedSymbols;
Eric Liu692aca62016-05-04 08:22:35 +0000123}
124
125} // namespace include_fixer
126} // namespace clang