PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/syngen/error.c

https://github.com/salty-horse/syn68k
C | 116 lines | 80 code | 27 blank | 9 comment | 12 complexity | 842b056fb396973aff7260e5a97e5a2c MD5 | raw file
  1. /*
  2. * error.c
  3. */
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "token.h"
  8. #include "common.h"
  9. #include "list.h"
  10. /* Prints out an error message and returns. */
  11. void
  12. error (const char *fmt, ...)
  13. {
  14. va_list ap;
  15. va_start (ap, fmt);
  16. vfprintf (stderr, fmt, ap);
  17. }
  18. /* Prints out an error message and aborts. */
  19. void
  20. fatal_error (const char *fmt, ...)
  21. {
  22. va_list ap;
  23. va_start (ap, fmt);
  24. vfprintf (stderr, fmt, ap);
  25. fputs ("*** Exit\n", stderr);
  26. abort ();
  27. }
  28. /* Prints out an error message for a given List and returns. */
  29. void
  30. parse_error (const List *ls, const char *fmt, ...)
  31. {
  32. va_list ap;
  33. va_start (ap, fmt);
  34. fprintf (stderr, "%s, line %lu:\t", ls->token.filename, ls->token.lineno);
  35. vfprintf (stderr, fmt, ap);
  36. }
  37. /* Prints out an error message for a given List and aborts. */
  38. void
  39. fatal_parse_error (const List *ls, const char *fmt, ...)
  40. {
  41. va_list ap;
  42. va_start (ap, fmt);
  43. fprintf (stderr, "%s, line %lu:\t", ls->token.filename, ls->token.lineno);
  44. vfprintf (stderr, fmt, ap);
  45. fputs ("*** Exit\n", stderr);
  46. exit (-1);
  47. }
  48. /* Prints out an error message for the current file and returns. */
  49. void
  50. input_error (const char *fmt, ...)
  51. {
  52. va_list ap;
  53. const InputFile *current = get_input_file (0);
  54. const InputFile *tmp = get_input_file (1);
  55. int lev = 2;
  56. if (tmp != NULL)
  57. {
  58. error ("In file included from %s:%d", tmp->filename, tmp->lineno);
  59. for (tmp = get_input_file (2); tmp != NULL; tmp = get_input_file (++lev))
  60. error (", from %s:%d", tmp->filename, tmp->lineno);
  61. error (":\n");
  62. }
  63. if (current != NULL)
  64. error ("%s: %d: ", current->filename, current->lineno);
  65. va_start (ap, fmt);
  66. vfprintf (stderr, fmt, ap);
  67. }
  68. /* Prints out an error message for the current file and aborts. */
  69. void
  70. fatal_input_error (const char *fmt, ...)
  71. {
  72. va_list ap;
  73. const InputFile *current = get_input_file (0);
  74. const InputFile *tmp = get_input_file (1);
  75. int lev = 2;
  76. if (tmp != NULL)
  77. {
  78. error ("In file included from %s:%d", tmp->filename, tmp->lineno);
  79. for (tmp = get_input_file (2); tmp != NULL; tmp = get_input_file (++lev))
  80. error (", from %s:%d", tmp->filename, tmp->lineno);
  81. error (":\n");
  82. }
  83. if (current != NULL)
  84. error ("%s: %d: ", current->filename, current->lineno);
  85. va_start (ap, fmt);
  86. vfprintf (stderr, fmt, ap);
  87. fputs ("*** Exit\n", stderr);
  88. exit (-1);
  89. }