[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
diff --git a/clang-tools-extra/include-fixer/SymbolIndexManager.cpp b/clang-tools-extra/include-fixer/SymbolIndexManager.cpp
index 9982a0e..3813a54 100644
--- a/clang-tools-extra/include-fixer/SymbolIndexManager.cpp
+++ b/clang-tools-extra/include-fixer/SymbolIndexManager.cpp
@@ -24,53 +24,61 @@
llvm::SmallVector<llvm::StringRef, 8> Names;
Identifier.split(Names, "::");
- 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());
- }
-
- DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
- << Symbols.size() << " results...\n");
-
+ // 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;
- 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;
+ 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());
+ }
+
+ DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
+ << Symbols.size() << " results...\n");
+
+ 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;
+ }
+ }
+
+ // 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 + "\"");
}
}
-
- // 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 + "\"");
- }
}
+ Names.pop_back();
}
+
return Results;
}