Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/lib/Core/Reader.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/Core/Reader.cpp ------------------------------------------------===//
2
//
3
//                             The LLVM Linker
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 "lld/Core/Reader.h"
11
#include "lld/Core/File.h"
12
#include "lld/Core/Reference.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/BinaryFormat/Magic.h"
15
#include "llvm/Support/Errc.h"
16
#include "llvm/Support/FileSystem.h"
17
#include "llvm/Support/MemoryBuffer.h"
18
#include <algorithm>
19
#include <memory>
20
21
using llvm::file_magic;
22
using llvm::identify_magic;
23
24
namespace lld {
25
26
669
YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() = default;
27
28
892
void Registry::add(std::unique_ptr<Reader> reader) {
29
892
  _readers.push_back(std::move(reader));
30
892
}
31
32
669
void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) {
33
669
  _yamlHandlers.push_back(std::move(handler));
34
669
}
35
36
ErrorOr<std::unique_ptr<File>>
37
323
Registry::loadFile(std::unique_ptr<MemoryBuffer> mb) const {
38
323
  // Get file magic.
39
323
  StringRef content(mb->getBufferStart(), mb->getBufferSize());
40
323
  file_magic fileType = identify_magic(content);
41
323
42
323
  // Ask each registered reader if it can handle this file type or extension.
43
1.18k
  for (const std::unique_ptr<Reader> &reader : _readers) {
44
1.18k
    if (!reader->canParse(fileType, mb->getMemBufferRef()))
45
867
      continue;
46
322
    return reader->loadFile(std::move(mb), *this);
47
322
  }
48
1
49
1
  // No Reader could parse this file.
50
1
  return make_error_code(llvm::errc::executable_format_error);
51
1
}
52
53
static const Registry::KindStrings kindStrings[] = {
54
    {Reference::kindLayoutAfter, "layout-after"},
55
    {Reference::kindAssociate, "associate"},
56
    LLD_KIND_STRING_END};
57
58
242
Registry::Registry() {
59
242
  addKindTable(Reference::KindNamespace::all, Reference::KindArch::all,
60
242
               kindStrings);
61
242
}
62
63
bool Registry::handleTaggedDoc(llvm::yaml::IO &io,
64
341
                               const lld::File *&file) const {
65
341
  for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers)
66
492
    
if (492
h->handledDocTag(io, file)492
)
67
272
      return true;
68
69
  return false;
69
69
}
70
71
void Registry::addKindTable(Reference::KindNamespace ns,
72
                            Reference::KindArch arch,
73
465
                            const KindStrings array[]) {
74
465
  KindEntry entry = { ns, arch, array };
75
465
  _kindEntries.push_back(entry);
76
465
}
77
78
bool Registry::referenceKindFromString(StringRef inputStr,
79
                                       Reference::KindNamespace &ns,
80
                                       Reference::KindArch &arch,
81
18
                                       Reference::KindValue &value) const {
82
36
  for (const KindEntry &entry : _kindEntries) {
83
373
    for (const KindStrings *pair = entry.array; 
!pair->name.empty()373
;
++pair337
) {
84
355
      if (!inputStr.equals(pair->name))
85
337
        continue;
86
18
      ns = entry.ns;
87
18
      arch = entry.arch;
88
18
      value = pair->value;
89
18
      return true;
90
18
    }
91
36
  }
92
0
  return false;
93
18
}
94
95
bool Registry::referenceKindToString(Reference::KindNamespace ns,
96
                                     Reference::KindArch arch,
97
                                     Reference::KindValue value,
98
442
                                     StringRef &str) const {
99
880
  for (const KindEntry &entry : _kindEntries) {
100
880
    if (entry.ns != ns)
101
438
      continue;
102
442
    
if (442
entry.arch != arch442
)
103
0
      continue;
104
4.85k
    
for (const KindStrings *pair = entry.array; 442
!pair->name.empty()4.85k
;
++pair4.41k
) {
105
4.85k
      if (pair->value != value)
106
4.41k
        continue;
107
442
      str = pair->name;
108
442
      return true;
109
442
    }
110
880
  }
111
0
  return false;
112
442
}
113
114
} // end namespace lld