• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2006 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_BASE_CHECKS_H_
12 #define WEBRTC_BASE_CHECKS_H_
13 
14 #include <sstream>
15 #include <string>
16 
17 #ifdef WEBRTC_CHROMIUM_BUILD
18 // Include logging.h in a Chromium build to enable the overrides mechanism for
19 // using Chromium's macros. Otherwise, don't depend on logging.h.
20 // TODO(ajm): Ideally, checks.h would be combined with logging.h, but
21 // consolidation with system_wrappers/logging.h should happen first.
22 #include "webrtc/base/logging.h"
23 #endif
24 #include "webrtc/typedefs.h"
25 
26 // The macros here print a message to stderr and abort under various
27 // conditions. All will accept additional stream messages. For example:
28 // DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
29 //
30 // - CHECK(x) is an assertion that x is always true, and that if it isn't, it's
31 //   better to terminate the process than to continue. During development, the
32 //   reason that it's better to terminate might simply be that the error
33 //   handling code isn't in place yet; in production, the reason might be that
34 //   the author of the code truly believes that x will always be true, but that
35 //   she recognizes that if she is wrong, abrupt and unpleasant process
36 //   termination is still better than carrying on with the assumption violated.
37 //
38 // - DCHECK(x) is the same as CHECK(x)---an assertion that x is always
39 //   true---except that x will only be evaluated in debug builds; in production
40 //   builds, x is simply assumed to be true. This is useful if evaluating x is
41 //   expensive and the expected cost of failing to detect the violated
42 //   assumption is acceptable. You should not handle cases where a production
43 //   build fails to spot a violated condition, even those that would result in
44 //   crashes. If the code needs to cope with the error, make it cope, but don't
45 //   call DCHECK; if the condition really can't occur, but you'd sleep better
46 //   at night knowing that the process will suicide instead of carrying on in
47 //   case you were wrong, use CHECK instead of DCHECK.
48 //
49 // - CHECK_EQ, _NE, _GT, ..., and DCHECK_EQ, _NE, _GT, ... are specialized
50 //   variants of CHECK and DCHECK that print prettier messages if the condition
51 //   doesn't hold. Prefer them to raw CHECK and DCHECK.
52 //
53 // - FATAL() aborts unconditionally.
54 
55 namespace rtc {
56 
57 // The use of overrides/webrtc/base/logging.h in a Chromium build results in
58 // redefined macro errors. Fortunately, Chromium's macros can be used as drop-in
59 // replacements for the standalone versions.
60 #ifndef WEBRTC_CHROMIUM_BUILD
61 
62 // Helper macro which avoids evaluating the arguments to a stream if
63 // the condition doesn't hold.
64 #define LAZY_STREAM(stream, condition)                                        \
65   !(condition) ? static_cast<void>(0) : rtc::FatalMessageVoidify() & (stream)
66 
67 // The actual stream used isn't important.
68 #define EAT_STREAM_PARAMETERS                                           \
69   true ? static_cast<void>(0)                                           \
70        : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream()
71 
72 // CHECK dies with a fatal error if condition is not true.  It is *not*
73 // controlled by NDEBUG, so the check will be executed regardless of
74 // compilation mode.
75 //
76 // We make sure CHECK et al. always evaluates their arguments, as
77 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
78 #define CHECK(condition)                                                    \
79   LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), !(condition)) \
80   << "Check failed: " #condition << std::endl << "# "
81 
82 // Helper macro for binary operators.
83 // Don't use this macro directly in your code, use CHECK_EQ et al below.
84 //
85 // TODO(akalin): Rewrite this so that constructs like if (...)
86 // CHECK_EQ(...) else { ... } work properly.
87 #define CHECK_OP(name, op, val1, val2)                      \
88   if (std::string* _result =                                \
89       rtc::Check##name##Impl((val1), (val2),                \
90                              #val1 " " #op " " #val2))      \
91     rtc::FatalMessage(__FILE__, __LINE__, _result).stream()
92 
93 // Build the error message string.  This is separate from the "Impl"
94 // function template because it is not performance critical and so can
95 // be out of line, while the "Impl" code should be inline.  Caller
96 // takes ownership of the returned string.
97 template<class t1, class t2>
MakeCheckOpString(const t1 & v1,const t2 & v2,const char * names)98 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
99   std::ostringstream ss;
100   ss << names << " (" << v1 << " vs. " << v2 << ")";
101   std::string* msg = new std::string(ss.str());
102   return msg;
103 }
104 
105 // MSVC doesn't like complex extern templates and DLLs.
106 #if !defined(COMPILER_MSVC)
107 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
108 // in logging.cc.
109 extern template std::string* MakeCheckOpString<int, int>(
110     const int&, const int&, const char* names);
111 extern template
112 std::string* MakeCheckOpString<unsigned long, unsigned long>(
113     const unsigned long&, const unsigned long&, const char* names);
114 extern template
115 std::string* MakeCheckOpString<unsigned long, unsigned int>(
116     const unsigned long&, const unsigned int&, const char* names);
117 extern template
118 std::string* MakeCheckOpString<unsigned int, unsigned long>(
119     const unsigned int&, const unsigned long&, const char* names);
120 extern template
121 std::string* MakeCheckOpString<std::string, std::string>(
122     const std::string&, const std::string&, const char* name);
123 #endif
124 
125 // Helper functions for CHECK_OP macro.
126 // The (int, int) specialization works around the issue that the compiler
127 // will not instantiate the template version of the function on values of
128 // unnamed enum type - see comment below.
129 #define DEFINE_CHECK_OP_IMPL(name, op) \
130   template <class t1, class t2> \
131   inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
132                                         const char* names) { \
133     if (v1 op v2) return NULL; \
134     else return rtc::MakeCheckOpString(v1, v2, names); \
135   } \
136   inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
137     if (v1 op v2) return NULL; \
138     else return rtc::MakeCheckOpString(v1, v2, names); \
139   }
140 DEFINE_CHECK_OP_IMPL(EQ, ==)
141 DEFINE_CHECK_OP_IMPL(NE, !=)
142 DEFINE_CHECK_OP_IMPL(LE, <=)
143 DEFINE_CHECK_OP_IMPL(LT, < )
144 DEFINE_CHECK_OP_IMPL(GE, >=)
145 DEFINE_CHECK_OP_IMPL(GT, > )
146 #undef DEFINE_CHECK_OP_IMPL
147 
148 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
149 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
150 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
151 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
152 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
153 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
154 
155 // The DCHECK macro is equivalent to CHECK except that it only generates code in
156 // debug builds.
157 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
158 #define DCHECK(condition) CHECK(condition)
159 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2)
160 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2)
161 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2)
162 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2)
163 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2)
164 #define DCHECK_GT(v1, v2) CHECK_GT(v1, v2)
165 #else
166 #define DCHECK(condition) EAT_STREAM_PARAMETERS
167 #define DCHECK_EQ(v1, v2) EAT_STREAM_PARAMETERS
168 #define DCHECK_NE(v1, v2) EAT_STREAM_PARAMETERS
169 #define DCHECK_LE(v1, v2) EAT_STREAM_PARAMETERS
170 #define DCHECK_LT(v1, v2) EAT_STREAM_PARAMETERS
171 #define DCHECK_GE(v1, v2) EAT_STREAM_PARAMETERS
172 #define DCHECK_GT(v1, v2) EAT_STREAM_PARAMETERS
173 #endif
174 
175 // This is identical to LogMessageVoidify but in name.
176 class FatalMessageVoidify {
177  public:
FatalMessageVoidify()178   FatalMessageVoidify() { }
179   // This has to be an operator with a precedence lower than << but
180   // higher than ?:
181   void operator&(std::ostream&) { }
182 };
183 
184 #endif  // WEBRTC_CHROMIUM_BUILD
185 
186 #define FATAL() rtc::FatalMessage(__FILE__, __LINE__).stream()
187 // TODO(ajm): Consider adding NOTIMPLEMENTED and NOTREACHED macros when
188 // base/logging.h and system_wrappers/logging.h are consolidated such that we
189 // can match the Chromium behavior.
190 
191 // Like a stripped-down LogMessage from logging.h, except that it aborts.
192 class FatalMessage {
193  public:
194   FatalMessage(const char* file, int line);
195   // Used for CHECK_EQ(), etc. Takes ownership of the given string.
196   FatalMessage(const char* file, int line, std::string* result);
197   NO_RETURN ~FatalMessage();
198 
stream()199   std::ostream& stream() { return stream_; }
200 
201  private:
202   void Init(const char* file, int line);
203 
204   std::ostringstream stream_;
205 };
206 
207 }  // namespace rtc
208 
209 #endif  // WEBRTC_BASE_CHECKS_H_
210