BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsla_error.h
Go to the documentation of this file.
1/// @file bsla_error.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsla_error.h -*-C++-*-
8#ifndef INCLUDED_BSLA_ERROR
9#define INCLUDED_BSLA_ERROR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsla_error bsla_error
15/// @brief Provide a macro to emit an error message when a function is called.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsla
19/// @{
20/// @addtogroup bsla_error
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsla_error-purpose"> Purpose</a>
25/// * <a href="#bsla_error-macros"> Macros </a>
26/// * <a href="#bsla_error-description"> Description </a>
27/// * <a href="#bsla_error-macro-reference"> Macro Reference </a>
28/// * <a href="#bsla_error-usage"> Usage </a>
29/// * <a href="#bsla_error-example-1-flagging-a-function-for-a-compile-failure-and-message-if-used"> Example 1: Flagging a Function for a Compile Failure and Message if Used </a>
30///
31/// # Purpose {#bsla_error-purpose}
32/// Provide a macro to emit an error message when a function is called.
33///
34/// # Macros {#bsla_error-macros}
35///
36/// - BSLA_ERROR(QUOTED_MESSAGE): emit error message and fail compilation
37/// - BSLA_ERROR_IS_ACTIVE: defined if `BSLA_ERROR` is active
38///
39/// @see bsla_annotations
40///
41/// # Description {#bsla_error-description}
42/// This component provides a preprocessor macro that flags a
43/// function such that the compile will fail with an error message when the
44/// function is called. On platforms where the appropriate attribute is not
45/// supported, the macro expands to nothing.
46///
47/// ## Macro Reference {#bsla_error-macro-reference}
48///
49///
50/// `BSLA_ERROR(QUOTED_MESSAGE)`:
51/// This annotation, when used, will cause compilation to fail with an
52/// error message when a call to the so-annotated function is not removed
53/// through dead-code elimination or other optimizations. While it is
54/// possible to leave the function undefined, thus incurring a link-time
55/// failure, with the use of this macro the invalid call will be diagnosed
56/// earlier (i.e., at compile time), and the diagnostic will include the
57/// location of the function call. The message `QUOTED_MESSAGE`, which
58/// should be a double-quoted string, will appear in the error message.
59///
60/// `BSLA_ERROR_IS_ACTIVE`:
61/// The macro `BSLA_ERROR_IS_ACTIVE` is defined if `BSLA_ERROR` expands to
62/// something with the desired effect; otherwise `BSLA_ERROR_IS_ACTIVE` is
63/// not defined and `BSLA_ERROR` expands to nothing.
64///
65/// ## Usage {#bsla_error-usage}
66///
67///
68/// This section illustrates intended use of this component.
69///
70/// ### Example 1: Flagging a Function for a Compile Failure and Message if Used {#bsla_error-example-1-flagging-a-function-for-a-compile-failure-and-message-if-used}
71///
72///
73/// First, we declare and define a function annotated with `BSLA_ERROR`. Note
74/// that the argument to `BSLA_ERROR` must be a quoted string:
75/// @code
76/// void usageFunc() BSLA_ERROR("Don't call 'usageFunc'");
77/// // Do nothing.
78///
79/// void usageFunc()
80/// {
81/// }
82/// @endcode
83/// Now, we call `usageFunc`:
84/// @code
85/// usageFunc();
86/// @endcode
87/// Finally, observe that the compile fails with the following error message:
88/// @code
89/// .../bsla_error.t.cpp:226:16: error: call to 'usageFunc' declared with
90/// attribute error: Don't call 'usageFunc'
91/// usageFunc();
92/// ^
93/// @endcode
94/// @}
95/** @} */
96/** @} */
97
98/** @addtogroup bsl
99 * @{
100 */
101/** @addtogroup bsla
102 * @{
103 */
104/** @addtogroup bsla_error
105 * @{
106 */
107
108#include <bsls_platform.h>
109
110 // =============================
111 // Checks for Pre-Defined macros
112 // =============================
113
114#if defined(BSLA_ERROR)
115#error BSLA_ERROR is already defined!
116#endif
117
118#if defined(BSLA_ERROR_IS_ACTIVE)
119#error BSLA_ERROR_IS_ACTIVE is already defined!
120#endif
121
122 // =========================
123 // Set macros as appropriate
124 // =========================
125
126#if defined(BSLS_PLATFORM_CMP_GNU)
127 // The '__error__' attribute is not supported by clang as of version 7.0.
128
129 #define BSLA_ERROR(QUOTED_MESSAGE) \
130 __attribute__((__error__(QUOTED_MESSAGE)))
131
132 #define BSLA_ERROR_IS_ACTIVE 1
133#else
134 #define BSLA_ERROR(QUOTED_MESSAGE)
135#endif
136
137#endif
138
139// ----------------------------------------------------------------------------
140// Copyright 2019 Bloomberg Finance L.P.
141//
142// Licensed under the Apache License, Version 2.0 (the "License");
143// you may not use this file except in compliance with the License.
144// You may obtain a copy of the License at
145//
146// http://www.apache.org/licenses/LICENSE-2.0
147//
148// Unless required by applicable law or agreed to in writing, software
149// distributed under the License is distributed on an "AS IS" BASIS,
150// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
151// See the License for the specific language governing permissions and
152// limitations under the License.
153// ----------------------------- END-OF-FILE ----------------------------------
154
155/** @} */
156/** @} */
157/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195