Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:49

0001 //===--- DiagnosticError.h - Diagnostic payload for llvm::Error -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
0010 #define LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
0011 
0012 #include "clang/Basic/PartialDiagnostic.h"
0013 #include "llvm/Support/Error.h"
0014 #include <optional>
0015 
0016 namespace clang {
0017 
0018 /// Carries a Clang diagnostic in an llvm::Error.
0019 ///
0020 /// Users should emit the stored diagnostic using the DiagnosticsEngine.
0021 class DiagnosticError : public llvm::ErrorInfo<DiagnosticError> {
0022 public:
0023   DiagnosticError(PartialDiagnosticAt Diag) : Diag(std::move(Diag)) {}
0024 
0025   void log(raw_ostream &OS) const override { OS << "clang diagnostic"; }
0026 
0027   PartialDiagnosticAt &getDiagnostic() { return Diag; }
0028   const PartialDiagnosticAt &getDiagnostic() const { return Diag; }
0029 
0030   /// Creates a new \c DiagnosticError that contains the given diagnostic at
0031   /// the given location.
0032   static llvm::Error create(SourceLocation Loc, PartialDiagnostic Diag) {
0033     return llvm::make_error<DiagnosticError>(
0034         PartialDiagnosticAt(Loc, std::move(Diag)));
0035   }
0036 
0037   /// Extracts and returns the diagnostic payload from the given \c Error if
0038   /// the error is a \c DiagnosticError. Returns std::nullopt if the given error
0039   /// is not a \c DiagnosticError.
0040   static std::optional<PartialDiagnosticAt> take(llvm::Error &Err) {
0041     std::optional<PartialDiagnosticAt> Result;
0042     Err = llvm::handleErrors(std::move(Err), [&](DiagnosticError &E) {
0043       Result = std::move(E.getDiagnostic());
0044     });
0045     return Result;
0046   }
0047 
0048   static char ID;
0049 
0050 private:
0051   // Users are not expected to use error_code.
0052   std::error_code convertToErrorCode() const override {
0053     return llvm::inconvertibleErrorCode();
0054   }
0055 
0056   PartialDiagnosticAt Diag;
0057 };
0058 
0059 } // end namespace clang
0060 
0061 #endif // LLVM_CLANG_BASIC_DIAGNOSTICERROR_H