[metadata] Return metadata in postorder.

Change-Id: I06f89b9bda42d54a9e721e499c6d415fb0f0852d
Reviewed-on: https://gn-review.googlesource.com/c/3640
Commit-Queue: Julie Hockett <juliehockett@google.com>
Reviewed-by: Brett Wilson <brettw@chromium.org>
diff --git a/tools/gn/functions_target.cc b/tools/gn/functions_target.cc
index 93553b9..6cbab00 100644
--- a/tools/gn/functions_target.cc
+++ b/tools/gn/functions_target.cc
@@ -795,9 +795,13 @@
   optional values of the `rebase` and `walk_keys` variables. See
   `gn help metadata`.
 
+  Collected metadata, if specified, will be returned in postorder of
+  dependencies. See the example for details.
+
 Example (metadata collection)
 
-  Given the following targets defined in //base/BUILD.gn, where A -> B and B -> C and D:
+  Given the following targets defined in //base/BUILD.gn, where A depends on B
+  and B depends on C and D:
 
     group("a") {
       metadata = {
@@ -846,10 +850,10 @@
   The following will be written to "$root_build_dir/my_files.json" (less the
   comments):
     [
-      "foo.cpp",  // from //base:a
-      "bar.cpp",  // from //base:b via //base:a
       "baz.cpp",  // from //base:c via //base:b
       "missing.cpp"  // from //base:d via //base:b
+      "bar.cpp",  // from //base:b via //base:a
+      "foo.cpp",  // from //base:a
     ]
 
   Alternatively, as an example of using walk_keys, if the following
@@ -866,9 +870,9 @@
   The following will be written to "$root_build_dir/my_files.json" (again less
   the comments):
     [
-      "foo.cpp",  // from //base:a
-      "bar.cpp",  // from //base:b via //base:a
       "baz.cpp",  // from //base:c via //base:b
+      "bar.cpp",  // from //base:b via //base:a
+      "foo.cpp",  // from //base:a
     ]
 
   If `rebase` is used in the following generated_file target:
@@ -885,9 +889,9 @@
   The following will be written to "$root_build_dir/my_files.json" (again less
   the comments) (assuming root_build_dir = "//out"):
     [
-      "../base/foo.cpp",  // from //base:a
-      "../base/bar.cpp",  // from //base:b via //base:a
       "../base/baz.cpp",  // from //base:c via //base:b
+      "../base/bar.cpp",  // from //base:b via //base:a
+      "../base/foo.cpp",  // from //base:a
     ]
 
 
diff --git a/tools/gn/metadata_walk_unittest.cc b/tools/gn/metadata_walk_unittest.cc
index 4ceea7b..634a2c5 100644
--- a/tools/gn/metadata_walk_unittest.cc
+++ b/tools/gn/metadata_walk_unittest.cc
@@ -106,9 +106,9 @@
   EXPECT_FALSE(err.has_error());
 
   std::vector<Value> expected;
+  expected.push_back(Value(nullptr, "bar"));
   expected.push_back(Value(nullptr, "foo"));
   expected.push_back(Value(nullptr, true));
-  expected.push_back(Value(nullptr, "bar"));
   EXPECT_EQ(result, expected);
 
   std::set<const Target*> expected_walked_targets;
@@ -163,8 +163,8 @@
   EXPECT_FALSE(err.has_error()) << err.message();
 
   std::vector<Value> expected;
-  expected.push_back(Value(nullptr, "foo"));
   expected.push_back(Value(nullptr, "bar"));
+  expected.push_back(Value(nullptr, "foo"));
   EXPECT_EQ(result, expected) << result.size();
 
   std::set<const Target*> expected_walked_targets;
diff --git a/tools/gn/target.cc b/tools/gn/target.cc
index 0e9b245..c61758b 100644
--- a/tools/gn/target.cc
+++ b/tools/gn/target.cc
@@ -893,6 +893,7 @@
                          std::set<const Target*>* targets_walked,
                          Err* err) const {
   std::vector<Value> next_walk_keys;
+  std::vector<Value> current_result;
   // If deps_only, this is the top-level target and thus we don't want to
   // collect its metadata, only that of its deps and data_deps.
   if (deps_only) {
@@ -903,8 +904,8 @@
   } else {
     // Otherwise, we walk this target and collect the appropriate data.
     if (!metadata_.WalkStep(settings()->build_settings(), keys_to_extract,
-                            keys_to_walk, rebase_dir, &next_walk_keys, result,
-                            err))
+                            keys_to_walk, rebase_dir, &next_walk_keys,
+                            &current_result, err))
       return false;
   }
 
@@ -963,5 +964,7 @@
       return false;
     }
   }
+  result->insert(result->end(), std::make_move_iterator(current_result.begin()),
+                 std::make_move_iterator(current_result.end()));
   return true;
 }
diff --git a/tools/gn/target_unittest.cc b/tools/gn/target_unittest.cc
index 1cc4d96..9d9173e 100644
--- a/tools/gn/target_unittest.cc
+++ b/tools/gn/target_unittest.cc
@@ -1164,9 +1164,9 @@
   EXPECT_FALSE(err.has_error());
 
   std::vector<Value> expected;
+  expected.push_back(Value(nullptr, "bar"));
   expected.push_back(Value(nullptr, "foo"));
   expected.push_back(Value(nullptr, true));
-  expected.push_back(Value(nullptr, "bar"));
   EXPECT_EQ(result, expected);
 }
 
@@ -1214,8 +1214,8 @@
   EXPECT_FALSE(err.has_error()) << err.message();
 
   std::vector<Value> expected;
-  expected.push_back(Value(nullptr, "foo"));
   expected.push_back(Value(nullptr, "bar"));
+  expected.push_back(Value(nullptr, "foo"));
   EXPECT_EQ(result, expected) << result.size();
 }