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 | |
| 27 | std::vector<clang::find_all_symbols::SymbolInfo> Symbols; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame^] | 28 | for (const auto &DB : SymbolIndices) { |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 29 | 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 Wu | 1a352d5 | 2016-05-11 08:38:21 +0000 | [diff] [blame] | 39 | if (Symbol.getName() == Names.back()) { |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 40 | bool IsMatched = true; |
Haojian Wu | 1a352d5 | 2016-05-11 08:38:21 +0000 | [diff] [blame] | 41 | auto SymbolContext = Symbol.getContexts().begin(); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 42 | // Match the remaining context names. |
| 43 | for (auto IdentiferContext = Names.rbegin() + 1; |
| 44 | IdentiferContext != Names.rend() && |
Haojian Wu | 1a352d5 | 2016-05-11 08:38:21 +0000 | [diff] [blame] | 45 | SymbolContext != Symbol.getContexts().end(); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 46 | ++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 Wu | 1a352d5 | 2016-05-11 08:38:21 +0000 | [diff] [blame] | 59 | std::string FilePath = Symbol.getFilePath().str(); |
| 60 | Results.push_back((FilePath[0] == '"' || FilePath[0] == '<') |
| 61 | ? FilePath |
| 62 | : "\"" + FilePath + "\""); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | return Results; |
| 67 | } |
| 68 | |
| 69 | } // namespace include_fixer |
| 70 | } // namespace clang |