clang-tools  7.0.0
YamlSymbolIndex.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
9 
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>
18 
21 
22 namespace clang {
23 namespace include_fixer {
24 
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();
30 
31  return std::unique_ptr<YamlSymbolIndex>(new YamlSymbolIndex(
32  find_all_symbols::ReadSymbolInfosFromYAML(Buffer.get()->getBuffer())));
33 }
34 
35 llvm::ErrorOr<std::unique_ptr<YamlSymbolIndex>>
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;
46  }
47  return llvm::make_error_code(llvm::errc::no_such_file_or_directory);
48 }
49 
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);
56  }
57  return Results;
58 }
59 
60 } // namespace include_fixer
61 } // namespace clang
llvm::StringRef Name
std::vector< CodeCompletionResult > Results
std::vector< SymbolAndSignals > ReadSymbolInfosFromYAML(llvm::StringRef Yaml)
Read SymbolInfos from a YAML document.
Definition: SymbolInfo.cpp:129
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
clang::find_all_symbols::SymbolInfo SymbolInfo
std::vector< find_all_symbols::SymbolAndSignals > search(llvm::StringRef Identifier) override
Search for all SymbolInfos corresponding to an identifier.
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.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static llvm::ErrorOr< std::unique_ptr< YamlSymbolIndex > > createFromFile(llvm::StringRef FilePath)
Create a new Yaml db from a file.