Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Error.cpp
Line
Count
Source (jump to first uncovered line)
1
//===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
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/Support/Error.h"
10
#include "llvm/ADT/Twine.h"
11
#include "llvm/Support/ErrorHandling.h"
12
#include "llvm/Support/ManagedStatic.h"
13
#include <system_error>
14
15
using namespace llvm;
16
17
namespace {
18
19
  enum class ErrorErrorCode : int {
20
    MultipleErrors = 1,
21
    FileError,
22
    InconvertibleError
23
  };
24
25
  // FIXME: This class is only here to support the transition to llvm::Error. It
26
  // will be removed once this transition is complete. Clients should prefer to
27
  // deal with the Error value directly, rather than converting to error_code.
28
  class ErrorErrorCategory : public std::error_category {
29
  public:
30
0
    const char *name() const noexcept override { return "Error"; }
31
32
0
    std::string message(int condition) const override {
33
0
      switch (static_cast<ErrorErrorCode>(condition)) {
34
0
      case ErrorErrorCode::MultipleErrors:
35
0
        return "Multiple errors";
36
0
      case ErrorErrorCode::InconvertibleError:
37
0
        return "Inconvertible error value. An error has occurred that could "
38
0
               "not be converted to a known std::error_code. Please file a "
39
0
               "bug.";
40
0
      case ErrorErrorCode::FileError:
41
0
          return "A file error occurred.";
42
0
      }
43
0
      llvm_unreachable("Unhandled error code");
44
0
    }
45
  };
46
47
}
48
49
static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
50
51
namespace llvm {
52
53
0
void ErrorInfoBase::anchor() {}
54
char ErrorInfoBase::ID = 0;
55
char ErrorList::ID = 0;
56
0
void ECError::anchor() {}
57
char ECError::ID = 0;
58
char StringError::ID = 0;
59
char FileError::ID = 0;
60
61
1.38k
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
62
1.38k
  if (!E)
63
794
    return;
64
589
  OS << ErrorBanner;
65
631
  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
66
631
    EI.log(OS);
67
631
    OS << "\n";
68
631
  });
69
589
}
70
71
72
0
std::error_code ErrorList::convertToErrorCode() const {
73
0
  return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
74
0
                         *ErrorErrorCat);
75
0
}
76
77
556k
std::error_code inconvertibleErrorCode() {
78
556k
  return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
79
556k
                         *ErrorErrorCat);
80
556k
}
81
82
0
std::error_code FileError::convertToErrorCode() const {
83
0
  return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
84
0
                         *ErrorErrorCat);
85
0
}
86
87
532k
Error errorCodeToError(std::error_code EC) {
88
532k
  if (!EC)
89
12.9k
    return Error::success();
90
519k
  return Error(llvm::make_unique<ECError>(ECError(EC)));
91
519k
}
92
93
524k
std::error_code errorToErrorCode(Error Err) {
94
524k
  std::error_code EC;
95
524k
  handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
96
517k
    EC = EI.convertToErrorCode();
97
517k
  });
98
524k
  if (EC == inconvertibleErrorCode())
99
0
    report_fatal_error(EC.message());
100
524k
  return EC;
101
524k
}
102
103
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
104
void Error::fatalUncheckedError() const {
105
  dbgs() << "Program aborted due to an unhandled Error:\n";
106
  if (getPtr())
107
    getPtr()->log(dbgs());
108
  else
109
    dbgs() << "Error value was Success. (Note: Success values must still be "
110
              "checked prior to being destroyed).\n";
111
  abort();
112
}
113
#endif
114
115
StringError::StringError(std::error_code EC, const Twine &S)
116
121
    : Msg(S.str()), EC(EC) {}
117
118
StringError::StringError(const Twine &S, std::error_code EC)
119
33.3k
    : Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
120
121
3.52k
void StringError::log(raw_ostream &OS) const {
122
3.52k
  if (PrintMsgOnly) {
123
3.49k
    OS << Msg;
124
3.49k
  } else {
125
32
    OS << EC.message();
126
32
    if (!Msg.empty())
127
26
      OS << (" " + Msg);
128
32
  }
129
3.52k
}
130
131
45
std::error_code StringError::convertToErrorCode() const {
132
45
  return EC;
133
45
}
134
135
137
Error createStringError(std::error_code EC, char const *Msg) {
136
137
  return make_error<StringError>(Msg, EC);
137
137
}
138
139
0
void report_fatal_error(Error Err, bool GenCrashDiag) {
140
0
  assert(Err && "report_fatal_error called with success value");
141
0
  std::string ErrMsg;
142
0
  {
143
0
    raw_string_ostream ErrStream(ErrMsg);
144
0
    logAllUnhandledErrors(std::move(Err), ErrStream);
145
0
  }
146
0
  report_fatal_error(ErrMsg);
147
0
}
148
149
} // end namespace llvm
150
151
1
LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
152
1
  return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
153
1
}
154
155
0
void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
156
157
1
char *LLVMGetErrorMessage(LLVMErrorRef Err) {
158
1
  std::string Tmp = toString(unwrap(Err));
159
1
  char *ErrMsg = new char[Tmp.size() + 1];
160
1
  memcpy(ErrMsg, Tmp.data(), Tmp.size());
161
1
  ErrMsg[Tmp.size()] = '\0';
162
1
  return ErrMsg;
163
1
}
164
165
1
void LLVMDisposeErrorMessage(char *ErrMsg) { delete[] ErrMsg; }
166
167
1
LLVMErrorTypeId LLVMGetStringErrorTypeId() {
168
1
  return reinterpret_cast<void *>(&StringError::ID);
169
1
}
170
171
#ifndef _MSC_VER
172
namespace llvm {
173
174
// One of these two variables will be referenced by a symbol defined in
175
// llvm-config.h. We provide a link-time (or load time for DSO) failure when
176
// there is a mismatch in the build configuration of the API client and LLVM.
177
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
178
int EnableABIBreakingChecks;
179
#else
180
int DisableABIBreakingChecks;
181
#endif
182
183
} // end namespace llvm
184
#endif