[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / check.h
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_CHECK_H_
6 #define BASE_CHECK_H_
7
8 #include <iosfwd>
9
10 #include "base/base_export.h"
11 #include "base/compiler_specific.h"
12 #include "base/dcheck_is_on.h"
13 #include "base/debug/debugging_buildflags.h"
14 #include "base/immediate_crash.h"
15
16 // This header defines the CHECK, DCHECK, and DPCHECK macros.
17 //
18 // CHECK dies with a fatal error if its condition is not true. It is not
19 // controlled by NDEBUG, so the check will be executed regardless of compilation
20 // mode.
21 //
22 // DCHECK, the "debug mode" check, is enabled depending on NDEBUG and
23 // DCHECK_ALWAYS_ON, and its severity depends on DCHECK_IS_CONFIGURABLE.
24 //
25 // (D)PCHECK is like (D)CHECK, but includes the system error code (c.f.
26 // perror(3)).
27 //
28 // Additional information can be streamed to these macros and will be included
29 // in the log output if the condition doesn't hold (you may need to include
30 // <ostream>):
31 //
32 //   CHECK(condition) << "Additional info.";
33 //
34 // The condition is evaluated exactly once. Even in build modes where e.g.
35 // DCHECK is disabled, the condition and any stream arguments are still
36 // referenced to avoid warnings about unused variables and functions.
37 //
38 // For the (D)CHECK_EQ, etc. macros, see base/check_op.h. However, that header
39 // is *significantly* larger than check.h, so try to avoid including it in
40 // header files.
41
42 namespace logging {
43
44 // Class used to explicitly ignore an ostream, and optionally a boolean value.
45 class VoidifyStream {
46  public:
47   VoidifyStream() = default;
48   explicit VoidifyStream(bool ignored) {}
49
50   // This operator has lower precedence than << but higher than ?:
51   void operator&(std::ostream&) {}
52 };
53
54 // Helper macro which avoids evaluating the arguents to a stream if the
55 // condition is false.
56 #define LAZY_CHECK_STREAM(stream, condition) \
57   !(condition) ? (void)0 : ::logging::VoidifyStream() & (stream)
58
59 // Macro which uses but does not evaluate expr and any stream parameters.
60 #define EAT_CHECK_STREAM_PARAMS(expr) \
61   true ? (void)0                      \
62        : ::logging::VoidifyStream(expr) & (*::logging::g_swallow_stream)
63 BASE_EXPORT extern std::ostream* g_swallow_stream;
64
65 class CheckOpResult;
66 class LogMessage;
67
68 // Class used for raising a check error upon destruction.
69 class BASE_EXPORT CheckError {
70  public:
71   static CheckError Check(const char* file, int line, const char* condition);
72   static CheckError CheckOp(const char* file, int line, CheckOpResult* result);
73
74   static CheckError DCheck(const char* file, int line, const char* condition);
75   static CheckError DCheckOp(const char* file, int line, CheckOpResult* result);
76
77   static CheckError PCheck(const char* file, int line, const char* condition);
78   static CheckError PCheck(const char* file, int line);
79
80   static CheckError DPCheck(const char* file, int line, const char* condition);
81
82   static CheckError NotImplemented(const char* file,
83                                    int line,
84                                    const char* function);
85
86   static CheckError NotReached(const char* file, int line);
87
88   // Stream for adding optional details to the error message.
89   std::ostream& stream();
90
91   NOMERGE NOT_TAIL_CALLED ~CheckError();
92
93   CheckError(const CheckError&) = delete;
94   CheckError& operator=(const CheckError&) = delete;
95
96  private:
97   explicit CheckError(LogMessage* log_message);
98
99   LogMessage* const log_message_;
100 };
101
102 #define CHECK_FUNCTION_IMPL(check_function, condition)                       \
103   LAZY_CHECK_STREAM(check_function(__FILE__, __LINE__, #condition).stream(), \
104                     !ANALYZER_ASSUME_TRUE(condition))
105
106 #if defined(OFFICIAL_BUILD) && defined(NDEBUG) && \
107     !BUILDFLAG(DCHECK_IS_CONFIGURABLE)
108
109 // Discard log strings to reduce code bloat.
110 //
111 // This is not calling BreakDebugger since this is called frequently, and
112 // calling an out-of-line function instead of a noreturn inline macro prevents
113 // compiler optimizations.
114 #define CHECK(condition) \
115   UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_CHECK_STREAM_PARAMS()
116
117 #define CHECK_WILL_STREAM() false
118
119 #define PCHECK(condition)                                         \
120   LAZY_CHECK_STREAM(                                              \
121       ::logging::CheckError::PCheck(__FILE__, __LINE__).stream(), \
122       UNLIKELY(!(condition)))
123
124 #else
125
126 #define CHECK_WILL_STREAM() true
127
128 #define CHECK(condition) \
129   CHECK_FUNCTION_IMPL(::logging::CheckError::Check, condition)
130
131 #define PCHECK(condition) \
132   CHECK_FUNCTION_IMPL(::logging::CheckError::PCheck, condition)
133
134 #endif
135
136 #if DCHECK_IS_ON()
137
138 #define DCHECK(condition) \
139   CHECK_FUNCTION_IMPL(::logging::CheckError::DCheck, condition)
140 #define DPCHECK(condition) \
141   CHECK_FUNCTION_IMPL(::logging::CheckError::DPCheck, condition)
142
143 #else
144
145 #define DCHECK(condition) EAT_CHECK_STREAM_PARAMS(!(condition))
146 #define DPCHECK(condition) EAT_CHECK_STREAM_PARAMS(!(condition))
147
148 #endif
149
150 // Async signal safe checking mechanism.
151 BASE_EXPORT void RawCheck(const char* message);
152 BASE_EXPORT void RawError(const char* message);
153 #define RAW_CHECK(condition)                                 \
154   do {                                                       \
155     if (!(condition))                                        \
156       ::logging::RawCheck("Check failed: " #condition "\n"); \
157   } while (0)
158
159 }  // namespace logging
160
161 #endif  // BASE_CHECK_H_