2 * Copyright © 2017 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
31 #include "hb-atomic.hh"
32 #include "hb-dsalgs.hh"
41 * Global runtime options.
46 bool unused : 1; /* In-case sign bit is here. */
48 bool uniscribe_bug_compatible : 1;
52 union hb_options_union_t {
56 static_assert ((sizeof (hb_atomic_int_t) >= sizeof (hb_options_union_t)), "");
61 extern HB_INTERNAL hb_atomic_int_t _hb_options;
63 static inline hb_options_t
66 /* Make a local copy, so we can access bitfield threadsafely. */
68 u.i = _hb_options.get_relaxed ();
73 u.i = _hb_options.get_relaxed ();
81 * Debug output (needs enabling at compile time.)
85 _hb_debug (unsigned int level,
86 unsigned int max_level)
88 return level < max_level;
91 #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
92 #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
95 _hb_print_func (const char *func)
99 unsigned int func_len = strlen (func);
101 if (0 == strncmp (func, "static ", 7))
103 /* Skip "typename" */
104 if (0 == strncmp (func, "typename ", 9))
106 /* Skip return type */
107 const char *space = strchr (func, ' ');
110 /* Skip parameter list */
111 const char *paren = strchr (func, '(');
113 func_len = paren - func;
114 fprintf (stderr, "%.*s", func_len, func);
118 template <int max_level> static inline void
119 _hb_debug_msg_va (const char *what,
126 va_list ap) HB_PRINTF_FUNC(7, 0);
127 template <int max_level> static inline void
128 _hb_debug_msg_va (const char *what,
137 if (!_hb_debug (level, max_level))
140 fprintf (stderr, "%-10s", what ? what : "");
143 fprintf (stderr, "(%*p) ", (unsigned int) (2 * sizeof (void *)), obj);
145 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
148 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
149 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
150 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
151 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
152 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
153 static const char bars[] =
154 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
155 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
156 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
157 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
158 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR;
159 fprintf (stderr, "%2u %s" VRBAR "%s",
161 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars) - 1, (unsigned int) (sizeof (VBAR) - 1) * level),
162 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
164 fprintf (stderr, " " VRBAR LBAR);
166 _hb_print_func (func);
170 fprintf (stderr, ": ");
171 vfprintf (stderr, message, ap);
174 fprintf (stderr, "\n");
176 template <> inline void HB_PRINTF_FUNC(7, 0)
177 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
178 const void *obj HB_UNUSED,
179 const char *func HB_UNUSED,
180 bool indented HB_UNUSED,
181 unsigned int level HB_UNUSED,
182 int level_dir HB_UNUSED,
183 const char *message HB_UNUSED,
184 va_list ap HB_UNUSED) {}
186 template <int max_level> static inline void
187 _hb_debug_msg (const char *what,
194 ...) HB_PRINTF_FUNC(7, 8);
195 template <int max_level> static inline void HB_PRINTF_FUNC(7, 8)
196 _hb_debug_msg (const char *what,
206 va_start (ap, message);
207 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
210 template <> inline void
211 _hb_debug_msg<0> (const char *what HB_UNUSED,
212 const void *obj HB_UNUSED,
213 const char *func HB_UNUSED,
214 bool indented HB_UNUSED,
215 unsigned int level HB_UNUSED,
216 int level_dir HB_UNUSED,
217 const char *message HB_UNUSED,
218 ...) HB_PRINTF_FUNC(7, 8);
219 template <> inline void HB_PRINTF_FUNC(7, 8)
220 _hb_debug_msg<0> (const char *what HB_UNUSED,
221 const void *obj HB_UNUSED,
222 const char *func HB_UNUSED,
223 bool indented HB_UNUSED,
224 unsigned int level HB_UNUSED,
225 int level_dir HB_UNUSED,
226 const char *message HB_UNUSED,
229 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), nullptr, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
230 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), nullptr, false, 0, 0, __VA_ARGS__)
231 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
238 template <typename T>
239 struct hb_printer_t {
240 const char *print (const T&) { return "something"; }
244 struct hb_printer_t<bool> {
245 const char *print (bool v) { return v ? "true" : "false"; }
249 struct hb_printer_t<hb_void_t> {
250 const char *print (hb_void_t) { return ""; }
258 template <typename T>
259 static inline void _hb_warn_no_return (bool returned)
261 if (unlikely (!returned)) {
262 fprintf (stderr, "OUCH, returned with no call to return_trace(). This is a bug, please report.\n");
266 /*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
269 template <int max_level, typename ret_t>
270 struct hb_auto_trace_t
272 explicit inline hb_auto_trace_t (unsigned int *plevel_,
277 ...) HB_PRINTF_FUNC(6, 7)
278 : plevel (plevel_), what (what_), obj (obj_), returned (false)
280 if (plevel) ++*plevel;
283 va_start (ap, message);
284 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
289 _hb_warn_no_return<ret_t> (returned);
291 _hb_debug_msg<max_level> (what, obj, nullptr, true, plevel ? *plevel : 1, -1, " ");
293 if (plevel) --*plevel;
297 const char *func = "",
298 unsigned int line = 0)
300 if (unlikely (returned)) {
301 fprintf (stderr, "OUCH, double calls to return_trace(). This is a bug, please report.\n");
305 _hb_debug_msg<max_level> (what, obj, func, true, plevel ? *plevel : 1, -1,
306 "return %s (line %d)",
307 hb_printer_t<ret_t>().print (v), line);
308 if (plevel) --*plevel;
315 unsigned int *plevel;
320 template <typename ret_t> /* Make sure we don't use hb_auto_trace_t when not tracing. */
321 struct hb_auto_trace_t<0, ret_t>
323 explicit inline hb_auto_trace_t (unsigned int *plevel_,
328 ...) HB_PRINTF_FUNC(6, 7) {}
331 const char *func HB_UNUSED = nullptr,
332 unsigned int line HB_UNUSED = 0) { return v; }
335 /* For disabled tracing; optimize out everything.
336 * https://github.com/harfbuzz/harfbuzz/pull/605 */
337 template <typename ret_t>
338 struct hb_no_trace_t {
340 const char *func HB_UNUSED = "",
341 unsigned int line HB_UNUSED = 0) { return v; }
344 #define return_trace(RET) return trace.ret (RET, HB_FUNC, __LINE__)
351 #ifndef HB_DEBUG_ARABIC
352 #define HB_DEBUG_ARABIC (HB_DEBUG+0)
355 #ifndef HB_DEBUG_BLOB
356 #define HB_DEBUG_BLOB (HB_DEBUG+0)
359 #ifndef HB_DEBUG_CORETEXT
360 #define HB_DEBUG_CORETEXT (HB_DEBUG+0)
363 #ifndef HB_DEBUG_DIRECTWRITE
364 #define HB_DEBUG_DIRECTWRITE (HB_DEBUG+0)
368 #define HB_DEBUG_FT (HB_DEBUG+0)
371 #ifndef HB_DEBUG_GET_COVERAGE
372 #define HB_DEBUG_GET_COVERAGE (HB_DEBUG+0)
375 #ifndef HB_DEBUG_OBJECT
376 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
379 #ifndef HB_DEBUG_SHAPE_PLAN
380 #define HB_DEBUG_SHAPE_PLAN (HB_DEBUG+0)
383 #ifndef HB_DEBUG_UNISCRIBE
384 #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
391 #ifndef HB_DEBUG_APPLY
392 #define HB_DEBUG_APPLY (HB_DEBUG+0)
395 #define TRACE_APPLY(this) \
396 hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
397 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
398 "idx %d gid %u lookup %d", \
399 c->buffer->idx, c->buffer->cur().codepoint, (int) c->lookup_index)
401 #define TRACE_APPLY(this) hb_no_trace_t<bool> trace
404 #ifndef HB_DEBUG_SANITIZE
405 #define HB_DEBUG_SANITIZE (HB_DEBUG+0)
407 #if HB_DEBUG_SANITIZE
408 #define TRACE_SANITIZE(this) \
409 hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \
410 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
413 #define TRACE_SANITIZE(this) hb_no_trace_t<bool> trace
416 #ifndef HB_DEBUG_SERIALIZE
417 #define HB_DEBUG_SERIALIZE (HB_DEBUG+0)
419 #if HB_DEBUG_SERIALIZE
420 #define TRACE_SERIALIZE(this) \
421 hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \
422 (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \
425 #define TRACE_SERIALIZE(this) hb_no_trace_t<bool> trace
428 #ifndef HB_DEBUG_SUBSET
429 #define HB_DEBUG_SUBSET (HB_DEBUG+0)
432 #define TRACE_SUBSET(this) \
433 hb_auto_trace_t<HB_DEBUG_SUBSET, bool> trace \
434 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
437 #define TRACE_SUBSET(this) hb_no_trace_t<bool> trace
440 #ifndef HB_DEBUG_WOULD_APPLY
441 #define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
443 #if HB_DEBUG_WOULD_APPLY
444 #define TRACE_WOULD_APPLY(this) \
445 hb_auto_trace_t<HB_DEBUG_WOULD_APPLY, bool> trace \
446 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
447 "%d glyphs", c->len);
449 #define TRACE_WOULD_APPLY(this) hb_no_trace_t<bool> trace
452 #ifndef HB_DEBUG_DISPATCH
453 #define HB_DEBUG_DISPATCH ( \
455 HB_DEBUG_SANITIZE + \
456 HB_DEBUG_SERIALIZE + \
458 HB_DEBUG_WOULD_APPLY + \
461 #if HB_DEBUG_DISPATCH
462 #define TRACE_DISPATCH(this, format) \
463 hb_auto_trace_t<context_t::max_debug_depth, typename context_t::return_t> trace \
464 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
465 "format %d", (int) format);
467 #define TRACE_DISPATCH(this, format) hb_no_trace_t<typename context_t::return_t> trace
471 #endif /* HB_DEBUG_HH */