Minor refactoring: reduce code duplication by introducing a macro for dispatch_sync_f, dispatch_async_f, dispatch_barrier_async_f bodies.


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@162199 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_mac.cc b/lib/asan/asan_mac.cc
index bd44305..bda4e22 100644
--- a/lib/asan/asan_mac.cc
+++ b/lib/asan/asan_mac.cc
@@ -279,32 +279,25 @@
   return asan_ctxt;
 }
 
-// TODO(glider): can we reduce code duplication by introducing a macro?
-INTERCEPTOR(void, dispatch_async_f, dispatch_queue_t dq, void *ctxt,
-                                    dispatch_function_t func) {
-  GET_STACK_TRACE_HERE(kStackTraceMax);
-  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
-  if (flags()->verbosity >= 2) {
-    Report("dispatch_async_f(): context: %p, pthread_self: %p\n",
-        asan_ctxt, pthread_self());
-    PRINT_CURRENT_STACK();
-  }
-  return REAL(dispatch_async_f)(dq, (void*)asan_ctxt,
-                                asan_dispatch_call_block_and_release);
-}
+// Define interceptor for dispatch_*_f function with the three most common
+// parameters: dispatch_queue_t, context, dispatch_function_t.
+#define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f)                                \
+  INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt,            \
+                                  dispatch_function_t func) {                 \
+    GET_STACK_TRACE_HERE(kStackTraceMax);                                     \
+    asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
+    if (flags()->verbosity >= 2) {                                            \
+      Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n",             \
+             asan_ctxt, pthread_self());                                      \
+       PRINT_CURRENT_STACK();                                                 \
+     }                                                                        \
+     return REAL(dispatch_x_f)(dq, (void*)asan_ctxt,                          \
+                               asan_dispatch_call_block_and_release);         \
+   }
 
-INTERCEPTOR(void, dispatch_sync_f, dispatch_queue_t dq, void *ctxt,
-                                   dispatch_function_t func) {
-  GET_STACK_TRACE_HERE(kStackTraceMax);
-  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
-  if (flags()->verbosity >= 2) {
-    Report("dispatch_sync_f(): context: %p, pthread_self: %p\n",
-        asan_ctxt, pthread_self());
-    PRINT_CURRENT_STACK();
-  }
-  return REAL(dispatch_sync_f)(dq, (void*)asan_ctxt,
-                               asan_dispatch_call_block_and_release);
-}
+INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
+INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
+INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
 
 INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
                                     dispatch_queue_t dq, void *ctxt,
@@ -319,19 +312,6 @@
                                 asan_dispatch_call_block_and_release);
 }
 
-INTERCEPTOR(void, dispatch_barrier_async_f, dispatch_queue_t dq, void *ctxt,
-                                            dispatch_function_t func) {
-  GET_STACK_TRACE_HERE(kStackTraceMax);
-  asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
-  if (flags()->verbosity >= 2) {
-    Report("dispatch_barrier_async_f(): context: %p, pthread_self: %p\n",
-           asan_ctxt, pthread_self());
-    PRINT_CURRENT_STACK();
-  }
-  REAL(dispatch_barrier_async_f)(dq, (void*)asan_ctxt,
-                                 asan_dispatch_call_block_and_release);
-}
-
 INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
                                           dispatch_queue_t dq, void *ctxt,
                                           dispatch_function_t func) {