blob: 1800fab41999ab17b3f88e7eb8d20c8c74d0e25f [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"
12#include "llvm/ADT/SmallVector.h"
13#include "llvm/Support/Debug.h"
14
15#define DEBUG_TYPE "include-fixer"
16
17namespace clang {
18namespace include_fixer {
19
Benjamin Kramer658d2802016-05-31 14:33:28 +000020using clang::find_all_symbols::SymbolInfo;
21
22/// Sorts and uniques SymbolInfos based on the popularity info in SymbolInfo.
23static void rankByPopularity(std::vector<SymbolInfo> &Symbols) {
24 // First collect occurrences per header file.
25 std::map<llvm::StringRef, unsigned> HeaderPopularity;
26 for (const SymbolInfo &Symbol : Symbols) {
27 unsigned &Popularity = HeaderPopularity[Symbol.getFilePath()];
28 Popularity = std::max(Popularity, Symbol.getNumOccurrences());
29 }
30
31 // Sort by the gathered popularities. Use file name as a tie breaker so we can
32 // deduplicate.
33 std::sort(Symbols.begin(), Symbols.end(),
34 [&](const SymbolInfo &A, const SymbolInfo &B) {
35 auto APop = HeaderPopularity[A.getFilePath()];
36 auto BPop = HeaderPopularity[B.getFilePath()];
37 if (APop != BPop)
38 return APop > BPop;
39 return A.getFilePath() < B.getFilePath();
40 });
41
42 // Deduplicate based on the file name. They will have the same popularity and
43 // we don't want to suggest the same header twice.
44 Symbols.erase(std::unique(Symbols.begin(), Symbols.end(),
45 [](const SymbolInfo &A, const SymbolInfo &B) {
46 return A.getFilePath() == B.getFilePath();
47 }),
48 Symbols.end());
49}
50
Eric Liu692aca62016-05-04 08:22:35 +000051std::vector<std::string>
Benjamin Kramera3d82332016-05-13 09:27:54 +000052SymbolIndexManager::search(llvm::StringRef Identifier) const {
Eric Liu692aca62016-05-04 08:22:35 +000053 // The identifier may be fully qualified, so split it and get all the context
54 // names.
55 llvm::SmallVector<llvm::StringRef, 8> Names;
56 Identifier.split(Names, "::");
57
Benjamin Kramer9b15b6f2016-05-19 12:41:56 +000058 bool IsFullyQualified = false;
59 if (Identifier.startswith("::")) {
60 Names.erase(Names.begin()); // Drop first (empty) element.
61 IsFullyQualified = true;
62 }
63
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000064 // As long as we don't find a result keep stripping name parts from the end.
65 // This is to support nested classes which aren't recorded in the database.
66 // Eventually we will either hit a class (namespaces aren't in the database
67 // either) and can report that result.
Eric Liu692aca62016-05-04 08:22:35 +000068 std::vector<std::string> Results;
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000069 while (Results.empty() && !Names.empty()) {
70 std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
71 for (const auto &DB : SymbolIndices) {
72 auto Res = DB->search(Names.back().str());
73 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
74 }
75
76 DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
77 << Symbols.size() << " results...\n");
78
Benjamin Kramer658d2802016-05-31 14:33:28 +000079 rankByPopularity(Symbols);
80
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000081 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()) {
111 // FIXME: file path should never be in the form of <...> or "...", but
112 // the unit test with fixed database use <...> file path, which might
113 // need to be changed.
114 // FIXME: if the file path is a system header name, we want to use
115 // angle brackets.
116 std::string FilePath = Symbol.getFilePath().str();
117 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
118 ? FilePath
119 : "\"" + FilePath + "\"");
Eric Liu692aca62016-05-04 08:22:35 +0000120 }
121 }
Eric Liu692aca62016-05-04 08:22:35 +0000122 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000123 Names.pop_back();
Eric Liu692aca62016-05-04 08:22:35 +0000124 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +0000125
Eric Liu692aca62016-05-04 08:22:35 +0000126 return Results;
127}
128
129} // namespace include_fixer
130} // namespace clang