Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/IRReader/IRReader.cpp
Line
Count
Source (jump to first uncovered line)
1
//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
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 "llvm/IRReader/IRReader.h"
10
#include "llvm-c/IRReader.h"
11
#include "llvm/AsmParser/Parser.h"
12
#include "llvm/Bitcode/BitcodeReader.h"
13
#include "llvm/IR/LLVMContext.h"
14
#include "llvm/IR/Module.h"
15
#include "llvm/Support/MemoryBuffer.h"
16
#include "llvm/Support/SourceMgr.h"
17
#include "llvm/Support/Timer.h"
18
#include "llvm/Support/raw_ostream.h"
19
#include <system_error>
20
21
using namespace llvm;
22
23
namespace llvm {
24
  extern bool TimePassesIsEnabled;
25
}
26
27
static const char *const TimeIRParsingGroupName = "irparse";
28
static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
29
static const char *const TimeIRParsingName = "parse";
30
static const char *const TimeIRParsingDescription = "Parse IR";
31
32
std::unique_ptr<Module>
33
llvm::getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
34
550
                      LLVMContext &Context, bool ShouldLazyLoadMetadata) {
35
550
  if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
36
550
                (const unsigned char *)Buffer->getBufferEnd())) {
37
181
    Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
38
181
        std::move(Buffer), Context, ShouldLazyLoadMetadata);
39
181
    if (Error E = ModuleOrErr.takeError()) {
40
0
      handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
41
0
        Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
42
0
                           EIB.message());
43
0
      });
44
0
      return nullptr;
45
0
    }
46
181
    return std::move(ModuleOrErr.get());
47
181
  }
48
369
49
369
  return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
50
369
}
51
52
std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
53
                                                  SMDiagnostic &Err,
54
                                                  LLVMContext &Context,
55
550
                                                  bool ShouldLazyLoadMetadata) {
56
550
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
57
550
      MemoryBuffer::getFileOrSTDIN(Filename);
58
550
  if (std::error_code EC = FileOrErr.getError()) {
59
0
    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
60
0
                       "Could not open input file: " + EC.message());
61
0
    return nullptr;
62
0
  }
63
550
64
550
  return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
65
550
                         ShouldLazyLoadMetadata);
66
550
}
67
68
std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
69
                                      LLVMContext &Context,
70
                                      bool UpgradeDebugInfo,
71
38.5k
                                      StringRef DataLayoutString) {
72
38.5k
  NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
73
38.5k
                     TimeIRParsingGroupName, TimeIRParsingGroupDescription,
74
38.5k
                     TimePassesIsEnabled);
75
38.5k
  if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
76
38.5k
                (const unsigned char *)Buffer.getBufferEnd())) {
77
4.46k
    Expected<std::unique_ptr<Module>> ModuleOrErr =
78
4.46k
        parseBitcodeFile(Buffer, Context);
79
4.46k
    if (Error E = ModuleOrErr.takeError()) {
80
0
      handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
81
0
        Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
82
0
                           EIB.message());
83
0
      });
84
0
      return nullptr;
85
0
    }
86
4.46k
    if (!DataLayoutString.empty())
87
1
      ModuleOrErr.get()->setDataLayout(DataLayoutString);
88
4.46k
    return std::move(ModuleOrErr.get());
89
4.46k
  }
90
34.0k
91
34.0k
  return parseAssembly(Buffer, Err, Context, nullptr, UpgradeDebugInfo,
92
34.0k
                       DataLayoutString);
93
34.0k
}
94
95
std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
96
                                          LLVMContext &Context,
97
                                          bool UpgradeDebugInfo,
98
34.3k
                                          StringRef DataLayoutString) {
99
34.3k
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
100
34.3k
      MemoryBuffer::getFileOrSTDIN(Filename);
101
34.3k
  if (std::error_code EC = FileOrErr.getError()) {
102
2
    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
103
2
                       "Could not open input file: " + EC.message());
104
2
    return nullptr;
105
2
  }
106
34.3k
107
34.3k
  return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
108
34.3k
                 UpgradeDebugInfo, DataLayoutString);
109
34.3k
}
110
111
//===----------------------------------------------------------------------===//
112
// C API.
113
//===----------------------------------------------------------------------===//
114
115
LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
116
                              LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
117
0
                              char **OutMessage) {
118
0
  SMDiagnostic Diag;
119
0
120
0
  std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
121
0
  *OutM =
122
0
      wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
123
0
124
0
  if(!*OutM) {
125
0
    if (OutMessage) {
126
0
      std::string buf;
127
0
      raw_string_ostream os(buf);
128
0
129
0
      Diag.print(nullptr, os, false);
130
0
      os.flush();
131
0
132
0
      *OutMessage = strdup(buf.c_str());
133
0
    }
134
0
    return 1;
135
0
  }
136
0
137
0
  return 0;
138
0
}