DiagnosticError.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- DiagnosticError.h - Diagnostic payload for llvm::Error -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
  14. #define LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
  15. #include "clang/Basic/PartialDiagnostic.h"
  16. #include "llvm/Support/Error.h"
  17. namespace clang {
  18. /// Carries a Clang diagnostic in an llvm::Error.
  19. ///
  20. /// Users should emit the stored diagnostic using the DiagnosticsEngine.
  21. class DiagnosticError : public llvm::ErrorInfo<DiagnosticError> {
  22. public:
  23. DiagnosticError(PartialDiagnosticAt Diag) : Diag(std::move(Diag)) {}
  24. void log(raw_ostream &OS) const override { OS << "clang diagnostic"; }
  25. PartialDiagnosticAt &getDiagnostic() { return Diag; }
  26. const PartialDiagnosticAt &getDiagnostic() const { return Diag; }
  27. /// Creates a new \c DiagnosticError that contains the given diagnostic at
  28. /// the given location.
  29. static llvm::Error create(SourceLocation Loc, PartialDiagnostic Diag) {
  30. return llvm::make_error<DiagnosticError>(
  31. PartialDiagnosticAt(Loc, std::move(Diag)));
  32. }
  33. /// Extracts and returns the diagnostic payload from the given \c Error if
  34. /// the error is a \c DiagnosticError. Returns none if the given error is not
  35. /// a \c DiagnosticError.
  36. static Optional<PartialDiagnosticAt> take(llvm::Error &Err) {
  37. Optional<PartialDiagnosticAt> Result;
  38. Err = llvm::handleErrors(std::move(Err), [&](DiagnosticError &E) {
  39. Result = std::move(E.getDiagnostic());
  40. });
  41. return Result;
  42. }
  43. static char ID;
  44. private:
  45. // Users are not expected to use error_code.
  46. std::error_code convertToErrorCode() const override {
  47. return llvm::inconvertibleErrorCode();
  48. }
  49. PartialDiagnosticAt Diag;
  50. };
  51. } // end namespace clang
  52. #endif // LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
  53. #ifdef __GNUC__
  54. #pragma GCC diagnostic pop
  55. #endif