kmp_i18n.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * kmp_i18n.h
  3. */
  4. //===----------------------------------------------------------------------===//
  5. //
  6. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  7. // See https://llvm.org/LICENSE.txt for license information.
  8. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #ifndef KMP_I18N_H
  12. #define KMP_I18N_H
  13. #include "kmp_str.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif // __cplusplus
  17. /* kmp_i18n_id.inc defines kmp_i18n_id_t type. It is an enumeration with
  18. identifiers of all the messages in the catalog. There is one special
  19. identifier: kmp_i18n_null, which denotes absence of message. */
  20. #include "kmp_i18n_id.inc" // Generated file. Do not edit it manually.
  21. /* Low-level functions handling message catalog. __kmp_i18n_open() opens message
  22. catalog, __kmp_i18n_closes() it. Explicit opening is not required: if message
  23. catalog is not yet open, __kmp_i18n_catgets() will open it implicitly.
  24. However, catalog should be explicitly closed, otherwise resources (mamory,
  25. handles) may leak.
  26. __kmp_i18n_catgets() returns read-only string. It should not be freed.
  27. KMP_I18N_STR macro simplifies access to strings in message catalog a bit.
  28. Following two lines are equivalent:
  29. __kmp_i18n_catgets( kmp_i18n_str_Warning )
  30. KMP_I18N_STR( Warning )
  31. */
  32. void __kmp_i18n_catopen();
  33. void __kmp_i18n_catclose();
  34. char const *__kmp_i18n_catgets(kmp_i18n_id_t id);
  35. #define KMP_I18N_STR(id) __kmp_i18n_catgets(kmp_i18n_str_##id)
  36. /* High-level interface for printing strings targeted to the user.
  37. All the strings are divided into 3 types:
  38. * messages,
  39. * hints,
  40. * system errors.
  41. There are 3 kind of message severities:
  42. * informational messages,
  43. * warnings (non-fatal errors),
  44. * fatal errors.
  45. For example:
  46. OMP: Warning #2: Cannot open message catalog "libguide.cat": (1)
  47. OMP: System error #2: No such file or directory (2)
  48. OMP: Hint: Please check NLSPATH environment variable. (3)
  49. OMP: Info #3: Default messages will be used. (4)
  50. where
  51. (1) is a message of warning severity,
  52. (2) is a system error caused the previous warning,
  53. (3) is a hint for the user how to fix the problem,
  54. (4) is a message of informational severity.
  55. Usage in complex cases (message is accompanied with hints and system errors):
  56. int error = errno; // We need save errno immediately, because it may
  57. // be changed.
  58. __kmp_msg(
  59. kmp_ms_warning, // Severity
  60. KMP_MSG( CantOpenMessageCatalog, name ), // Primary message
  61. KMP_ERR( error ), // System error
  62. KMP_HNT( CheckNLSPATH ), // Hint
  63. __kmp_msg_null // Variadic argument list finisher
  64. );
  65. Usage in simple cases (just a message, no system errors or hints):
  66. KMP_INFORM( WillUseDefaultMessages );
  67. KMP_WARNING( CantOpenMessageCatalog, name );
  68. KMP_FATAL( StackOverlap );
  69. KMP_SYSFAIL( "pthread_create", status );
  70. KMP_CHECK_SYSFAIL( "pthread_create", status );
  71. KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
  72. */
  73. enum kmp_msg_type {
  74. kmp_mt_dummy = 0, // Special type for internal purposes.
  75. kmp_mt_mesg =
  76. 4, // Primary OpenMP message, could be information, warning, or fatal.
  77. kmp_mt_hint = 5, // Hint to the user.
  78. kmp_mt_syserr = -1 // System error message.
  79. }; // enum kmp_msg_type
  80. typedef enum kmp_msg_type kmp_msg_type_t;
  81. struct kmp_msg {
  82. kmp_msg_type_t type;
  83. int num;
  84. char *str;
  85. size_t len;
  86. }; // struct kmp_message
  87. typedef struct kmp_msg kmp_msg_t;
  88. // Special message to denote the end of variadic list of arguments.
  89. extern kmp_msg_t __kmp_msg_null;
  90. // Helper functions. Creates messages either from message catalog or from
  91. // system. Note: these functions allocate memory. You should pass created
  92. // messages to __kmp_msg() function, it will print messages and destroy them.
  93. kmp_msg_t __kmp_msg_format(unsigned id_arg, ...);
  94. kmp_msg_t __kmp_msg_error_code(int code);
  95. kmp_msg_t __kmp_msg_error_mesg(char const *mesg);
  96. // Helper macros to make calls shorter.
  97. #define KMP_MSG(...) __kmp_msg_format(kmp_i18n_msg_##__VA_ARGS__)
  98. #define KMP_HNT(...) __kmp_msg_format(kmp_i18n_hnt_##__VA_ARGS__)
  99. #define KMP_SYSERRCODE(code) __kmp_msg_error_code(code)
  100. #define KMP_SYSERRMESG(mesg) __kmp_msg_error_mesg(mesg)
  101. #define KMP_ERR KMP_SYSERRCODE
  102. // Message severity.
  103. enum kmp_msg_severity {
  104. kmp_ms_inform, // Just information for the user.
  105. kmp_ms_warning, // Non-fatal error, execution continues.
  106. kmp_ms_fatal // Fatal error, program aborts.
  107. }; // enum kmp_msg_severity
  108. typedef enum kmp_msg_severity kmp_msg_severity_t;
  109. // Primary function for printing messages for the user. The first message is
  110. // mandatory. Any number of system errors and hints may be specified. Argument
  111. // list must be finished with __kmp_msg_null.
  112. void __kmp_msg(kmp_msg_severity_t severity, kmp_msg_t message, ...);
  113. KMP_NORETURN void __kmp_fatal(kmp_msg_t message, ...);
  114. // Helper macros to make calls shorter in simple cases.
  115. #define KMP_INFORM(...) \
  116. __kmp_msg(kmp_ms_inform, KMP_MSG(__VA_ARGS__), __kmp_msg_null)
  117. #define KMP_WARNING(...) \
  118. __kmp_msg(kmp_ms_warning, KMP_MSG(__VA_ARGS__), __kmp_msg_null)
  119. #define KMP_FATAL(...) __kmp_fatal(KMP_MSG(__VA_ARGS__), __kmp_msg_null)
  120. #define KMP_SYSFAIL(func, error) \
  121. __kmp_fatal(KMP_MSG(FunctionError, func), KMP_SYSERRCODE(error), \
  122. __kmp_msg_null)
  123. // Check error, if not zero, generate fatal error message.
  124. #define KMP_CHECK_SYSFAIL(func, error) \
  125. { \
  126. if (error) { \
  127. KMP_SYSFAIL(func, error); \
  128. } \
  129. }
  130. // Check status, if not zero, generate fatal error message using errno.
  131. #define KMP_CHECK_SYSFAIL_ERRNO(func, status) \
  132. { \
  133. if (status != 0) { \
  134. int error = errno; \
  135. KMP_SYSFAIL(func, error); \
  136. } \
  137. }
  138. #ifdef KMP_DEBUG
  139. void __kmp_i18n_dump_catalog(kmp_str_buf_t *buffer);
  140. #endif // KMP_DEBUG
  141. #ifdef __cplusplus
  142. } // extern "C"
  143. #endif // __cplusplus
  144. #endif // KMP_I18N_H
  145. // end of file //