1 //===-- YamlSymbolIndex.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "YamlSymbolIndex.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/Support/Errc.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/Support/Path.h"
19 using clang::find_all_symbols::SymbolInfo
;
20 using clang::find_all_symbols::SymbolAndSignals
;
23 namespace include_fixer
{
25 llvm::ErrorOr
<std::unique_ptr
<YamlSymbolIndex
>>
26 YamlSymbolIndex::createFromFile(llvm::StringRef FilePath
) {
27 auto Buffer
= llvm::MemoryBuffer::getFile(FilePath
);
29 return Buffer
.getError();
31 return std::unique_ptr
<YamlSymbolIndex
>(new YamlSymbolIndex(
32 find_all_symbols::ReadSymbolInfosFromYAML(Buffer
.get()->getBuffer())));
35 llvm::ErrorOr
<std::unique_ptr
<YamlSymbolIndex
>>
36 YamlSymbolIndex::createFromDirectory(llvm::StringRef Directory
,
37 llvm::StringRef Name
) {
38 // Walk upwards from Directory, looking for files.
39 for (llvm::SmallString
<128> PathStorage
= Directory
; !Directory
.empty();
40 Directory
= llvm::sys::path::parent_path(Directory
)) {
41 assert(Directory
.size() <= PathStorage
.size());
42 PathStorage
.resize(Directory
.size()); // Shrink to parent.
43 llvm::sys::path::append(PathStorage
, Name
);
44 if (auto DB
= createFromFile(PathStorage
))
47 return llvm::make_error_code(llvm::errc::no_such_file_or_directory
);
50 std::vector
<SymbolAndSignals
>
51 YamlSymbolIndex::search(llvm::StringRef Identifier
) {
52 std::vector
<SymbolAndSignals
> Results
;
53 for (const auto &Symbol
: Symbols
) {
54 if (Symbol
.Symbol
.getName() == Identifier
)
55 Results
.push_back(Symbol
);
60 } // namespace include_fixer