PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/cc65-2.13.2/src/cc65/error.c

http://github.com/gilligan/snesdev
C | 242 lines | 128 code | 61 blank | 53 comment | 10 complexity | 2e2a8505c85440a5c57d1f9b53ca09db MD5 | raw file
  1. /*****************************************************************************/
  2. /* */
  3. /* error.c */
  4. /* */
  5. /* Error handling for the cc65 C compiler */
  6. /* */
  7. /* */
  8. /* */
  9. /* (C) 1998-2009, Ullrich von Bassewitz */
  10. /* Roemerstrasse 52 */
  11. /* D-70794 Filderstadt */
  12. /* EMail: uz@cc65.org */
  13. /* */
  14. /* */
  15. /* This software is provided 'as-is', without any expressed or implied */
  16. /* warranty. In no event will the authors be held liable for any damages */
  17. /* arising from the use of this software. */
  18. /* */
  19. /* Permission is granted to anyone to use this software for any purpose, */
  20. /* including commercial applications, and to alter it and redistribute it */
  21. /* freely, subject to the following restrictions: */
  22. /* */
  23. /* 1. The origin of this software must not be misrepresented; you must not */
  24. /* claim that you wrote the original software. If you use this software */
  25. /* in a product, an acknowledgment in the product documentation would be */
  26. /* appreciated but is not required. */
  27. /* 2. Altered source versions must be plainly marked as such, and must not */
  28. /* be misrepresented as being the original software. */
  29. /* 3. This notice may not be removed or altered from any source */
  30. /* distribution. */
  31. /* */
  32. /*****************************************************************************/
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <stdarg.h>
  36. /* common */
  37. #include "print.h"
  38. /* cc65 */
  39. #include "global.h"
  40. #include "input.h"
  41. #include "lineinfo.h"
  42. #include "scanner.h"
  43. #include "stmt.h"
  44. #include "error.h"
  45. /*****************************************************************************/
  46. /* Data */
  47. /*****************************************************************************/
  48. /* Count of errors/warnings */
  49. unsigned ErrorCount = 0;
  50. unsigned WarningCount = 0;
  51. /*****************************************************************************/
  52. /* Code */
  53. /*****************************************************************************/
  54. static void IntWarning (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
  55. /* Print warning message - internal function. */
  56. {
  57. if (!IS_Get (&WarnDisable)) {
  58. fprintf (stderr, "%s(%u): Warning: ", Filename, LineNo);
  59. vfprintf (stderr, Msg, ap);
  60. fprintf (stderr, "\n");
  61. if (Line) {
  62. Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
  63. }
  64. ++WarningCount;
  65. }
  66. }
  67. void Warning (const char* Format, ...)
  68. /* Print warning message. */
  69. {
  70. va_list ap;
  71. va_start (ap, Format);
  72. IntWarning (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
  73. va_end (ap);
  74. }
  75. void LIWarning (const LineInfo* LI, const char* Format, ...)
  76. /* Print a warning message with the line info given explicitly */
  77. {
  78. va_list ap;
  79. va_start (ap, Format);
  80. IntWarning (GetInputName (LI), GetInputLine (LI), Format, ap);
  81. va_end (ap);
  82. }
  83. void PPWarning (const char* Format, ...)
  84. /* Print warning message. For use within the preprocessor. */
  85. {
  86. va_list ap;
  87. va_start (ap, Format);
  88. IntWarning (GetCurrentFile(), GetCurrentLine(), Format, ap);
  89. va_end (ap);
  90. }
  91. static void IntError (const char* Filename, unsigned LineNo, const char* Msg, va_list ap)
  92. /* Print an error message - internal function*/
  93. {
  94. fprintf (stderr, "%s(%u): Error: ", Filename, LineNo);
  95. vfprintf (stderr, Msg, ap);
  96. fprintf (stderr, "\n");
  97. if (Line) {
  98. Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
  99. }
  100. ++ErrorCount;
  101. if (ErrorCount > 10) {
  102. Fatal ("Too many errors");
  103. }
  104. }
  105. void Error (const char* Format, ...)
  106. /* Print an error message */
  107. {
  108. va_list ap;
  109. va_start (ap, Format);
  110. IntError (GetInputName (CurTok.LI), GetInputLine (CurTok.LI), Format, ap);
  111. va_end (ap);
  112. }
  113. void LIError (const LineInfo* LI, const char* Format, ...)
  114. /* Print an error message with the line info given explicitly */
  115. {
  116. va_list ap;
  117. va_start (ap, Format);
  118. IntError (GetInputName (LI), GetInputLine (LI), Format, ap);
  119. va_end (ap);
  120. }
  121. void PPError (const char* Format, ...)
  122. /* Print an error message. For use within the preprocessor. */
  123. {
  124. va_list ap;
  125. va_start (ap, Format);
  126. IntError (GetCurrentFile(), GetCurrentLine(), Format, ap);
  127. va_end (ap);
  128. }
  129. void Fatal (const char* Format, ...)
  130. /* Print a message about a fatal error and die */
  131. {
  132. va_list ap;
  133. const char* FileName;
  134. unsigned LineNum;
  135. if (CurTok.LI) {
  136. FileName = GetInputName (CurTok.LI);
  137. LineNum = GetInputLine (CurTok.LI);
  138. } else {
  139. FileName = GetCurrentFile ();
  140. LineNum = GetCurrentLine ();
  141. }
  142. fprintf (stderr, "%s(%u): Fatal: ", FileName, LineNum);
  143. va_start (ap, Format);
  144. vfprintf (stderr, Format, ap);
  145. va_end (ap);
  146. fprintf (stderr, "\n");
  147. if (Line) {
  148. Print (stderr, 1, "Input: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
  149. }
  150. exit (EXIT_FAILURE);
  151. }
  152. void Internal (const char* Format, ...)
  153. /* Print a message about an internal compiler error and die. */
  154. {
  155. va_list ap;
  156. const char* FileName;
  157. unsigned LineNum;
  158. if (CurTok.LI) {
  159. FileName = GetInputName (CurTok.LI);
  160. LineNum = GetInputLine (CurTok.LI);
  161. } else {
  162. FileName = GetCurrentFile ();
  163. LineNum = GetCurrentLine ();
  164. }
  165. fprintf (stderr, "%s(%u): Internal compiler error:\n",
  166. FileName, LineNum);
  167. va_start (ap, Format);
  168. vfprintf (stderr, Format, ap);
  169. va_end (ap);
  170. fprintf (stderr, "\n");
  171. if (Line) {
  172. fprintf (stderr, "\nInput: %.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
  173. }
  174. /* Use abort to create a core dump */
  175. abort ();
  176. }
  177. void ErrorReport (void)
  178. /* Report errors (called at end of compile) */
  179. {
  180. Print (stdout, 1, "%u errors, %u warnings\n", ErrorCount, WarningCount);
  181. }