[BuildLibcalls, Attrs] Support more variants of C++'s new, add attributes for C+...
[lldb.git] / clang-tools-extra / clang-include-fixer / YamlSymbolIndex.cpp
1 //===-- YamlSymbolIndex.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "YamlSymbolIndex.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/Support/Errc.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/Path.h"
15 #include <string>
16 #include <vector>
17
18 using clang::find_all_symbols::SymbolInfo;
19 using clang::find_all_symbols::SymbolAndSignals;
20
21 namespace clang {
22 namespace include_fixer {
23
24 llvm::ErrorOr<std::unique_ptr<YamlSymbolIndex>>
25 YamlSymbolIndex::createFromFile(llvm::StringRef FilePath) {
26   auto Buffer = llvm::MemoryBuffer::getFile(FilePath);
27   if (!Buffer)
28     return Buffer.getError();
29
30   return std::unique_ptr<YamlSymbolIndex>(new YamlSymbolIndex(
31       find_all_symbols::ReadSymbolInfosFromYAML(Buffer.get()->getBuffer())));
32 }
33
34 llvm::ErrorOr<std::unique_ptr<YamlSymbolIndex>>
35 YamlSymbolIndex::createFromDirectory(llvm::StringRef Directory,
36                                      llvm::StringRef Name) {
37   // Walk upwards from Directory, looking for files.
38   for (llvm::SmallString<128> PathStorage = Directory; !Directory.empty();
39        Directory = llvm::sys::path::parent_path(Directory)) {
40     assert(Directory.size() <= PathStorage.size());
41     PathStorage.resize(Directory.size()); // Shrink to parent.
42     llvm::sys::path::append(PathStorage, Name);
43     if (auto DB = createFromFile(PathStorage))
44       return DB;
45   }
46   return llvm::make_error_code(llvm::errc::no_such_file_or_directory);
47 }
48
49 std::vector<SymbolAndSignals>
50 YamlSymbolIndex::search(llvm::StringRef Identifier) {
51   std::vector<SymbolAndSignals> Results;
52   for (const auto &Symbol : Symbols) {
53     if (Symbol.Symbol.getName() == Identifier)
54       Results.push_back(Symbol);
55   }
56   return Results;
57 }
58
59 } // namespace include_fixer
60 } // namespace clang