1 //===--- DiagnosticError.h - Diagnostic payload for llvm::Error -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
10 #define LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
12 #include "clang/Basic/PartialDiagnostic.h"
13 #include "llvm/Support/Error.h"
17 /// Carries a Clang diagnostic in an llvm::Error.
19 /// Users should emit the stored diagnostic using the DiagnosticsEngine.
20 class DiagnosticError : public llvm::ErrorInfo<DiagnosticError> {
22 DiagnosticError(PartialDiagnosticAt Diag) : Diag(std::move(Diag)) {}
24 void log(raw_ostream &OS) const override { OS << "clang diagnostic"; }
26 PartialDiagnosticAt &getDiagnostic() { return Diag; }
27 const PartialDiagnosticAt &getDiagnostic() const { return Diag; }
29 /// Creates a new \c DiagnosticError that contains the given diagnostic at
30 /// the given location.
31 static llvm::Error create(SourceLocation Loc, PartialDiagnostic Diag) {
32 return llvm::make_error<DiagnosticError>(
33 PartialDiagnosticAt(Loc, std::move(Diag)));
36 /// Extracts and returns the diagnostic payload from the given \c Error if
37 /// the error is a \c DiagnosticError. Returns none if the given error is not
38 /// a \c DiagnosticError.
39 static Optional<PartialDiagnosticAt> take(llvm::Error &Err) {
40 Optional<PartialDiagnosticAt> Result;
41 Err = llvm::handleErrors(std::move(Err), [&](DiagnosticError &E) {
42 Result = std::move(E.getDiagnostic());
50 // Users are not expected to use error_code.
51 std::error_code convertToErrorCode() const override {
52 return llvm::inconvertibleErrorCode();
55 PartialDiagnosticAt Diag;
58 } // end namespace clang
60 #endif // LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H