Coverage Report

Created: 2019-07-24 05:18

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