Add --use-asan and --use-ubsan options to build/gen.py

Change-Id: I2d06e9f42219b140fe33b979b87df935762efeaf
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/15400
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Reviewed-by: Petr Hosek <phosek@google.com>
Commit-Queue: Roland McGrath <mcgrathr@google.com>
diff --git a/build/gen.py b/build/gen.py
index 6c7ac3a..dbf6035 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -164,6 +164,10 @@
                     help='Enable the use of LTO')
   args_list.add('--use-icf', action='store_true',
                     help='Enable the use of Identical Code Folding')
+  args_list.add('--use-asan', action='store_true',
+                    help='Enable the use of AddressSanitizer')
+  args_list.add('--use-ubsan', action='store_true',
+                    help='Enable the use of UndefinedBehaviorSanitizer')
   args_list.add('--no-last-commit-position', action='store_true',
                     help='Do not generate last_commit_position.h.')
   args_list.add('--out-path',
@@ -430,6 +434,14 @@
         cflags.extend(['-flto', '-fwhole-program-vtables'])
         ldflags.extend(['-flto', '-fwhole-program-vtables'])
 
+    if options.use_asan:
+      cflags.append('-fsanitize=address')
+      ldflags.append('-fsanitize=address')
+
+    if options.use_ubsan:
+      cflags.append('-fsanitize=undefined')
+      ldflags.append('-fsanitize=undefined')
+
     if not options.allow_warnings:
       cflags.append('-Werror')