clang  13.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_DIAGNOSTIC_ERROR_H
10 #define LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
11 
13 #include "llvm/Support/Error.h"
14 
15 namespace clang {
16 
17 /// Carries a Clang diagnostic in an llvm::Error.
18 ///
19 /// Users should emit the stored diagnostic using the DiagnosticsEngine.
20 class DiagnosticError : public llvm::ErrorInfo<DiagnosticError> {
21 public:
22  DiagnosticError(PartialDiagnosticAt Diag) : Diag(std::move(Diag)) {}
23 
24  void log(raw_ostream &OS) const override { OS << "clang diagnostic"; }
25 
26  PartialDiagnosticAt &getDiagnostic() { return Diag; }
27  const PartialDiagnosticAt &getDiagnostic() const { return Diag; }
28 
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)));
34  }
35 
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) {
41  Err = llvm::handleErrors(std::move(Err), [&](DiagnosticError &E) {
42  Result = std::move(E.getDiagnostic());
43  });
44  return Result;
45  }
46 
47  static char ID;
48 
49 private:
50  // Users are not expected to use error_code.
51  std::error_code convertToErrorCode() const override {
52  return llvm::inconvertibleErrorCode();
53  }
54 
56 };
57 
58 } // end namespace clang
59 
60 #endif // LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
clang::DiagnosticError::take
static 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:39
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:89
clang::DiagnosticError
Carries a Clang diagnostic in an llvm::Error.
Definition: DiagnosticError.h:20
llvm::Optional
Definition: LLVM.h:40
clang::DiagnosticError::log
void log(raw_ostream &OS) const override
Definition: DiagnosticError.h:24
clang::DiagnosticError::getDiagnostic
const PartialDiagnosticAt & getDiagnostic() const
Definition: DiagnosticError.h:27
clang::PartialDiagnosticAt
std::pair< SourceLocation, PartialDiagnostic > PartialDiagnosticAt
A partial diagnostic along with the source location where this diagnostic occurs.
Definition: PartialDiagnostic.h:208
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:31
clang::DiagnosticError::DiagnosticError
DiagnosticError(PartialDiagnosticAt Diag)
Definition: DiagnosticError.h:22
clang::PartialDiagnostic
Definition: PartialDiagnostic.h:34
clang::DiagnosticError::getDiagnostic
PartialDiagnosticAt & getDiagnostic()
Definition: DiagnosticError.h:26
std
Definition: Format.h:3629
PartialDiagnostic.h
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::DiagnosticError::ID
static char ID
Definition: DiagnosticError.h:47