Error.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains error handling helper routines to pretty-print diagnostic
  11. // messages from tblgen.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/TableGen/Error.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/Support/Signals.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <cstdlib>
  19. namespace llvm {
  20. SourceMgr SrcMgr;
  21. unsigned ErrorsPrinted = 0;
  22. static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
  23. const Twine &Msg) {
  24. // Count the total number of errors printed.
  25. // This is used to exit with an error code if there were any errors.
  26. if (Kind == SourceMgr::DK_Error)
  27. ++ErrorsPrinted;
  28. SMLoc NullLoc;
  29. if (Loc.empty())
  30. Loc = NullLoc;
  31. SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
  32. for (unsigned i = 1; i < Loc.size(); ++i)
  33. SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
  34. "instantiated from multiclass");
  35. }
  36. void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
  37. PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
  38. }
  39. void PrintWarning(const char *Loc, const Twine &Msg) {
  40. SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
  41. }
  42. void PrintWarning(const Twine &Msg) {
  43. errs() << "warning:" << Msg << "\n";
  44. }
  45. void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
  46. PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
  47. }
  48. void PrintError(const char *Loc, const Twine &Msg) {
  49. SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
  50. }
  51. void PrintError(const Twine &Msg) {
  52. errs() << "error:" << Msg << "\n";
  53. }
  54. void PrintFatalError(const Twine &Msg) {
  55. PrintError(Msg);
  56. // The following call runs the file cleanup handlers.
  57. sys::RunInterruptHandlers();
  58. std::exit(1);
  59. }
  60. void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
  61. PrintError(ErrorLoc, Msg);
  62. // The following call runs the file cleanup handlers.
  63. sys::RunInterruptHandlers();
  64. std::exit(1);
  65. }
  66. } // end namespace llvm