Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 1 | //===-- SymbolIndexManager.cpp - Managing multiple SymbolIndices-*- C++ -*-===// |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 2 | // |
| 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 Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 10 | #include "SymbolIndexManager.h" |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 11 | #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 | |
| 17 | namespace clang { |
| 18 | namespace include_fixer { |
| 19 | |
| 20 | std::vector<std::string> |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 21 | SymbolIndexManager::search(llvm::StringRef Identifier) const { |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 22 | // 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 Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame^] | 27 | // As long as we don't find a result keep stripping name parts from the end. |
| 28 | // This is to support nested classes which aren't recorded in the database. |
| 29 | // Eventually we will either hit a class (namespaces aren't in the database |
| 30 | // either) and can report that result. |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 31 | std::vector<std::string> Results; |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame^] | 32 | while (Results.empty() && !Names.empty()) { |
| 33 | std::vector<clang::find_all_symbols::SymbolInfo> Symbols; |
| 34 | for (const auto &DB : SymbolIndices) { |
| 35 | auto Res = DB->search(Names.back().str()); |
| 36 | Symbols.insert(Symbols.end(), Res.begin(), Res.end()); |
| 37 | } |
| 38 | |
| 39 | DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got " |
| 40 | << Symbols.size() << " results...\n"); |
| 41 | |
| 42 | for (const auto &Symbol : Symbols) { |
| 43 | // Match the identifier name without qualifier. |
| 44 | if (Symbol.getName() == Names.back()) { |
| 45 | bool IsMatched = true; |
| 46 | auto SymbolContext = Symbol.getContexts().begin(); |
| 47 | auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name. |
| 48 | // Match the remaining context names. |
| 49 | while (IdentiferContext != Names.rend() && |
| 50 | SymbolContext != Symbol.getContexts().end()) { |
| 51 | if (SymbolContext->second == *IdentiferContext) { |
| 52 | ++IdentiferContext; |
| 53 | ++SymbolContext; |
| 54 | } else if (SymbolContext->first == |
| 55 | find_all_symbols::SymbolInfo::ContextType::EnumDecl) { |
| 56 | // Skip non-scoped enum context. |
| 57 | ++SymbolContext; |
| 58 | } else { |
| 59 | IsMatched = false; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // FIXME: Support full match. At this point, we only find symbols in |
| 65 | // database which end with the same contexts with the identifier. |
| 66 | if (IsMatched && IdentiferContext == Names.rend()) { |
| 67 | // FIXME: file path should never be in the form of <...> or "...", but |
| 68 | // the unit test with fixed database use <...> file path, which might |
| 69 | // need to be changed. |
| 70 | // FIXME: if the file path is a system header name, we want to use |
| 71 | // angle brackets. |
| 72 | std::string FilePath = Symbol.getFilePath().str(); |
| 73 | Results.push_back((FilePath[0] == '"' || FilePath[0] == '<') |
| 74 | ? FilePath |
| 75 | : "\"" + FilePath + "\""); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 78 | } |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame^] | 79 | Names.pop_back(); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 80 | } |
Benjamin Kramer | 5e6b35f | 2016-05-18 16:42:38 +0000 | [diff] [blame^] | 81 | |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 82 | return Results; |
| 83 | } |
| 84 | |
| 85 | } // namespace include_fixer |
| 86 | } // namespace clang |