blob: 2a677b9abe2d64341f17d31488cfd30732d6fecb [file] [log] [blame]
Zachary Turner819e77d2016-05-06 20:51:57 +00001//===- Error.cpp - system_error extensions for PDB --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/PDB/GenericError.h"
11#include "llvm/Support/ErrorHandling.h"
12#include "llvm/Support/ManagedStatic.h"
13
14using namespace llvm;
15using namespace llvm::pdb;
16
Benjamin Kramera65b6102016-05-15 15:18:11 +000017namespace {
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000018// FIXME: This class is only here to support the transition to llvm::Error. It
19// will be removed once this transition is complete. Clients should prefer to
20// deal with the Error value directly, rather than converting to error_code.
Zachary Turner819e77d2016-05-06 20:51:57 +000021class GenericErrorCategory : public std::error_category {
22public:
Reid Kleckner990504e2016-10-19 23:52:38 +000023 const char *name() const noexcept override { return "llvm.pdb"; }
Zachary Turner819e77d2016-05-06 20:51:57 +000024
25 std::string message(int Condition) const override {
26 switch (static_cast<generic_error_code>(Condition)) {
27 case generic_error_code::unspecified:
28 return "An unknown error has occurred.";
Reid Kleckner6597c282017-07-13 20:12:23 +000029 case generic_error_code::type_server_not_found:
30 return "Type server PDB was not found.";
Zachary Turner819e77d2016-05-06 20:51:57 +000031 case generic_error_code::dia_sdk_not_present:
32 return "LLVM was not compiled with support for DIA. This usually means "
Hiroshi Inoue9ff23802018-04-09 04:37:53 +000033 "that you are not using MSVC, or your Visual Studio "
Zachary Turner819e77d2016-05-06 20:51:57 +000034 "installation "
35 "is corrupt.";
36 case generic_error_code::invalid_path:
37 return "Unable to load PDB. Make sure the file exists and is readable.";
38 }
39 llvm_unreachable("Unrecognized generic_error_code");
40 }
41};
Benjamin Kramera65b6102016-05-15 15:18:11 +000042} // end anonymous namespace
Zachary Turner819e77d2016-05-06 20:51:57 +000043
44static ManagedStatic<GenericErrorCategory> Category;
45
46char GenericError::ID = 0;
47
48GenericError::GenericError(generic_error_code C) : GenericError(C, "") {}
49
Zachary Turner38380322016-10-19 16:42:20 +000050GenericError::GenericError(StringRef Context)
Zachary Turner819e77d2016-05-06 20:51:57 +000051 : GenericError(generic_error_code::unspecified, Context) {}
52
Zachary Turner38380322016-10-19 16:42:20 +000053GenericError::GenericError(generic_error_code C, StringRef Context) : Code(C) {
Zachary Turner819e77d2016-05-06 20:51:57 +000054 ErrMsg = "PDB Error: ";
55 std::error_code EC = convertToErrorCode();
56 if (Code != generic_error_code::unspecified)
57 ErrMsg += EC.message() + " ";
58 if (!Context.empty())
59 ErrMsg += Context;
60}
61
62void GenericError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
63
Zachary Turner38380322016-10-19 16:42:20 +000064StringRef GenericError::getErrorMessage() const { return ErrMsg; }
Zachary Turner819e77d2016-05-06 20:51:57 +000065
66std::error_code GenericError::convertToErrorCode() const {
67 return std::error_code(static_cast<int>(Code), *Category);
68}