Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/AsmParser/Parser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
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
// This library implements the functionality defined in llvm/AsmParser/Parser.h
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/AsmParser/Parser.h"
14
#include "LLParser.h"
15
#include "llvm/ADT/STLExtras.h"
16
#include "llvm/IR/Module.h"
17
#include "llvm/IR/ModuleSummaryIndex.h"
18
#include "llvm/Support/MemoryBuffer.h"
19
#include "llvm/Support/SourceMgr.h"
20
#include "llvm/Support/raw_ostream.h"
21
#include <cstring>
22
#include <system_error>
23
using namespace llvm;
24
25
bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M,
26
                             ModuleSummaryIndex *Index, SMDiagnostic &Err,
27
                             SlotMapping *Slots, bool UpgradeDebugInfo,
28
37.9k
                             StringRef DataLayoutString) {
29
37.9k
  SourceMgr SM;
30
37.9k
  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
31
37.9k
  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
32
37.9k
33
37.9k
  LLVMContext Context;
34
37.9k
  return LLParser(F.getBuffer(), SM, Err, M, Index,
35
37.9k
                  M ? M->getContext() : 
Context0
, Slots, UpgradeDebugInfo,
36
37.9k
                  DataLayoutString)
37
37.9k
      .Run();
38
37.9k
}
39
40
std::unique_ptr<Module>
41
llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
42
                    SlotMapping *Slots, bool UpgradeDebugInfo,
43
36.7k
                    StringRef DataLayoutString) {
44
36.7k
  std::unique_ptr<Module> M =
45
36.7k
      make_unique<Module>(F.getBufferIdentifier(), Context);
46
36.7k
47
36.7k
  if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
48
36.7k
                        DataLayoutString))
49
14
    return nullptr;
50
36.7k
51
36.7k
  return M;
52
36.7k
}
53
54
std::unique_ptr<Module>
55
llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
56
                        LLVMContext &Context, SlotMapping *Slots,
57
774
                        bool UpgradeDebugInfo, StringRef DataLayoutString) {
58
774
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
59
774
      MemoryBuffer::getFileOrSTDIN(Filename);
60
774
  if (std::error_code EC = FileOrErr.getError()) {
61
0
    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
62
0
                       "Could not open input file: " + EC.message());
63
0
    return nullptr;
64
0
  }
65
774
66
774
  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
67
774
                       UpgradeDebugInfo, DataLayoutString);
68
774
}
69
70
ParsedModuleAndIndex llvm::parseAssemblyWithIndex(
71
    MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
72
1.24k
    SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
73
1.24k
  std::unique_ptr<Module> M =
74
1.24k
      make_unique<Module>(F.getBufferIdentifier(), Context);
75
1.24k
  std::unique_ptr<ModuleSummaryIndex> Index =
76
1.24k
      make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
77
1.24k
78
1.24k
  if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
79
1.24k
                        DataLayoutString))
80
205
    return {nullptr, nullptr};
81
1.04k
82
1.04k
  return {std::move(M), std::move(Index)};
83
1.04k
}
84
85
ParsedModuleAndIndex llvm::parseAssemblyFileWithIndex(
86
    StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
87
1.24k
    SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
88
1.24k
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
89
1.24k
      MemoryBuffer::getFileOrSTDIN(Filename);
90
1.24k
  if (std::error_code EC = FileOrErr.getError()) {
91
0
    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
92
0
                       "Could not open input file: " + EC.message());
93
0
    return {nullptr, nullptr};
94
0
  }
95
1.24k
96
1.24k
  return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
97
1.24k
                                Context, Slots, UpgradeDebugInfo,
98
1.24k
                                DataLayoutString);
99
1.24k
}
100
101
std::unique_ptr<Module>
102
llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err,
103
                          LLVMContext &Context, SlotMapping *Slots,
104
386
                          bool UpgradeDebugInfo, StringRef DataLayoutString) {
105
386
  MemoryBufferRef F(AsmString, "<string>");
106
386
  return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
107
386
                       DataLayoutString);
108
386
}
109
110
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F,
111
                                          ModuleSummaryIndex &Index,
112
0
                                          SMDiagnostic &Err) {
113
0
  SourceMgr SM;
114
0
  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
115
0
  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
116
0
117
0
  // The parser holds a reference to a context that is unused when parsing the
118
0
  // index, but we need to initialize it.
119
0
  LLVMContext unusedContext;
120
0
  return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run();
121
0
}
122
123
std::unique_ptr<ModuleSummaryIndex>
124
0
llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) {
125
0
  std::unique_ptr<ModuleSummaryIndex> Index =
126
0
      make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
127
0
128
0
  if (parseSummaryIndexAssemblyInto(F, *Index, Err))
129
0
    return nullptr;
130
0
131
0
  return Index;
132
0
}
133
134
std::unique_ptr<ModuleSummaryIndex>
135
0
llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) {
136
0
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
137
0
      MemoryBuffer::getFileOrSTDIN(Filename);
138
0
  if (std::error_code EC = FileOrErr.getError()) {
139
0
    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
140
0
                       "Could not open input file: " + EC.message());
141
0
    return nullptr;
142
0
  }
143
0
144
0
  return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
145
0
}
146
147
Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
148
2.19k
                                   const Module &M, const SlotMapping *Slots) {
149
2.19k
  SourceMgr SM;
150
2.19k
  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
151
2.19k
  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
152
2.19k
  Constant *C;
153
2.19k
  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
154
2.19k
          .parseStandaloneConstantValue(C, Slots))
155
6
    return nullptr;
156
2.18k
  return C;
157
2.18k
}
158
159
Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
160
11
                      const SlotMapping *Slots) {
161
11
  unsigned Read;
162
11
  Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
163
11
  if (!Ty)
164
0
    return nullptr;
165
11
  if (Read != Asm.size()) {
166
1
    SourceMgr SM;
167
1
    std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
168
1
    SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
169
1
    Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
170
1
                        SourceMgr::DK_Error, "expected end of string");
171
1
    return nullptr;
172
1
  }
173
10
  return Ty;
174
10
}
175
Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
176
                                 SMDiagnostic &Err, const Module &M,
177
22
                                 const SlotMapping *Slots) {
178
22
  SourceMgr SM;
179
22
  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
180
22
  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
181
22
  Type *Ty;
182
22
  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
183
22
          .parseTypeAtBeginning(Ty, Read, Slots))
184
0
    return nullptr;
185
22
  return Ty;
186
22
}