Enable C++ runtime assertions in debug mode.

Enabling these runtime assertions should prevent
bugs like the one introduced in [1], which went
unnoticed, and had to be fixed in [2].

This should work on non-Windows platforms when using
libstdc++ or libc++.

This is also disabled on MacOS due to linking errors
with the system-installed XCode on Catalina. E.g.:

```
/opt/s/w/ir/x/w/cipd/bin/clang++ --target=x86_64-apple-darwin --sysroot=/opt/s/w/ir/cache/macos_sdk/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -mmacosx-version-min=10.9 -pthread -o gn src/gn/gn_main.o  base.a gn_lib.a
ld64.lld: error: undefined symbol: std::__1::__libcpp_debug_function
```

(where __libcpp_debug_function is supposed to be provided by libc++
and is the function called in case of runtime assertion failure). Looking
for clues on your favorite search engine does not provide any answer
to the issue :-(

+ Fix compiler warnings with GCC 13 in json_project_writer.cc.
  In debug mode, the compiler complains that with a warning that
  doesn't really make sense, but which can be work-around by
  using assign() instead of clear() + insert() on a vector.

  The error looks like:

  ```
  /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/debug/functions.h:110:44: error: no matching function for call to '__addressof'
        return __foreign_iterator_aux4(__it, std::__addressof(*__other));
  ...
  ../src/gn/json_project_writer.cc:74:14: note: in instantiation of function template specialization 'std::vector<const Target *>::insert<PointerSet<const Target>::const_iter
ator, void>' requested here
      targets->insert(targets->end(), target_set.begin(), target_set.end());
               ^
  /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/move.h:51:5: note: candidate function [with _Tp = const Target *] not viable: expects an lvalue for
  1st argument
      __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
  ```

[1] https://gn-review.googlesource.com/c/gn/+/16620
[2] https://gn-review.googlesource.com/c/gn/+/16700

Change-Id: I71cad8103b221145db75e639a6237f006c8fa37a
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/16740
Commit-Queue: David Turner <digit@google.com>
Reviewed-by: Takuto Ikuta <tikuta@google.com>
diff --git a/build/gen.py b/build/gen.py
index 5250954..3364cb9 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -408,6 +408,12 @@
   if not platform.is_msvc():
     if options.debug:
       cflags.extend(['-O0', '-g'])
+      # Enable libc++ or libstdc++ assertions in debug mode.
+      # Just set both macros to avoid detecting the C++ runtime being used.
+      # Currently disabled on MacOS since this results in linking errors at the
+      # moment, due to what looks like an XCode-specific Clang packaging error.
+      if not platform.is_darwin():
+        cflags.extend(['-D_LIBCPP_DEBUG=1', '-D_GLIBCXX_DEBUG=1'])
     else:
       cflags.append('-DNDEBUG')
       cflags.append('-O3')
diff --git a/src/gn/json_project_writer.cc b/src/gn/json_project_writer.cc
index a37fa9b..96ddacc 100644
--- a/src/gn/json_project_writer.cc
+++ b/src/gn/json_project_writer.cc
@@ -5,14 +5,12 @@
 #include "gn/json_project_writer.h"
 
 #include <algorithm>
-#include <fstream>
 #include <memory>
 #include <unordered_map>
 #include <vector>
 
 #include "base/command_line.h"
 #include "base/files/file_path.h"
-#include "base/files/file_util.h"
 #include "base/json/json_writer.h"
 #include "base/json/string_escape.h"
 #include "base/strings/string_number_conversions.h"
@@ -73,8 +71,7 @@
     for (const auto* target : *targets)
       AddTargetDependencies(target, &target_set);
 
-    targets->clear();
-    targets->insert(targets->end(), target_set.begin(), target_set.end());
+    targets->assign(target_set.begin(), target_set.end());
   }
 
   // Sort the list of targets per-label to get a consistent ordering of them