blob: 4d76a86d3a9ffbcfafca6919285995f2e7dca748 [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
27 std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
Benjamin Kramera3d82332016-05-13 09:27:54 +000028 for (const auto &DB : SymbolIndices) {
Eric Liu692aca62016-05-04 08:22:35 +000029 auto Res = DB->search(Names.back().str());
30 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
31 }
32
33 DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
34 << Symbols.size() << " results...\n");
35
36 std::vector<std::string> Results;
37 for (const auto &Symbol : Symbols) {
38 // Match the identifier name without qualifier.
Haojian Wu1a352d52016-05-11 08:38:21 +000039 if (Symbol.getName() == Names.back()) {
Eric Liu692aca62016-05-04 08:22:35 +000040 bool IsMatched = true;
Haojian Wu1a352d52016-05-11 08:38:21 +000041 auto SymbolContext = Symbol.getContexts().begin();
Eric Liu692aca62016-05-04 08:22:35 +000042 // Match the remaining context names.
43 for (auto IdentiferContext = Names.rbegin() + 1;
44 IdentiferContext != Names.rend() &&
Haojian Wu1a352d52016-05-11 08:38:21 +000045 SymbolContext != Symbol.getContexts().end();
Eric Liu692aca62016-05-04 08:22:35 +000046 ++IdentiferContext, ++SymbolContext) {
47 if (SymbolContext->second != *IdentiferContext) {
48 IsMatched = false;
49 break;
50 }
51 }
52
53 if (IsMatched) {
54 // FIXME: file path should never be in the form of <...> or "...", but
55 // the unit test with fixed database use <...> file path, which might
56 // need to be changed.
57 // FIXME: if the file path is a system header name, we want to use angle
58 // brackets.
Haojian Wu1a352d52016-05-11 08:38:21 +000059 std::string FilePath = Symbol.getFilePath().str();
60 Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
61 ? FilePath
62 : "\"" + FilePath + "\"");
Eric Liu692aca62016-05-04 08:22:35 +000063 }
64 }
65 }
66 return Results;
67}
68
69} // namespace include_fixer
70} // namespace clang