ErrorHandling.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //===-- ErrorHandling.h - Error handler -------------------------*- 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. #ifndef LLVM_TOOLS_LLVM_PROFGEN_ERRORHANDLING_H
  9. #define LLVM_TOOLS_LLVM_PROFGEN_ERRORHANDLING_H
  10. #include "llvm/ADT/Twine.h"
  11. #include "llvm/Support/Errc.h"
  12. #include "llvm/Support/Error.h"
  13. #include "llvm/Support/ErrorOr.h"
  14. #include "llvm/Support/WithColor.h"
  15. #include <system_error>
  16. using namespace llvm;
  17. [[noreturn]] inline void exitWithError(const Twine &Message,
  18. StringRef Whence = StringRef(),
  19. StringRef Hint = StringRef()) {
  20. WithColor::error(errs(), "llvm-profgen");
  21. if (!Whence.empty())
  22. errs() << Whence.str() << ": ";
  23. errs() << Message << "\n";
  24. if (!Hint.empty())
  25. WithColor::note() << Hint.str() << "\n";
  26. ::exit(EXIT_FAILURE);
  27. }
  28. [[noreturn]] inline void exitWithError(std::error_code EC,
  29. StringRef Whence = StringRef()) {
  30. exitWithError(EC.message(), Whence);
  31. }
  32. [[noreturn]] inline void exitWithError(Error E, StringRef Whence) {
  33. exitWithError(errorToErrorCode(std::move(E)), Whence);
  34. }
  35. template <typename T, typename... Ts>
  36. T unwrapOrError(Expected<T> EO, Ts &&... Args) {
  37. if (EO)
  38. return std::move(*EO);
  39. exitWithError(EO.takeError(), std::forward<Ts>(Args)...);
  40. }
  41. inline void emitWarningSummary(uint64_t Num, uint64_t Total, StringRef Msg) {
  42. if (!Total || !Num)
  43. return;
  44. WithColor::warning() << format("%.2f", static_cast<double>(Num) * 100 / Total)
  45. << "%(" << Num << "/" << Total << ") " << Msg << "\n";
  46. }
  47. #endif