1345153Sdim/*
2345153Sdim * kmp_i18n.h
3345153Sdim */
4345153Sdim
5345153Sdim//===----------------------------------------------------------------------===//
6345153Sdim//
7353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8353358Sdim// See https://llvm.org/LICENSE.txt for license information.
9353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10345153Sdim//
11345153Sdim//===----------------------------------------------------------------------===//
12345153Sdim
13345153Sdim#ifndef KMP_I18N_H
14345153Sdim#define KMP_I18N_H
15345153Sdim
16345153Sdim#include "kmp_str.h"
17345153Sdim
18345153Sdim#ifdef __cplusplus
19345153Sdimextern "C" {
20345153Sdim#endif // __cplusplus
21345153Sdim
22345153Sdim/* kmp_i18n_id.inc defines kmp_i18n_id_t type. It is an enumeration with
23345153Sdim   identifiers of all the messages in the catalog. There is one special
24345153Sdim   identifier: kmp_i18n_null, which denotes absence of message. */
25345153Sdim#include "kmp_i18n_id.inc" // Generated file. Do not edit it manually.
26345153Sdim
27345153Sdim/* Low-level functions handling message catalog. __kmp_i18n_open() opens message
28345153Sdim   catalog, __kmp_i18n_closes() it. Explicit opening is not required: if message
29345153Sdim   catalog is not yet open, __kmp_i18n_catgets() will open it implicitly.
30345153Sdim   However, catalog should be explicitly closed, otherwise resources (mamory,
31345153Sdim   handles) may leak.
32345153Sdim
33345153Sdim   __kmp_i18n_catgets() returns read-only string. It should not be freed.
34345153Sdim
35345153Sdim   KMP_I18N_STR macro simplifies acces to strings in message catalog a bit.
36345153Sdim   Following two lines are equivalent:
37345153Sdim
38345153Sdim   __kmp_i18n_catgets( kmp_i18n_str_Warning )
39345153Sdim   KMP_I18N_STR( Warning )
40345153Sdim*/
41345153Sdim
42345153Sdimvoid __kmp_i18n_catopen();
43345153Sdimvoid __kmp_i18n_catclose();
44345153Sdimchar const *__kmp_i18n_catgets(kmp_i18n_id_t id);
45345153Sdim
46345153Sdim#define KMP_I18N_STR(id) __kmp_i18n_catgets(kmp_i18n_str_##id)
47345153Sdim
48345153Sdim/* High-level interface for printing strings targeted to the user.
49345153Sdim
50345153Sdim   All the strings are divided into 3 types:
51345153Sdim   * messages,
52345153Sdim   * hints,
53345153Sdim   * system errors.
54345153Sdim
55345153Sdim   There are 3 kind of message severities:
56345153Sdim   * informational messages,
57345153Sdim   * warnings (non-fatal errors),
58345153Sdim   * fatal errors.
59345153Sdim
60345153Sdim   For example:
61345153Sdim     OMP: Warning #2: Cannot open message catalog "libguide.cat":   (1)
62345153Sdim     OMP: System error #2: No such file or directory                (2)
63345153Sdim     OMP: Hint: Please check NLSPATH environment variable.          (3)
64345153Sdim     OMP: Info #3: Default messages will be used.                   (4)
65345153Sdim
66345153Sdim   where
67345153Sdim   (1) is a message of warning severity,
68345153Sdim   (2) is a system error caused the previous warning,
69345153Sdim   (3) is a hint for the user how to fix the problem,
70345153Sdim   (4) is a message of informational severity.
71345153Sdim
72345153Sdim   Usage in complex cases (message is accompanied with hints and system errors):
73345153Sdim
74345153Sdim   int error = errno; // We need save errno immediately, because it may
75345153Sdim                      // be changed.
76345153Sdim   __kmp_msg(
77345153Sdim       kmp_ms_warning,                        // Severity
78345153Sdim       KMP_MSG( CantOpenMessageCatalog, name ), // Primary message
79345153Sdim       KMP_ERR( error ),                      // System error
80345153Sdim       KMP_HNT( CheckNLSPATH ),               // Hint
81345153Sdim       __kmp_msg_null                         // Variadic argument list finisher
82345153Sdim   );
83345153Sdim
84345153Sdim   Usage in simple cases (just a message, no system errors or hints):
85345153Sdim   KMP_INFORM( WillUseDefaultMessages );
86345153Sdim   KMP_WARNING( CantOpenMessageCatalog, name );
87345153Sdim   KMP_FATAL( StackOverlap );
88345153Sdim   KMP_SYSFAIL( "pthread_create", status );
89345153Sdim   KMP_CHECK_SYSFAIL( "pthread_create", status );
90345153Sdim   KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
91345153Sdim*/
92345153Sdim
93345153Sdimenum kmp_msg_type {
94345153Sdim  kmp_mt_dummy = 0, // Special type for internal purposes.
95345153Sdim  kmp_mt_mesg =
96345153Sdim      4, // Primary OpenMP message, could be information, warning, or fatal.
97345153Sdim  kmp_mt_hint = 5, // Hint to the user.
98345153Sdim  kmp_mt_syserr = -1 // System error message.
99345153Sdim}; // enum kmp_msg_type
100345153Sdimtypedef enum kmp_msg_type kmp_msg_type_t;
101345153Sdim
102345153Sdimstruct kmp_msg {
103345153Sdim  kmp_msg_type_t type;
104345153Sdim  int num;
105345153Sdim  char *str;
106345153Sdim  int len;
107345153Sdim}; // struct kmp_message
108345153Sdimtypedef struct kmp_msg kmp_msg_t;
109345153Sdim
110345153Sdim// Special message to denote the end of variadic list of arguments.
111345153Sdimextern kmp_msg_t __kmp_msg_null;
112345153Sdim
113345153Sdim// Helper functions. Creates messages either from message catalog or from
114345153Sdim// system. Note: these functions allocate memory. You should pass created
115345153Sdim// messages to __kmp_msg() function, it will print messages and destroy them.
116345153Sdimkmp_msg_t __kmp_msg_format(unsigned id_arg, ...);
117345153Sdimkmp_msg_t __kmp_msg_error_code(int code);
118345153Sdimkmp_msg_t __kmp_msg_error_mesg(char const *mesg);
119345153Sdim
120345153Sdim// Helper macros to make calls shorter.
121345153Sdim#define KMP_MSG(...) __kmp_msg_format(kmp_i18n_msg_##__VA_ARGS__)
122345153Sdim#define KMP_HNT(...) __kmp_msg_format(kmp_i18n_hnt_##__VA_ARGS__)
123345153Sdim#define KMP_SYSERRCODE(code) __kmp_msg_error_code(code)
124345153Sdim#define KMP_SYSERRMESG(mesg) __kmp_msg_error_mesg(mesg)
125345153Sdim#define KMP_ERR KMP_SYSERRCODE
126345153Sdim
127345153Sdim// Message severity.
128345153Sdimenum kmp_msg_severity {
129345153Sdim  kmp_ms_inform, // Just information for the user.
130345153Sdim  kmp_ms_warning, // Non-fatal error, execution continues.
131345153Sdim  kmp_ms_fatal // Fatal error, program aborts.
132345153Sdim}; // enum kmp_msg_severity
133345153Sdimtypedef enum kmp_msg_severity kmp_msg_severity_t;
134345153Sdim
135345153Sdim// Primary function for printing messages for the user. The first message is
136345153Sdim// mandatory. Any number of system errors and hints may be specified. Argument
137345153Sdim// list must be finished with __kmp_msg_null.
138345153Sdimvoid __kmp_msg(kmp_msg_severity_t severity, kmp_msg_t message, ...);
139345153SdimKMP_NORETURN void __kmp_fatal(kmp_msg_t message, ...);
140345153Sdim
141345153Sdim// Helper macros to make calls shorter in simple cases.
142345153Sdim#define KMP_INFORM(...)                                                        \
143345153Sdim  __kmp_msg(kmp_ms_inform, KMP_MSG(__VA_ARGS__), __kmp_msg_null)
144345153Sdim#define KMP_WARNING(...)                                                       \
145345153Sdim  __kmp_msg(kmp_ms_warning, KMP_MSG(__VA_ARGS__), __kmp_msg_null)
146345153Sdim#define KMP_FATAL(...) __kmp_fatal(KMP_MSG(__VA_ARGS__), __kmp_msg_null)
147345153Sdim#define KMP_SYSFAIL(func, error)                                               \
148345153Sdim  __kmp_fatal(KMP_MSG(FunctionError, func), KMP_SYSERRCODE(error),             \
149345153Sdim              __kmp_msg_null)
150345153Sdim
151345153Sdim// Check error, if not zero, generate fatal error message.
152345153Sdim#define KMP_CHECK_SYSFAIL(func, error)                                         \
153345153Sdim  {                                                                            \
154345153Sdim    if (error) {                                                               \
155345153Sdim      KMP_SYSFAIL(func, error);                                                \
156345153Sdim    }                                                                          \
157345153Sdim  }
158345153Sdim
159345153Sdim// Check status, if not zero, generate fatal error message using errno.
160345153Sdim#define KMP_CHECK_SYSFAIL_ERRNO(func, status)                                  \
161345153Sdim  {                                                                            \
162345153Sdim    if (status != 0) {                                                         \
163345153Sdim      int error = errno;                                                       \
164345153Sdim      KMP_SYSFAIL(func, error);                                                \
165345153Sdim    }                                                                          \
166345153Sdim  }
167345153Sdim
168345153Sdim#ifdef KMP_DEBUG
169345153Sdimvoid __kmp_i18n_dump_catalog(kmp_str_buf_t *buffer);
170345153Sdim#endif // KMP_DEBUG
171345153Sdim
172345153Sdim#ifdef __cplusplus
173345153Sdim}; // extern "C"
174345153Sdim#endif // __cplusplus
175345153Sdim
176345153Sdim#endif // KMP_I18N_H
177345153Sdim
178345153Sdim// end of file //
179