deprecation.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright 2001,2005-2006,2009-2012,2016,2018-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include "gsubr.h"
  22. #include "list.h"
  23. #include "pairs.h"
  24. #include "ports.h"
  25. #include "private-options.h"
  26. #include "strings.h"
  27. #include "threads.h"
  28. #include "deprecation.h"
  29. struct issued_warning {
  30. struct issued_warning *prev;
  31. const char *message;
  32. };
  33. static scm_i_pthread_mutex_t warn_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  34. static struct issued_warning *issued_warnings;
  35. static int print_summary = 0;
  36. void
  37. scm_c_issue_deprecation_warning (const char *msg)
  38. {
  39. if (!SCM_WARN_DEPRECATED)
  40. print_summary = 1;
  41. else
  42. {
  43. struct issued_warning *iw;
  44. scm_i_pthread_mutex_lock (&warn_lock);
  45. for (iw = issued_warnings; iw; iw = iw->prev)
  46. if (!strcmp (iw->message, msg))
  47. {
  48. msg = NULL;
  49. break;
  50. }
  51. if (msg)
  52. {
  53. msg = strdup (msg);
  54. iw = malloc (sizeof (struct issued_warning));
  55. if (msg == NULL || iw == NULL)
  56. /* Nothing sensible to do if you can't allocate this small
  57. amount of memory. */
  58. abort ();
  59. iw->message = msg;
  60. iw->prev = issued_warnings;
  61. issued_warnings = iw;
  62. }
  63. scm_i_pthread_mutex_unlock (&warn_lock);
  64. /* All this dance is to avoid printing to a port inside a mutex,
  65. which could recurse and deadlock. */
  66. if (msg)
  67. {
  68. scm_puts (msg, scm_current_warning_port ());
  69. scm_newline (scm_current_warning_port ());
  70. }
  71. }
  72. }
  73. void
  74. scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
  75. {
  76. va_list ap;
  77. char buf[512];
  78. va_start (ap, msg);
  79. vsnprintf (buf, 511, msg, ap);
  80. va_end (ap);
  81. buf[511] = '\0';
  82. scm_c_issue_deprecation_warning (buf);
  83. }
  84. SCM_DEFINE(scm_issue_deprecation_warning,
  85. "issue-deprecation-warning", 0, 0, 1,
  86. (SCM msgs),
  87. "Output @var{msgs} to @code{(current-error-port)} when this "
  88. "is the first call to @code{issue-deprecation-warning} with "
  89. "this specific @var{msgs}. Do nothing otherwise. "
  90. "The argument @var{msgs} should be a list of strings; "
  91. "they are printed in turn, each one followed by a newline.")
  92. #define FUNC_NAME s_scm_issue_deprecation_warning
  93. {
  94. if (!SCM_WARN_DEPRECATED)
  95. print_summary = 1;
  96. else
  97. {
  98. SCM nl = scm_from_utf8_string ("\n");
  99. SCM msgs_nl = SCM_EOL;
  100. char *c_msgs;
  101. while (scm_is_pair (msgs))
  102. {
  103. if (!scm_is_null (msgs_nl))
  104. msgs_nl = scm_cons (nl, msgs_nl);
  105. msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
  106. msgs = SCM_CDR (msgs);
  107. }
  108. msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
  109. c_msgs = scm_to_locale_string (msgs_nl);
  110. scm_c_issue_deprecation_warning (c_msgs);
  111. free (c_msgs);
  112. }
  113. return SCM_UNSPECIFIED;
  114. }
  115. #undef FUNC_NAME
  116. static void
  117. print_deprecation_summary (void)
  118. {
  119. if (print_summary)
  120. {
  121. fputs ("\n"
  122. "Some deprecated features have been used. Set the environment\n"
  123. "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
  124. "program to get more information. Set it to \"no\" to suppress\n"
  125. "this message.\n", stderr);
  126. }
  127. }
  128. SCM_DEFINE(scm_include_deprecated_features,
  129. "include-deprecated-features", 0, 0, 0,
  130. (),
  131. "Return @code{#t} iff deprecated features should be included "
  132. "in public interfaces.")
  133. #define FUNC_NAME s_scm_include_deprecated_features
  134. {
  135. return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
  136. }
  137. #undef FUNC_NAME
  138. void
  139. scm_init_deprecation ()
  140. {
  141. const char *level = getenv ("GUILE_WARN_DEPRECATED");
  142. if (level == NULL)
  143. level = SCM_WARN_DEPRECATED_DEFAULT;
  144. if (!strcmp (level, "detailed"))
  145. SCM_WARN_DEPRECATED = 1;
  146. else if (!strcmp (level, "no"))
  147. SCM_WARN_DEPRECATED = 0;
  148. else
  149. {
  150. SCM_WARN_DEPRECATED = 0;
  151. atexit (print_deprecation_summary);
  152. }
  153. #include "deprecation.x"
  154. }