blob: ae9faa28523ecd36251ba7990c59f82b055bcb41 [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
23/// Sorts and uniques SymbolInfos based on the popularity info in SymbolInfo.
24static 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 });
42
43 // Deduplicate based on the file name. They will have the same popularity and
44 // we don't want to suggest the same header twice.
45 Symbols.erase(std::unique(Symbols.begin(), Symbols.end(),
46 [](const SymbolInfo &A, const SymbolInfo &B) {
47 return A.getFilePath() == B.getFilePath();
48 }),
49 Symbols.end());
50}
51
Eric Liu692aca62016-05-04 08:22:35 +000052std::vector<std::string>
Benjamin Kramera3d82332016-05-13 09:27:54 +000053SymbolIndexManager::search(llvm::StringRef Identifier) const {
Eric Liu692aca62016-05-04 08:22:35 +000054 // The identifier may be fully qualified, so split it and get all the context
55 // names.
56 llvm::SmallVector<llvm::StringRef, 8> Names;
57 Identifier.split(Names, "::");
58
Benjamin Kramer9b15b6f2016-05-19 12:41:56 +000059 bool IsFullyQualified = false;
60 if (Identifier.startswith("::")) {
61 Names.erase(Names.begin()); // Drop first (empty) element.
62 IsFullyQualified = true;
63 }
64
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000065 // As long as we don't find a result keep stripping name parts from the end.
66 // This is to support nested classes which aren't recorded in the database.
67 // Eventually we will either hit a class (namespaces aren't in the database
68 // either) and can report that result.
Benjamin Kramerb53452b2016-06-03 14:07:38 +000069 bool TookPrefix = false;
Eric Liua4be83a2016-06-13 19:05:07 +000070 std::vector<clang::find_all_symbols::SymbolInfo> MatchedSymbols;
71 while (MatchedSymbols.empty() && !Names.empty()) {
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000072 std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
73 for (const auto &DB : SymbolIndices) {
74 auto Res = DB->search(Names.back().str());
75 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
76 }
77
78 DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
79 << Symbols.size() << " results...\n");
80
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 Kramer9b15b6f2016-05-19 12:41:56 +0000103 // 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 Kramer5e6b35f2016-05-18 16:42:38 +0000108 // 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()) {
Benjamin Kramerb53452b2016-06-03 14:07:38 +0000111 // If we're in a situation where we took a prefix but the thing we
112 // found couldn't possibly have a nested member ignore it.
113 if (TookPrefix &&
114 (Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Function ||
115 Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Variable ||
116 Symbol.getSymbolKind() ==
117 SymbolInfo::SymbolKind::EnumConstantDecl ||
118 Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Macro))
119 continue;
120
Eric Liua4be83a2016-06-13 19:05:07 +0000121 MatchedSymbols.push_back(Symbol);
Eric Liu692aca62016-05-04 08:22:35 +0000122 }
123 }
Eric Liu692aca62016-05-04 08:22:35 +0000124 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000125 Names.pop_back();
Benjamin Kramerb53452b2016-06-03 14:07:38 +0000126 TookPrefix = true;
Eric Liu692aca62016-05-04 08:22:35 +0000127 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000128
Eric Liua4be83a2016-06-13 19:05:07 +0000129 rankByPopularity(MatchedSymbols);
130 std::vector<std::string> Results;
131 for (const auto &Symbol : MatchedSymbols) {
132 // FIXME: file path should never be in the form of <...> or "...", but
133 // the unit test with fixed database use <...> file path, which might
134 // need to be changed.
135 // FIXME: if the file path is a system header name, we want to use
136 // angle brackets.
137 std::string FilePath = Symbol.getFilePath().str();
138 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
139 ? FilePath
140 : "\"" + FilePath + "\"");
141 }
142
Eric Liu692aca62016-05-04 08:22:35 +0000143 return Results;
144}
145
146} // namespace include_fixer
147} // namespace clang