2016-05-13 09:27:54 +00:00
|
|
|
//===-- SymbolIndexManager.cpp - Managing multiple SymbolIndices-*- C++ -*-===//
|
2016-05-04 08:22:35 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-13 09:27:54 +00:00
|
|
|
#include "SymbolIndexManager.h"
|
2016-05-04 08:22:35 +00:00
|
|
|
#include "find-all-symbols/SymbolInfo.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "include-fixer"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace include_fixer {
|
|
|
|
|
|
|
|
std::vector<std::string>
|
2016-05-13 09:27:54 +00:00
|
|
|
SymbolIndexManager::search(llvm::StringRef Identifier) const {
|
2016-05-04 08:22:35 +00:00
|
|
|
// The identifier may be fully qualified, so split it and get all the context
|
|
|
|
// names.
|
|
|
|
llvm::SmallVector<llvm::StringRef, 8> Names;
|
|
|
|
Identifier.split(Names, "::");
|
|
|
|
|
2016-05-19 12:41:56 +00:00
|
|
|
bool IsFullyQualified = false;
|
|
|
|
if (Identifier.startswith("::")) {
|
|
|
|
Names.erase(Names.begin()); // Drop first (empty) element.
|
|
|
|
IsFullyQualified = true;
|
|
|
|
}
|
|
|
|
|
2016-05-18 16:42:38 +00:00
|
|
|
// As long as we don't find a result keep stripping name parts from the end.
|
|
|
|
// This is to support nested classes which aren't recorded in the database.
|
|
|
|
// Eventually we will either hit a class (namespaces aren't in the database
|
|
|
|
// either) and can report that result.
|
|
|
|
std::vector<std::string> Results;
|
|
|
|
while (Results.empty() && !Names.empty()) {
|
|
|
|
std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
|
|
|
|
for (const auto &DB : SymbolIndices) {
|
|
|
|
auto Res = DB->search(Names.back().str());
|
|
|
|
Symbols.insert(Symbols.end(), Res.begin(), Res.end());
|
|
|
|
}
|
2016-05-04 08:22:35 +00:00
|
|
|
|
2016-05-18 16:42:38 +00:00
|
|
|
DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
|
|
|
|
<< Symbols.size() << " results...\n");
|
2016-05-04 08:22:35 +00:00
|
|
|
|
2016-05-18 16:42:38 +00:00
|
|
|
for (const auto &Symbol : Symbols) {
|
|
|
|
// Match the identifier name without qualifier.
|
|
|
|
if (Symbol.getName() == Names.back()) {
|
|
|
|
bool IsMatched = true;
|
|
|
|
auto SymbolContext = Symbol.getContexts().begin();
|
|
|
|
auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
|
|
|
|
// Match the remaining context names.
|
|
|
|
while (IdentiferContext != Names.rend() &&
|
|
|
|
SymbolContext != Symbol.getContexts().end()) {
|
|
|
|
if (SymbolContext->second == *IdentiferContext) {
|
|
|
|
++IdentiferContext;
|
|
|
|
++SymbolContext;
|
|
|
|
} else if (SymbolContext->first ==
|
|
|
|
find_all_symbols::SymbolInfo::ContextType::EnumDecl) {
|
|
|
|
// Skip non-scoped enum context.
|
|
|
|
++SymbolContext;
|
|
|
|
} else {
|
|
|
|
IsMatched = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 08:22:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 12:41:56 +00:00
|
|
|
// If the name was qualified we only want to add results if we evaluated
|
|
|
|
// all contexts.
|
|
|
|
if (IsFullyQualified)
|
|
|
|
IsMatched &= (SymbolContext == Symbol.getContexts().end());
|
|
|
|
|
2016-05-18 16:42:38 +00:00
|
|
|
// FIXME: Support full match. At this point, we only find symbols in
|
|
|
|
// database which end with the same contexts with the identifier.
|
|
|
|
if (IsMatched && IdentiferContext == Names.rend()) {
|
|
|
|
// FIXME: file path should never be in the form of <...> or "...", but
|
|
|
|
// the unit test with fixed database use <...> file path, which might
|
|
|
|
// need to be changed.
|
|
|
|
// FIXME: if the file path is a system header name, we want to use
|
|
|
|
// angle brackets.
|
|
|
|
std::string FilePath = Symbol.getFilePath().str();
|
|
|
|
Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
|
|
|
|
? FilePath
|
|
|
|
: "\"" + FilePath + "\"");
|
|
|
|
}
|
2016-05-04 08:22:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-18 16:42:38 +00:00
|
|
|
Names.pop_back();
|
2016-05-04 08:22:35 +00:00
|
|
|
}
|
2016-05-18 16:42:38 +00:00
|
|
|
|
2016-05-04 08:22:35 +00:00
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace include_fixer
|
|
|
|
} // namespace clang
|