blob: 2a0547a473a85a77643c7ab5fc9f45a5af472065 [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 Kramer9b15b6f2016-05-19 12:41:56 +000027 bool IsFullyQualified = false;
28 if (Identifier.startswith("::")) {
29 Names.erase(Names.begin()); // Drop first (empty) element.
30 IsFullyQualified = true;
31 }
32
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000033 // As long as we don't find a result keep stripping name parts from the end.
34 // This is to support nested classes which aren't recorded in the database.
35 // Eventually we will either hit a class (namespaces aren't in the database
36 // either) and can report that result.
Eric Liu692aca62016-05-04 08:22:35 +000037 std::vector<std::string> Results;
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000038 while (Results.empty() && !Names.empty()) {
39 std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
40 for (const auto &DB : SymbolIndices) {
41 auto Res = DB->search(Names.back().str());
42 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
43 }
44
45 DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
46 << Symbols.size() << " results...\n");
47
48 for (const auto &Symbol : Symbols) {
49 // Match the identifier name without qualifier.
50 if (Symbol.getName() == Names.back()) {
51 bool IsMatched = true;
52 auto SymbolContext = Symbol.getContexts().begin();
53 auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
54 // Match the remaining context names.
55 while (IdentiferContext != Names.rend() &&
56 SymbolContext != Symbol.getContexts().end()) {
57 if (SymbolContext->second == *IdentiferContext) {
58 ++IdentiferContext;
59 ++SymbolContext;
60 } else if (SymbolContext->first ==
61 find_all_symbols::SymbolInfo::ContextType::EnumDecl) {
62 // Skip non-scoped enum context.
63 ++SymbolContext;
64 } else {
65 IsMatched = false;
66 break;
67 }
68 }
69
Benjamin Kramer9b15b6f2016-05-19 12:41:56 +000070 // If the name was qualified we only want to add results if we evaluated
71 // all contexts.
72 if (IsFullyQualified)
73 IsMatched &= (SymbolContext == Symbol.getContexts().end());
74
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000075 // FIXME: Support full match. At this point, we only find symbols in
76 // database which end with the same contexts with the identifier.
77 if (IsMatched && IdentiferContext == Names.rend()) {
78 // FIXME: file path should never be in the form of <...> or "...", but
79 // the unit test with fixed database use <...> file path, which might
80 // need to be changed.
81 // FIXME: if the file path is a system header name, we want to use
82 // angle brackets.
83 std::string FilePath = Symbol.getFilePath().str();
84 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
85 ? FilePath
86 : "\"" + FilePath + "\"");
Eric Liu692aca62016-05-04 08:22:35 +000087 }
88 }
Eric Liu692aca62016-05-04 08:22:35 +000089 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000090 Names.pop_back();
Eric Liu692aca62016-05-04 08:22:35 +000091 }
Benjamin Kramer5e6b35f2016-05-18 16:42:38 +000092
Eric Liu692aca62016-05-04 08:22:35 +000093 return Results;
94}
95
96} // namespace include_fixer
97} // namespace clang