Coverage Report

Created: 2018-08-19 14:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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
672
YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() = default;
27
28
896
void Registry::add(std::unique_ptr<Reader> reader) {
29
896
  _readers.push_back(std::move(reader));
30
896
}
31
32
672
void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) {
33
672
  _yamlHandlers.push_back(std::move(handler));
34
672
}
35
36
ErrorOr<std::unique_ptr<File>>
37
324
Registry::loadFile(std::unique_ptr<MemoryBuffer> mb) const {
38
324
  // Get file magic.
39
324
  StringRef content(mb->getBufferStart(), mb->getBufferSize());
40
324
  file_magic fileType = identify_magic(content);
41
324
42
324
  // Ask each registered reader if it can handle this file type or extension.
43
1.19k
  for (const std::unique_ptr<Reader> &reader : _readers) {
44
1.19k
    if (!reader->canParse(fileType, mb->getMemBufferRef()))
45
870
      continue;
46
323
    return reader->loadFile(std::move(mb), *this);
47
323
  }
48
324
49
324
  // No Reader could parse this file.
50
324
  
return make_error_code(llvm::errc::executable_format_error)1
;
51
324
}
52
53
static const Registry::KindStrings kindStrings[] = {
54
    {Reference::kindLayoutAfter, "layout-after"},
55
    {Reference::kindAssociate, "associate"},
56
    LLD_KIND_STRING_END};
57
58
243
Registry::Registry() {
59
243
  addKindTable(Reference::KindNamespace::all, Reference::KindArch::all,
60
243
               kindStrings);
61
243
}
62
63
bool Registry::handleTaggedDoc(llvm::yaml::IO &io,
64
342
                               const lld::File *&file) const {
65
342
  for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers)
66
494
    if (h->handledDocTag(io, file))
67
273
      return true;
68
342
  
return false69
;
69
342
}
70
71
void Registry::addKindTable(Reference::KindNamespace ns,
72
                            Reference::KindArch arch,
73
467
                            const KindStrings array[]) {
74
467
  KindEntry entry = { ns, arch, array };
75
467
  _kindEntries.push_back(entry);
76
467
}
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(); 
++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
18
  
return false0
;
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 (entry.arch != arch)
103
0
      continue;
104
4.85k
    
for (const KindStrings *pair = entry.array; 442
!pair->name.empty();
++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
442
  }
111
442
  
return false0
;
112
442
}
113
114
} // end namespace lld