blob: b3ecca4928997e5d21c75556282ba9c6951d26b5 [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 Liu692aca62016-05-04 08:22:35 +000070 std::vector<std::string> Results;
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000071 while (Results.empty() && !Names.empty()) {
72 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
Benjamin Kramer658d2802016-05-31 14:33:28 +000081 rankByPopularity(Symbols);
82
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000083 for (const auto &Symbol : Symbols) {
84 // Match the identifier name without qualifier.
85 if (Symbol.getName() == Names.back()) {
86 bool IsMatched = true;
87 auto SymbolContext = Symbol.getContexts().begin();
88 auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
89 // Match the remaining context names.
90 while (IdentiferContext != Names.rend() &&
91 SymbolContext != Symbol.getContexts().end()) {
92 if (SymbolContext->second == *IdentiferContext) {
93 ++IdentiferContext;
94 ++SymbolContext;
95 } else if (SymbolContext->first ==
96 find_all_symbols::SymbolInfo::ContextType::EnumDecl) {
97 // Skip non-scoped enum context.
98 ++SymbolContext;
99 } else {
100 IsMatched = false;
101 break;
102 }
103 }
104
Benjamin Kramer9b15b6f2016-05-19 12:41:56 +0000105 // If the name was qualified we only want to add results if we evaluated
106 // all contexts.
107 if (IsFullyQualified)
108 IsMatched &= (SymbolContext == Symbol.getContexts().end());
109
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000110 // FIXME: Support full match. At this point, we only find symbols in
111 // database which end with the same contexts with the identifier.
112 if (IsMatched && IdentiferContext == Names.rend()) {
Benjamin Kramerb53452b2016-06-03 14:07:38 +0000113 // If we're in a situation where we took a prefix but the thing we
114 // found couldn't possibly have a nested member ignore it.
115 if (TookPrefix &&
116 (Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Function ||
117 Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Variable ||
118 Symbol.getSymbolKind() ==
119 SymbolInfo::SymbolKind::EnumConstantDecl ||
120 Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Macro))
121 continue;
122
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000123 // FIXME: file path should never be in the form of <...> or "...", but
124 // the unit test with fixed database use <...> file path, which might
125 // need to be changed.
126 // FIXME: if the file path is a system header name, we want to use
127 // angle brackets.
128 std::string FilePath = Symbol.getFilePath().str();
129 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
130 ? FilePath
131 : "\"" + FilePath + "\"");
Eric Liu692aca62016-05-04 08:22:35 +0000132 }
133 }
Eric Liu692aca62016-05-04 08:22:35 +0000134 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000135 Names.pop_back();
Benjamin Kramerb53452b2016-06-03 14:07:38 +0000136 TookPrefix = true;
Eric Liu692aca62016-05-04 08:22:35 +0000137 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000138
Eric Liu692aca62016-05-04 08:22:35 +0000139 return Results;
140}
141
142} // namespace include_fixer
143} // namespace clang