blob: 3813a5453bf99c271426fb8489da72cb755ccadc [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
20std::vector<std::string>
Benjamin Kramera3d82332016-05-13 09:27:54 +000021SymbolIndexManager::search(llvm::StringRef Identifier) const {
Eric Liu692aca62016-05-04 08:22:35 +000022 // The identifier may be fully qualified, so split it and get all the context
23 // names.
24 llvm::SmallVector<llvm::StringRef, 8> Names;
25 Identifier.split(Names, "::");
26
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000027 // As long as we don't find a result keep stripping name parts from the end.
28 // This is to support nested classes which aren't recorded in the database.
29 // Eventually we will either hit a class (namespaces aren't in the database
30 // either) and can report that result.
Eric Liu692aca62016-05-04 08:22:35 +000031 std::vector<std::string> Results;
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000032 while (Results.empty() && !Names.empty()) {
33 std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
34 for (const auto &DB : SymbolIndices) {
35 auto Res = DB->search(Names.back().str());
36 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
37 }
38
39 DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
40 << Symbols.size() << " results...\n");
41
42 for (const auto &Symbol : Symbols) {
43 // Match the identifier name without qualifier.
44 if (Symbol.getName() == Names.back()) {
45 bool IsMatched = true;
46 auto SymbolContext = Symbol.getContexts().begin();
47 auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
48 // Match the remaining context names.
49 while (IdentiferContext != Names.rend() &&
50 SymbolContext != Symbol.getContexts().end()) {
51 if (SymbolContext->second == *IdentiferContext) {
52 ++IdentiferContext;
53 ++SymbolContext;
54 } else if (SymbolContext->first ==
55 find_all_symbols::SymbolInfo::ContextType::EnumDecl) {
56 // Skip non-scoped enum context.
57 ++SymbolContext;
58 } else {
59 IsMatched = false;
60 break;
61 }
62 }
63
64 // FIXME: Support full match. At this point, we only find symbols in
65 // database which end with the same contexts with the identifier.
66 if (IsMatched && IdentiferContext == Names.rend()) {
67 // FIXME: file path should never be in the form of <...> or "...", but
68 // the unit test with fixed database use <...> file path, which might
69 // need to be changed.
70 // FIXME: if the file path is a system header name, we want to use
71 // angle brackets.
72 std::string FilePath = Symbol.getFilePath().str();
73 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
74 ? FilePath
75 : "\"" + FilePath + "\"");
Eric Liu692aca62016-05-04 08:22:35 +000076 }
77 }
Eric Liu692aca62016-05-04 08:22:35 +000078 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000079 Names.pop_back();
Eric Liu692aca62016-05-04 08:22:35 +000080 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000081
Eric Liu692aca62016-05-04 08:22:35 +000082 return Results;
83}
84
85} // namespace include_fixer
86} // namespace clang