summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/include-fixer/YamlXrefsDB.cpp
blob: 6d6590f8d908f0227fd4489c438b013231af7e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//===-- YamlXrefsDB.cpp ---------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "YamlXrefsDB.h"

#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include <string>
#include <vector>

namespace clang {
namespace include_fixer {

YamlXrefsDB::YamlXrefsDB(llvm::StringRef FilePath) {
  int ReadFD = 0;
  if (llvm::sys::fs::openFileForRead(FilePath, ReadFD))
    return;
  auto Buffer = llvm::MemoryBuffer::getOpenFile(ReadFD, FilePath, -1);
  if (!Buffer)
    return;
  Symbols = clang::find_all_symbols::ReadSymbolInfosFromYAML(
      Buffer.get()->getBuffer());
}

std::vector<std::string> YamlXrefsDB::search(llvm::StringRef Identifier) {
  llvm::SmallVector<llvm::StringRef, 16> Names;
  std::vector<std::string> Results;

  // The identifier may be fully qualified, so split it and get all the context
  // names.
  Identifier.split(Names, "::");
  for (const auto &Symbol : Symbols) {
    // Match the identifier name without qualifier.
    if (Symbol.Name == Names.back()) {
      bool IsMatched = true;
      auto SymbolContext = Symbol.Contexts.begin();
      // Match the remaining context names.
      for (auto IdentiferContext = Names.rbegin() + 1;
           IdentiferContext != Names.rend() &&
           SymbolContext != Symbol.Contexts.end();
           ++IdentiferContext, ++SymbolContext) {
        if (SymbolContext->second != *IdentiferContext) {
          IsMatched = false;
          break;
        }
      }

      if (IsMatched) {
        Results.push_back("\"" + Symbol.FilePath + "\"");
      }
    }
  }
  return Results;
}

} // namespace include_fixer
} // namespace clang
OpenPOWER on IntegriCloud