diff options
Diffstat (limited to 'include/llvm/Support/Error.h')
-rw-r--r-- | include/llvm/Support/Error.h | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/include/llvm/Support/Error.h b/include/llvm/Support/Error.h index ee2cbeec97a8..299fce7a1368 100644 --- a/include/llvm/Support/Error.h +++ b/include/llvm/Support/Error.h @@ -1,9 +1,8 @@ //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -1161,8 +1160,8 @@ private: /// Create formatted StringError object. template <typename... Ts> -Error createStringError(std::error_code EC, char const *Fmt, - const Ts &... Vals) { +inline Error createStringError(std::error_code EC, char const *Fmt, + const Ts &... Vals) { std::string Buffer; raw_string_ostream Stream(Buffer); Stream << format(Fmt, Vals...); @@ -1171,18 +1170,27 @@ Error createStringError(std::error_code EC, char const *Fmt, Error createStringError(std::error_code EC, char const *Msg); +template <typename... Ts> +inline Error createStringError(std::errc EC, char const *Fmt, + const Ts &... Vals) { + return createStringError(std::make_error_code(EC), Fmt, Vals...); +} + /// This class wraps a filename and another Error. /// /// In some cases, an error needs to live along a 'source' name, in order to /// show more detailed information to the user. class FileError final : public ErrorInfo<FileError> { - friend Error createFileError(std::string, Error); + friend Error createFileError(const Twine &, Error); + friend Error createFileError(const Twine &, size_t, Error); public: void log(raw_ostream &OS) const override { assert(Err && !FileName.empty() && "Trying to log after takeError()."); OS << "'" << FileName << "': "; + if (Line.hasValue()) + OS << "line " << Line.getValue() << ": "; Err->log(OS); } @@ -1194,29 +1202,51 @@ public: static char ID; private: - FileError(std::string F, std::unique_ptr<ErrorInfoBase> E) { + FileError(const Twine &F, Optional<size_t> LineNum, + std::unique_ptr<ErrorInfoBase> E) { assert(E && "Cannot create FileError from Error success value."); - assert(!F.empty() && + assert(!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty."); - FileName = F; + FileName = F.str(); Err = std::move(E); + Line = std::move(LineNum); } - static Error build(std::string F, Error E) { - return Error(std::unique_ptr<FileError>(new FileError(F, E.takePayload()))); + static Error build(const Twine &F, Optional<size_t> Line, Error E) { + return Error( + std::unique_ptr<FileError>(new FileError(F, Line, E.takePayload()))); } std::string FileName; + Optional<size_t> Line; std::unique_ptr<ErrorInfoBase> Err; }; /// Concatenate a source file path and/or name with an Error. The resulting /// Error is unchecked. -inline Error createFileError(std::string F, Error E) { - return FileError::build(F, std::move(E)); +inline Error createFileError(const Twine &F, Error E) { + return FileError::build(F, Optional<size_t>(), std::move(E)); +} + +/// Concatenate a source file path and/or name with line number and an Error. +/// The resulting Error is unchecked. +inline Error createFileError(const Twine &F, size_t Line, Error E) { + return FileError::build(F, Optional<size_t>(Line), std::move(E)); +} + +/// Concatenate a source file path and/or name with a std::error_code +/// to form an Error object. +inline Error createFileError(const Twine &F, std::error_code EC) { + return createFileError(F, errorCodeToError(EC)); +} + +/// Concatenate a source file path and/or name with line number and +/// std::error_code to form an Error object. +inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { + return createFileError(F, Line, errorCodeToError(EC)); } -Error createFileError(std::string F, ErrorSuccess) = delete; +Error createFileError(const Twine &F, ErrorSuccess) = delete; /// Helper for check-and-exit error handling. /// |