D34444: Teach codegen to work in incremental processing mode.
[llvm-project.git] / clang-tools-extra / include-fixer / YamlSymbolIndex.cpp
blobf3f2d5a8955ebd48e446854d1685d7da9f8d8ccf
1 //===-- YamlSymbolIndex.cpp -----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
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"
16 #include <string>
17 #include <vector>
19 using clang::find_all_symbols::SymbolInfo;
20 using clang::find_all_symbols::SymbolAndSignals;
22 namespace clang {
23 namespace include_fixer {
25 llvm::ErrorOr<std::unique_ptr<YamlSymbolIndex>>
26 YamlSymbolIndex::createFromFile(llvm::StringRef FilePath) {
27 auto Buffer = llvm::MemoryBuffer::getFile(FilePath);
28 if (!Buffer)
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))
45 return DB;
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);
57 return Results;
60 } // namespace include_fixer
61 } // namespace clang