errors.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Default error handlers for CPP Library.
  2. Copyright (C) 1986-2015 Free Software Foundation, Inc.
  3. Written by Per Bothner, 1994.
  4. Based on CCCP program by Paul Rubin, June 1986
  5. Adapted to ANSI C, Richard Stallman, Jan 1987
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>.
  17. In other words, you are welcome to use, share and improve this program.
  18. You are forbidden to forbid anyone else to use, share and improve
  19. what you give them. Help stamp out software-hoarding! */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "cpplib.h"
  23. #include "internal.h"
  24. /* Print a diagnostic at the location of the previously lexed token. */
  25. ATTRIBUTE_FPTR_PRINTF(4,0)
  26. static bool
  27. cpp_diagnostic (cpp_reader * pfile, int level, int reason,
  28. const char *msgid, va_list *ap)
  29. {
  30. source_location src_loc;
  31. bool ret;
  32. if (CPP_OPTION (pfile, traditional))
  33. {
  34. if (pfile->state.in_directive)
  35. src_loc = pfile->directive_line;
  36. else
  37. src_loc = pfile->line_table->highest_line;
  38. }
  39. /* We don't want to refer to a token before the beginning of the
  40. current run -- that is invalid. */
  41. else if (pfile->cur_token == pfile->cur_run->base)
  42. {
  43. src_loc = 0;
  44. }
  45. else
  46. {
  47. src_loc = pfile->cur_token[-1].src_loc;
  48. }
  49. if (!pfile->cb.error)
  50. abort ();
  51. ret = pfile->cb.error (pfile, level, reason, src_loc, 0, _(msgid), ap);
  52. return ret;
  53. }
  54. /* Print a warning or error, depending on the value of LEVEL. */
  55. bool
  56. cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
  57. {
  58. va_list ap;
  59. bool ret;
  60. va_start (ap, msgid);
  61. ret = cpp_diagnostic (pfile, level, CPP_W_NONE, msgid, &ap);
  62. va_end (ap);
  63. return ret;
  64. }
  65. /* Print a warning. The warning reason may be given in REASON. */
  66. bool
  67. cpp_warning (cpp_reader * pfile, int reason, const char *msgid, ...)
  68. {
  69. va_list ap;
  70. bool ret;
  71. va_start (ap, msgid);
  72. ret = cpp_diagnostic (pfile, CPP_DL_WARNING, reason, msgid, &ap);
  73. va_end (ap);
  74. return ret;
  75. }
  76. /* Print a pedantic warning. The warning reason may be given in REASON. */
  77. bool
  78. cpp_pedwarning (cpp_reader * pfile, int reason, const char *msgid, ...)
  79. {
  80. va_list ap;
  81. bool ret;
  82. va_start (ap, msgid);
  83. ret = cpp_diagnostic (pfile, CPP_DL_PEDWARN, reason, msgid, &ap);
  84. va_end (ap);
  85. return ret;
  86. }
  87. /* Print a warning, including system headers. The warning reason may be
  88. given in REASON. */
  89. bool
  90. cpp_warning_syshdr (cpp_reader * pfile, int reason, const char *msgid, ...)
  91. {
  92. va_list ap;
  93. bool ret;
  94. va_start (ap, msgid);
  95. ret = cpp_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, reason, msgid, &ap);
  96. va_end (ap);
  97. return ret;
  98. }
  99. /* Print a diagnostic at a specific location. */
  100. ATTRIBUTE_FPTR_PRINTF(6,0)
  101. static bool
  102. cpp_diagnostic_with_line (cpp_reader * pfile, int level, int reason,
  103. source_location src_loc, unsigned int column,
  104. const char *msgid, va_list *ap)
  105. {
  106. bool ret;
  107. if (!pfile->cb.error)
  108. abort ();
  109. ret = pfile->cb.error (pfile, level, reason, src_loc, column, _(msgid), ap);
  110. return ret;
  111. }
  112. /* Print a warning or error, depending on the value of LEVEL. */
  113. bool
  114. cpp_error_with_line (cpp_reader *pfile, int level,
  115. source_location src_loc, unsigned int column,
  116. const char *msgid, ...)
  117. {
  118. va_list ap;
  119. bool ret;
  120. va_start (ap, msgid);
  121. ret = cpp_diagnostic_with_line (pfile, level, CPP_W_NONE, src_loc,
  122. column, msgid, &ap);
  123. va_end (ap);
  124. return ret;
  125. }
  126. /* Print a warning. The warning reason may be given in REASON. */
  127. bool
  128. cpp_warning_with_line (cpp_reader *pfile, int reason,
  129. source_location src_loc, unsigned int column,
  130. const char *msgid, ...)
  131. {
  132. va_list ap;
  133. bool ret;
  134. va_start (ap, msgid);
  135. ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING, reason, src_loc,
  136. column, msgid, &ap);
  137. va_end (ap);
  138. return ret;
  139. }
  140. /* Print a pedantic warning. The warning reason may be given in REASON. */
  141. bool
  142. cpp_pedwarning_with_line (cpp_reader *pfile, int reason,
  143. source_location src_loc, unsigned int column,
  144. const char *msgid, ...)
  145. {
  146. va_list ap;
  147. bool ret;
  148. va_start (ap, msgid);
  149. ret = cpp_diagnostic_with_line (pfile, CPP_DL_PEDWARN, reason, src_loc,
  150. column, msgid, &ap);
  151. va_end (ap);
  152. return ret;
  153. }
  154. /* Print a warning, including system headers. The warning reason may be
  155. given in REASON. */
  156. bool
  157. cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
  158. source_location src_loc, unsigned int column,
  159. const char *msgid, ...)
  160. {
  161. va_list ap;
  162. bool ret;
  163. va_start (ap, msgid);
  164. ret = cpp_diagnostic_with_line (pfile, CPP_DL_WARNING_SYSHDR, reason, src_loc,
  165. column, msgid, &ap);
  166. va_end (ap);
  167. return ret;
  168. }
  169. /* Print a warning or error, depending on the value of LEVEL. Include
  170. information from errno. */
  171. bool
  172. cpp_errno (cpp_reader *pfile, int level, const char *msgid)
  173. {
  174. if (msgid[0] == '\0')
  175. msgid = _("stdout");
  176. return cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
  177. }