blob: 70b143f74286c556432b9d80554eb7667e8a9b22 [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.
Eric Liu692aca62016-05-04 08:22:35 +000069 std::vector<std::string> Results;
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000070 while (Results.empty() && !Names.empty()) {
71 std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
72 for (const auto &DB : SymbolIndices) {
73 auto Res = DB->search(Names.back().str());
74 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
75 }
76
77 DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
78 << Symbols.size() << " results...\n");
79
Benjamin Kramer658d2802016-05-31 14:33:28 +000080 rankByPopularity(Symbols);
81
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000082 for (const auto &Symbol : Symbols) {
83 // Match the identifier name without qualifier.
84 if (Symbol.getName() == Names.back()) {
85 bool IsMatched = true;
86 auto SymbolContext = Symbol.getContexts().begin();
87 auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
88 // Match the remaining context names.
89 while (IdentiferContext != Names.rend() &&
90 SymbolContext != Symbol.getContexts().end()) {
91 if (SymbolContext->second == *IdentiferContext) {
92 ++IdentiferContext;
93 ++SymbolContext;
94 } else if (SymbolContext->first ==
95 find_all_symbols::SymbolInfo::ContextType::EnumDecl) {
96 // Skip non-scoped enum context.
97 ++SymbolContext;
98 } else {
99 IsMatched = false;
100 break;
101 }
102 }
103
Benjamin Kramer9b15b6f2016-05-19 12:41:56 +0000104 // If the name was qualified we only want to add results if we evaluated
105 // all contexts.
106 if (IsFullyQualified)
107 IsMatched &= (SymbolContext == Symbol.getContexts().end());
108
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000109 // FIXME: Support full match. At this point, we only find symbols in
110 // database which end with the same contexts with the identifier.
111 if (IsMatched && IdentiferContext == Names.rend()) {
112 // FIXME: file path should never be in the form of <...> or "...", but
113 // the unit test with fixed database use <...> file path, which might
114 // need to be changed.
115 // FIXME: if the file path is a system header name, we want to use
116 // angle brackets.
117 std::string FilePath = Symbol.getFilePath().str();
118 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
119 ? FilePath
120 : "\"" + FilePath + "\"");
Eric Liu692aca62016-05-04 08:22:35 +0000121 }
122 }
Eric Liu692aca62016-05-04 08:22:35 +0000123 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000124 Names.pop_back();
Eric Liu692aca62016-05-04 08:22:35 +0000125 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000126
Eric Liu692aca62016-05-04 08:22:35 +0000127 return Results;
128}
129
130} // namespace include_fixer
131} // namespace clang