Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Remarks/RemarkParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- RemarkParser.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
// This file provides utility methods used by clients that want to use the
10
// parser for remark diagnostics in LLVM.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Remarks/RemarkParser.h"
15
#include "YAMLRemarkParser.h"
16
#include "llvm-c/Remarks.h"
17
#include "llvm/ADT/STLExtras.h"
18
#include "llvm/Support/CBindingWrapping.h"
19
20
using namespace llvm;
21
using namespace llvm::remarks;
22
23
char EndOfFileError::ID = 0;
24
25
4
ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
26
18
  while (!InBuffer.empty()) {
27
14
    // Strings are separated by '\0' bytes.
28
14
    std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
29
14
    // We only store the offset from the beginning of the buffer.
30
14
    Offsets.push_back(Split.first.data() - Buffer.data());
31
14
    InBuffer = Split.second;
32
14
  }
33
4
}
34
35
15
Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
36
15
  if (Index >= Offsets.size())
37
2
    return createStringError(
38
2
        std::make_error_code(std::errc::invalid_argument),
39
2
        "String with index %u is out of bounds (size = %u).", Index,
40
2
        Offsets.size());
41
13
42
13
  size_t Offset = Offsets[Index];
43
13
  // If it's the last offset, we can't use the next offset to know the size of
44
13
  // the string.
45
13
  size_t NextOffset =
46
13
      (Index == Offsets.size() - 1) ? 
Buffer.size()2
:
Offsets[Index + 1]11
;
47
13
  return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
48
13
}
49
50
Expected<std::unique_ptr<Parser>>
51
49
llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) {
52
49
  switch (ParserFormat) {
53
49
  case Format::YAML:
54
49
    return llvm::make_unique<YAMLRemarkParser>(Buf);
55
49
  case Format::YAMLStrTab:
56
0
    return createStringError(
57
0
        std::make_error_code(std::errc::invalid_argument),
58
0
        "The YAML with string table format requires a parsed string table.");
59
49
  case Format::Unknown:
60
0
    return createStringError(std::make_error_code(std::errc::invalid_argument),
61
0
                             "Unknown remark parser format.");
62
49
  }
63
49
}
64
65
Expected<std::unique_ptr<Parser>>
66
llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf,
67
2
                                  ParsedStringTable StrTab) {
68
2
  switch (ParserFormat) {
69
2
  case Format::YAML:
70
0
    return createStringError(std::make_error_code(std::errc::invalid_argument),
71
0
                             "The YAML format can't be used with a string "
72
0
                             "table. Use yaml-strtab instead.");
73
2
  case Format::YAMLStrTab:
74
2
    return llvm::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab));
75
2
  case Format::Unknown:
76
0
    return createStringError(std::make_error_code(std::errc::invalid_argument),
77
0
                             "Unknown remark parser format.");
78
2
  }
79
2
}
80
81
// Wrapper that holds the state needed to interact with the C API.
82
struct CParser {
83
  std::unique_ptr<Parser> TheParser;
84
  Optional<std::string> Err;
85
86
  CParser(Format ParserFormat, StringRef Buf,
87
          Optional<ParsedStringTable> StrTab = None)
88
      : TheParser(cantFail(
89
            StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
90
1
                   : createRemarkParser(ParserFormat, Buf))) {}
91
92
0
  void handleError(Error E) { Err.emplace(toString(std::move(E))); }
93
1
  bool hasError() const { return Err.hasValue(); }
94
0
  const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
95
};
96
97
// Create wrappers for C Binding types (see CBindingWrapping.h).
98
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef)
99
100
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
101
1
                                                          uint64_t Size) {
102
1
  return wrap(new CParser(Format::YAML,
103
1
                          StringRef(static_cast<const char *>(Buf), Size)));
104
1
}
105
106
extern "C" LLVMRemarkEntryRef
107
2
LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
108
2
  CParser &TheCParser = *unwrap(Parser);
109
2
  remarks::Parser &TheParser = *TheCParser.TheParser;
110
2
111
2
  Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
112
2
  if (Error E = MaybeRemark.takeError()) {
113
1
    if (E.isA<EndOfFileError>()) {
114
1
      consumeError(std::move(E));
115
1
      return nullptr;
116
1
    }
117
0
118
0
    // Handle the error. Allow it to be checked through HasError and
119
0
    // GetErrorMessage.
120
0
    TheCParser.handleError(std::move(E));
121
0
    return nullptr;
122
0
  }
123
1
124
1
  // Valid remark.
125
1
  return wrap(MaybeRemark->release());
126
1
}
127
128
1
extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
129
1
  return unwrap(Parser)->hasError();
130
1
}
131
132
extern "C" const char *
133
0
LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
134
0
  return unwrap(Parser)->getMessage();
135
0
}
136
137
1
extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
138
1
  delete unwrap(Parser);
139
1
}