clang-tools  15.0.0git
YamlSymbolIndex.cpp
Go to the documentation of this file.
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 
19 
20 namespace clang {
21 namespace include_fixer {
22 
23 llvm::ErrorOr<std::unique_ptr<YamlSymbolIndex>>
24 YamlSymbolIndex::createFromFile(llvm::StringRef FilePath) {
25  auto Buffer = llvm::MemoryBuffer::getFile(FilePath);
26  if (!Buffer)
27  return Buffer.getError();
28 
29  return std::unique_ptr<YamlSymbolIndex>(new YamlSymbolIndex(
30  find_all_symbols::ReadSymbolInfosFromYAML(Buffer.get()->getBuffer())));
31 }
32 
33 llvm::ErrorOr<std::unique_ptr<YamlSymbolIndex>>
35  llvm::StringRef Name) {
36  // Walk upwards from Directory, looking for files.
37  for (llvm::SmallString<128> PathStorage = Directory; !Directory.empty();
38  Directory = llvm::sys::path::parent_path(Directory)) {
39  assert(Directory.size() <= PathStorage.size());
40  PathStorage.resize(Directory.size()); // Shrink to parent.
41  llvm::sys::path::append(PathStorage, Name);
42  if (auto DB = createFromFile(PathStorage))
43  return DB;
44  }
45  return llvm::make_error_code(llvm::errc::no_such_file_or_directory);
46 }
47 
48 std::vector<SymbolAndSignals>
49 YamlSymbolIndex::search(llvm::StringRef Identifier) {
50  std::vector<SymbolAndSignals> Results;
51  for (const auto &Symbol : Symbols) {
52  if (Symbol.Symbol.getName() == Identifier)
53  Results.push_back(Symbol);
54  }
55  return Results;
56 }
57 
58 } // namespace include_fixer
59 } // namespace clang
clang::find_all_symbols::SymbolAndSignals
Definition: SymbolInfo.h:126
clang::include_fixer::YamlSymbolIndex::search
std::vector< find_all_symbols::SymbolAndSignals > search(llvm::StringRef Identifier) override
Search for all SymbolInfos corresponding to an identifier.
Definition: YamlSymbolIndex.cpp:49
Directory
llvm::StringRef Directory
Definition: Serialization.cpp:420
Results
std::vector< CodeCompletionResult > Results
Definition: CodeComplete.cpp:784
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::find_all_symbols::ReadSymbolInfosFromYAML
std::vector< SymbolAndSignals > ReadSymbolInfosFromYAML(llvm::StringRef Yaml)
Read SymbolInfos from a YAML document.
Definition: SymbolInfo.cpp:126
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::include_fixer::YamlSymbolIndex::createFromDirectory
static llvm::ErrorOr< std::unique_ptr< YamlSymbolIndex > > createFromDirectory(llvm::StringRef Directory, llvm::StringRef Name)
Look for a file called Name in Directory and all parent directories.
Definition: YamlSymbolIndex.cpp:34
clang::include_fixer::YamlSymbolIndex::createFromFile
static llvm::ErrorOr< std::unique_ptr< YamlSymbolIndex > > createFromFile(llvm::StringRef FilePath)
Create a new Yaml db from a file.
Definition: YamlSymbolIndex.cpp:24
clang::include_fixer::YamlSymbolIndex
Yaml format database.
Definition: YamlSymbolIndex.h:22
YamlSymbolIndex.h