clang  17.0.0git
DiagnosticError.h
Go to the documentation of this file.
1 //===--- DiagnosticError.h - Diagnostic payload for llvm::Error -*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
10 #define LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
11 
13 #include "llvm/Support/Error.h"
14 #include <optional>
15 
16 namespace clang {
17 
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 
25  void log(raw_ostream &OS) const override { OS << "clang diagnostic"; }
26 
27  PartialDiagnosticAt &getDiagnostic() { return Diag; }
28  const PartialDiagnosticAt &getDiagnostic() const { return Diag; }
29 
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)));
35  }
36 
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 std::optional<PartialDiagnosticAt> take(llvm::Error &Err) {
41  std::optional<PartialDiagnosticAt> Result;
42  Err = llvm::handleErrors(std::move(Err), [&](DiagnosticError &E) {
43  Result = std::move(E.getDiagnostic());
44  });
45  return Result;
46  }
47 
48  static char ID;
49 
50 private:
51  // Users are not expected to use error_code.
52  std::error_code convertToErrorCode() const override {
53  return llvm::inconvertibleErrorCode();
54  }
55 
57 };
58 
59 } // end namespace clang
60 
61 #endif // LLVM_CLANG_BASIC_DIAGNOSTICERROR_H
clang::DiagnosticError::take
static std::optional< PartialDiagnosticAt > take(llvm::Error &Err)
Extracts and returns the diagnostic payload from the given Error if the error is a DiagnosticError.
Definition: DiagnosticError.h:40
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::DiagnosticError
Carries a Clang diagnostic in an llvm::Error.
Definition: DiagnosticError.h:21
clang::DiagnosticError::log
void log(raw_ostream &OS) const override
Definition: DiagnosticError.h:25
clang::DiagnosticError::getDiagnostic
const PartialDiagnosticAt & getDiagnostic() const
Definition: DiagnosticError.h:28
clang::PartialDiagnosticAt
std::pair< SourceLocation, PartialDiagnostic > PartialDiagnosticAt
A partial diagnostic along with the source location where this diagnostic occurs.
Definition: PartialDiagnostic.h:205
clang::DiagnosticError::create
static llvm::Error create(SourceLocation Loc, PartialDiagnostic Diag)
Creates a new DiagnosticError that contains the given diagnostic at the given location.
Definition: DiagnosticError.h:32
clang::DiagnosticError::DiagnosticError
DiagnosticError(PartialDiagnosticAt Diag)
Definition: DiagnosticError.h:23
clang::PartialDiagnostic
Definition: PartialDiagnostic.h:31
clang::DiagnosticError::getDiagnostic
PartialDiagnosticAt & getDiagnostic()
Definition: DiagnosticError.h:27
std
Definition: Format.h:4605
PartialDiagnostic.h
clang
Definition: CalledOnceCheck.h:17
clang::DiagnosticError::ID
static char ID
Definition: DiagnosticError.h:48