1 //===--- DiagnosticError.h - Diagnostic payload for llvm::Error -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
11 #define LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
13 #include "clang/Basic/PartialDiagnostic.h"
14 #include "llvm/Support/Error.h"
18 /// Carries a Clang diagnostic in an llvm::Error.
20 /// Users should emit the stored diagnostic using the DiagnosticsEngine.
21 class DiagnosticError : public llvm::ErrorInfo<DiagnosticError> {
23 DiagnosticError(PartialDiagnosticAt Diag) : Diag(std::move(Diag)) {}
25 void log(raw_ostream &OS) const override { OS << "clang diagnostic"; }
27 PartialDiagnosticAt &getDiagnostic() { return Diag; }
28 const PartialDiagnosticAt &getDiagnostic() const { return Diag; }
30 /// Creates a new \c DiagnosticError that contains the given diagnostic at
31 /// the given location.
32 static llvm::Error create(SourceLocation Loc, PartialDiagnostic Diag) {
33 return llvm::make_error<DiagnosticError>(
34 PartialDiagnosticAt(Loc, std::move(Diag)));
37 /// Extracts and returns the diagnostic payload from the given \c Error if
38 /// the error is a \c DiagnosticError. Returns none if the given error is not
39 /// a \c DiagnosticError.
40 static Optional<PartialDiagnosticAt> take(llvm::Error &Err) {
41 Optional<PartialDiagnosticAt> Result;
42 Err = llvm::handleErrors(std::move(Err), [&](DiagnosticError &E) {
43 Result = std::move(E.getDiagnostic());
51 // Users are not expected to use error_code.
52 std::error_code convertToErrorCode() const override {
53 return llvm::inconvertibleErrorCode();
56 PartialDiagnosticAt Diag;
59 } // end namespace clang
61 #endif // LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H