Put tint::debugger::Break() behind a build flag

Breaking isn't always desirable, as there are many tests that intentionally trigger ICEs, which are caught by a EXPECT_DEATH().

On Linux the break also performs IO, which will likely cause problems in Chromium's sandboxed environment.

Change-Id: Ic2e1f5d13c9e986c066eaf364ffa5759c7299f6a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/81103
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f929ca9..769a0c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -92,6 +92,8 @@
 option_if_not_defined(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
 option_if_not_defined(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
 
+option_if_not_defined(TINT_ENABLE_BREAK_IN_DEBUGGER "Enable tint::debugger::Break()" OFF)
+
 option_if_not_defined(TINT_EMIT_COVERAGE "Emit code coverage information" OFF)
 
 option_if_not_defined(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8a4f2eb..84a0b27 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -53,6 +53,11 @@
 )
 tint_default_compile_options(tint_diagnostic_utils)
 
+if (TINT_ENABLE_BREAK_IN_DEBUGGER)
+  set_source_files_properties(utils/debugger.cc
+    PROPERTIES COMPILE_DEFINITIONS "TINT_ENABLE_BREAK_IN_DEBUGGER=1" )
+endif()
+
 set(TINT_LIB_SRCS
   ../include/tint/tint.h
   ast/access.cc
diff --git a/src/utils/debugger.cc b/src/utils/debugger.cc
index 5d8444c..fe8cc27 100644
--- a/src/utils/debugger.cc
+++ b/src/utils/debugger.cc
@@ -14,6 +14,8 @@
 
 #include "src/utils/debugger.h"
 
+#ifdef TINT_ENABLE_BREAK_IN_DEBUGGER
+
 #ifdef _MSC_VER
 #include <Windows.h>
 #elif defined(__linux__)
@@ -23,7 +25,7 @@
 #endif
 
 #ifdef _MSC_VER
-
+#define TINT_DEBUGGER_BREAK_DEFINED
 void tint::debugger::Break() {
   if (::IsDebuggerPresent()) {
     ::DebugBreak();
@@ -32,6 +34,7 @@
 
 #elif defined(__linux__)
 
+#define TINT_DEBUGGER_BREAK_DEFINED
 void tint::debugger::Break() {
   // A process is being traced (debugged) if "/proc/self/status" contains a
   // line with "TracerPid: <non-zero-digit>...".
@@ -51,9 +54,10 @@
     raise(SIGTRAP);
   }
 }
+#endif  // platform
 
-#else
+#endif  // TINT_ENABLE_BREAK_IN_DEBUGGER
 
+#ifndef TINT_DEBUGGER_BREAK_DEFINED
 void tint::debugger::Break() {}
-
 #endif
diff --git a/src/utils/debugger.h b/src/utils/debugger.h
index c40f3e9..7a4f2bd 100644
--- a/src/utils/debugger.h
+++ b/src/utils/debugger.h
@@ -16,8 +16,12 @@
 #define SRC_UTILS_DEBUGGER_H_
 
 namespace tint::debugger {
-/// If debugger is attached, will break into it at the call site.
+
+/// If debugger is attached and the `TINT_ENABLE_BREAK_IN_DEBUGGER` preprocessor
+/// macro is defined for `debugger.cc`, calling `Break()` will cause the
+/// debugger to break at the call site.
 void Break();
+
 }  // namespace tint::debugger
 
 #endif  // SRC_UTILS_DEBUGGER_H_