summaryrefslogtreecommitdiff
path: root/parallel-libs/streamexecutor/lib/Utils/Error.cpp
blob: f3d09673c2119f5220ef2a0e579a27a5c63c9484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//===-- Error.cpp - Error handling ----------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Types for returning recoverable errors.
///
//===----------------------------------------------------------------------===//

#include "streamexecutor/Utils/Error.h"

#include "llvm/ADT/StringRef.h"

namespace {

// An error with a string message describing the cause.
class StreamExecutorError : public llvm::ErrorInfo<StreamExecutorError> {
public:
  StreamExecutorError(llvm::StringRef Message) : Message(Message.str()) {}

  void log(llvm::raw_ostream &OS) const override { OS << Message; }

  std::error_code convertToErrorCode() const override {
    llvm_unreachable(
        "StreamExecutorError does not support conversion to std::error_code");
  }

  std::string getErrorMessage() const { return Message; }

  static char ID;

private:
  std::string Message;
};

char StreamExecutorError::ID = 0;

} // namespace

namespace streamexecutor {

Error make_error(Twine Message) {
  return llvm::make_error<StreamExecutorError>(Message.str());
}

std::string consumeAndGetMessage(Error &&E) {
  if (!E) {
    return "success";
  }
  std::string Message;
  llvm::handleAllErrors(std::move(E),
                        [&Message](const StreamExecutorError &SEE) {
                          Message = SEE.getErrorMessage();
                        });
  return Message;
}

} // namespace streamexecutor