arphaman updated this revision to Diff 112169. arphaman retitled this revision from "[Basic] Add a DiagnosticOr type" to "[Basic] Add a DiagnosticError llvm::ErrorInfo subclass". arphaman edited the summary of this revision. arphaman added a comment.
Use `ErrorInfo` subclass as suggested by Vedant. Repository: rL LLVM https://reviews.llvm.org/D36969 Files: include/clang/Basic/DiagnosticError.h lib/Basic/Diagnostic.cpp unittests/Basic/DiagnosticTest.cpp
Index: unittests/Basic/DiagnosticTest.cpp =================================================================== --- unittests/Basic/DiagnosticTest.cpp +++ unittests/Basic/DiagnosticTest.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticError.h" #include "clang/Basic/DiagnosticIDs.h" #include "gtest/gtest.h" @@ -72,4 +73,27 @@ } } +TEST(DiagnosticTest, diagnosticError) { + DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions, + new IgnoringDiagConsumer()); + PartialDiagnostic::StorageAllocator Alloc; + llvm::Expected<std::pair<int, int>> Value = + llvm::make_error<DiagnosticError>(PartialDiagnosticAt( + SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc) + << "file" + << "error")); + EXPECT_TRUE(!Value); + PartialDiagnosticAt ErrDiag = PartialDiagnosticAt( + SourceLocation(), PartialDiagnostic(PartialDiagnostic::NullDiagnostic())); + llvm::handleAllErrors(Value.takeError(), [&](DiagnosticError &Err) { + ErrDiag = std::move(Err.getDiagnostic()); + }); + EXPECT_EQ(ErrDiag.first, SourceLocation()); + EXPECT_EQ(ErrDiag.second.getDiagID(), diag::err_cannot_open_file); + + Value = std::make_pair(20, 1); + EXPECT_FALSE(!Value); + EXPECT_EQ(*Value, std::make_pair(20, 1)); + EXPECT_EQ(Value->first, 20); +} } Index: lib/Basic/Diagnostic.cpp =================================================================== --- lib/Basic/Diagnostic.cpp +++ lib/Basic/Diagnostic.cpp @@ -11,8 +11,9 @@ // //===----------------------------------------------------------------------===// -#include "clang/Basic/CharInfo.h" #include "clang/Basic/Diagnostic.h" +#include "clang/Basic/CharInfo.h" +#include "clang/Basic/DiagnosticError.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/PartialDiagnostic.h" @@ -1050,3 +1051,5 @@ llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lam"); } + +char DiagnosticError::ID; Index: include/clang/Basic/DiagnosticError.h =================================================================== --- /dev/null +++ include/clang/Basic/DiagnosticError.h @@ -0,0 +1,43 @@ +//===--- DiagnosticError.h - Diagnostic payload for llvm::Error -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H +#define LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H + +#include "clang/Basic/PartialDiagnostic.h" +#include "llvm/Support/Error.h" + +namespace clang { + +/// \brief Carries a Clang disgnostic in an llvm::Error. +/// +/// Users should emit the stored diagnostic using the DiagnosticsEngine. +class DiagnosticError : public llvm::ErrorInfo<DiagnosticError> { +public: + DiagnosticError(PartialDiagnosticAt Diag) : Diag(std::move(Diag)) {} + + void log(raw_ostream &OS) const override { OS << "clang diagnostic"; } + + PartialDiagnosticAt &getDiagnostic() { return Diag; } + const PartialDiagnosticAt &getDiagnostic() const { return Diag; } + + static char ID; + +private: + // Users are not expected to use error_code. + std::error_code convertToErrorCode() const override { + return llvm::inconvertibleErrorCode(); + } + + PartialDiagnosticAt Diag; +}; + +} // end namespace clang + +#endif // LLVM_CLANG_BASIC_DIAGNOSTIC_ERROR_H
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits