[include-fixer] Also look up prefixes of queries.

This is used to find nested classes. For a nested name foo::bar::qux we
will first look up foo::bar::qux, then foo::bar, then foo unless we find
a result. This is used to support nested classes which are not part of
the index but can only be used if the header for the parent context is
included.

Differential Revision: http://reviews.llvm.org/D20372

llvm-svn: 269956
This commit is contained in:
Benjamin Kramer 2016-05-18 16:42:38 +00:00
parent 350b29862f
commit 5e6b35f4d1
2 changed files with 50 additions and 45 deletions

View File

@ -24,6 +24,12 @@ SymbolIndexManager::search(llvm::StringRef Identifier) const {
llvm::SmallVector<llvm::StringRef, 8> Names; llvm::SmallVector<llvm::StringRef, 8> Names;
Identifier.split(Names, "::"); Identifier.split(Names, "::");
// 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; std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
for (const auto &DB : SymbolIndices) { for (const auto &DB : SymbolIndices) {
auto Res = DB->search(Names.back().str()); auto Res = DB->search(Names.back().str());
@ -33,13 +39,12 @@ SymbolIndexManager::search(llvm::StringRef Identifier) const {
DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got " DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
<< Symbols.size() << " results...\n"); << Symbols.size() << " results...\n");
std::vector<std::string> Results;
for (const auto &Symbol : Symbols) { for (const auto &Symbol : Symbols) {
// Match the identifier name without qualifier. // Match the identifier name without qualifier.
if (Symbol.getName() == Names.back()) { if (Symbol.getName() == Names.back()) {
bool IsMatched = true; bool IsMatched = true;
auto SymbolContext = Symbol.getContexts().begin(); auto SymbolContext = Symbol.getContexts().begin();
auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name; auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
// Match the remaining context names. // Match the remaining context names.
while (IdentiferContext != Names.rend() && while (IdentiferContext != Names.rend() &&
SymbolContext != Symbol.getContexts().end()) { SymbolContext != Symbol.getContexts().end()) {
@ -62,8 +67,8 @@ SymbolIndexManager::search(llvm::StringRef Identifier) const {
// FIXME: file path should never be in the form of <...> or "...", but // FIXME: file path should never be in the form of <...> or "...", but
// the unit test with fixed database use <...> file path, which might // the unit test with fixed database use <...> file path, which might
// need to be changed. // need to be changed.
// FIXME: if the file path is a system header name, we want to use angle // FIXME: if the file path is a system header name, we want to use
// brackets. // angle brackets.
std::string FilePath = Symbol.getFilePath().str(); std::string FilePath = Symbol.getFilePath().str();
Results.push_back((FilePath[0] == '"' || FilePath[0] == '<') Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
? FilePath ? FilePath
@ -71,6 +76,9 @@ SymbolIndexManager::search(llvm::StringRef Identifier) const {
} }
} }
} }
Names.pop_back();
}
return Results; return Results;
} }

View File

@ -55,9 +55,6 @@ static std::string runIncludeFixer(
{{SymbolInfo::ContextType::Namespace, "std"}}), {{SymbolInfo::ContextType::Namespace, "std"}}),
SymbolInfo("sting", SymbolInfo::SymbolKind::Class, "\"sting\"", 1, SymbolInfo("sting", SymbolInfo::SymbolKind::Class, "\"sting\"", 1,
{{SymbolInfo::ContextType::Namespace, "std"}}), {{SymbolInfo::ContextType::Namespace, "std"}}),
SymbolInfo("size_type", SymbolInfo::SymbolKind::Variable, "<string>", 1,
{{SymbolInfo::ContextType::Namespace, "string"},
{SymbolInfo::ContextType::Namespace, "std"}}),
SymbolInfo("foo", SymbolInfo::SymbolKind::Class, "\"dir/otherdir/qux.h\"", SymbolInfo("foo", SymbolInfo::SymbolKind::Class, "\"dir/otherdir/qux.h\"",
1, {{SymbolInfo::ContextType::Namespace, "b"}, 1, {{SymbolInfo::ContextType::Namespace, "b"},
{SymbolInfo::ContextType::Namespace, "a"}}), {SymbolInfo::ContextType::Namespace, "a"}}),